xref: /petsc/src/mat/utils/zerodiag.c (revision 87828ca270d8140797fd4271705413c4ecfcb535)
1*87828ca2SBarry Smith /*$Id: zerodiag.c,v 1.43 2001/03/23 23:22:45 balay Exp bsmith $*/
2ad608de0SBarry Smith 
3ad608de0SBarry Smith /*
4ad608de0SBarry Smith     This file contains routines to reorder a matrix so that the diagonal
548b35521SBarry Smith     elements are nonzero.
6ad608de0SBarry Smith  */
7ad608de0SBarry Smith 
8e090d566SSatish Balay #include "src/mat/matimpl.h"       /*I  "petscmat.h"  I*/
9ad608de0SBarry Smith 
1048b35521SBarry Smith #define SWAP(a,b) {int _t; _t = a; a = b; b = _t; }
1148b35521SBarry Smith 
124a2ae208SSatish Balay #undef __FUNCT__
134a2ae208SSatish Balay #define __FUNCT__ "MatReorderForNonzeroDiagonal"
14ad608de0SBarry Smith /*@
1548b35521SBarry Smith     MatReorderForNonzeroDiagonal - Changes matrix ordering to remove
1648b35521SBarry Smith     zeros from diagonal. This may help in the LU factorization to
1748b35521SBarry Smith     prevent a zero pivot.
18ad608de0SBarry Smith 
19fee21e36SBarry Smith     Collective on Mat
20fee21e36SBarry Smith 
2198a79cdbSBarry Smith     Input Parameters:
2298a79cdbSBarry Smith +   mat  - matrix to reorder
2398a79cdbSBarry Smith -   rmap,cmap - row and column permutations.  Usually obtained from
2491e9ee9fSBarry Smith                MatGetOrdering().
2598a79cdbSBarry Smith 
2615091d37SBarry Smith     Level: intermediate
2715091d37SBarry Smith 
28ad608de0SBarry Smith     Notes:
29ad608de0SBarry Smith     This is not intended as a replacement for pivoting for matrices that
30d252947aSBarry Smith     have ``bad'' structure. It is only a stop-gap measure. Should be called
3191e9ee9fSBarry Smith     after a call to MatGetOrdering(), this routine changes the column
32d252947aSBarry Smith     ordering defined in cis.
33d252947aSBarry Smith 
34b259b22eSLois Curfman McInnes     Options Database Keys (When using SLES):
3598a79cdbSBarry Smith +      -pc_ilu_nonzeros_along_diagonal
3698a79cdbSBarry Smith -      -pc_lu_nonzeros_along_diagonal
37ad608de0SBarry Smith 
3833f51a72SBarry Smith     Algorithm Notes:
3933f51a72SBarry Smith     Column pivoting is used.
4033f51a72SBarry Smith 
4133f51a72SBarry Smith     1) Choice of column is made by looking at the
4233f51a72SBarry Smith        non-zero elements in the troublesome row for columns that are not yet
4333f51a72SBarry Smith        included (moving from left to right).
4433f51a72SBarry Smith 
4533f51a72SBarry Smith     2) If (1) fails we check all the columns to the left of the current row
46814823dcSBarry Smith        and see if one of them has could be swapped. It can be swapped if
47814823dcSBarry Smith        its corresponding row has a non-zero in the column it is being
48814823dcSBarry Smith        swapped with; to make sure the previous nonzero diagonal remains
49814823dcSBarry Smith        nonzero
5033f51a72SBarry Smith 
5198a79cdbSBarry Smith 
52ad608de0SBarry Smith @*/
53329f5518SBarry Smith int MatReorderForNonzeroDiagonal(Mat mat,PetscReal atol,IS ris,IS cis)
54ad608de0SBarry Smith {
5572af6520SSatish Balay   int      ierr,prow,k,nz,n,repl,*j,*col,*row,m,*icol,nnz,*jj,kk;
56*87828ca2SBarry Smith   PetscScalar   *v,*vv;
57329f5518SBarry Smith   PetscReal   repla;
58fd61f322SBarry Smith   IS       icis;
59ad608de0SBarry Smith 
603a40ed3dSBarry Smith   PetscFunctionBegin;
6190f02eecSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
6290f02eecSBarry Smith   PetscValidHeaderSpecific(ris,IS_COOKIE);
6390f02eecSBarry Smith   PetscValidHeaderSpecific(cis,IS_COOKIE);
6490f02eecSBarry Smith 
6548b35521SBarry Smith   ierr = ISGetIndices(ris,&row);CHKERRQ(ierr);
6648b35521SBarry Smith   ierr = ISGetIndices(cis,&col);CHKERRQ(ierr);
674c49b128SBarry Smith   ierr = ISInvertPermutation(cis,PETSC_DECIDE,&icis);CHKERRQ(ierr);
6833f51a72SBarry Smith   ierr = ISGetIndices(icis,&icol);CHKERRQ(ierr);
6948b35521SBarry Smith   ierr = MatGetSize(mat,&m,&n);CHKERRQ(ierr);
70ad608de0SBarry Smith 
71ad608de0SBarry Smith   for (prow=0; prow<n; prow++) {
7290f02eecSBarry Smith     ierr = MatGetRow(mat,row[prow],&nz,&j,&v);CHKERRQ(ierr);
7333f51a72SBarry Smith     for (k=0; k<nz; k++) {if (icol[j[k]] == prow) break;}
74cddf8d76SBarry Smith     if (k >= nz || PetscAbsScalar(v[k]) <= atol) {
75ad608de0SBarry Smith       /* Element too small or zero; find the best candidate */
76cddf8d76SBarry Smith       repla = (k >= nz) ? 0.0 : PetscAbsScalar(v[k]);
77d4bb536fSBarry Smith       /*
78fd61f322SBarry Smith           Look for a later column we can swap with this one
79d4bb536fSBarry Smith       */
8048b35521SBarry Smith       for (k=0; k<nz; k++) {
8133f51a72SBarry Smith 	if (icol[j[k]] > prow && PetscAbsScalar(v[k]) > repla) {
82fd61f322SBarry Smith           /* found a suitable later column */
8333f51a72SBarry Smith 	  repl  = icol[j[k]];
8433f51a72SBarry Smith           SWAP(icol[col[prow]],icol[col[repl]]);
85ad608de0SBarry Smith           SWAP(col[prow],col[repl]);
86fd61f322SBarry Smith           goto found;
87fd61f322SBarry Smith         }
88fd61f322SBarry Smith       }
89fd61f322SBarry Smith       /*
90fd61f322SBarry Smith            Did not find a suitable later column so look for an earlier column
91fd61f322SBarry Smith 	   We need to be sure that we don't introduce a zero in a previous
92fd61f322SBarry Smith 	   diagonal
93fd61f322SBarry Smith       */
94fd61f322SBarry Smith       for (k=0; k<nz; k++) {
95fd61f322SBarry Smith         if (icol[j[k]] < prow && PetscAbsScalar(v[k]) > repla) {
96fd61f322SBarry Smith           /* See if this one will work */
97fd61f322SBarry Smith           repl  = icol[j[k]];
98fd61f322SBarry Smith           ierr = MatGetRow(mat,row[repl],&nnz,&jj,&vv);CHKERRQ(ierr);
99fd61f322SBarry Smith           for (kk=0; kk<nnz; kk++) {
100fd61f322SBarry Smith             if (icol[jj[kk]] == prow && PetscAbsScalar(vv[kk]) > atol) {
101fd61f322SBarry Smith               ierr = MatRestoreRow(mat,row[repl],&nnz,&jj,&vv);CHKERRQ(ierr);
102fd61f322SBarry Smith               SWAP(icol[col[prow]],icol[col[repl]]);
103fd61f322SBarry Smith               SWAP(col[prow],col[repl]);
104fd61f322SBarry Smith               goto found;
105fd61f322SBarry Smith 	    }
106fd61f322SBarry Smith           }
107fd61f322SBarry Smith           ierr = MatRestoreRow(mat,row[repl],&nnz,&jj,&vv);CHKERRQ(ierr);
108fd61f322SBarry Smith         }
109fd61f322SBarry Smith       }
110fd61f322SBarry Smith       /*
111fd61f322SBarry Smith           No column  suitable; instead check all future rows
112fd61f322SBarry Smith           Note: this will be very slow
113fd61f322SBarry Smith       */
114fd61f322SBarry Smith       for (k=prow+1; k<n; k++) {
115fd61f322SBarry Smith         ierr = MatGetRow(mat,row[k],&nnz,&jj,&vv);CHKERRQ(ierr);
116fd61f322SBarry Smith         for (kk=0; kk<nnz; kk++) {
117fd61f322SBarry Smith           if (icol[jj[kk]] == prow && PetscAbsScalar(vv[kk]) > atol) {
118fd61f322SBarry Smith             /* found a row */
119fd61f322SBarry Smith             SWAP(row[prow],row[k]);
120fd61f322SBarry Smith             goto found;
121fd61f322SBarry Smith           }
122fd61f322SBarry Smith         }
123fd61f322SBarry Smith         ierr = MatRestoreRow(mat,row[k],&nnz,&jj,&vv);CHKERRQ(ierr);
124fd61f322SBarry Smith       }
125fd61f322SBarry Smith 
126fd61f322SBarry Smith       found:;
127ad608de0SBarry Smith     }
12890f02eecSBarry Smith     ierr = MatRestoreRow(mat,row[prow],&nz,&j,&v);CHKERRQ(ierr);
129ad608de0SBarry Smith   }
13048b35521SBarry Smith   ierr = ISRestoreIndices(ris,&row);CHKERRQ(ierr);
13148b35521SBarry Smith   ierr = ISRestoreIndices(cis,&col);CHKERRQ(ierr);
13233f51a72SBarry Smith   ierr = ISRestoreIndices(icis,&icol);CHKERRQ(ierr);
13333f51a72SBarry Smith   ierr = ISDestroy(icis);CHKERRQ(ierr);
1343a40ed3dSBarry Smith   PetscFunctionReturn(0);
135ad608de0SBarry Smith }
13648b35521SBarry Smith 
13748b35521SBarry Smith 
13848b35521SBarry Smith 
139