xref: /petsc/src/ksp/pc/impls/spai/dspai.c (revision e7e72b3d0edcd0d15e7f68c03be08666507fc872)
101a81e61SBarry Smith #define PETSCKSP_DLL
201a81e61SBarry Smith 
301a81e61SBarry Smith #include "petscmat.h"
401a81e61SBarry Smith 
501a81e61SBarry Smith /*
601a81e61SBarry Smith      MatDumpSPAI - Dumps a PETSc matrix to a file in an ASCII format
701a81e61SBarry Smith   suitable for the SPAI code of Stephen Barnard to solve. This routine
801a81e61SBarry Smith   is simply here to allow testing of matrices directly with the SPAI
901a81e61SBarry Smith   code, rather then through the PETSc interface.
1001a81e61SBarry Smith 
1101a81e61SBarry Smith */
1201a81e61SBarry Smith PetscErrorCode PETSCKSP_DLLEXPORT MatDumpSPAI(Mat A,FILE *file)
1301a81e61SBarry Smith {
1401a81e61SBarry Smith   const PetscScalar *vals;
1501a81e61SBarry Smith   PetscErrorCode ierr;
1601a81e61SBarry Smith   int               i,j,n,size,nz;
1701a81e61SBarry Smith   const int         *cols;
1801a81e61SBarry Smith   MPI_Comm          comm;
1901a81e61SBarry Smith 
2001a81e61SBarry Smith   PetscObjectGetComm((PetscObject)A,&comm);
2101a81e61SBarry Smith 
2201a81e61SBarry Smith   MPI_Comm_size(comm,&size);
23*e7e72b3dSBarry Smith   if (size > 1) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_SUP,"Only single processor dumps");
2401a81e61SBarry Smith 
2501a81e61SBarry Smith   ierr = MatGetSize(A,&n,&n);CHKERRQ(ierr);
2601a81e61SBarry Smith 
2701a81e61SBarry Smith   /* print the matrix */
2801a81e61SBarry Smith   fprintf(file,"%d\n",n);
2901a81e61SBarry Smith   for (i=0; i<n; i++) {
3001a81e61SBarry Smith     ierr     = MatGetRow(A,i,&nz,&cols,&vals);CHKERRQ(ierr);
3101a81e61SBarry Smith     for (j=0; j<nz; j++) {
3201a81e61SBarry Smith       fprintf(file,"%d %d %16.14e\n",i+1,cols[j]+1,vals[j]);
3301a81e61SBarry Smith     }
3401a81e61SBarry Smith     ierr     = MatRestoreRow(A,i,&nz,&cols,&vals);CHKERRQ(ierr);
3501a81e61SBarry Smith   }
3601a81e61SBarry Smith 
3701a81e61SBarry Smith   PetscFunctionReturn(0);
3801a81e61SBarry Smith }
3901a81e61SBarry Smith 
4001a81e61SBarry Smith PetscErrorCode PETSCKSP_DLLEXPORT VecDumpSPAI(Vec b,FILE *file)
4101a81e61SBarry Smith {
4201a81e61SBarry Smith   PetscErrorCode ierr;
4301a81e61SBarry Smith   int    n,i;
4401a81e61SBarry Smith   PetscScalar *array;
4501a81e61SBarry Smith 
4601a81e61SBarry Smith   ierr = VecGetSize(b,&n);CHKERRQ(ierr);
4701a81e61SBarry Smith   ierr = VecGetArray(b,&array);CHKERRQ(ierr);
4801a81e61SBarry Smith 
4901a81e61SBarry Smith   fprintf(file,"%d\n",n);
5001a81e61SBarry Smith   for (i=0; i<n; i++) {
5101a81e61SBarry Smith     fprintf(file,"%d %16.14e\n",i+1,array[i]);
5201a81e61SBarry Smith   }
5301a81e61SBarry Smith 
5401a81e61SBarry Smith   PetscFunctionReturn(0);
5501a81e61SBarry Smith }
56