xref: /petsc/src/dm/tutorials/ex9.c (revision b122ec5aa1bd4469eb4e0673542fb7de3f411254)
1 static char help[] = "Demonstrates HDF5 vector input/ouput\n\n";
2 
3 /*T
4    Concepts: viewers
5    Concepts: HDF5
6    Processors: n
7 T*/
8 #include <petscsys.h>
9 #include <petscdm.h>
10 #include <petscdmda.h>
11 #include <petscviewerhdf5.h>
12 
13 int main(int argc,char **argv)
14 {
15   PetscErrorCode ierr;
16   PetscViewer    viewer;
17   DM             da;
18   Vec            global,local,global2;
19   PetscMPIInt    rank;
20   PetscBool      flg;
21   PetscInt       ndof;
22 
23   /*
24     Every PETSc routine should begin with the PetscInitialize() routine.
25     argc, argv - These command line arguments are taken to extract the options
26                  supplied to PETSc and options supplied to MPI.
27     help       - When PETSc executable is invoked with the option -help,
28                  it prints the various options that can be applied at
29                  runtime.  The user can use the "help" variable place
30                  additional help messages in this printout.
31   */
32   CHKERRQ(PetscInitialize(&argc,&argv,(char*)0,help));
33   /* Get number of DOF's from command line */
34   ierr = PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"DMDA VecView/VecLoad example","");CHKERRQ(ierr);
35   {
36     ndof = 1;
37     PetscOptionsBoundedInt("-ndof","Number of DOF's in DMDA","",ndof,&ndof,NULL,1);
38   }
39   ierr = PetscOptionsEnd();CHKERRQ(ierr);
40 
41   /* Create a DMDA and an associated vector */
42   CHKERRQ(DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,100,90,PETSC_DECIDE,PETSC_DECIDE,ndof,1,NULL,NULL,&da));
43   CHKERRQ(DMSetFromOptions(da));
44   CHKERRQ(DMSetUp(da));
45   CHKERRQ(DMCreateGlobalVector(da,&global));
46   CHKERRQ(DMCreateLocalVector(da,&local));
47   CHKERRQ(VecSet(global,-1.0));
48   CHKERRQ(DMGlobalToLocalBegin(da,global,INSERT_VALUES,local));
49   CHKERRQ(DMGlobalToLocalEnd(da,global,INSERT_VALUES,local));
50   CHKERRMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
51   CHKERRQ(VecScale(local,rank+1));
52   CHKERRQ(DMLocalToGlobalBegin(da,local,ADD_VALUES,global));
53   CHKERRQ(DMLocalToGlobalEnd(da,local,ADD_VALUES,global));
54 
55   /* Create the HDF5 viewer for writing */
56   CHKERRQ(PetscViewerHDF5Open(PETSC_COMM_WORLD,"hdf5output.h5",FILE_MODE_WRITE,&viewer));
57   CHKERRQ(PetscViewerSetFromOptions(viewer));
58 
59   /* Write the Vec without one extra dimension for BS */
60   CHKERRQ(PetscViewerHDF5SetBaseDimension2(viewer, PETSC_FALSE));
61   CHKERRQ(PetscObjectSetName((PetscObject) global, "noBsDim"));
62   CHKERRQ(VecView(global,viewer));
63 
64   /* Write the Vec with one extra, 1-sized, dimension for BS */
65   CHKERRQ(PetscViewerHDF5SetBaseDimension2(viewer, PETSC_TRUE));
66   CHKERRQ(PetscObjectSetName((PetscObject) global, "bsDim"));
67   CHKERRQ(VecView(global,viewer));
68 
69   CHKERRQ(PetscViewerDestroy(&viewer));
70   CHKERRMPI(MPI_Barrier(PETSC_COMM_WORLD));
71   CHKERRQ(VecDuplicate(global,&global2));
72 
73   /* Create the HDF5 viewer for reading */
74   CHKERRQ(PetscViewerHDF5Open(PETSC_COMM_WORLD,"hdf5output.h5",FILE_MODE_READ,&viewer));
75   CHKERRQ(PetscViewerSetFromOptions(viewer));
76 
77   /* Load the Vec without the BS dim and compare */
78   CHKERRQ(PetscObjectSetName((PetscObject) global2, "noBsDim"));
79   CHKERRQ(VecLoad(global2,viewer));
80 
81   CHKERRQ(VecEqual(global,global2,&flg));
82   if (!flg) {
83     CHKERRQ(PetscPrintf(PETSC_COMM_WORLD,"Error: Vectors are not equal\n"));
84   }
85 
86   /* Load the Vec with one extra, 1-sized, BS dim and compare */
87   CHKERRQ(PetscObjectSetName((PetscObject) global2, "bsDim"));
88   CHKERRQ(VecLoad(global2,viewer));
89   CHKERRQ(PetscViewerDestroy(&viewer));
90 
91   CHKERRQ(VecEqual(global,global2,&flg));
92   if (!flg) {
93     CHKERRQ(PetscPrintf(PETSC_COMM_WORLD,"Error: Vectors are not equal\n"));
94   }
95 
96   /* clean up and exit */
97   CHKERRQ(VecDestroy(&local));
98   CHKERRQ(VecDestroy(&global));
99   CHKERRQ(VecDestroy(&global2));
100   CHKERRQ(DMDestroy(&da));
101 
102   CHKERRQ(PetscFinalize());
103   return 0;
104 }
105 
106 /*TEST
107 
108       build:
109          requires: hdf5
110 
111       test:
112          nsize: 4
113 
114       test:
115          nsize: 4
116          suffix: 2
117          args: -ndof 2
118          output_file: output/ex9_1.out
119 
120 TEST*/
121