1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER 2*5f051944SBarry Smith static char vcid[] = "$Id: zerodiag.c,v 1.24 1998/11/04 16:18:38 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, 22ec201a9bSBarry Smith double atol,int* rc,double* rcv,int nz, int *j, Scalar *v) 2348b35521SBarry Smith { 24ec201a9bSBarry Smith int k, repl, kk, nnz, *jj,ierr; 25ec201a9bSBarry Smith Scalar *vv; 2648b35521SBarry Smith 273a40ed3dSBarry Smith PetscFunctionBegin; 28d4bb536fSBarry Smith /* 29d4bb536fSBarry Smith Here one could sort the col[j[k]] to try to select the column closest 30d4bb536fSBarry Smith to the diagonal (in the new ordering) that satisfies the criteria 31d4bb536fSBarry 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); 423a40ed3dSBarry Smith PetscFunctionReturn(1); 4348b35521SBarry Smith } 4448b35521SBarry Smith } 4590f02eecSBarry Smith ierr = MatRestoreRow( mat, row[repl], &nnz, &jj, &vv ); CHKERRQ(ierr); 4648b35521SBarry Smith } 4748b35521SBarry Smith } 483a40ed3dSBarry Smith PetscFunctionReturn(0); 4948b35521SBarry Smith } 50ad608de0SBarry Smith 515615d1e5SSatish Balay #undef __FUNC__ 525615d1e5SSatish Balay #define __FUNC__ "MatReorderForNonzeroDiagonal" 53ad608de0SBarry Smith /*@ 5448b35521SBarry Smith MatReorderForNonzeroDiagonal - Changes matrix ordering to remove 5548b35521SBarry Smith zeros from diagonal. This may help in the LU factorization to 5648b35521SBarry Smith prevent a zero pivot. 57ad608de0SBarry Smith 58fee21e36SBarry Smith Collective on Mat 59fee21e36SBarry Smith 6098a79cdbSBarry Smith Input Parameters: 6198a79cdbSBarry Smith + mat - matrix to reorder 6298a79cdbSBarry Smith - rmap,cmap - row and column permutations. Usually obtained from 6398a79cdbSBarry Smith MatGetReordering(). 6498a79cdbSBarry 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 71b259b22eSLois Curfman McInnes Options Database Keys (When using SLES): 7298a79cdbSBarry Smith + -pc_ilu_nonzeros_along_diagonal 7398a79cdbSBarry 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. 8098a79cdbSBarry Smith 81ad608de0SBarry Smith @*/ 8248b35521SBarry Smith int MatReorderForNonzeroDiagonal(Mat mat,double atol,IS ris,IS cis ) 83ad608de0SBarry Smith { 8448b35521SBarry Smith int ierr, prow, k, nz, n, repl, *j, *col, *row, m; 85d93a2b8dSBarry Smith Scalar *v; 86d93a2b8dSBarry Smith double repla; 87ad608de0SBarry Smith 883a40ed3dSBarry Smith PetscFunctionBegin; 8990f02eecSBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE); 9090f02eecSBarry Smith PetscValidHeaderSpecific(ris,IS_COOKIE); 9190f02eecSBarry Smith PetscValidHeaderSpecific(cis,IS_COOKIE); 9290f02eecSBarry Smith 9348b35521SBarry Smith ierr = ISGetIndices(ris,&row); CHKERRQ(ierr); 9448b35521SBarry Smith ierr = ISGetIndices(cis,&col); CHKERRQ(ierr); 9548b35521SBarry Smith ierr = MatGetSize(mat,&m,&n); CHKERRQ(ierr); 96ad608de0SBarry Smith 97ad608de0SBarry Smith for (prow=0; prow<n; prow++) { 9890f02eecSBarry Smith ierr = MatGetRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr); 9948b35521SBarry Smith for (k=0; k<nz; k++) {if (col[j[k]] == prow) break;} 100cddf8d76SBarry Smith if (k >= nz || PetscAbsScalar(v[k]) <= atol) { 101ad608de0SBarry Smith /* Element too small or zero; find the best candidate */ 102ad608de0SBarry Smith repl = prow; 103cddf8d76SBarry Smith repla = (k >= nz) ? 0.0 : PetscAbsScalar(v[k]); 104d4bb536fSBarry Smith /* 105d4bb536fSBarry Smith Here one could sort the col[j[k]] list to try to select the 106d4bb536fSBarry Smith column closest to the diagonal in the new ordering. (Note have 107d4bb536fSBarry Smith to permute the v[k] values as well, and use a fixed bound on the 108d4bb536fSBarry Smith quality of repla rather then looking for the absolute largest. 109d4bb536fSBarry Smith */ 11048b35521SBarry Smith for (k=0; k<nz; k++) { 111cddf8d76SBarry Smith if (col[j[k]] > prow && PetscAbsScalar(v[k]) > repla) { 112ad608de0SBarry Smith repl = col[j[k]]; 113cddf8d76SBarry Smith repla = PetscAbsScalar(v[k]); 114ad608de0SBarry Smith } 11548b35521SBarry Smith } 116ad608de0SBarry Smith if (prow == repl) { 1175f944b44SBarry Smith /* 1185f944b44SBarry Smith Look for an element that allows us 119ad608de0SBarry Smith to pivot with a previous column. To do this, we need 120ad608de0SBarry Smith to be sure that we don't introduce a zero in a previous 1215f944b44SBarry Smith diagonal 1225f944b44SBarry Smith */ 123ec201a9bSBarry Smith if (!MatZeroFindPre_Private(mat,prow,row,col,repla,atol,&repl,&repla,nz,j,v)){ 124*5f051944SBarry Smith (*PetscErrorPrintf)("Permuted row number %d\n",prow); 125a8c6a408SBarry Smith SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,0,"Cannot reorder matrix to eliminate zero diagonal entry"); 126ad608de0SBarry Smith } 127ad608de0SBarry Smith } 128ad608de0SBarry Smith SWAP(col[prow],col[repl]); 129ad608de0SBarry Smith } 13090f02eecSBarry Smith ierr = MatRestoreRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr); 131ad608de0SBarry Smith } 13248b35521SBarry Smith ierr = ISRestoreIndices(ris,&row); CHKERRQ(ierr); 13348b35521SBarry Smith ierr = ISRestoreIndices(cis,&col); CHKERRQ(ierr); 1343a40ed3dSBarry Smith PetscFunctionReturn(0); 135ad608de0SBarry Smith } 13648b35521SBarry Smith 13748b35521SBarry Smith 13848b35521SBarry Smith 139