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;
54: /*
55: Every PETSc routine should begin with the PetscInitialize() routine.
56: argc, argv - These command line arguments are taken to extract the options
57: supplied to PETSc and options supplied to MPI.
58: help - When PETSc executable is invoked with the option -help,
59: it prints the various options that can be applied at
60: runtime. The user can use the "help" variable place
61: additional help messages in this printout.
62: */
63: PetscInitialize(&argc,&argv,(char *)0,help);
65: /* Create an empty bag */
66: PetscBagCreate(PETSC_COMM_WORLD,sizeof(Parameter),&bag);
67: PetscBagGetData(bag,(void **)¶ms);
69: /* register variables, defaults, names, help strings */
70: PetscBagSetName(bag,"ParameterBag","contains parameters for simulations of top-secret, dangerous physics");
71: PetscBagSetOptionsPrefix(bag, "pbag_");
72: PetscBagRegisterString(bag,¶ms->filename,PETSC_MAX_PATH_LEN,"myfile","filename","Name of secret file");
73: PetscBagRegisterReal (bag,¶ms->rho,3.0,"rho","Density, kg/m^3");
74: PetscBagRegisterScalar(bag,¶ms->W, 5.0,"W","Vertical velocity, m/sec");
75: PetscBagRegisterInt (bag,¶ms->Ii, 2,"modes_x","Number of modes in x-direction");
76: PetscBagRegisterBool (bag,¶ms->T, PETSC_FALSE,"do_output","Write output file (yes/no)");
77: PetscBagRegisterEnum (bag,¶ms->dt, PetscDataTypes,(PetscEnum)PETSC_INT,"dt","meaningless datatype");
78: PetscBagRegisterReal (bag,¶ms->pos.x1,1.0,"x1","x position");
79: PetscBagRegisterReal (bag,¶ms->pos.x2,1.9,"x2","y position");
80: PetscBagRegisterEnum (bag,¶ms->which, EnumeratedChoices, (PetscEnum)THAT, "choose","Express yourself by choosing among enumerated things");
82: /* write bag to stdio & binary file */
83: PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD);
84: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"binaryoutput",FILE_MODE_WRITE,&viewer);
85: PetscBagView(bag,viewer);
86: PetscViewerDestroy(&viewer);
87: PetscBagDestroy(&bag);
89: /* load bag from file & write to stdio */
90: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"binaryoutput",FILE_MODE_READ,&viewer);
91: PetscBagLoad(viewer,&bag);
92: PetscViewerDestroy(&viewer);
93: PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD);
95: /* reuse the parameter struct */
96: PetscBagGetData(bag,(void**)¶ms);
97: PetscPrintf(PETSC_COMM_WORLD,"The value of rho after loading is: %f\n",(double)params->rho);
99: #if defined(PETSC_USE_SOCKET_VIEWER)
100: {
101: PetscBool flg;
102: PetscOptionsName("-bag_view_socket","Sends bag to socket (can be read from matlab)","PetscBagView",&flg);
103: if (flg) {
104: PetscBagView(bag,PETSC_VIEWER_SOCKET_(PETSC_COMM_WORLD));
105: PetscViewerFlush(PETSC_VIEWER_SOCKET_(PETSC_COMM_WORLD));
106: }
107: }
108: #endif
110: /* clean up and exit */
111: PetscBagDestroy(&bag);
112: PetscFinalize();
113: return 0;
114: }