Actual source code: dspai.c

      1: #define PETSCKSP_DLL

      3: #include "petscmat.h"

      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.

     11: */
     12: PetscErrorCode PETSCKSP_DLLEXPORT MatDumpSPAI(Mat A,FILE *file)
     13: {
     14:   const PetscScalar *vals;
     16:   int               i,j,n,size,nz;
     17:   const int         *cols;
     18:   MPI_Comm          comm;

     20:   PetscObjectGetComm((PetscObject)A,&comm);
     21:
     22:   MPI_Comm_size(comm,&size);
     23:   if (size > 1) SETERRQ(PETSC_ERR_SUP,"Only single processor dumps");

     25:   MatGetSize(A,&n,&n);

     27:   /* print the matrix */
     28:   fprintf(file,"%d\n",n);
     29:   for (i=0; i<n; i++) {
     30:     MatGetRow(A,i,&nz,&cols,&vals);
     31:     for (j=0; j<nz; j++) {
     32:       fprintf(file,"%d %d %16.14e\n",i+1,cols[j]+1,vals[j]);
     33:     }
     34:     MatRestoreRow(A,i,&nz,&cols,&vals);
     35:   }

     37:   return(0);
     38: }

     40: PetscErrorCode PETSCKSP_DLLEXPORT VecDumpSPAI(Vec b,FILE *file)
     41: {
     43:   int    n,i;
     44:   PetscScalar *array;

     46:   VecGetSize(b,&n);
     47:   VecGetArray(b,&array);

     49:   fprintf(file,"%d\n",n);
     50:   for (i=0; i<n; i++) {
     51:     fprintf(file,"%d %16.14e\n",i+1,array[i]);
     52:   }

     54:   return(0);
     55: }