101a81e61SBarry Smith 2*c6db04a5SJed Brown #include <petscmat.h> 301a81e61SBarry Smith 401a81e61SBarry Smith /* 501a81e61SBarry Smith MatDumpSPAI - Dumps a PETSc matrix to a file in an ASCII format 601a81e61SBarry Smith suitable for the SPAI code of Stephen Barnard to solve. This routine 701a81e61SBarry Smith is simply here to allow testing of matrices directly with the SPAI 801a81e61SBarry Smith code, rather then through the PETSc interface. 901a81e61SBarry Smith 1001a81e61SBarry Smith */ 117087cfbeSBarry Smith PetscErrorCode MatDumpSPAI(Mat A,FILE *file) 1201a81e61SBarry Smith { 1301a81e61SBarry Smith const PetscScalar *vals; 1401a81e61SBarry Smith PetscErrorCode ierr; 1501a81e61SBarry Smith int i,j,n,size,nz; 1601a81e61SBarry Smith const int *cols; 1701a81e61SBarry Smith MPI_Comm comm; 1801a81e61SBarry Smith 1901a81e61SBarry Smith PetscObjectGetComm((PetscObject)A,&comm); 2001a81e61SBarry Smith 2101a81e61SBarry Smith MPI_Comm_size(comm,&size); 22e7e72b3dSBarry Smith if (size > 1) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_SUP,"Only single processor dumps"); 2301a81e61SBarry Smith 2401a81e61SBarry Smith ierr = MatGetSize(A,&n,&n);CHKERRQ(ierr); 2501a81e61SBarry Smith 2601a81e61SBarry Smith /* print the matrix */ 2701a81e61SBarry Smith fprintf(file,"%d\n",n); 2801a81e61SBarry Smith for (i=0; i<n; i++) { 2901a81e61SBarry Smith ierr = MatGetRow(A,i,&nz,&cols,&vals);CHKERRQ(ierr); 3001a81e61SBarry Smith for (j=0; j<nz; j++) { 3101a81e61SBarry Smith fprintf(file,"%d %d %16.14e\n",i+1,cols[j]+1,vals[j]); 3201a81e61SBarry Smith } 3301a81e61SBarry Smith ierr = MatRestoreRow(A,i,&nz,&cols,&vals);CHKERRQ(ierr); 3401a81e61SBarry Smith } 3501a81e61SBarry Smith 3601a81e61SBarry Smith PetscFunctionReturn(0); 3701a81e61SBarry Smith } 3801a81e61SBarry Smith 397087cfbeSBarry Smith PetscErrorCode VecDumpSPAI(Vec b,FILE *file) 4001a81e61SBarry Smith { 4101a81e61SBarry Smith PetscErrorCode ierr; 4201a81e61SBarry Smith int n,i; 4301a81e61SBarry Smith PetscScalar *array; 4401a81e61SBarry Smith 4501a81e61SBarry Smith ierr = VecGetSize(b,&n);CHKERRQ(ierr); 4601a81e61SBarry Smith ierr = VecGetArray(b,&array);CHKERRQ(ierr); 4701a81e61SBarry Smith 4801a81e61SBarry Smith fprintf(file,"%d\n",n); 4901a81e61SBarry Smith for (i=0; i<n; i++) { 5001a81e61SBarry Smith fprintf(file,"%d %16.14e\n",i+1,array[i]); 5101a81e61SBarry Smith } 5201a81e61SBarry Smith 5301a81e61SBarry Smith PetscFunctionReturn(0); 5401a81e61SBarry Smith } 55