xref: /petsc/src/ksp/pc/impls/spai/dspai.c (revision bcaeba4d41d6ca6c6dc4189db20683073a9959ce)
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++) {
31       fprintf(file,"%d %d %16.14e\n",i+1,cols[j]+1,vals[j]);
32     }
33     ierr     = MatRestoreRow(A,i,&nz,&cols,&vals);CHKERRQ(ierr);
34   }
35 
36   PetscFunctionReturn(0);
37 }
38 
39 PetscErrorCode  VecDumpSPAI(Vec b,FILE *file)
40 {
41   PetscErrorCode ierr;
42   int    n,i;
43   PetscScalar *array;
44 
45   ierr = VecGetSize(b,&n);CHKERRQ(ierr);
46   ierr = VecGetArray(b,&array);CHKERRQ(ierr);
47 
48   fprintf(file,"%d\n",n);
49   for (i=0; i<n; i++) {
50     fprintf(file,"%d %16.14e\n",i+1,array[i]);
51   }
52 
53   PetscFunctionReturn(0);
54 }
55