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, (char *)0, 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));
 20:   PetscCall(VecSet(u, 0.));

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

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

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

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

 42: /*TEST

 44:      build:
 45:        requires: hdf5

 47:      test:

 49: TEST*/