xref: /petsc/src/mat/tests/ex65.c (revision 8fb5bd83c3955fefcf33a54e3bb66920a9fa884b)
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 {
8   Mat            A;
9   PetscInt       m = 100,n = 11,js[11],i,j,cnt;
10   PetscScalar    values[11];
11   PetscViewer    view;
12 
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) {
22         js[cnt++] = j;
23       }
24     } else {
25       ;
26     }
27     PetscCall(MatSetValues(A,1,&i,cnt,js,values,INSERT_VALUES));
28   }
29   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
30   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
31 
32   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,"rect",FILE_MODE_WRITE,&view));
33   PetscCall(MatView(A,view));
34   PetscCall(PetscViewerDestroy(&view));
35 
36   PetscCall(MatDestroy(&A));
37 
38   PetscCall(PetscFinalize());
39   return 0;
40 }
41 
42 /*TEST
43 
44    test:
45 
46 TEST*/
47