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