Actual source code: ex5.c
2: static char help[] = "Demonstrates using the PetscBag Object\n\n";
4: #include <petscsys.h>
5: #include <petscbag.h>
6: #include <petscviewer.h>
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};
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;
29: /*
30: Define a C struct that will contain my program's parameters.
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;
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";
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: */
66: PetscInitialize(&argc, &argv, (char *)0, help);
68: /* Create an empty bag */
69: PetscBagCreate(PETSC_COMM_WORLD, sizeof(Parameter), &bag);
70: PetscBagGetData(bag, (void **)¶ms);
72: /* register variables, defaults, names, help strings */
73: PetscBagSetName(bag, "ParameterBag", "contains parameters for simulations of top-secret, dangerous physics");
74: PetscBagSetOptionsPrefix(bag, "pbag_");
75: PetscBagRegisterString(bag, ¶ms->filename, PETSC_MAX_PATH_LEN, "myfile", "filename", "Name of secret file");
76: PetscBagRegisterReal(bag, ¶ms->rho, 3.0, "rho", "Density, kg/m^3");
77: PetscBagRegisterScalar(bag, ¶ms->W, 5.0, "W", "Vertical velocity, m/sec");
78: PetscBagRegisterInt(bag, ¶ms->Ii, 2, "modes_x", "Number of modes in x-direction");
80: params->iarray[0] = 1;
81: params->iarray[1] = 2;
82: params->iarray[2] = 3;
84: PetscBagRegisterIntArray(bag, ¶ms->iarray, 3, "int_array", "Int array with 3 locations");
86: params->rarray[0] = -1.0;
87: params->rarray[1] = -2.0;
89: PetscBagRegisterRealArray(bag, ¶ms->rarray, 2, "real_array", "Real array with 2 locations");
90: PetscBagRegisterBool(bag, ¶ms->T, PETSC_FALSE, "do_output", "Write output file (yes/no)");
91: PetscBagRegisterBoolArray(bag, ¶ms->Tarray, 3, "bool_array", "Bool array with 3 locations");
92: PetscBagRegisterEnum(bag, ¶ms->dt, PetscDataTypes, (PetscEnum)PETSC_INT, "dt", "meaningless datatype");
93: PetscBagRegisterReal(bag, ¶ms->pos.x1, 1.0, "x1", "x position");
94: PetscBagRegisterReal(bag, ¶ms->pos.x2, 1.9, "x2", "y position");
95: PetscBagRegisterEnum(bag, ¶ms->which, EnumeratedChoices, (PetscEnum)THAT, "choose", "Express yourself by choosing among enumerated things");
97: /* This option allows loading user-provided PetscBag */
98: PetscOptionsGetString(NULL, NULL, "-f", filename, sizeof(filename), &flg);
99: if (!flg) {
100: /* write bag to stdio & binary file */
101: PetscBagView(bag, PETSC_VIEWER_STDOUT_WORLD);
102: PetscViewerBinaryOpen(PETSC_COMM_WORLD, filename, FILE_MODE_WRITE, &viewer);
103: PetscBagView(bag, viewer);
104: PetscViewerDestroy(&viewer);
105: }
107: PetscMemzero(params, sizeof(Parameter));
109: /* load bag from file & write to stdio */
110: PetscViewerBinaryOpen(PETSC_COMM_WORLD, filename, FILE_MODE_READ, &viewer);
111: PetscBagLoad(viewer, bag);
112: PetscViewerDestroy(&viewer);
113: PetscBagSetFromOptions(bag);
114: PetscBagView(bag, PETSC_VIEWER_STDOUT_WORLD);
116: /* reuse the parameter struct */
117: PetscBagGetData(bag, (void **)¶ms);
118: PetscPrintf(PETSC_COMM_WORLD, "The value of rho after loading is: %f\n", (double)params->rho);
120: /* clean up and exit */
121: PetscBagDestroy(&bag);
122: PetscFinalize();
123: return 0;
124: }
126: /*TEST
128: test:
129: args: -pbag_rho 44 -pbag_do_output true
130: requires: !complex
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
139: TEST*/