Actual source code: ex65.c


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

  4: #include <petscmat.h>

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

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

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

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

 31:   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "rect", FILE_MODE_WRITE, &view));
 32:   PetscCall(MatView(A, view));
 33:   PetscCall(PetscViewerDestroy(&view));

 35:   PetscCall(MatDestroy(&A));

 37:   PetscCall(PetscFinalize());
 38:   return 0;
 39: }

 41: /*TEST

 43:    test:

 45: TEST*/