xref: /petsc/src/mat/utils/zerodiag.c (revision 3a40ed3dce77c081171d005ae1a6ff4bb9d13b6f)
1 #ifdef PETSC_RCS_HEADER
2 static char vcid[] = "$Id: zerodiag.c,v 1.17 1997/08/22 15:15:22 bsmith Exp bsmith $";
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 __FUNC__
16 #define __FUNC__ "MatZeroFindPre_Private"
17 /*
18    Given a current row and current permutation, find a column permutation
19    that removes a zero diagonal.
20 */
21 int MatZeroFindPre_Private(Mat mat,int prow,int* row,int* col,double repla,
22                            double atol,int* rc,double* rcv )
23 {
24   int      k, nz, repl, *j, kk, nnz, *jj,ierr;
25   Scalar   *v, *vv;
26 
27   PetscFunctionBegin;
28   ierr = MatGetRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr);
29 /*
30     Here one could sort the col[j[k]] to try to select the column closest
31   to the diagonal (in the new ordering) that satisfies the criteria
32 */
33   for (k=0; k<nz; k++) {
34     if (col[j[k]] < prow && PetscAbsScalar(v[k]) > repla) {
35       /* See if this one will work */
36       repl  = col[j[k]];
37       ierr = MatGetRow( mat, row[repl], &nnz, &jj, &vv ); CHKERRQ(ierr);
38       for (kk=0; kk<nnz; kk++) {
39 	if (col[jj[kk]] == prow && PetscAbsScalar(vv[kk]) > atol) {
40 	  *rcv = PetscAbsScalar(v[k]);
41 	  *rc  = repl;
42           ierr = MatRestoreRow( mat, row[repl], &nnz, &jj, &vv ); CHKERRQ(ierr);
43           ierr = MatRestoreRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr);
44 	  PetscFunctionReturn(1);
45 	}
46       }
47       ierr = MatRestoreRow( mat, row[repl], &nnz, &jj, &vv ); CHKERRQ(ierr);
48     }
49   }
50   ierr = MatRestoreRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr);
51   PetscFunctionReturn(0);
52 }
53 
54 #undef __FUNC__
55 #define __FUNC__ "MatReorderForNonzeroDiagonal"
56 /*@
57     MatReorderForNonzeroDiagonal - Changes matrix ordering to remove
58     zeros from diagonal. This may help in the LU factorization to
59     prevent a zero pivot.
60 
61     Input Parameters:
62 .   mat  - matrix to reorder
63 .   rmap,cmap - row and column permutations.  Usually obtained from
64 .               MatGetReordering().
65 
66     Notes:
67     This is not intended as a replacement for pivoting for matrices that
68     have ``bad'' structure. It is only a stop-gap measure. Should be called
69     after a call to MatGetReordering(), this routine changes the column
70     ordering defined in cis.
71 
72     Options Database Keys: (When using SLES)
73 .      -pc_ilu_nonzeros_along_diagonal
74 .      -pc_lu_nonzeros_along_diagonal
75 
76     Algorithm:
77     Column pivoting is used.  Choice of column is made by looking at the
78     non-zero elements in the row.  This algorithm is simple and fast but
79     does NOT guarantee that a non-singular or well conditioned
80     principle submatrix will be produced.
81 @*/
82 int MatReorderForNonzeroDiagonal(Mat mat,double atol,IS ris,IS cis )
83 {
84   int      ierr, prow, k, nz, n, repl, *j, *col, *row, m;
85   Scalar   *v;
86   double   repla;
87 
88   PetscFunctionBegin;
89   PetscValidHeaderSpecific(mat,MAT_COOKIE);
90   PetscValidHeaderSpecific(ris,IS_COOKIE);
91   PetscValidHeaderSpecific(cis,IS_COOKIE);
92 
93   ierr = ISGetIndices(ris,&row); CHKERRQ(ierr);
94   ierr = ISGetIndices(cis,&col); CHKERRQ(ierr);
95   ierr = MatGetSize(mat,&m,&n); CHKERRQ(ierr);
96 
97   for (prow=0; prow<n; prow++) {
98     ierr = MatGetRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr);
99     for (k=0; k<nz; k++) {if (col[j[k]] == prow) break;}
100     if (k >= nz || PetscAbsScalar(v[k]) <= atol) {
101       /* Element too small or zero; find the best candidate */
102       repl  = prow;
103       repla = (k >= nz) ? 0.0 : PetscAbsScalar(v[k]);
104 /*
105    Here one could sort the col[j[k]] list to try to select the
106   column closest to the diagonal in the new ordering. (Note have
107   to permute the v[k] values as well, and use a fixed bound on the
108   quality of repla rather then looking for the absolute largest.
109 */
110       for (k=0; k<nz; k++) {
111 	if (col[j[k]] > prow && PetscAbsScalar(v[k]) > repla) {
112 	  repl  = col[j[k]];
113 	  repla = PetscAbsScalar(v[k]);
114         }
115       }
116       if (prow == repl) {
117 	    /* Look for an element that allows us
118 	       to pivot with a previous column.  To do this, we need
119 	       to be sure that we don't introduce a zero in a previous
120 	       diagonal */
121         if (!MatZeroFindPre_Private(mat,prow,row,col,repla,atol,&repl,&repla)){
122 	  SETERRQ(1,0,"Cannot reorder matrix to eliminate zero diagonal entry");
123 	}
124       }
125       SWAP(col[prow],col[repl]);
126     }
127     ierr = MatRestoreRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr);
128   }
129   ierr = ISRestoreIndices(ris,&row); CHKERRQ(ierr);
130   ierr = ISRestoreIndices(cis,&col); CHKERRQ(ierr);
131   PetscFunctionReturn(0);
132 }
133 
134 
135 
136