xref: /petsc/src/sys/tutorials/ex5.c (revision b122ec5aa1bd4469eb4e0673542fb7de3f411254)
1 
2 static char help[] = "Demonstrates using the PetscBag Object\n\n";
3 
4 /*T
5    Concepts: bags;
6    Processors: n
7 T*/
8 
9 #include <petscsys.h>
10 #include <petscbag.h>
11 #include <petscviewer.h>
12 
13 /*
14   Enum variables can be stored in a bag but require a string array
15   to name their fields.  The fourth entry in this example is the name
16   of the enum, the fifth is the prefix (none in this case), and the last
17   entry is the null string.
18 */
19 typedef enum {
20   THIS = 0, THAT = 1, THE_OTHER = 2
21 } YourChoice;
22 const char *EnumeratedChoices[] = {"THIS","THAT","THE_OTHER","EnumeratedChoices","",0};
23 
24 /*
25   Data structures can be used in a bag as long as they
26   are declared in the bag with a variable, not with a pointer.
27 */
28 typedef struct {
29   PetscReal x1,x2;
30 } TwoVec;
31 
32 /*
33   Define a C struct that will contain my program's parameters.
34 
35   A PETSc bag is merely a representation of a C struct that can be printed, saved to a file and loaded from a file.
36 */
37 typedef struct {
38   PetscScalar   W;
39   PetscReal     rho;
40   TwoVec        pos;
41   PetscInt      Ii;
42   PetscInt      iarray[3];
43   PetscReal     rarray[2];
44   PetscBool     T;
45   PetscBool     Tarray[3];
46   PetscDataType dt;
47   char          filename[PETSC_MAX_PATH_LEN];
48   YourChoice    which;
49 } Parameter;
50 
51 int main(int argc,char **argv)
52 {
53   PetscBag       bag;
54   Parameter      *params;
55   PetscViewer    viewer;
56   PetscBool      flg;
57   char           filename[PETSC_MAX_PATH_LEN] = "binaryoutput";
58 
59   /*
60     Every PETSc routine should begin with the PetscInitialize() routine.
61     argc, argv - These command line arguments are taken to extract the options
62                  supplied to PETSc and options supplied to MPI.
63     help       - When PETSc executable is invoked with the option -help,
64                  it prints the various options that can be applied at
65                  runtime.  The user can use the "help" variable place
66                  additional help messages in this printout.
67   */
68   CHKERRQ(PetscInitialize(&argc,&argv,(char*)0,help));
69 
70   /* Create an empty bag */
71   CHKERRQ(PetscBagCreate(PETSC_COMM_WORLD,sizeof(Parameter),&bag));
72   CHKERRQ(PetscBagGetData(bag,(void**)&params));
73 
74   /* register variables, defaults, names, help strings */
75   CHKERRQ(PetscBagSetName(bag,"ParameterBag","contains parameters for simulations of top-secret, dangerous physics"));
76   CHKERRQ(PetscBagSetOptionsPrefix(bag, "pbag_"));
77   CHKERRQ(PetscBagRegisterString(bag,&params->filename,PETSC_MAX_PATH_LEN,"myfile","filename","Name of secret file"));
78   CHKERRQ(PetscBagRegisterReal  (bag,&params->rho,3.0,"rho","Density, kg/m^3"));
79   CHKERRQ(PetscBagRegisterScalar(bag,&params->W,  5.0,"W","Vertical velocity, m/sec"));
80   CHKERRQ(PetscBagRegisterInt   (bag,&params->Ii, 2,"modes_x","Number of modes in x-direction"));
81 
82   params->iarray[0] = 1;
83   params->iarray[1] = 2;
84   params->iarray[2] = 3;
85 
86   CHKERRQ(PetscBagRegisterIntArray(bag,&params->iarray, 3,"int_array","Int array with 3 locations"));
87 
88   params->rarray[0] = -1.0;
89   params->rarray[1] = -2.0;
90 
91   CHKERRQ(PetscBagRegisterRealArray(bag,&params->rarray, 2,"real_array","Real array with 2 locations"));
92   CHKERRQ(PetscBagRegisterBool (bag,&params->T,  PETSC_FALSE,"do_output","Write output file (yes/no)"));
93   CHKERRQ(PetscBagRegisterBoolArray(bag,&params->Tarray, 3,"bool_array","Bool array with 3 locations"));
94   CHKERRQ(PetscBagRegisterEnum  (bag,&params->dt, PetscDataTypes,(PetscEnum)PETSC_INT,"dt","meaningless datatype"));
95   CHKERRQ(PetscBagRegisterReal  (bag,&params->pos.x1,1.0,"x1","x position"));
96   CHKERRQ(PetscBagRegisterReal  (bag,&params->pos.x2,1.9,"x2","y position"));
97   CHKERRQ(PetscBagRegisterEnum  (bag,&params->which, EnumeratedChoices, (PetscEnum)THAT, "choose","Express yourself by choosing among enumerated things"));
98 
99   /* This option allows loading user-provided PetscBag */
100   CHKERRQ(PetscOptionsGetString(NULL,NULL,"-f",filename,sizeof(filename),&flg));
101   if (!flg) {
102 
103     /* write bag to stdio & binary file */
104     CHKERRQ(PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD));
105     CHKERRQ(PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_WRITE,&viewer));
106     CHKERRQ(PetscBagView(bag,viewer));
107     CHKERRQ(PetscViewerDestroy(&viewer));
108   }
109 
110   CHKERRQ(PetscMemzero(params,sizeof(Parameter)));
111 
112   /* load bag from file & write to stdio */
113   CHKERRQ(PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer));
114   CHKERRQ(PetscBagLoad(viewer,bag));
115   CHKERRQ(PetscViewerDestroy(&viewer));
116   CHKERRQ(PetscBagSetFromOptions(bag));
117   CHKERRQ(PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD));
118 
119   /* reuse the parameter struct */
120   CHKERRQ(PetscBagGetData(bag,(void**)&params));
121   CHKERRQ(PetscPrintf(PETSC_COMM_WORLD,"The value of rho after loading is: %f\n",(double)params->rho));
122 
123   /* clean up and exit */
124   CHKERRQ(PetscBagDestroy(&bag));
125   CHKERRQ(PetscFinalize());
126   return 0;
127 }
128 
129 /*TEST
130 
131    test:
132       args: -pbag_rho 44 -pbag_do_output true
133       requires: !complex
134 
135    test:
136       suffix: yaml
137       requires: !complex
138       args: -options_file bag.yml -options_view
139       filter: egrep -v "(options_left|options_view|malloc_dump|malloc_test|saws_port_auto_select|display|check_pointer_intensity|error_output_stdout|nox|vecscatter_mpi1|use_gpu_aware_mpi|checkstack)"
140       localrunfiles: bag.yml
141 
142 TEST*/
143