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 */ 12*d71ae5a4SJacob Faibussowitsch PetscErrorCode MatDumpSPAI(Mat A, FILE *file) 13*d71ae5a4SJacob Faibussowitsch { 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 195f80ce2aSJacob Faibussowitsch PetscFunctionBegin; 2001a81e61SBarry Smith PetscObjectGetComm((PetscObject)A, &comm); 2101a81e61SBarry Smith MPI_Comm_size(comm, &size); 227827d75bSBarry Smith PetscCheck(size == 1, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Only single processor dumps"); 239566063dSJacob Faibussowitsch PetscCall(MatGetSize(A, &n, &n)); 2401a81e61SBarry Smith /* print the matrix */ 2501a81e61SBarry Smith fprintf(file, "%d\n", n); 2601a81e61SBarry Smith for (i = 0; i < n; i++) { 279566063dSJacob Faibussowitsch PetscCall(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]); 299566063dSJacob Faibussowitsch PetscCall(MatRestoreRow(A, i, &nz, &cols, &vals)); 3001a81e61SBarry Smith } 3101a81e61SBarry Smith PetscFunctionReturn(0); 3201a81e61SBarry Smith } 3301a81e61SBarry Smith 34*d71ae5a4SJacob Faibussowitsch PetscErrorCode VecDumpSPAI(Vec b, FILE *file) 35*d71ae5a4SJacob Faibussowitsch { 3601a81e61SBarry Smith int n, i; 3701a81e61SBarry Smith PetscScalar *array; 3801a81e61SBarry Smith 395f80ce2aSJacob Faibussowitsch PetscFunctionBegin; 409566063dSJacob Faibussowitsch PetscCall(VecGetSize(b, &n)); 419566063dSJacob Faibussowitsch PetscCall(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