1ad608de0SBarry Smith #ifndef lint 2*90f02eecSBarry Smith static char vcid[] = "$Id: zerodiag.c,v 1.8 1996/08/08 14:44:19 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 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 { 20*90f02eecSBarry Smith int k, nz, repl, *j, kk, nnz, *jj,ierr; 2148b35521SBarry Smith Scalar *v, *vv; 2248b35521SBarry Smith 23*90f02eecSBarry Smith ierr = MatGetRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr); 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]]; 28*90f02eecSBarry Smith ierr = MatGetRow( mat, row[repl], &nnz, &jj, &vv ); CHKERRQ(ierr); 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; 33*90f02eecSBarry Smith ierr = MatRestoreRow( mat, row[repl], &nnz, &jj, &vv ); CHKERRQ(ierr); 34*90f02eecSBarry Smith ierr = MatRestoreRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr); 3548b35521SBarry Smith return 1; 3648b35521SBarry Smith } 3748b35521SBarry Smith } 38*90f02eecSBarry Smith ierr = MatRestoreRow( mat, row[repl], &nnz, &jj, &vv ); CHKERRQ(ierr); 3948b35521SBarry Smith } 4048b35521SBarry Smith } 41*90f02eecSBarry Smith ierr = MatRestoreRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr); 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 71*90f02eecSBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE); 72*90f02eecSBarry Smith PetscValidHeaderSpecific(ris,IS_COOKIE); 73*90f02eecSBarry Smith PetscValidHeaderSpecific(cis,IS_COOKIE); 74*90f02eecSBarry Smith 7548b35521SBarry Smith ierr = ISGetIndices(ris,&row); CHKERRQ(ierr); 7648b35521SBarry Smith ierr = ISGetIndices(cis,&col); CHKERRQ(ierr); 7748b35521SBarry Smith ierr = MatGetSize(mat,&m,&n); CHKERRQ(ierr); 78ad608de0SBarry Smith 79ad608de0SBarry Smith for (prow=0; prow<n; prow++) { 80*90f02eecSBarry Smith ierr = MatGetRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr); 8148b35521SBarry Smith for (k=0; k<nz; k++) {if (col[j[k]] == prow) break;} 82cddf8d76SBarry Smith if (k >= nz || PetscAbsScalar(v[k]) <= atol) { 83ad608de0SBarry Smith /* Element too small or zero; find the best candidate */ 84ad608de0SBarry Smith repl = prow; 85cddf8d76SBarry Smith repla = (k >= nz) ? 0.0 : PetscAbsScalar(v[k]); 8648b35521SBarry Smith for (k=0; k<nz; k++) { 87cddf8d76SBarry Smith if (col[j[k]] > prow && PetscAbsScalar(v[k]) > repla) { 88ad608de0SBarry Smith repl = col[j[k]]; 89cddf8d76SBarry Smith repla = PetscAbsScalar(v[k]); 90ad608de0SBarry Smith } 9148b35521SBarry Smith } 92ad608de0SBarry Smith if (prow == repl) { 93ad608de0SBarry Smith /* Now we need to look for an element that allows us 94ad608de0SBarry Smith to pivot with a previous column. To do this, we need 95ad608de0SBarry Smith to be sure that we don't introduce a zero in a previous 96ad608de0SBarry Smith diagonal */ 97d5d45c9bSBarry Smith if (!MatZeroFindPre_Private(mat,prow,row,col,repla,atol,&repl,&repla)){ 98bbb6d6a8SBarry Smith SETERRQ(1,"MatReorderForNonzeroDiagonal:Can not reorder matrix"); 99ad608de0SBarry Smith } 100ad608de0SBarry Smith } 101ad608de0SBarry Smith SWAP(col[prow],col[repl]); 102ad608de0SBarry Smith } 103*90f02eecSBarry Smith ierr = MatRestoreRow( mat, row[prow], &nz, &j, &v ); CHKERRQ(ierr); 104ad608de0SBarry Smith } 10548b35521SBarry Smith ierr = ISRestoreIndices(ris,&row); CHKERRQ(ierr); 10648b35521SBarry Smith ierr = ISRestoreIndices(cis,&col); CHKERRQ(ierr); 107ad608de0SBarry Smith return 0; 108ad608de0SBarry Smith } 10948b35521SBarry Smith 11048b35521SBarry Smith 11148b35521SBarry Smith 112