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