xref: /petsc/src/vec/vec/tests/ex55.c (revision 0e6b6b5985dd9b1172860d21fb88bd3966bf7c54)
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 
6 int main(int argc, char **args) {
7   Vec         u;
8   PetscViewer viewer;
9   char       *attrReadVal, attrWriteVal[20] = {"Hello World!!"};
10 
11   PetscFunctionBeginUser;
12   PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
13 
14   /* PART 1:  Generate vector, then write it in the given data format */
15   PetscCall(VecCreate(PETSC_COMM_WORLD, &u));
16   PetscCall(PetscObjectSetName((PetscObject)u, "Test_Vec"));
17   PetscCall(VecSetSizes(u, PETSC_DECIDE, 10));
18   PetscCall(VecSetFromOptions(u));
19   PetscCall(VecSet(u, 0.));
20 
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));
26 
27   PetscCall(PetscViewerDestroy(&viewer));
28   PetscCall(VecDestroy(&u));
29 
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));
35 
36   PetscCall(PetscViewerDestroy(&viewer));
37   PetscCall(PetscFinalize());
38   return 0;
39 }
40 
41 /*TEST
42 
43      build:
44        requires: hdf5
45 
46      test:
47 
48 TEST*/
49