1 2 #include <petscmat.h> 3 4 /* 5 MatDumpSPAI - Dumps a PETSc matrix to a file in an ASCII format 6 suitable for the SPAI code of Stephen Barnard to solve. This routine 7 is simply here to allow testing of matrices directly with the SPAI 8 code, rather then through the PETSc interface. 9 10 */ 11 PetscErrorCode MatDumpSPAI(Mat A,FILE *file) 12 { 13 const PetscScalar *vals; 14 PetscErrorCode ierr; 15 int i,j,n,size,nz; 16 const int *cols; 17 MPI_Comm comm; 18 19 PetscObjectGetComm((PetscObject)A,&comm); 20 21 MPI_Comm_size(comm,&size); 22 if (size > 1) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_SUP,"Only single processor dumps"); 23 24 ierr = MatGetSize(A,&n,&n);CHKERRQ(ierr); 25 26 /* print the matrix */ 27 fprintf(file,"%d\n",n); 28 for (i=0; i<n; i++) { 29 ierr = MatGetRow(A,i,&nz,&cols,&vals);CHKERRQ(ierr); 30 for (j=0; j<nz; j++) fprintf(file,"%d %d %16.14e\n",i+1,cols[j]+1,vals[j]); 31 ierr = MatRestoreRow(A,i,&nz,&cols,&vals);CHKERRQ(ierr); 32 } 33 PetscFunctionReturn(0); 34 } 35 36 PetscErrorCode VecDumpSPAI(Vec b,FILE *file) 37 { 38 PetscErrorCode ierr; 39 int n,i; 40 PetscScalar *array; 41 42 ierr = VecGetSize(b,&n);CHKERRQ(ierr); 43 ierr = VecGetArray(b,&array);CHKERRQ(ierr); 44 45 fprintf(file,"%d\n",n); 46 for (i=0; i<n; i++) fprintf(file,"%d %16.14e\n",i+1,array[i]); 47 PetscFunctionReturn(0); 48 } 49