1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER 2*d4bb536fSBarry Smith static char vcid[] = "$Id: zerodiag.c,v 1.16 1997/07/09 20:56:43 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" 17*d4bb536fSBarry Smith /* 18*d4bb536fSBarry Smith Given a current row and current permutation, find a column permutation 19*d4bb536fSBarry Smith that removes a zero diagonal. 20*d4bb536fSBarry 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 2790f02eecSBarry Smith ierr = MatGetRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr); 28*d4bb536fSBarry Smith /* 29*d4bb536fSBarry Smith Here one could sort the col[j[k]] to try to select the column closest 30*d4bb536fSBarry Smith to the diagonal (in the new ordering) that satisfies the criteria 31*d4bb536fSBarry Smith */ 3248b35521SBarry Smith for (k=0; k<nz; k++) { 33cddf8d76SBarry Smith if (col[j[k]] < prow && PetscAbsScalar(v[k]) > repla) { 3448b35521SBarry Smith /* See if this one will work */ 3548b35521SBarry Smith repl = col[j[k]]; 3690f02eecSBarry Smith ierr = MatGetRow( mat, row[repl], &nnz, &jj, &vv ); CHKERRQ(ierr); 3748b35521SBarry Smith for (kk=0; kk<nnz; kk++) { 38cddf8d76SBarry Smith if (col[jj[kk]] == prow && PetscAbsScalar(vv[kk]) > atol) { 39cddf8d76SBarry Smith *rcv = PetscAbsScalar(v[k]); 4048b35521SBarry Smith *rc = repl; 4190f02eecSBarry Smith ierr = MatRestoreRow( mat, row[repl], &nnz, &jj, &vv ); CHKERRQ(ierr); 4290f02eecSBarry Smith ierr = MatRestoreRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr); 4348b35521SBarry Smith return 1; 4448b35521SBarry Smith } 4548b35521SBarry Smith } 4690f02eecSBarry Smith ierr = MatRestoreRow( mat, row[repl], &nnz, &jj, &vv ); CHKERRQ(ierr); 4748b35521SBarry Smith } 4848b35521SBarry Smith } 4990f02eecSBarry Smith ierr = MatRestoreRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr); 5048b35521SBarry Smith return 0; 5148b35521SBarry Smith } 52ad608de0SBarry Smith 535615d1e5SSatish Balay #undef __FUNC__ 545615d1e5SSatish Balay #define __FUNC__ "MatReorderForNonzeroDiagonal" 55ad608de0SBarry Smith /*@ 5648b35521SBarry Smith MatReorderForNonzeroDiagonal - Changes matrix ordering to remove 5748b35521SBarry Smith zeros from diagonal. This may help in the LU factorization to 5848b35521SBarry Smith prevent a zero pivot. 59ad608de0SBarry Smith 60ad608de0SBarry Smith Input Parameters: 61ad608de0SBarry Smith . mat - matrix to reorder 6248b35521SBarry Smith . rmap,cmap - row and column permutations. Usually obtained from 6348b35521SBarry Smith . MatGetReordering(). 64ad608de0SBarry Smith 65ad608de0SBarry Smith Notes: 66ad608de0SBarry Smith This is not intended as a replacement for pivoting for matrices that 67d252947aSBarry Smith have ``bad'' structure. It is only a stop-gap measure. Should be called 68d252947aSBarry Smith after a call to MatGetReordering(), this routine changes the column 69d252947aSBarry Smith ordering defined in cis. 70d252947aSBarry Smith 7151d1478bSLois Curfman McInnes Options Database Keys: (When using SLES) 72d252947aSBarry Smith . -pc_ilu_nonzeros_along_diagonal 73d252947aSBarry Smith . -pc_lu_nonzeros_along_diagonal 74ad608de0SBarry Smith 75ad608de0SBarry Smith Algorithm: 76ad608de0SBarry Smith Column pivoting is used. Choice of column is made by looking at the 77ad608de0SBarry Smith non-zero elements in the row. This algorithm is simple and fast but 78d252947aSBarry Smith does NOT guarantee that a non-singular or well conditioned 79ad608de0SBarry Smith principle submatrix will be produced. 80ad608de0SBarry Smith @*/ 8148b35521SBarry Smith int MatReorderForNonzeroDiagonal(Mat mat,double atol,IS ris,IS cis ) 82ad608de0SBarry Smith { 8348b35521SBarry Smith int ierr, prow, k, nz, n, repl, *j, *col, *row, m; 84d93a2b8dSBarry Smith Scalar *v; 85d93a2b8dSBarry Smith double repla; 86ad608de0SBarry Smith 8790f02eecSBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE); 8890f02eecSBarry Smith PetscValidHeaderSpecific(ris,IS_COOKIE); 8990f02eecSBarry Smith PetscValidHeaderSpecific(cis,IS_COOKIE); 9090f02eecSBarry Smith 9148b35521SBarry Smith ierr = ISGetIndices(ris,&row); CHKERRQ(ierr); 9248b35521SBarry Smith ierr = ISGetIndices(cis,&col); CHKERRQ(ierr); 9348b35521SBarry Smith ierr = MatGetSize(mat,&m,&n); CHKERRQ(ierr); 94ad608de0SBarry Smith 95ad608de0SBarry Smith for (prow=0; prow<n; prow++) { 9690f02eecSBarry Smith ierr = MatGetRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr); 9748b35521SBarry Smith for (k=0; k<nz; k++) {if (col[j[k]] == prow) break;} 98cddf8d76SBarry Smith if (k >= nz || PetscAbsScalar(v[k]) <= atol) { 99ad608de0SBarry Smith /* Element too small or zero; find the best candidate */ 100ad608de0SBarry Smith repl = prow; 101cddf8d76SBarry Smith repla = (k >= nz) ? 0.0 : PetscAbsScalar(v[k]); 102*d4bb536fSBarry Smith /* 103*d4bb536fSBarry Smith Here one could sort the col[j[k]] list to try to select the 104*d4bb536fSBarry Smith column closest to the diagonal in the new ordering. (Note have 105*d4bb536fSBarry Smith to permute the v[k] values as well, and use a fixed bound on the 106*d4bb536fSBarry Smith quality of repla rather then looking for the absolute largest. 107*d4bb536fSBarry Smith */ 10848b35521SBarry Smith for (k=0; k<nz; k++) { 109cddf8d76SBarry Smith if (col[j[k]] > prow && PetscAbsScalar(v[k]) > repla) { 110ad608de0SBarry Smith repl = col[j[k]]; 111cddf8d76SBarry Smith repla = PetscAbsScalar(v[k]); 112ad608de0SBarry Smith } 11348b35521SBarry Smith } 114ad608de0SBarry Smith if (prow == repl) { 115*d4bb536fSBarry Smith /* Look for an element that allows us 116ad608de0SBarry Smith to pivot with a previous column. To do this, we need 117ad608de0SBarry Smith to be sure that we don't introduce a zero in a previous 118ad608de0SBarry Smith diagonal */ 119d5d45c9bSBarry Smith if (!MatZeroFindPre_Private(mat,prow,row,col,repla,atol,&repl,&repla)){ 120*d4bb536fSBarry Smith SETERRQ(1,0,"Cannot reorder matrix to eliminate zero diagonal entry"); 121ad608de0SBarry Smith } 122ad608de0SBarry Smith } 123ad608de0SBarry Smith SWAP(col[prow],col[repl]); 124ad608de0SBarry Smith } 12590f02eecSBarry Smith ierr = MatRestoreRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr); 126ad608de0SBarry Smith } 12748b35521SBarry Smith ierr = ISRestoreIndices(ris,&row); CHKERRQ(ierr); 12848b35521SBarry Smith ierr = ISRestoreIndices(cis,&col); CHKERRQ(ierr); 129ad608de0SBarry Smith return 0; 130ad608de0SBarry Smith } 13148b35521SBarry Smith 13248b35521SBarry Smith 13348b35521SBarry Smith 134