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