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