Actual source code: ex5.c
petsc-3.4.2 2013-07-02

      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:   PetscDataType dt;
     43:   char          filename[PETSC_MAX_PATH_LEN];
     44:   YourChoice    which;
     45: } Parameter;

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

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

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

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

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

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

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

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

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

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

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

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

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

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