xref: /petsc/src/sys/tutorials/ex5.c (revision c4762a1b19cd2af06abeed90e8f9d34fb975dd94)
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 
10 #include <petscsys.h>
11 #include <petscbag.h>
12 #include <petscviewer.h>
13 
14 /*
15   Enum variables can be stored in a bag but require a string array
16   to name their fields.  The fourth entry in this example is the name
17   of the enum, the fifth is the prefix (none in this case), and the last
18   entry is the null string.
19 */
20 typedef enum {
21   THIS = 0, THAT = 1, THE_OTHER = 2
22 } YourChoice;
23 const char *EnumeratedChoices[] = {"THIS","THAT","THE_OTHER","EnumeratedChoices","",0};
24 
25 /*
26   Data structures can be used in a bag as long as they
27   are declared in the bag with a variable, not with a pointer.
28 */
29 typedef struct {
30   PetscReal x1,x2;
31 } TwoVec;
32 
33 /*
34   Define a C struct that will contain my program's parameters.
35 
36   A PETSc bag is merely a representation of a C struct that can be printed, saved to a file and loaded from a file.
37 */
38 typedef struct {
39   PetscScalar   W;
40   PetscReal     rho;
41   TwoVec        pos;
42   PetscInt      Ii;
43   PetscInt      iarray[3];
44   PetscReal     rarray[2];
45   PetscBool     T;
46   PetscBool     Tarray[3];
47   PetscDataType dt;
48   char          filename[PETSC_MAX_PATH_LEN];
49   YourChoice    which;
50 } Parameter;
51 
52 
53 int main(int argc,char **argv)
54 {
55   PetscErrorCode ierr;
56   PetscBag       bag;
57   Parameter      *params;
58   PetscViewer    viewer;
59   PetscBool      flg;
60   char           filename[PETSC_MAX_PATH_LEN] = "binaryoutput";
61 
62   /*
63     Every PETSc routine should begin with the PetscInitialize() routine.
64     argc, argv - These command line arguments are taken to extract the options
65                  supplied to PETSc and options supplied to MPI.
66     help       - When PETSc executable is invoked with the option -help,
67                  it prints the various options that can be applied at
68                  runtime.  The user can use the "help" variable place
69                  additional help messages in this printout.
70   */
71   ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
72 
73   /* Create an empty bag */
74   ierr = PetscBagCreate(PETSC_COMM_WORLD,sizeof(Parameter),&bag);CHKERRQ(ierr);
75   ierr = PetscBagGetData(bag,(void**)&params);CHKERRQ(ierr);
76 
77   /* register variables, defaults, names, help strings */
78   ierr = PetscBagSetName(bag,"ParameterBag","contains parameters for simulations of top-secret, dangerous physics");CHKERRQ(ierr);
79   ierr = PetscBagSetOptionsPrefix(bag, "pbag_");CHKERRQ(ierr);
80   ierr = PetscBagRegisterString(bag,&params->filename,PETSC_MAX_PATH_LEN,"myfile","filename","Name of secret file");CHKERRQ(ierr);
81   ierr = PetscBagRegisterReal  (bag,&params->rho,3.0,"rho","Density, kg/m^3");CHKERRQ(ierr);
82   ierr = PetscBagRegisterScalar(bag,&params->W,  5.0,"W","Vertical velocity, m/sec");CHKERRQ(ierr);
83   ierr = PetscBagRegisterInt   (bag,&params->Ii, 2,"modes_x","Number of modes in x-direction");CHKERRQ(ierr);
84 
85   params->iarray[0] = 1;
86   params->iarray[1] = 2;
87   params->iarray[2] = 3;
88 
89   ierr = PetscBagRegisterIntArray(bag,&params->iarray, 3,"int_array","Int array with 3 locations");CHKERRQ(ierr);
90 
91   params->rarray[0] = -1.0;
92   params->rarray[1] = -2.0;
93 
94   ierr = PetscBagRegisterRealArray(bag,&params->rarray, 2,"real_array","Real array with 2 locations");CHKERRQ(ierr);
95   ierr = PetscBagRegisterBool (bag,&params->T,  PETSC_FALSE,"do_output","Write output file (yes/no)");CHKERRQ(ierr);
96   ierr = PetscBagRegisterBoolArray(bag,&params->Tarray, 3,"bool_array","Bool array with 3 locations");CHKERRQ(ierr);
97   ierr = PetscBagRegisterEnum  (bag,&params->dt, PetscDataTypes,(PetscEnum)PETSC_INT,"dt","meaningless datatype");CHKERRQ(ierr);
98   ierr = PetscBagRegisterReal  (bag,&params->pos.x1,1.0,"x1","x position");CHKERRQ(ierr);
99   ierr = PetscBagRegisterReal  (bag,&params->pos.x2,1.9,"x2","y position");CHKERRQ(ierr);
100   ierr = PetscBagRegisterEnum  (bag,&params->which, EnumeratedChoices, (PetscEnum)THAT, "choose","Express yourself by choosing among enumerated things");CHKERRQ(ierr);
101 
102 
103   /* This option allows loading user-provided PetscBag */
104   ierr = PetscOptionsGetString(NULL,NULL,"-f",filename,sizeof(filename),&flg);CHKERRQ(ierr);
105   if (!flg) {
106 
107     /* write bag to stdio & binary file */
108     ierr = PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
109     ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
110     ierr = PetscBagView(bag,viewer);CHKERRQ(ierr);
111     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
112   }
113 
114   ierr = PetscMemzero(params,sizeof(Parameter));CHKERRQ(ierr);
115 
116   /* load bag from file & write to stdio */
117   ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);CHKERRQ(ierr);
118   ierr = PetscBagLoad(viewer,bag);CHKERRQ(ierr);
119   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
120   ierr = PetscBagSetFromOptions(bag);CHKERRQ(ierr);
121   ierr = PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
122 
123   /* reuse the parameter struct */
124   ierr = PetscBagGetData(bag,(void**)&params);CHKERRQ(ierr);
125   ierr = PetscPrintf(PETSC_COMM_WORLD,"The value of rho after loading is: %f\n",(double)params->rho);CHKERRQ(ierr);
126 
127   /* clean up and exit */
128   ierr = PetscBagDestroy(&bag);CHKERRQ(ierr);
129   ierr = PetscFinalize();
130   return ierr;
131 }
132 
133 
134 /*TEST
135 
136    test:
137       args: -pbag_rho 44 -pbag_do_output true
138       requires: yaml !complex
139 
140    test:
141       suffix: yaml
142       requires: yaml !complex
143       args: -options_file_yaml bag.yml -options_view
144       filter:  grep -v saws_port_auto_select |grep -v malloc_dump | grep -v display | grep -v check_pointer_intensity | grep -v error_output_stdout | grep -v nox | grep -v nox_warning
145       localrunfiles: bag.yml
146 
147 TEST*/
148