xref: /petsc/src/dm/tests/ex35.c (revision d5b43468fb8780a8feea140ccd6fa3e6a50411cc)
1 
2 static char help[] = "MatLoad test for loading matrices that are created by DMCreateMatrix() and\n\
3                       stored in binary via MatView_MPI_DA.MatView_MPI_DA stores the matrix\n\
4                       in natural ordering. Hence MatLoad() has to read the matrix first in\n\
5                       natural ordering and then permute it back to the application ordering.This\n\
6                       example is used for testing the subroutine MatLoad_MPI_DA\n\n";
7 
8 #include <petscdm.h>
9 #include <petscdmda.h>
10 
11 int main(int argc, char **argv)
12 {
13   PetscInt    X = 10, Y = 8, Z = 8;
14   DM          da;
15   PetscViewer viewer;
16   Mat         A;
17 
18   PetscFunctionBeginUser;
19   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
20   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "temp.dat", FILE_MODE_WRITE, &viewer));
21 
22   /* Read options */
23   PetscCall(PetscOptionsGetInt(NULL, NULL, "-X", &X, NULL));
24   PetscCall(PetscOptionsGetInt(NULL, NULL, "-Y", &Y, NULL));
25   PetscCall(PetscOptionsGetInt(NULL, NULL, "-Z", &Z, NULL));
26 
27   /* Create distributed array and get vectors */
28   PetscCall(DMDACreate3d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DMDA_STENCIL_STAR, X, Y, Z, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE, 1, 1, NULL, NULL, NULL, &da));
29   PetscCall(DMSetFromOptions(da));
30   PetscCall(DMSetUp(da));
31   PetscCall(DMSetMatType(da, MATMPIAIJ));
32   PetscCall(DMCreateMatrix(da, &A));
33   PetscCall(MatShift(A, X));
34   PetscCall(MatView(A, viewer));
35   PetscCall(MatDestroy(&A));
36   PetscCall(PetscViewerDestroy(&viewer));
37 
38   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "temp.dat", FILE_MODE_READ, &viewer));
39   PetscCall(DMCreateMatrix(da, &A));
40   PetscCall(MatLoad(A, viewer));
41 
42   /* Free memory */
43   PetscCall(MatDestroy(&A));
44   PetscCall(PetscViewerDestroy(&viewer));
45   PetscCall(DMDestroy(&da));
46   PetscCall(PetscFinalize());
47   return 0;
48 }
49