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