xref: /petsc/src/ksp/pc/impls/spai/dspai.c (revision 5f80ce2ab25dff0f4601e710601cbbcecf323266)
101a81e61SBarry Smith 
2c6db04a5SJed Brown #include <petscmat.h>
3af0996ceSBarry Smith #include <petsc/private/petscimpl.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 */
127087cfbeSBarry Smith PetscErrorCode  MatDumpSPAI(Mat A,FILE *file)
1301a81e61SBarry Smith {
1401a81e61SBarry Smith   const PetscScalar *vals;
1501a81e61SBarry Smith   int               i,j,n,size,nz;
1601a81e61SBarry Smith   const int         *cols;
1701a81e61SBarry Smith   MPI_Comm          comm;
1801a81e61SBarry Smith 
19*5f80ce2aSJacob Faibussowitsch   PetscFunctionBegin;
2001a81e61SBarry Smith   PetscObjectGetComm((PetscObject)A,&comm);
2101a81e61SBarry Smith   MPI_Comm_size(comm,&size);
22*5f80ce2aSJacob Faibussowitsch   PetscCheck(size <= 1,PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Only single processor dumps");
23*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatGetSize(A,&n,&n));
2401a81e61SBarry Smith   /* print the matrix */
2501a81e61SBarry Smith   fprintf(file,"%d\n",n);
2601a81e61SBarry Smith   for (i=0; i<n; i++) {
27*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatGetRow(A,i,&nz,&cols,&vals));
282fa5cd67SKarl Rupp     for (j=0; j<nz; j++) fprintf(file,"%d %d %16.14e\n",i+1,cols[j]+1,vals[j]);
29*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatRestoreRow(A,i,&nz,&cols,&vals));
3001a81e61SBarry Smith   }
3101a81e61SBarry Smith   PetscFunctionReturn(0);
3201a81e61SBarry Smith }
3301a81e61SBarry Smith 
347087cfbeSBarry Smith PetscErrorCode  VecDumpSPAI(Vec b,FILE *file)
3501a81e61SBarry Smith {
3601a81e61SBarry Smith   int          n,i;
3701a81e61SBarry Smith   PetscScalar *array;
3801a81e61SBarry Smith 
39*5f80ce2aSJacob Faibussowitsch   PetscFunctionBegin;
40*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetSize(b,&n));
41*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetArray(b,&array));
4201a81e61SBarry Smith   fprintf(file,"%d\n",n);
432fa5cd67SKarl Rupp   for (i=0; i<n; i++) fprintf(file,"%d %16.14e\n",i+1,array[i]);
4401a81e61SBarry Smith   PetscFunctionReturn(0);
4501a81e61SBarry Smith }
46