xref: /petsc/src/mat/utils/zerodiag.c (revision 63c41f6a1560bbb6cf7ee09697a660f5641fb9ab)
1 #ifndef lint
2 static char vcid[] = "$Id: zerodiag.c,v 1.9 1996/11/19 16:31:54 bsmith Exp balay $";
3 #endif
4 
5 /*
6     This file contains routines to reorder a matrix so that the diagonal
7     elements are nonzero.
8  */
9 
10 #include "src/mat/matimpl.h"       /*I  "mat.h"  I*/
11 #include <math.h>
12 
13 #define SWAP(a,b) {int _t; _t = a; a = b; b = _t; }
14 
15 #undef __FUNCTION__
16 #define __FUNCTION__ "MatZeroFindPre_Private"
17 /* Given a current row and current permutation, find a column permutation
18    that removes a zero diagonal */
19 int MatZeroFindPre_Private(Mat mat,int prow,int* row,int* col,double repla,
20                            double atol,int* rc,double* rcv )
21 {
22   int      k, nz, repl, *j, kk, nnz, *jj,ierr;
23   Scalar   *v, *vv;
24 
25   ierr = MatGetRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr);
26   for (k=0; k<nz; k++) {
27     if (col[j[k]] < prow && PetscAbsScalar(v[k]) > repla) {
28       /* See if this one will work */
29       repl  = col[j[k]];
30       ierr = MatGetRow( mat, row[repl], &nnz, &jj, &vv ); CHKERRQ(ierr);
31       for (kk=0; kk<nnz; kk++) {
32 	if (col[jj[kk]] == prow && PetscAbsScalar(vv[kk]) > atol) {
33 	  *rcv = PetscAbsScalar(v[k]);
34 	  *rc  = repl;
35           ierr = MatRestoreRow( mat, row[repl], &nnz, &jj, &vv ); CHKERRQ(ierr);
36           ierr = MatRestoreRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr);
37 	  return 1;
38 	}
39       }
40       ierr = MatRestoreRow( mat, row[repl], &nnz, &jj, &vv ); CHKERRQ(ierr);
41     }
42   }
43   ierr = MatRestoreRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr);
44   return 0;
45 }
46 
47 #undef __FUNCTION__
48 #define __FUNCTION__ "MatReorderForNonzeroDiagonal"
49 /*@
50     MatReorderForNonzeroDiagonal - Changes matrix ordering to remove
51         zeros from diagonal. This may help in the LU factorization to
52         prevent a zero pivot.
53 
54     Input Parameters:
55 .   mat  - matrix to reorder
56 .   rmap,cmap - row and column permutations.  Usually obtained from
57 .               MatGetReordering().
58 
59     Notes:
60     This is not intended as a replacement for pivoting for matrices that
61     have ``bad'' structure. It is only a stop-gap measure.
62 
63     Algorithm:
64     Column pivoting is used.  Choice of column is made by looking at the
65     non-zero elements in the row.  This algorithm is simple and fast but
66     does NOT guarentee that a non-singular or well conditioned
67     principle submatrix will be produced.
68 @*/
69 int MatReorderForNonzeroDiagonal(Mat mat,double atol,IS ris,IS cis )
70 {
71   int      ierr, prow, k, nz, n, repl, *j, *col, *row, m;
72   Scalar   *v;
73   double   repla;
74 
75   PetscValidHeaderSpecific(mat,MAT_COOKIE);
76   PetscValidHeaderSpecific(ris,IS_COOKIE);
77   PetscValidHeaderSpecific(cis,IS_COOKIE);
78 
79   ierr = ISGetIndices(ris,&row); CHKERRQ(ierr);
80   ierr = ISGetIndices(cis,&col); CHKERRQ(ierr);
81   ierr = MatGetSize(mat,&m,&n); CHKERRQ(ierr);
82 
83   for (prow=0; prow<n; prow++) {
84     ierr = MatGetRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr);
85     for (k=0; k<nz; k++) {if (col[j[k]] == prow) break;}
86     if (k >= nz || PetscAbsScalar(v[k]) <= atol) {
87       /* Element too small or zero; find the best candidate */
88       repl  = prow;
89       repla = (k >= nz) ? 0.0 : PetscAbsScalar(v[k]);
90       for (k=0; k<nz; k++) {
91 	if (col[j[k]] > prow && PetscAbsScalar(v[k]) > repla) {
92 	  repl = col[j[k]];
93 	  repla = PetscAbsScalar(v[k]);
94         }
95       }
96       if (prow == repl) {
97 	    /* Now we need to look for an element that allows us
98 	       to pivot with a previous column.  To do this, we need
99 	       to be sure that we don't introduce a zero in a previous
100 	       diagonal */
101         if (!MatZeroFindPre_Private(mat,prow,row,col,repla,atol,&repl,&repla)){
102 	  SETERRQ(1,"MatReorderForNonzeroDiagonal:Can not reorder matrix");
103 	}
104       }
105       SWAP(col[prow],col[repl]);
106     }
107     ierr = MatRestoreRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr);
108   }
109   ierr = ISRestoreIndices(ris,&row); CHKERRQ(ierr);
110   ierr = ISRestoreIndices(cis,&col); CHKERRQ(ierr);
111   return 0;
112 }
113 
114 
115 
116