Actual source code: ex5.c

petsc-3.3-p7 2013-05-11
  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>

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

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

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

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

 58:   /*
 59:     Every PETSc routine should begin with the PetscInitialize() routine.
 60:     argc, argv - These command line arguments are taken to extract the options
 61:                  supplied to PETSc and options supplied to MPI.
 62:     help       - When PETSc executable is invoked with the option -help, 
 63:                  it prints the various options that can be applied at 
 64:                  runtime.  The user can use the "help" variable place
 65:                  additional help messages in this printout.
 66:   */
 67:   PetscInitialize(&argc,&argv,(char *)0,help);

 69:   /* Create an empty bag */
 70:   PetscBagCreate(PETSC_COMM_WORLD,sizeof(Parameter),&bag);
 71:   PetscBagGetData(bag,(void **)&params);
 72: 
 73:   /* register variables, defaults, names, help strings */
 74:   PetscBagSetName(bag,"ParameterBag","contains parameters for simulations of top-secret, dangerous physics");
 75:   PetscBagSetOptionsPrefix(bag, "pbag_");
 76:   PetscBagRegisterString(bag,&params->filename,PETSC_MAX_PATH_LEN,"myfile","filename","Name of secret file");
 77:   PetscBagRegisterReal  (bag,&params->rho,3.0,"rho","Density, kg/m^3");
 78:   PetscBagRegisterScalar(bag,&params->W,  5.0,"W","Vertical velocity, m/sec");
 79:   PetscBagRegisterInt   (bag,&params->Ii, 2,"modes_x","Number of modes in x-direction");
 80:   params->iarray[0] = 1;
 81:   params->iarray[1] = 2;
 82:   params->iarray[2] = 3;
 83:   PetscBagRegisterIntArray(bag,&params->iarray, 3,"int_array","Int array with 3 locations");
 84:   params->rarray[0] = -1.0;
 85:   params->rarray[1] = -2.0;
 86:   PetscBagRegisterRealArray(bag,&params->rarray, 2,"real_array","Real array with 2 locations");
 87:   PetscBagRegisterBool (bag,&params->T,  PETSC_FALSE,"do_output","Write output file (yes/no)");
 88:   PetscBagRegisterEnum  (bag,&params->dt, PetscDataTypes,(PetscEnum)PETSC_INT,"dt","meaningless datatype");
 89:   PetscBagRegisterReal  (bag,&params->pos.x1,1.0,"x1","x position");
 90:   PetscBagRegisterReal  (bag,&params->pos.x2,1.9,"x2","y position");
 91:   PetscBagRegisterEnum  (bag,&params->which, EnumeratedChoices, (PetscEnum)THAT, "choose","Express yourself by choosing among enumerated things");

 93: 
 94:   /* This option allows loading user-provided PetscBag */
 95:   PetscOptionsGetString(PETSC_NULL,"-f",filename,sizeof filename,&flg);
 96:   if (!flg) {

 98:     /* write bag to stdio & binary file */
 99:     PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD);
100:     PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_WRITE,&viewer);
101:     PetscBagView(bag,viewer);
102:     PetscViewerDestroy(&viewer);
103:   }
104: 
105:   PetscMemzero(params,sizeof(Parameter));

107:   /* load bag from file & write to stdio */
108:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);
109:   PetscBagLoad(viewer,bag);
110:   PetscViewerDestroy(&viewer);
111:   PetscBagSetFromOptions(bag);
112:   PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD);

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

118: #if defined(PETSC_USE_SOCKET_VIEWER)
119:   {
120:     PetscBool  flg;
121:     PetscOptionsName("-bag_view_socket","Sends bag to socket (can be read from matlab)","PetscBagView",&flg);
122:     if (flg) {
123:       PetscBagView(bag,PETSC_VIEWER_SOCKET_(PETSC_COMM_WORLD));
124:       PetscViewerFlush(PETSC_VIEWER_SOCKET_(PETSC_COMM_WORLD));
125:     }
126:   }
127: #endif

129:   /* clean up and exit */
130:   PetscBagDestroy(&bag);
131:   PetscFinalize();
132:   return 0;
133: }