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 int i, j, n, size, nz; 16 const int *cols; 17 MPI_Comm comm; 18 19 PetscFunctionBegin; 20 PetscObjectGetComm((PetscObject)A, &comm); 21 MPI_Comm_size(comm, &size); 22 PetscCheck(size == 1, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Only single processor dumps"); 23 PetscCall(MatGetSize(A, &n, &n)); 24 /* print the matrix */ 25 fprintf(file, "%d\n", n); 26 for (i = 0; i < n; i++) { 27 PetscCall(MatGetRow(A, i, &nz, &cols, &vals)); 28 for (j = 0; j < nz; j++) fprintf(file, "%d %d %16.14e\n", i + 1, cols[j] + 1, vals[j]); 29 PetscCall(MatRestoreRow(A, i, &nz, &cols, &vals)); 30 } 31 PetscFunctionReturn(0); 32 } 33 34 PetscErrorCode VecDumpSPAI(Vec b, FILE *file) 35 { 36 int n, i; 37 PetscScalar *array; 38 39 PetscFunctionBegin; 40 PetscCall(VecGetSize(b, &n)); 41 PetscCall(VecGetArray(b, &array)); 42 fprintf(file, "%d\n", n); 43 for (i = 0; i < n; i++) fprintf(file, "%d %16.14e\n", i + 1, array[i]); 44 PetscFunctionReturn(0); 45 } 46