xref: /petsc/src/mat/utils/zerodiag.c (revision 5f944b446aff79ca3f481c6ccde8fa8f6547f772)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*5f944b44SBarry Smith static char vcid[] = "$Id: zerodiag.c,v 1.22 1998/04/24 02:16:14 bsmith 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"
17d4bb536fSBarry Smith /*
18d4bb536fSBarry Smith    Given a current row and current permutation, find a column permutation
19d4bb536fSBarry Smith    that removes a zero diagonal.
20d4bb536fSBarry Smith */
21d5d45c9bSBarry Smith int MatZeroFindPre_Private(Mat mat,int prow,int* row,int* col,double repla,
22d93a2b8dSBarry Smith                            double atol,int* rc,double* rcv )
2348b35521SBarry Smith {
2490f02eecSBarry Smith   int      k, nz, repl, *j, kk, nnz, *jj,ierr;
2548b35521SBarry Smith   Scalar   *v, *vv;
2648b35521SBarry Smith 
273a40ed3dSBarry Smith   PetscFunctionBegin;
2890f02eecSBarry Smith   ierr = MatGetRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr);
29d4bb536fSBarry Smith    /*
30d4bb536fSBarry Smith       Here one could sort the col[j[k]] to try to select the column closest
31d4bb536fSBarry Smith      to the diagonal (in the new ordering) that satisfies the criteria
32d4bb536fSBarry Smith   */
3348b35521SBarry Smith   for (k=0; k<nz; k++) {
34cddf8d76SBarry Smith     if (col[j[k]] < prow && PetscAbsScalar(v[k]) > repla) {
3548b35521SBarry Smith       /* See if this one will work */
3648b35521SBarry Smith       repl  = col[j[k]];
3790f02eecSBarry Smith       ierr = MatGetRow( mat, row[repl], &nnz, &jj, &vv ); CHKERRQ(ierr);
3848b35521SBarry Smith       for (kk=0; kk<nnz; kk++) {
39cddf8d76SBarry Smith 	if (col[jj[kk]] == prow && PetscAbsScalar(vv[kk]) > atol) {
40cddf8d76SBarry Smith 	  *rcv = PetscAbsScalar(v[k]);
4148b35521SBarry Smith 	  *rc  = repl;
4290f02eecSBarry Smith           ierr = MatRestoreRow( mat, row[repl], &nnz, &jj, &vv ); CHKERRQ(ierr);
4390f02eecSBarry Smith           ierr = MatRestoreRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr);
443a40ed3dSBarry Smith 	  PetscFunctionReturn(1);
4548b35521SBarry Smith 	}
4648b35521SBarry Smith       }
4790f02eecSBarry Smith       ierr = MatRestoreRow( mat, row[repl], &nnz, &jj, &vv ); CHKERRQ(ierr);
4848b35521SBarry Smith     }
4948b35521SBarry Smith   }
5090f02eecSBarry Smith   ierr = MatRestoreRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr);
513a40ed3dSBarry Smith   PetscFunctionReturn(0);
5248b35521SBarry Smith }
53ad608de0SBarry Smith 
545615d1e5SSatish Balay #undef __FUNC__
555615d1e5SSatish Balay #define __FUNC__ "MatReorderForNonzeroDiagonal"
56ad608de0SBarry Smith /*@
5748b35521SBarry Smith     MatReorderForNonzeroDiagonal - Changes matrix ordering to remove
5848b35521SBarry Smith     zeros from diagonal. This may help in the LU factorization to
5948b35521SBarry Smith     prevent a zero pivot.
60ad608de0SBarry Smith 
61fee21e36SBarry Smith     Collective on Mat
62fee21e36SBarry Smith 
6398a79cdbSBarry Smith     Input Parameters:
6498a79cdbSBarry Smith +   mat  - matrix to reorder
6598a79cdbSBarry Smith -   rmap,cmap - row and column permutations.  Usually obtained from
6698a79cdbSBarry Smith                MatGetReordering().
6798a79cdbSBarry Smith 
68ad608de0SBarry Smith     Notes:
69ad608de0SBarry Smith     This is not intended as a replacement for pivoting for matrices that
70d252947aSBarry Smith     have ``bad'' structure. It is only a stop-gap measure. Should be called
71d252947aSBarry Smith     after a call to MatGetReordering(), this routine changes the column
72d252947aSBarry Smith     ordering defined in cis.
73d252947aSBarry Smith 
74b259b22eSLois Curfman McInnes     Options Database Keys (When using SLES):
7598a79cdbSBarry Smith +      -pc_ilu_nonzeros_along_diagonal
7698a79cdbSBarry Smith -      -pc_lu_nonzeros_along_diagonal
77ad608de0SBarry Smith 
78ad608de0SBarry Smith     Algorithm:
79ad608de0SBarry Smith     Column pivoting is used.  Choice of column is made by looking at the
80ad608de0SBarry Smith     non-zero elements in the row.  This algorithm is simple and fast but
81d252947aSBarry Smith     does NOT guarantee that a non-singular or well conditioned
82ad608de0SBarry Smith     principle submatrix will be produced.
8398a79cdbSBarry Smith 
84ad608de0SBarry Smith @*/
8548b35521SBarry Smith int MatReorderForNonzeroDiagonal(Mat mat,double atol,IS ris,IS cis )
86ad608de0SBarry Smith {
8748b35521SBarry Smith   int      ierr, prow, k, nz, n, repl, *j, *col, *row, m;
88d93a2b8dSBarry Smith   Scalar   *v;
89d93a2b8dSBarry Smith   double   repla;
90ad608de0SBarry Smith 
913a40ed3dSBarry Smith   PetscFunctionBegin;
9290f02eecSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
9390f02eecSBarry Smith   PetscValidHeaderSpecific(ris,IS_COOKIE);
9490f02eecSBarry Smith   PetscValidHeaderSpecific(cis,IS_COOKIE);
9590f02eecSBarry Smith 
9648b35521SBarry Smith   ierr = ISGetIndices(ris,&row); CHKERRQ(ierr);
9748b35521SBarry Smith   ierr = ISGetIndices(cis,&col); CHKERRQ(ierr);
9848b35521SBarry Smith   ierr = MatGetSize(mat,&m,&n); CHKERRQ(ierr);
99ad608de0SBarry Smith 
100ad608de0SBarry Smith   for (prow=0; prow<n; prow++) {
10190f02eecSBarry Smith     ierr = MatGetRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr);
10248b35521SBarry Smith     for (k=0; k<nz; k++) {if (col[j[k]] == prow) break;}
103cddf8d76SBarry Smith     if (k >= nz || PetscAbsScalar(v[k]) <= atol) {
104ad608de0SBarry Smith       /* Element too small or zero; find the best candidate */
105ad608de0SBarry Smith       repl  = prow;
106cddf8d76SBarry Smith       repla = (k >= nz) ? 0.0 : PetscAbsScalar(v[k]);
107d4bb536fSBarry Smith       /*
108d4bb536fSBarry Smith         Here one could sort the col[j[k]] list to try to select the
109d4bb536fSBarry Smith         column closest to the diagonal in the new ordering. (Note have
110d4bb536fSBarry Smith         to permute the v[k] values as well, and use a fixed bound on the
111d4bb536fSBarry Smith         quality of repla rather then looking for the absolute largest.
112d4bb536fSBarry Smith       */
11348b35521SBarry Smith       for (k=0; k<nz; k++) {
114cddf8d76SBarry Smith 	if (col[j[k]] > prow && PetscAbsScalar(v[k]) > repla) {
115ad608de0SBarry Smith 	  repl  = col[j[k]];
116cddf8d76SBarry Smith 	  repla = PetscAbsScalar(v[k]);
117ad608de0SBarry Smith         }
11848b35521SBarry Smith       }
119ad608de0SBarry Smith       if (prow == repl) {
120*5f944b44SBarry Smith 	/*
121*5f944b44SBarry Smith            Look for an element that allows us
122ad608de0SBarry Smith 	   to pivot with a previous column.  To do this, we need
123ad608de0SBarry Smith 	   to be sure that we don't introduce a zero in a previous
124*5f944b44SBarry Smith 	   diagonal
125*5f944b44SBarry Smith         */
126d5d45c9bSBarry Smith         if (!MatZeroFindPre_Private(mat,prow,row,col,repla,atol,&repl,&repla)){
127a8c6a408SBarry Smith 	  SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,0,"Cannot reorder matrix to eliminate zero diagonal entry");
128ad608de0SBarry Smith 	}
129ad608de0SBarry Smith       }
130ad608de0SBarry Smith       SWAP(col[prow],col[repl]);
131ad608de0SBarry Smith     }
13290f02eecSBarry Smith     ierr = MatRestoreRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr);
133ad608de0SBarry Smith   }
13448b35521SBarry Smith   ierr = ISRestoreIndices(ris,&row); CHKERRQ(ierr);
13548b35521SBarry Smith   ierr = ISRestoreIndices(cis,&col); CHKERRQ(ierr);
1363a40ed3dSBarry Smith   PetscFunctionReturn(0);
137ad608de0SBarry Smith }
13848b35521SBarry Smith 
13948b35521SBarry Smith 
14048b35521SBarry Smith 
141