1ad608de0SBarry Smith #ifndef lint 2*70f55243SBarry Smith static char vcid[] = "$Id: zerodiag.c,v 1.7 1995/11/02 04:19:15 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 10*70f55243SBarry 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 1548b35521SBarry Smith /* Given a current row and current permutation, find a column permutation 1648b35521SBarry Smith that removes a zero diagonal */ 17d5d45c9bSBarry Smith int MatZeroFindPre_Private(Mat mat,int prow,int* row,int* col,double repla, 18d93a2b8dSBarry Smith double atol,int* rc,double* rcv ) 1948b35521SBarry Smith { 2048b35521SBarry Smith int k, nz, repl, *j, kk, nnz, *jj; 2148b35521SBarry Smith Scalar *v, *vv; 2248b35521SBarry Smith 2348b35521SBarry Smith MatGetRow( mat, row[prow], &nz, &j, &v ); 2448b35521SBarry Smith for (k=0; k<nz; k++) { 25cddf8d76SBarry Smith if (col[j[k]] < prow && PetscAbsScalar(v[k]) > repla) { 2648b35521SBarry Smith /* See if this one will work */ 2748b35521SBarry Smith repl = col[j[k]]; 2848b35521SBarry Smith MatGetRow( mat, row[repl], &nnz, &jj, &vv ); 2948b35521SBarry Smith for (kk=0; kk<nnz; kk++) { 30cddf8d76SBarry Smith if (col[jj[kk]] == prow && PetscAbsScalar(vv[kk]) > atol) { 31cddf8d76SBarry Smith *rcv = PetscAbsScalar(v[k]); 3248b35521SBarry Smith *rc = repl; 3348b35521SBarry Smith MatRestoreRow( mat, row[repl], &nnz, &jj, &vv ); 3448b35521SBarry Smith MatRestoreRow( mat, row[prow], &nz, &j, &v ); 3548b35521SBarry Smith return 1; 3648b35521SBarry Smith } 3748b35521SBarry Smith } 3848b35521SBarry Smith MatRestoreRow( mat, row[repl], &nnz, &jj, &vv ); 3948b35521SBarry Smith } 4048b35521SBarry Smith } 4148b35521SBarry Smith MatRestoreRow( mat, row[prow], &nz, &j, &v ); 4248b35521SBarry Smith return 0; 4348b35521SBarry Smith } 44ad608de0SBarry Smith 45ad608de0SBarry Smith /*@ 4648b35521SBarry Smith MatReorderForNonzeroDiagonal - Changes matrix ordering to remove 4748b35521SBarry Smith zeros from diagonal. This may help in the LU factorization to 4848b35521SBarry Smith prevent a zero pivot. 49ad608de0SBarry Smith 50ad608de0SBarry Smith Input Parameters: 51ad608de0SBarry Smith . mat - matrix to reorder 5248b35521SBarry Smith . rmap,cmap - row and column permutations. Usually obtained from 5348b35521SBarry Smith . MatGetReordering(). 54ad608de0SBarry Smith 55ad608de0SBarry Smith Notes: 56ad608de0SBarry Smith This is not intended as a replacement for pivoting for matrices that 5748b35521SBarry Smith have ``bad'' structure. It is only a stop-gap measure. 58ad608de0SBarry Smith 59ad608de0SBarry Smith Algorithm: 60ad608de0SBarry Smith Column pivoting is used. Choice of column is made by looking at the 61ad608de0SBarry Smith non-zero elements in the row. This algorithm is simple and fast but 62ad608de0SBarry Smith does NOT guarentee that a non-singular or well conditioned 63ad608de0SBarry Smith principle submatrix will be produced. 64ad608de0SBarry Smith @*/ 6548b35521SBarry Smith int MatReorderForNonzeroDiagonal(Mat mat,double atol,IS ris,IS cis ) 66ad608de0SBarry Smith { 6748b35521SBarry Smith int ierr, prow, k, nz, n, repl, *j, *col, *row, m; 68d93a2b8dSBarry Smith Scalar *v; 69d93a2b8dSBarry Smith double repla; 70ad608de0SBarry Smith 7148b35521SBarry Smith ierr = ISGetIndices(ris,&row); CHKERRQ(ierr); 7248b35521SBarry Smith ierr = ISGetIndices(cis,&col); CHKERRQ(ierr); 7348b35521SBarry Smith ierr = MatGetSize(mat,&m,&n); CHKERRQ(ierr); 74ad608de0SBarry Smith 75ad608de0SBarry Smith for (prow=0; prow<n; prow++) { 7648b35521SBarry Smith MatGetRow( mat, row[prow], &nz, &j, &v ); 7748b35521SBarry Smith for (k=0; k<nz; k++) {if (col[j[k]] == prow) break;} 78cddf8d76SBarry Smith if (k >= nz || PetscAbsScalar(v[k]) <= atol) { 79ad608de0SBarry Smith /* Element too small or zero; find the best candidate */ 80ad608de0SBarry Smith repl = prow; 81cddf8d76SBarry Smith repla = (k >= nz) ? 0.0 : PetscAbsScalar(v[k]); 8248b35521SBarry Smith for (k=0; k<nz; k++) { 83cddf8d76SBarry Smith if (col[j[k]] > prow && PetscAbsScalar(v[k]) > repla) { 84ad608de0SBarry Smith repl = col[j[k]]; 85cddf8d76SBarry Smith repla = PetscAbsScalar(v[k]); 86ad608de0SBarry Smith } 8748b35521SBarry Smith } 88ad608de0SBarry Smith if (prow == repl) { 89ad608de0SBarry Smith /* Now we need to look for an element that allows us 90ad608de0SBarry Smith to pivot with a previous column. To do this, we need 91ad608de0SBarry Smith to be sure that we don't introduce a zero in a previous 92ad608de0SBarry Smith diagonal */ 93d5d45c9bSBarry Smith if (!MatZeroFindPre_Private(mat,prow,row,col,repla,atol,&repl,&repla)){ 94bbb6d6a8SBarry Smith SETERRQ(1,"MatReorderForNonzeroDiagonal:Can not reorder matrix"); 95ad608de0SBarry Smith } 96ad608de0SBarry Smith } 97ad608de0SBarry Smith SWAP(col[prow],col[repl]); 98ad608de0SBarry Smith } 9948b35521SBarry Smith MatRestoreRow( mat, row[prow], &nz, &j, &v ); 100ad608de0SBarry Smith } 10148b35521SBarry Smith ierr = ISRestoreIndices(ris,&row); CHKERRQ(ierr); 10248b35521SBarry Smith ierr = ISRestoreIndices(cis,&col); CHKERRQ(ierr); 103ad608de0SBarry Smith return 0; 104ad608de0SBarry Smith } 10548b35521SBarry Smith 10648b35521SBarry Smith 10748b35521SBarry Smith 108