xref: /petsc/src/vec/vec/tests/ex55.c (revision 732aec7a18f2199fb53bb9a2f3aef439a834ce31)
1 static char help[] = "Tests I/O of vector and string attribute for HDF5 format\n\n";
2 
3 #include <petscvec.h>
4 #include <petscviewerhdf5.h>
5 
main(int argc,char ** args)6 int main(int argc, char **args)
7 {
8   Vec         u;
9   PetscViewer viewer;
10   char       *attrReadVal, attrWriteVal[20] = {"Hello World!!"};
11 
12   PetscFunctionBeginUser;
13   PetscCall(PetscInitialize(&argc, &args, NULL, help));
14 
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.));
21 
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));
27 
28   PetscCall(PetscViewerDestroy(&viewer));
29   PetscCall(VecDestroy(&u));
30 
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));
36 
37   PetscCall(PetscViewerDestroy(&viewer));
38   PetscCall(PetscFinalize());
39   return 0;
40 }
41 
42 /*TEST
43 
44      build:
45        requires: hdf5
46 
47      test:
48 
49 TEST*/
50