xref: /petsc/src/mat/tests/ex65.c (revision a69119a591a03a9d906b29c0a4e9802e4d7c9795)
1 
2 static char help[] = "Saves a rectangular sparse matrix to disk.\n\n";
3 
4 #include <petscmat.h>
5 
6 int main(int argc, char **args) {
7   Mat         A;
8   PetscInt    m = 100, n = 11, js[11], i, j, cnt;
9   PetscScalar values[11];
10   PetscViewer view;
11 
12   PetscFunctionBeginUser;
13   PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
14   PetscCall(MatCreateSeqAIJ(PETSC_COMM_WORLD, m, n, 20, 0, &A));
15 
16   for (i = 0; i < n; i++) values[i] = (PetscReal)i;
17 
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     } else {
23       ;
24     }
25     PetscCall(MatSetValues(A, 1, &i, cnt, js, values, INSERT_VALUES));
26   }
27   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
28   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
29 
30   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "rect", FILE_MODE_WRITE, &view));
31   PetscCall(MatView(A, view));
32   PetscCall(PetscViewerDestroy(&view));
33 
34   PetscCall(MatDestroy(&A));
35 
36   PetscCall(PetscFinalize());
37   return 0;
38 }
39 
40 /*TEST
41 
42    test:
43 
44 TEST*/
45