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*/

  9: #include <petscsys.h>
 10: #include <petscbag.h>
 11: #include <petscviewer.h>

 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};

 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;

 32: /*
 33:   Define a C struct that will contain my program's parameters.

 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;

 51: int main(int argc,char **argv)
 52: {
 54:   PetscBag       bag;
 55:   Parameter      *params;
 56:   PetscViewer    viewer;
 57:   PetscBool      flg;
 58:   char           filename[PETSC_MAX_PATH_LEN] = "binaryoutput";

 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:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;

 71:   /* Create an empty bag */
 72:   PetscBagCreate(PETSC_COMM_WORLD,sizeof(Parameter),&bag);
 73:   PetscBagGetData(bag,(void**)&params);

 75:   /* register variables, defaults, names, help strings */
 76:   PetscBagSetName(bag,"ParameterBag","contains parameters for simulations of top-secret, dangerous physics");
 77:   PetscBagSetOptionsPrefix(bag, "pbag_");
 78:   PetscBagRegisterString(bag,&params->filename,PETSC_MAX_PATH_LEN,"myfile","filename","Name of secret file");
 79:   PetscBagRegisterReal  (bag,&params->rho,3.0,"rho","Density, kg/m^3");
 80:   PetscBagRegisterScalar(bag,&params->W,  5.0,"W","Vertical velocity, m/sec");
 81:   PetscBagRegisterInt   (bag,&params->Ii, 2,"modes_x","Number of modes in x-direction");

 83:   params->iarray[0] = 1;
 84:   params->iarray[1] = 2;
 85:   params->iarray[2] = 3;

 87:   PetscBagRegisterIntArray(bag,&params->iarray, 3,"int_array","Int array with 3 locations");

 89:   params->rarray[0] = -1.0;
 90:   params->rarray[1] = -2.0;

 92:   PetscBagRegisterRealArray(bag,&params->rarray, 2,"real_array","Real array with 2 locations");
 93:   PetscBagRegisterBool (bag,&params->T,  PETSC_FALSE,"do_output","Write output file (yes/no)");
 94:   PetscBagRegisterBoolArray(bag,&params->Tarray, 3,"bool_array","Bool array with 3 locations");
 95:   PetscBagRegisterEnum  (bag,&params->dt, PetscDataTypes,(PetscEnum)PETSC_INT,"dt","meaningless datatype");
 96:   PetscBagRegisterReal  (bag,&params->pos.x1,1.0,"x1","x position");
 97:   PetscBagRegisterReal  (bag,&params->pos.x2,1.9,"x2","y position");
 98:   PetscBagRegisterEnum  (bag,&params->which, EnumeratedChoices, (PetscEnum)THAT, "choose","Express yourself by choosing among enumerated things");

100:   /* This option allows loading user-provided PetscBag */
101:   PetscOptionsGetString(NULL,NULL,"-f",filename,sizeof(filename),&flg);
102:   if (!flg) {

104:     /* write bag to stdio & binary file */
105:     PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD);
106:     PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_WRITE,&viewer);
107:     PetscBagView(bag,viewer);
108:     PetscViewerDestroy(&viewer);
109:   }

111:   PetscMemzero(params,sizeof(Parameter));

113:   /* load bag from file & write to stdio */
114:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);
115:   PetscBagLoad(viewer,bag);
116:   PetscViewerDestroy(&viewer);
117:   PetscBagSetFromOptions(bag);
118:   PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD);

120:   /* reuse the parameter struct */
121:   PetscBagGetData(bag,(void**)&params);
122:   PetscPrintf(PETSC_COMM_WORLD,"The value of rho after loading is: %f\n",(double)params->rho);

124:   /* clean up and exit */
125:   PetscBagDestroy(&bag);
126:   PetscFinalize();
127:   return ierr;
128: }

130: /*TEST

132:    test:
133:       args: -pbag_rho 44 -pbag_do_output true
134:       requires: !complex

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

143: TEST*/