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*/
8: #include <petscsys.h>
9: #include <petscbag.h>
11: /*
12: Enum variables can be stored in a bag but require a string array
13: to name their fields. The fourth entry in this example is the name
14: of the enum, the fifth is the prefix (none in this case), and the last
15: entry is the null string.
16: */
17: typedef enum {
18: THIS = 0, THAT = 1, THE_OTHER = 2
19: } YourChoice;
20: const char *EnumeratedChoices[] = {"THIS","THAT","THE_OTHER","EnumeratedChoices","",0};
22: /*
23: Data structures can be used in a bag as long as they
24: are declared in the bag with a variable, not with a pointer.
25: */
26: typedef struct {
27: PetscReal x1,x2;
28: } TwoVec;
30: /*
31: Define a C struct that will contain my program's parameters.
32: */
33: typedef struct {
34: PetscScalar W;
35: PetscReal rho;
36: TwoVec pos;
37: PetscInt Ii;
38: PetscBool T;
39: PetscDataType dt;
40: char filename[PETSC_MAX_PATH_LEN];
41: YourChoice which;
42: } Parameter;
43:
47: int main(int argc,char **argv)
48: {
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: */
65: PetscInitialize(&argc,&argv,(char *)0,help);
67: /* Create an empty bag */
68: PetscBagCreate(PETSC_COMM_WORLD,sizeof(Parameter),&bag);
69: PetscBagGetData(bag,(void **)¶ms);
70:
71: /* register variables, defaults, names, help strings */
72: PetscBagSetName(bag,"ParameterBag","contains parameters for simulations of top-secret, dangerous physics");
73: PetscBagSetOptionsPrefix(bag, "pbag_");
74: PetscBagRegisterString(bag,¶ms->filename,PETSC_MAX_PATH_LEN,"myfile","filename","Name of secret file");
75: PetscBagRegisterReal (bag,¶ms->rho,3.0,"rho","Density, kg/m^3");
76: PetscBagRegisterScalar(bag,¶ms->W, 5.0,"W","Vertical velocity, m/sec");
77: PetscBagRegisterInt (bag,¶ms->Ii, 2,"modes_x","Number of modes in x-direction");
78: PetscBagRegisterBool (bag,¶ms->T, PETSC_FALSE,"do_output","Write output file (yes/no)");
79: PetscBagRegisterEnum (bag,¶ms->dt, PetscDataTypes,(PetscEnum)PETSC_INT,"dt","meaningless datatype");
80: PetscBagRegisterReal (bag,¶ms->pos.x1,1.0,"x1","x position");
81: PetscBagRegisterReal (bag,¶ms->pos.x2,1.9,"x2","y position");
82: PetscBagRegisterEnum (bag,¶ms->which, EnumeratedChoices, (PetscEnum)THAT, "choose","Express yourself by choosing among enumerated things");
84:
85: /* This option allows loading user-provided PetscBag */
86: PetscOptionsGetString(PETSC_NULL,"-f",filename,sizeof filename,&flg);
87: if (!flg) {
89: /* write bag to stdio & binary file */
90: PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD);
91: PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_WRITE,&viewer);
92: PetscBagView(bag,viewer);
93: PetscViewerDestroy(&viewer);
94: }
95:
96: PetscMemzero(params,sizeof(params));
98: /* load bag from file & write to stdio */
99: PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);
100: PetscBagLoad(viewer,bag);
101: PetscViewerDestroy(&viewer);
102: PetscBagSetFromOptions(bag);
103: PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD);
105: /* reuse the parameter struct */
106: PetscBagGetData(bag,(void**)¶ms);
107: PetscPrintf(PETSC_COMM_WORLD,"The value of rho after loading is: %f\n",(double)params->rho);
109: #if defined(PETSC_USE_SOCKET_VIEWER)
110: {
111: PetscBool flg;
112: PetscOptionsName("-bag_view_socket","Sends bag to socket (can be read from matlab)","PetscBagView",&flg);
113: if (flg) {
114: PetscBagView(bag,PETSC_VIEWER_SOCKET_(PETSC_COMM_WORLD));
115: PetscViewerFlush(PETSC_VIEWER_SOCKET_(PETSC_COMM_WORLD));
116: }
117: }
118: #endif
120: /* clean up and exit */
121: PetscBagDestroy(&bag);
122: PetscFinalize();
123: return 0;
124: }