xref: /petsc/src/mat/tests/ex136.c (revision 83d0d507e8eaf8d844b49c5dbd0d8d7c8cefa37b)
1 static char help[] = "Tests MatLoad() MatView() for MPIBAIJ.\n\n";
2 
3 #include <petscmat.h>
4 
5 int main(int argc, char **args)
6 {
7   Mat         A, B;
8   char        file[PETSC_MAX_PATH_LEN];
9   PetscBool   flg;
10   PetscViewer fd;
11 
12   PetscFunctionBeginUser;
13   PetscCall(PetscInitialize(&argc, &args, NULL, help));
14   PetscCall(PetscOptionsGetString(NULL, NULL, "-f", file, sizeof(file), &flg));
15   PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_USER, "Must indicate binary file with the -f option");
16 
17   /*
18      Open binary file.  Note that we use FILE_MODE_READ to indicate
19      reading from this file.
20   */
21   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, file, FILE_MODE_READ, &fd));
22 
23   /*
24      Load the matrix; then destroy the viewer.
25   */
26   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
27   PetscCall(MatSetFromOptions(A));
28   PetscCall(MatLoad(A, fd));
29   PetscCall(PetscViewerDestroy(&fd));
30 
31   /*
32      Open another binary file.  Note that we use FILE_MODE_WRITE to indicate writing to the file
33   */
34   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "fileoutput", FILE_MODE_WRITE, &fd));
35   PetscCall(PetscViewerBinarySetFlowControl(fd, 3));
36   /*
37      Save the matrix and vector; then destroy the viewer.
38   */
39   PetscCall(MatView(A, fd));
40   PetscCall(PetscViewerDestroy(&fd));
41 
42   /* load the new matrix */
43   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "fileoutput", FILE_MODE_READ, &fd));
44   PetscCall(MatCreate(PETSC_COMM_WORLD, &B));
45   PetscCall(MatSetFromOptions(B));
46   PetscCall(MatLoad(B, fd));
47   PetscCall(PetscViewerDestroy(&fd));
48 
49   PetscCall(MatEqual(A, B, &flg));
50   if (flg) {
51     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Matrices are equal\n"));
52   } else {
53     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Matrices are not equal\n"));
54   }
55 
56   PetscCall(MatDestroy(&A));
57   PetscCall(MatDestroy(&B));
58   PetscCall(PetscFinalize());
59   return 0;
60 }
61 
62 /*TEST
63 
64    test:
65       nsize: 3
66       requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
67       args: -f ${DATAFILESPATH}/matrices/cfd.2.100 -mat_view ascii::ascii_info
68 
69    test:
70       suffix: 2
71       nsize: 5
72       requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
73       args: -f ${DATAFILESPATH}/matrices/cfd.2.100 -mat_view ascii::ascii_info
74 
75    test:
76       suffix: 3
77       nsize: 7
78       requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
79       args: -f ${DATAFILESPATH}/matrices/cfd.2.100 -mat_view ascii::ascii_info
80 
81    test:
82       suffix: 4
83       nsize: 3
84       requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
85       args: -f ${DATAFILESPATH}/matrices/cfd.2.100 -mat_view ascii::ascii_info -mat_type baij
86 
87    test:
88       suffix: 5
89       nsize: 5
90       requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
91       args: -f ${DATAFILESPATH}/matrices/cfd.2.100 -mat_view ascii::ascii_info -mat_type baij
92 
93    test:
94       suffix: 6
95       nsize: 7
96       requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
97       args: -f ${DATAFILESPATH}/matrices/cfd.2.100 -mat_view ascii::ascii_info -mat_type baij
98 
99 TEST*/
100