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 petsc.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: char filename[PETSC_MAX_PATH_LEN];
35: PetscReal rho;
36: PetscScalar W;
37: PetscInt Ii;
38: PetscTruth T;
39: TwoVec pos;
40: PetscDataType dt;
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: PetscBagRegisterString(bag,¶ms->filename,PETSC_MAX_PATH_LEN,"myfile","filename","Name of secret file");
72: PetscBagRegisterReal (bag,¶ms->rho,3.0,"rho","Density, kg/m^3");
73: PetscBagRegisterScalar(bag,¶ms->W, 5.0,"W","Vertical velocity, m/sec");
74: PetscBagRegisterInt (bag,¶ms->Ii, 2,"modes_x","Number of modes in x-direction");
75: PetscBagRegisterTruth (bag,¶ms->T, PETSC_FALSE,"do_output","Write output file (yes/no)");
76: PetscBagRegisterEnum (bag,¶ms->dt, PetscDataTypes,(PetscEnum)PETSC_INT,"dt","meaningless datatype");
77: PetscBagRegisterReal (bag,¶ms->pos.x1,1.0,"x1","x position");
78: PetscBagRegisterReal (bag,¶ms->pos.x2,1.9,"x2","y position");
79: PetscBagRegisterEnum (bag,¶ms->which, EnumeratedChoices, (PetscEnum)THAT, "choose","Express yourself by choosing among enumerated things");
81: /* write bag to stdio & binary file */
82: PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD);
83: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"binaryoutput",FILE_MODE_WRITE,&viewer);
84: PetscBagView(bag,viewer);
85: PetscViewerDestroy(viewer);
86: PetscBagDestroy(bag);
88: /* load bag from file & write to stdio */
89: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"binaryoutput",FILE_MODE_READ,&viewer);
90: PetscBagLoad(viewer,&bag);
91: PetscViewerDestroy(viewer);
92: PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD);
94: /* reuse the parameter struct */
95: PetscBagGetData(bag,(void**)¶ms);
96: PetscPrintf(PETSC_COMM_WORLD,"The value of rho after loading is: %f\n",params->rho);
98: /* clean up and exit */
99: PetscBagDestroy(bag);
100: PetscFinalize();
101: return 0;
102: }