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*/
     8:  #include petsc.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:    It MUST begin with the PetscBag struct.
     33: */
     34: typedef struct {
     35:   PetscBag      bag;
     36:   char          filename[PETSC_MAX_PATH_LEN];
     37:   PetscReal     rho;
     38:   PetscScalar   W;
     39:   PetscInt      I;
     40:   PetscTruth    T;
     41:   TwoVec        pos;
     42:   PetscDataType dt;
     43:   YourChoice    which;
     44: } Parameter;
     45:

     49: int main(int argc,char **argv)
     50: {
     52:   PetscBag       *bag;
     53:   Parameter      *params;
     54:   PetscViewer    viewer;

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

     67:   /* Create an empty bag */
     68:   PetscBagCreate(PETSC_COMM_WORLD,Parameter,&bag);
     69:   params = (Parameter*)bag;

     71:   /* register variables, defaults, names, help strings */
     72:   PetscBagSetName(bag,"ParameterBag","contains parameters for simulations of top-secret, dangerous physics");
     73:   PetscBagRegisterString(bag,&params->filename,PETSC_MAX_PATH_LEN,"myfile","filename","Name of secret file");
     74:   PetscBagRegisterReal  (bag,&params->rho,3.0,"rho","Density, kg/m^3");
     75:   PetscBagRegisterScalar(bag,&params->W,  5.0,"W","Vertical velocity, m/sec");
     76:   PetscBagRegisterInt   (bag,&params->I,  2,"modes_x","Number of modes in x-direction");
     77:   PetscBagRegisterTruth (bag,&params->T,  PETSC_FALSE,"do_output","Write output file (yes/no)");
     78:   PetscBagRegisterEnum  (bag,&params->dt, PetscDataTypes,(PetscEnum)PETSC_INT,"dt","meaningless datatype");
     79:   PetscBagRegisterReal  (bag,&params->pos.x1,1.0,"x1","x position");
     80:   PetscBagRegisterReal  (bag,&params->pos.x2,1.9,"x2","y position");
     81:   PetscBagRegisterEnum  (bag,&params->which, EnumeratedChoices, (PetscEnum)THAT, "choose","Express yourself by choosing among enumerated things");

     83:   /* write bag to stdio & binary file */
     84:   PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD);
     85:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,"binaryoutput",PETSC_FILE_CREATE,&viewer);
     86:   PetscBagView(bag,viewer);
     87:   PetscViewerDestroy(viewer);
     88:   PetscBagDestroy(bag);

     90:   /* load bag from file & write to stdio */
     91:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,"binaryoutput",PETSC_FILE_RDONLY,&viewer);
     92:   PetscBagLoad(viewer,&bag);
     93:   PetscViewerDestroy(viewer);
     94:   PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD);

     96:   /* reuse the parameter struct */
     97:   params = (Parameter*)bag;
     98:   PetscPrintf(PETSC_COMM_WORLD,"The value of rho after loading is: %f\n",params->rho);

    100:   /* clean up and exit */
    101:   PetscBagDestroy(bag);
    102:   PetscFinalize();
    103:   return 0;
    104: }