Actual source code: ex5.c

petsc-3.5.4 2015-05-23
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*/
  8: #include <petscsys.h>
  9: #include <petscbag.h>
 10: #include <petscviewer.h>

 12: /*
 13:   Enum variables can be stored in a bag but require a string array
 14:   to name their fields.  The fourth entry in this example is the name
 15:   of the enum, the fifth is the prefix (none in this case), and the last
 16:   entry is the null string.
 17: */
 18: typedef enum {
 19:   THIS = 0, THAT = 1, THE_OTHER = 2
 20: } YourChoice;
 21: const char *EnumeratedChoices[] = {"THIS","THAT","THE_OTHER","EnumeratedChoices","",0};

 23: /*
 24:   Data structures can be used in a bag as long as they
 25:   are declared in the bag with a variable, not with a pointer.
 26: */
 27: typedef struct {
 28:   PetscReal x1,x2;
 29: } TwoVec;

 31: /*
 32:   Define a C struct that will contain my program's parameters.
 33: */
 34: typedef struct {
 35:   PetscScalar   W;
 36:   PetscReal     rho;
 37:   TwoVec        pos;
 38:   PetscInt      Ii;
 39:   PetscInt      iarray[3];
 40:   PetscReal     rarray[2];
 41:   PetscBool     T;
 42:   PetscBool     Tarray[3];
 43:   PetscDataType dt;
 44:   char          filename[PETSC_MAX_PATH_LEN];
 45:   YourChoice    which;
 46: } 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);

 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");


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

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

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

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

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

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