Actual source code: ex5.c

petsc-3.12.5 2020-03-29
Report Typos and Errors

  2: static char help[] = "Demonstrates using the PetscBag Object\n\n";

  4: /*T
  5:    Concepts: bags;
  6:    Processors: n
  7: T*/


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

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

 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;

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

 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;


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

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

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

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

 85:   params->iarray[0] = 1;
 86:   params->iarray[1] = 2;
 87:   params->iarray[2] = 3;

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

 91:   params->rarray[0] = -1.0;
 92:   params->rarray[1] = -2.0;

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


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

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

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

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

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

127:   /* clean up and exit */
128:   PetscBagDestroy(&bag);
129:   PetscFinalize();
130:   return ierr;
131: }


134: /*TEST

136:    test:
137:       args: -pbag_rho 44 -pbag_do_output true
138:       requires: yaml !complex

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

147: TEST*/