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