Actual source code: ex55.c

  1: static char help[] = "Tests I/O of vector and string attribute for HDF5 format\n\n";

  3: #include <petscvec.h>
  4: #include <petscviewerhdf5.h>

  6: int main(int argc, char **args)
  7: {
  8:   Vec         u;
  9:   PetscViewer viewer;
 10:   char       *attrReadVal, attrWriteVal[20] = {"Hello World!!"};

 12:   PetscFunctionBeginUser;
 13:   PetscCall(PetscInitialize(&argc, &args, NULL, help));

 15:   /* PART 1:  Generate vector, then write it in the given data format */
 16:   PetscCall(VecCreate(PETSC_COMM_WORLD, &u));
 17:   PetscCall(PetscObjectSetName((PetscObject)u, "Test_Vec"));
 18:   PetscCall(VecSetSizes(u, PETSC_DECIDE, 10));
 19:   PetscCall(VecSetFromOptions(u));

 21:   /* write vector and attribute*/
 22:   PetscCall(PetscViewerHDF5Open(PETSC_COMM_WORLD, "vector.dat", FILE_MODE_WRITE, &viewer));
 23:   PetscCall(VecView(u, viewer));
 24:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Attribute value written: '%s'\n\n", attrWriteVal));
 25:   PetscCall(PetscViewerHDF5WriteAttribute(viewer, "Test_Vec", "Test_Attr", PETSC_STRING, attrWriteVal));

 27:   PetscCall(PetscViewerDestroy(&viewer));
 28:   PetscCall(VecDestroy(&u));

 30:   /* PART 2:  Read in attribute */
 31:   PetscCall(PetscViewerHDF5Open(PETSC_COMM_WORLD, "vector.dat", FILE_MODE_READ, &viewer));
 32:   PetscCall(PetscViewerHDF5ReadAttribute(viewer, "Test_Vec", "Test_Attr", PETSC_STRING, NULL, &attrReadVal));
 33:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Attribute value read: '%s'\n\n", attrReadVal));
 34:   PetscCall(PetscFree(attrReadVal));

 36:   PetscCall(PetscViewerDestroy(&viewer));
 37:   PetscCall(PetscFinalize());
 38:   return 0;
 39: }

 41: /*TEST

 43:      build:
 44:        requires: hdf5

 46:      test:

 48: TEST*/