Actual source code: ex5.c

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

  3: #include <petscbag.h>

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

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

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

 29:   A PETSc bag is merely a representation of a C struct that can be printed, saved to a file and loaded from a file.
 30: */
 31: typedef struct {
 32:   PetscScalar   W;
 33:   PetscReal     rho;
 34:   TwoVec        pos;
 35:   PetscInt      Ii;
 36:   PetscInt      iarray[3];
 37:   PetscReal     rarray[2];
 38:   PetscBool     T;
 39:   PetscBool     Tarray[3];
 40:   PetscDataType dt;
 41:   char          filename[PETSC_MAX_PATH_LEN];
 42:   YourChoice    which;
 43: } Parameter;

 45: int main(int argc, char **argv)
 46: {
 47:   PetscBag    bag;
 48:   Parameter  *params;
 49:   PetscViewer viewer;
 50:   PetscBool   flg;
 51:   char        filename[PETSC_MAX_PATH_LEN] = "binaryoutput";

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

 65:   /* Create an empty bag */
 66:   PetscCall(PetscBagCreate(PETSC_COMM_WORLD, sizeof(Parameter), &bag));
 67:   PetscCall(PetscBagGetData(bag, &params));

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

 77:   params->iarray[0] = 1;
 78:   params->iarray[1] = 2;
 79:   params->iarray[2] = 3;

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

 83:   params->rarray[0] = -1.0;
 84:   params->rarray[1] = -2.0;

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

 94:   /* This option allows loading user-provided PetscBag */
 95:   PetscCall(PetscOptionsGetString(NULL, NULL, "-f", filename, sizeof(filename), &flg));
 96:   if (!flg) {
 97:     /* write bag to stdio & binary file */
 98:     PetscCall(PetscBagView(bag, PETSC_VIEWER_STDOUT_WORLD));
 99:     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, filename, FILE_MODE_WRITE, &viewer));
100:     PetscCall(PetscBagView(bag, viewer));
101:     PetscCall(PetscViewerDestroy(&viewer));
102:   }

104:   PetscCall(PetscMemzero(params, sizeof(Parameter)));

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

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

117:   /* clean up and exit */
118:   PetscCall(PetscBagDestroy(&bag));
119:   PetscCall(PetscFinalize());
120:   return 0;
121: }

123: /*TEST

125:    test:
126:       args: -pbag_rho 44 -pbag_do_output true
127:       requires: !complex

129:    test:
130:       suffix: yaml
131:       requires: !complex
132:       args: -options_file bag.yml -options_view
133:       filter: grep -E -v "(options_view)"
134:       localrunfiles: bag.yml

136:    test:
137:       suffix: help
138:       requires: !complex
139:       args: -help
140:       filter: grep -E "pbag_(int|real|bool)_array <"

142:    # bag options are in the Bag manual section, so "-help bag" selects exactly what plain -help shows
143:    test:
144:       suffix: help_bag
145:       requires: !complex
146:       args: -help bag
147:       filter: grep -E "pbag_(int|real|bool)_array <"
148:       output_file: output/ex5_help.out

150:    # and a manual section that no block declares selects none of them
151:    test:
152:       suffix: help_other_mansec
153:       requires: !complex
154:       args: -help NoSuchSection
155:       filter: grep -E "pbag_(int|real|bool)_array <"
156:       output_file: output/empty.out

158: TEST*/