xref: /petsc/src/mat/utils/zerodiag.c (revision d252947ab307443ec0f8cabd384072ff75afeb3e)
1ad608de0SBarry Smith #ifndef lint
2*d252947aSBarry Smith static char vcid[] = "$Id: zerodiag.c,v 1.13 1997/01/06 20:26:03 balay Exp bsmith $";
3ad608de0SBarry Smith #endif
4ad608de0SBarry Smith 
5ad608de0SBarry Smith /*
6ad608de0SBarry Smith     This file contains routines to reorder a matrix so that the diagonal
748b35521SBarry Smith     elements are nonzero.
8ad608de0SBarry Smith  */
9ad608de0SBarry Smith 
1070f55243SBarry Smith #include "src/mat/matimpl.h"       /*I  "mat.h"  I*/
11ad608de0SBarry Smith #include <math.h>
12ad608de0SBarry Smith 
1348b35521SBarry Smith #define SWAP(a,b) {int _t; _t = a; a = b; b = _t; }
1448b35521SBarry Smith 
155615d1e5SSatish Balay #undef __FUNC__
165615d1e5SSatish Balay #define __FUNC__ "MatZeroFindPre_Private"
1748b35521SBarry Smith /* Given a current row and current permutation, find a column permutation
1848b35521SBarry Smith    that removes a zero diagonal */
19d5d45c9bSBarry Smith int MatZeroFindPre_Private(Mat mat,int prow,int* row,int* col,double repla,
20d93a2b8dSBarry Smith                            double atol,int* rc,double* rcv )
2148b35521SBarry Smith {
2290f02eecSBarry Smith   int      k, nz, repl, *j, kk, nnz, *jj,ierr;
2348b35521SBarry Smith   Scalar   *v, *vv;
2448b35521SBarry Smith 
2590f02eecSBarry Smith   ierr = MatGetRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr);
2648b35521SBarry Smith   for (k=0; k<nz; k++) {
27cddf8d76SBarry Smith     if (col[j[k]] < prow && PetscAbsScalar(v[k]) > repla) {
2848b35521SBarry Smith       /* See if this one will work */
2948b35521SBarry Smith       repl  = col[j[k]];
3090f02eecSBarry Smith       ierr = MatGetRow( mat, row[repl], &nnz, &jj, &vv ); CHKERRQ(ierr);
3148b35521SBarry Smith       for (kk=0; kk<nnz; kk++) {
32cddf8d76SBarry Smith 	if (col[jj[kk]] == prow && PetscAbsScalar(vv[kk]) > atol) {
33cddf8d76SBarry Smith 	  *rcv = PetscAbsScalar(v[k]);
3448b35521SBarry Smith 	  *rc  = repl;
3590f02eecSBarry Smith           ierr = MatRestoreRow( mat, row[repl], &nnz, &jj, &vv ); CHKERRQ(ierr);
3690f02eecSBarry Smith           ierr = MatRestoreRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr);
3748b35521SBarry Smith 	  return 1;
3848b35521SBarry Smith 	}
3948b35521SBarry Smith       }
4090f02eecSBarry Smith       ierr = MatRestoreRow( mat, row[repl], &nnz, &jj, &vv ); CHKERRQ(ierr);
4148b35521SBarry Smith     }
4248b35521SBarry Smith   }
4390f02eecSBarry Smith   ierr = MatRestoreRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr);
4448b35521SBarry Smith   return 0;
4548b35521SBarry Smith }
46ad608de0SBarry Smith 
475615d1e5SSatish Balay #undef __FUNC__
485615d1e5SSatish Balay #define __FUNC__ "MatReorderForNonzeroDiagonal"
49ad608de0SBarry Smith /*@
5048b35521SBarry Smith     MatReorderForNonzeroDiagonal - Changes matrix ordering to remove
5148b35521SBarry Smith         zeros from diagonal. This may help in the LU factorization to
5248b35521SBarry Smith         prevent a zero pivot.
53ad608de0SBarry Smith 
54ad608de0SBarry Smith     Input Parameters:
55ad608de0SBarry Smith .   mat  - matrix to reorder
5648b35521SBarry Smith .   rmap,cmap - row and column permutations.  Usually obtained from
5748b35521SBarry Smith .               MatGetReordering().
58ad608de0SBarry Smith 
59ad608de0SBarry Smith     Notes:
60ad608de0SBarry Smith     This is not intended as a replacement for pivoting for matrices that
61*d252947aSBarry Smith     have ``bad'' structure. It is only a stop-gap measure. Should be called
62*d252947aSBarry Smith     after a call to MatGetReordering(), this routine changes the column
63*d252947aSBarry Smith     ordering defined in cis.
64*d252947aSBarry Smith 
65*d252947aSBarry Smith     Options Database: (When using SLES)
66*d252947aSBarry Smith .      -pc_ilu_nonzeros_along_diagonal
67*d252947aSBarry Smith .      -pc_lu_nonzeros_along_diagonal
68ad608de0SBarry Smith 
69ad608de0SBarry Smith     Algorithm:
70ad608de0SBarry Smith     Column pivoting is used.  Choice of column is made by looking at the
71ad608de0SBarry Smith     non-zero elements in the row.  This algorithm is simple and fast but
72*d252947aSBarry Smith     does NOT guarantee that a non-singular or well conditioned
73ad608de0SBarry Smith     principle submatrix will be produced.
74ad608de0SBarry Smith @*/
7548b35521SBarry Smith int MatReorderForNonzeroDiagonal(Mat mat,double atol,IS ris,IS cis )
76ad608de0SBarry Smith {
7748b35521SBarry Smith   int      ierr, prow, k, nz, n, repl, *j, *col, *row, m;
78d93a2b8dSBarry Smith   Scalar   *v;
79d93a2b8dSBarry Smith   double   repla;
80ad608de0SBarry Smith 
8190f02eecSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
8290f02eecSBarry Smith   PetscValidHeaderSpecific(ris,IS_COOKIE);
8390f02eecSBarry Smith   PetscValidHeaderSpecific(cis,IS_COOKIE);
8490f02eecSBarry Smith 
8548b35521SBarry Smith   ierr = ISGetIndices(ris,&row); CHKERRQ(ierr);
8648b35521SBarry Smith   ierr = ISGetIndices(cis,&col); CHKERRQ(ierr);
8748b35521SBarry Smith   ierr = MatGetSize(mat,&m,&n); CHKERRQ(ierr);
88ad608de0SBarry Smith 
89ad608de0SBarry Smith   for (prow=0; prow<n; prow++) {
9090f02eecSBarry Smith     ierr = MatGetRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr);
9148b35521SBarry Smith     for (k=0; k<nz; k++) {if (col[j[k]] == prow) break;}
92cddf8d76SBarry Smith     if (k >= nz || PetscAbsScalar(v[k]) <= atol) {
93ad608de0SBarry Smith       /* Element too small or zero; find the best candidate */
94ad608de0SBarry Smith       repl  = prow;
95cddf8d76SBarry Smith       repla = (k >= nz) ? 0.0 : PetscAbsScalar(v[k]);
9648b35521SBarry Smith       for (k=0; k<nz; k++) {
97cddf8d76SBarry Smith 	if (col[j[k]] > prow && PetscAbsScalar(v[k]) > repla) {
98ad608de0SBarry Smith 	  repl = col[j[k]];
99cddf8d76SBarry Smith 	  repla = PetscAbsScalar(v[k]);
100ad608de0SBarry Smith         }
10148b35521SBarry Smith       }
102ad608de0SBarry Smith       if (prow == repl) {
103ad608de0SBarry Smith 	    /* Now we need to look for an element that allows us
104ad608de0SBarry Smith 	       to pivot with a previous column.  To do this, we need
105ad608de0SBarry Smith 	       to be sure that we don't introduce a zero in a previous
106ad608de0SBarry Smith 	       diagonal */
107d5d45c9bSBarry Smith         if (!MatZeroFindPre_Private(mat,prow,row,col,repla,atol,&repl,&repla)){
108e3372554SBarry Smith 	  SETERRQ(1,0,"Can not reorder matrix");
109ad608de0SBarry Smith 	}
110ad608de0SBarry Smith       }
111ad608de0SBarry Smith       SWAP(col[prow],col[repl]);
112ad608de0SBarry Smith     }
11390f02eecSBarry Smith     ierr = MatRestoreRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr);
114ad608de0SBarry Smith   }
11548b35521SBarry Smith   ierr = ISRestoreIndices(ris,&row); CHKERRQ(ierr);
11648b35521SBarry Smith   ierr = ISRestoreIndices(cis,&col); CHKERRQ(ierr);
117ad608de0SBarry Smith   return 0;
118ad608de0SBarry Smith }
11948b35521SBarry Smith 
12048b35521SBarry Smith 
12148b35521SBarry Smith 
122