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