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