1ad608de0SBarry Smith #ifndef lint 2*d93a2b8dSBarry Smith static char vcid[] = "$Id: zerodiag.c,v 1.2 1995/07/06 17:20:03 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 1048b35521SBarry Smith #include "matimpl.h" 11ad608de0SBarry Smith #include <math.h> 12ad608de0SBarry Smith 1348b35521SBarry Smith #define SWAP(a,b) {int _t; _t = a; a = b; b = _t; } 14*d93a2b8dSBarry Smith #if !defined(PETSC_COMPLEX) 15*d93a2b8dSBarry Smith #define ABS(a) fabs(a) 16*d93a2b8dSBarry Smith #else 17*d93a2b8dSBarry Smith #define ABS(a) abs(a) 18*d93a2b8dSBarry Smith #endif 1948b35521SBarry Smith 2048b35521SBarry Smith /* Given a current row and current permutation, find a column permutation 2148b35521SBarry Smith that removes a zero diagonal */ 22*d93a2b8dSBarry Smith int SpiZeroFindPre_Private(Mat mat,int prow,int* row,int* col,double repla, 23*d93a2b8dSBarry Smith double atol,int* rc,double* rcv ) 2448b35521SBarry Smith { 2548b35521SBarry Smith int k, nz, repl, *j, kk, nnz, *jj; 2648b35521SBarry Smith Scalar *v, *vv; 2748b35521SBarry Smith 2848b35521SBarry Smith MatGetRow( mat, row[prow], &nz, &j, &v ); 2948b35521SBarry Smith for (k=0; k<nz; k++) { 30*d93a2b8dSBarry Smith if (col[j[k]] < prow && ABS(v[k]) > repla) { 3148b35521SBarry Smith /* See if this one will work */ 3248b35521SBarry Smith repl = col[j[k]]; 3348b35521SBarry Smith MatGetRow( mat, row[repl], &nnz, &jj, &vv ); 3448b35521SBarry Smith for (kk=0; kk<nnz; kk++) { 35*d93a2b8dSBarry Smith if (col[jj[kk]] == prow && ABS(vv[kk]) > atol) { 36*d93a2b8dSBarry Smith *rcv = ABS(v[k]); 3748b35521SBarry Smith *rc = repl; 3848b35521SBarry Smith MatRestoreRow( mat, row[repl], &nnz, &jj, &vv ); 3948b35521SBarry Smith MatRestoreRow( mat, row[prow], &nz, &j, &v ); 4048b35521SBarry Smith return 1; 4148b35521SBarry Smith } 4248b35521SBarry Smith } 4348b35521SBarry Smith MatRestoreRow( mat, row[repl], &nnz, &jj, &vv ); 4448b35521SBarry Smith } 4548b35521SBarry Smith } 4648b35521SBarry Smith MatRestoreRow( mat, row[prow], &nz, &j, &v ); 4748b35521SBarry Smith return 0; 4848b35521SBarry Smith } 49ad608de0SBarry Smith 50ad608de0SBarry Smith /*@ 5148b35521SBarry Smith MatReorderForNonzeroDiagonal - Changes matrix ordering to remove 5248b35521SBarry Smith zeros from diagonal. This may help in the LU factorization to 5348b35521SBarry Smith prevent a zero pivot. 54ad608de0SBarry Smith 55ad608de0SBarry Smith Input Parameters: 56ad608de0SBarry Smith . mat - matrix to reorder 5748b35521SBarry Smith . rmap,cmap - row and column permutations. Usually obtained from 5848b35521SBarry Smith . MatGetReordering(). 59ad608de0SBarry Smith 60ad608de0SBarry Smith Notes: 61ad608de0SBarry Smith This is not intended as a replacement for pivoting for matrices that 6248b35521SBarry Smith have ``bad'' structure. It is only a stop-gap measure. 63ad608de0SBarry Smith 64ad608de0SBarry Smith Algorithm: 65ad608de0SBarry Smith Column pivoting is used. Choice of column is made by looking at the 66ad608de0SBarry Smith non-zero elements in the row. This algorithm is simple and fast but 67ad608de0SBarry Smith does NOT guarentee that a non-singular or well conditioned 68ad608de0SBarry Smith principle submatrix will be produced. 69ad608de0SBarry Smith @*/ 7048b35521SBarry Smith int MatReorderForNonzeroDiagonal(Mat mat,double atol,IS ris,IS cis ) 71ad608de0SBarry Smith { 7248b35521SBarry Smith int ierr, prow, k, nz, n, repl, *j, *col, *row, m; 73*d93a2b8dSBarry Smith Scalar *v; 74*d93a2b8dSBarry Smith double repla; 75ad608de0SBarry Smith 7648b35521SBarry Smith ierr = ISGetIndices(ris,&row); CHKERRQ(ierr); 7748b35521SBarry Smith ierr = ISGetIndices(cis,&col); CHKERRQ(ierr); 7848b35521SBarry Smith ierr = MatGetSize(mat,&m,&n); CHKERRQ(ierr); 79ad608de0SBarry Smith 80ad608de0SBarry Smith for (prow=0; prow<n; prow++) { 8148b35521SBarry Smith MatGetRow( mat, row[prow], &nz, &j, &v ); 8248b35521SBarry Smith for (k=0; k<nz; k++) {if (col[j[k]] == prow) break;} 83*d93a2b8dSBarry Smith if (k >= nz || ABS(v[k]) <= atol) { 84ad608de0SBarry Smith /* Element too small or zero; find the best candidate */ 85ad608de0SBarry Smith repl = prow; 86*d93a2b8dSBarry Smith repla = (k >= nz) ? 0.0 : ABS(v[k]); 8748b35521SBarry Smith for (k=0; k<nz; k++) { 88*d93a2b8dSBarry Smith if (col[j[k]] > prow && ABS(v[k]) > repla) { 89ad608de0SBarry Smith repl = col[j[k]]; 90*d93a2b8dSBarry Smith repla = ABS(v[k]); 91ad608de0SBarry Smith } 9248b35521SBarry Smith } 93ad608de0SBarry Smith if (prow == repl) { 94ad608de0SBarry Smith /* Now we need to look for an element that allows us 95ad608de0SBarry Smith to pivot with a previous column. To do this, we need 96ad608de0SBarry Smith to be sure that we don't introduce a zero in a previous 97ad608de0SBarry Smith diagonal */ 9848b35521SBarry Smith if (!SpiZeroFindPre_Private(mat,prow,row,col,repla,atol,&repl,&repla)){ 9948b35521SBarry Smith SETERRQ(1,"Can not reorder matrix"); 100ad608de0SBarry Smith } 101ad608de0SBarry Smith } 102ad608de0SBarry Smith SWAP(col[prow],col[repl]); 103ad608de0SBarry Smith } 10448b35521SBarry Smith MatRestoreRow( mat, row[prow], &nz, &j, &v ); 105ad608de0SBarry Smith } 10648b35521SBarry Smith ierr = ISRestoreIndices(ris,&row); CHKERRQ(ierr); 10748b35521SBarry Smith ierr = ISRestoreIndices(cis,&col); CHKERRQ(ierr); 108ad608de0SBarry Smith return 0; 109ad608de0SBarry Smith } 11048b35521SBarry Smith 11148b35521SBarry Smith 11248b35521SBarry Smith 113