Actual source code: ex65.c

  1: static char help[] = "Saves a rectangular sparse matrix to disk.\n\n";

  3: #include <petscmat.h>

  5: int main(int argc, char **args)
  6: {
  7:   Mat         A;
  8:   PetscInt    m = 100, n = 11, js[11], i, j, cnt;
  9:   PetscScalar values[11];
 10:   PetscViewer view;

 12:   PetscFunctionBeginUser;
 13:   PetscCall(PetscInitialize(&argc, &args, NULL, help));
 14:   PetscCall(MatCreateSeqAIJ(PETSC_COMM_WORLD, m, n, 20, 0, &A));

 16:   for (i = 0; i < n; i++) values[i] = (PetscReal)i;

 18:   for (i = 0; i < m; i++) {
 19:     cnt = 0;
 20:     if (i % 2) {
 21:       for (j = 0; j < n; j += 2) js[cnt++] = j;
 22:     }
 23:     PetscCall(MatSetValues(A, 1, &i, cnt, js, values, INSERT_VALUES));
 24:   }
 25:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
 26:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));

 28:   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "rect", FILE_MODE_WRITE, &view));
 29:   PetscCall(MatView(A, view));
 30:   PetscCall(PetscViewerDestroy(&view));

 32:   PetscCall(MatDestroy(&A));

 34:   PetscCall(PetscFinalize());
 35:   return 0;
 36: }

 38: /*TEST

 40:    test:

 42: TEST*/