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