xref: /petsc/src/sys/classes/viewer/impls/hdf5/hdf5v.c (revision 48a46eb9bd028bec07ec0f396b1a3abb43f14558)
16b9fdc0aSVaclav Hapla #include <petsc/private/viewerhdf5impl.h> /*I "petscviewerhdf5.h" I*/
25c6c1daeSBarry Smith 
3bb286ee1SVaclav Hapla static PetscErrorCode PetscViewerHDF5Traverse_Internal(PetscViewer, const char[], PetscBool, PetscBool *, H5O_type_t *);
406db490cSVaclav Hapla static PetscErrorCode PetscViewerHDF5HasAttribute_Internal(PetscViewer, const char[], const char[], PetscBool *);
506db490cSVaclav Hapla 
69371c9d4SSatish Balay static PetscErrorCode PetscViewerHDF5GetAbsolutePath_Internal(PetscViewer viewer, const char path[], char *abspath[]) {
74302210dSVaclav Hapla   PetscBool   relative = PETSC_FALSE;
86c132bc1SVaclav Hapla   const char *group;
96c132bc1SVaclav Hapla   char        buf[PETSC_MAX_PATH_LEN] = "";
106c132bc1SVaclav Hapla 
116c132bc1SVaclav Hapla   PetscFunctionBegin;
129566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5GetGroup(viewer, &group));
139566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5PathIsRelative(path, PETSC_TRUE, &relative));
144302210dSVaclav Hapla   if (relative) {
159566063dSJacob Faibussowitsch     PetscCall(PetscStrcpy(buf, group));
169566063dSJacob Faibussowitsch     PetscCall(PetscStrcat(buf, "/"));
179566063dSJacob Faibussowitsch     PetscCall(PetscStrcat(buf, path));
189566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(buf, abspath));
194302210dSVaclav Hapla   } else {
209566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(path, abspath));
214302210dSVaclav Hapla   }
226c132bc1SVaclav Hapla   PetscFunctionReturn(0);
236c132bc1SVaclav Hapla }
246c132bc1SVaclav Hapla 
259371c9d4SSatish Balay static PetscErrorCode PetscViewerHDF5CheckNamedObject_Internal(PetscViewer viewer, PetscObject obj) {
26577e0e04SVaclav Hapla   PetscBool has;
27577e0e04SVaclav Hapla 
28577e0e04SVaclav Hapla   PetscFunctionBegin;
299566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5HasObject(viewer, obj, &has));
30577e0e04SVaclav Hapla   if (!has) {
3189e0ef10SVaclav Hapla     const char *group;
329566063dSJacob Faibussowitsch     PetscCall(PetscViewerHDF5GetGroup(viewer, &group));
3398921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Object (dataset) \"%s\" not stored in group %s", obj->name, group ? group : "/");
34577e0e04SVaclav Hapla   }
35577e0e04SVaclav Hapla   PetscFunctionReturn(0);
36577e0e04SVaclav Hapla }
37577e0e04SVaclav Hapla 
389371c9d4SSatish Balay static PetscErrorCode PetscViewerSetFromOptions_HDF5(PetscViewer v, PetscOptionItems *PetscOptionsObject) {
39ee8b9732SVaclav Hapla   PetscBool         flg  = PETSC_FALSE, set;
4082ea9b62SBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)v->data;
4182ea9b62SBarry Smith 
4282ea9b62SBarry Smith   PetscFunctionBegin;
43d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "HDF5 PetscViewer Options");
449566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-viewer_hdf5_base_dimension2", "1d Vectors get 2 dimensions in HDF5", "PetscViewerHDF5SetBaseDimension2", hdf5->basedimension2, &hdf5->basedimension2, NULL));
459566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-viewer_hdf5_sp_output", "Force data to be written in single precision", "PetscViewerHDF5SetSPOutput", hdf5->spoutput, &hdf5->spoutput, NULL));
469566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-viewer_hdf5_collective", "Enable collective transfer mode", "PetscViewerHDF5SetCollective", flg, &flg, &set));
479566063dSJacob Faibussowitsch   if (set) PetscCall(PetscViewerHDF5SetCollective(v, flg));
4819a20e4cSMatthew G. Knepley   flg = PETSC_FALSE;
499566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-viewer_hdf5_default_timestepping", "Set default timestepping state", "PetscViewerHDF5SetDefaultTimestepping", flg, &flg, &set));
509566063dSJacob Faibussowitsch   if (set) PetscCall(PetscViewerHDF5SetDefaultTimestepping(v, flg));
51d0609cedSBarry Smith   PetscOptionsHeadEnd();
5282ea9b62SBarry Smith   PetscFunctionReturn(0);
5382ea9b62SBarry Smith }
5482ea9b62SBarry Smith 
559371c9d4SSatish Balay static PetscErrorCode PetscViewerView_HDF5(PetscViewer v, PetscViewer viewer) {
561b793a25SVaclav Hapla   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)v->data;
5703fa8834SVaclav Hapla   PetscBool         flg;
581b793a25SVaclav Hapla 
591b793a25SVaclav Hapla   PetscFunctionBegin;
60*48a46eb9SPierre Jolivet   if (hdf5->filename) PetscCall(PetscViewerASCIIPrintf(viewer, "Filename: %s\n", hdf5->filename));
619566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "Vectors with blocksize 1 saved as 2D datasets: %s\n", PetscBools[hdf5->basedimension2]));
629566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "Enforce single precision storage: %s\n", PetscBools[hdf5->spoutput]));
639566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5GetCollective(v, &flg));
649566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "MPI-IO transfer mode: %s\n", flg ? "collective" : "independent"));
659566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "Default timestepping: %s\n", PetscBools[hdf5->defTimestepping]));
661b793a25SVaclav Hapla   PetscFunctionReturn(0);
671b793a25SVaclav Hapla }
681b793a25SVaclav Hapla 
699371c9d4SSatish Balay static PetscErrorCode PetscViewerFileClose_HDF5(PetscViewer viewer) {
705c6c1daeSBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data;
715c6c1daeSBarry Smith 
725c6c1daeSBarry Smith   PetscFunctionBegin;
739566063dSJacob Faibussowitsch   PetscCall(PetscFree(hdf5->filename));
74792fecdfSBarry Smith   if (hdf5->file_id) PetscCallHDF5(H5Fclose, (hdf5->file_id));
755c6c1daeSBarry Smith   PetscFunctionReturn(0);
765c6c1daeSBarry Smith }
775c6c1daeSBarry Smith 
789371c9d4SSatish Balay static PetscErrorCode PetscViewerFlush_HDF5(PetscViewer viewer) {
796226335aSVaclav Hapla   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data;
806226335aSVaclav Hapla 
816226335aSVaclav Hapla   PetscFunctionBegin;
82792fecdfSBarry Smith   if (hdf5->file_id) PetscCallHDF5(H5Fflush, (hdf5->file_id, H5F_SCOPE_LOCAL));
836226335aSVaclav Hapla   PetscFunctionReturn(0);
846226335aSVaclav Hapla }
856226335aSVaclav Hapla 
869371c9d4SSatish Balay static PetscErrorCode PetscViewerDestroy_HDF5(PetscViewer viewer) {
875c6c1daeSBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data;
885c6c1daeSBarry Smith 
895c6c1daeSBarry Smith   PetscFunctionBegin;
90792fecdfSBarry Smith   PetscCallHDF5(H5Pclose, (hdf5->dxpl_id));
919566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileClose_HDF5(viewer));
925c6c1daeSBarry Smith   while (hdf5->groups) {
937d964842SVaclav Hapla     PetscViewerHDF5GroupList *tmp = hdf5->groups->next;
945c6c1daeSBarry Smith 
959566063dSJacob Faibussowitsch     PetscCall(PetscFree(hdf5->groups->name));
969566063dSJacob Faibussowitsch     PetscCall(PetscFree(hdf5->groups));
975c6c1daeSBarry Smith     hdf5->groups = tmp;
985c6c1daeSBarry Smith   }
999566063dSJacob Faibussowitsch   PetscCall(PetscFree(hdf5));
1009566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", NULL));
1019566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetName_C", NULL));
1029566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", NULL));
1032e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", NULL));
1049566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerHDF5SetBaseDimension2_C", NULL));
1059566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerHDF5SetSPOutput_C", NULL));
1062e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerHDF5SetCollective_C", NULL));
1072e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerHDF5GetCollective_C", NULL));
1089566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerHDF5GetDefaultTimestepping_C", NULL));
1099566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerHDF5SetDefaultTimestepping_C", NULL));
1105c6c1daeSBarry Smith   PetscFunctionReturn(0);
1115c6c1daeSBarry Smith }
1125c6c1daeSBarry Smith 
1139371c9d4SSatish Balay static PetscErrorCode PetscViewerFileSetMode_HDF5(PetscViewer viewer, PetscFileMode type) {
1145c6c1daeSBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data;
1155c6c1daeSBarry Smith 
1165c6c1daeSBarry Smith   PetscFunctionBegin;
1175c6c1daeSBarry Smith   hdf5->btype = type;
1185c6c1daeSBarry Smith   PetscFunctionReturn(0);
1195c6c1daeSBarry Smith }
1205c6c1daeSBarry Smith 
1219371c9d4SSatish Balay static PetscErrorCode PetscViewerFileGetMode_HDF5(PetscViewer viewer, PetscFileMode *type) {
1220b62783dSVaclav Hapla   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data;
1230b62783dSVaclav Hapla 
1240b62783dSVaclav Hapla   PetscFunctionBegin;
1250b62783dSVaclav Hapla   *type = hdf5->btype;
1260b62783dSVaclav Hapla   PetscFunctionReturn(0);
1270b62783dSVaclav Hapla }
1280b62783dSVaclav Hapla 
1299371c9d4SSatish Balay static PetscErrorCode PetscViewerHDF5SetBaseDimension2_HDF5(PetscViewer viewer, PetscBool flg) {
13082ea9b62SBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data;
13182ea9b62SBarry Smith 
13282ea9b62SBarry Smith   PetscFunctionBegin;
13382ea9b62SBarry Smith   hdf5->basedimension2 = flg;
13482ea9b62SBarry Smith   PetscFunctionReturn(0);
13582ea9b62SBarry Smith }
13682ea9b62SBarry Smith 
137df863907SAlex Fikl /*@
13882ea9b62SBarry Smith      PetscViewerHDF5SetBaseDimension2 - Vectors of 1 dimension (i.e. bs/dof is 1) will be saved in the HDF5 file with a
13982ea9b62SBarry Smith        dimension of 2.
14082ea9b62SBarry Smith 
14182ea9b62SBarry Smith     Logically Collective on PetscViewer
14282ea9b62SBarry Smith 
14382ea9b62SBarry Smith   Input Parameters:
14482ea9b62SBarry Smith +  viewer - the PetscViewer; if it is not hdf5 then this command is ignored
14582ea9b62SBarry Smith -  flg - if PETSC_TRUE the vector will always have at least a dimension of 2 even if that first dimension is of size 1
14682ea9b62SBarry Smith 
14782ea9b62SBarry Smith   Options Database:
14882ea9b62SBarry Smith .  -viewer_hdf5_base_dimension2 - turns on (true) or off (false) using a dimension of 2 in the HDF5 file even if the bs/dof of the vector is 1
14982ea9b62SBarry Smith 
15095452b02SPatrick Sanan   Notes:
15195452b02SPatrick Sanan     Setting this option allegedly makes code that reads the HDF5 in easier since they do not have a "special case" of a bs/dof
15282ea9b62SBarry Smith          of one when the dimension is lower. Others think the option is crazy.
15382ea9b62SBarry Smith 
15482ea9b62SBarry Smith   Level: intermediate
15582ea9b62SBarry Smith 
156db781477SPatrick Sanan .seealso: `PetscViewerFileSetMode()`, `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerBinaryOpen()`
15782ea9b62SBarry Smith 
15882ea9b62SBarry Smith @*/
1599371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5SetBaseDimension2(PetscViewer viewer, PetscBool flg) {
16082ea9b62SBarry Smith   PetscFunctionBegin;
16182ea9b62SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
162cac4c232SBarry Smith   PetscTryMethod(viewer, "PetscViewerHDF5SetBaseDimension2_C", (PetscViewer, PetscBool), (viewer, flg));
16382ea9b62SBarry Smith   PetscFunctionReturn(0);
16482ea9b62SBarry Smith }
16582ea9b62SBarry Smith 
166df863907SAlex Fikl /*@
16782ea9b62SBarry Smith      PetscViewerHDF5GetBaseDimension2 - Vectors of 1 dimension (i.e. bs/dof is 1) will be saved in the HDF5 file with a
16882ea9b62SBarry Smith        dimension of 2.
16982ea9b62SBarry Smith 
17082ea9b62SBarry Smith     Logically Collective on PetscViewer
17182ea9b62SBarry Smith 
17282ea9b62SBarry Smith   Input Parameter:
17382ea9b62SBarry Smith .  viewer - the PetscViewer, must be of type HDF5
17482ea9b62SBarry Smith 
17582ea9b62SBarry Smith   Output Parameter:
17682ea9b62SBarry Smith .  flg - if PETSC_TRUE the vector will always have at least a dimension of 2 even if that first dimension is of size 1
17782ea9b62SBarry Smith 
17895452b02SPatrick Sanan   Notes:
17995452b02SPatrick Sanan     Setting this option allegedly makes code that reads the HDF5 in easier since they do not have a "special case" of a bs/dof
18082ea9b62SBarry Smith          of one when the dimension is lower. Others think the option is crazy.
18182ea9b62SBarry Smith 
18282ea9b62SBarry Smith   Level: intermediate
18382ea9b62SBarry Smith 
184db781477SPatrick Sanan .seealso: `PetscViewerFileSetMode()`, `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerBinaryOpen()`
18582ea9b62SBarry Smith 
18682ea9b62SBarry Smith @*/
1879371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5GetBaseDimension2(PetscViewer viewer, PetscBool *flg) {
18882ea9b62SBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data;
18982ea9b62SBarry Smith 
19082ea9b62SBarry Smith   PetscFunctionBegin;
19182ea9b62SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
19282ea9b62SBarry Smith   *flg = hdf5->basedimension2;
19382ea9b62SBarry Smith   PetscFunctionReturn(0);
19482ea9b62SBarry Smith }
19582ea9b62SBarry Smith 
1969371c9d4SSatish Balay static PetscErrorCode PetscViewerHDF5SetSPOutput_HDF5(PetscViewer viewer, PetscBool flg) {
1979a0502c6SHåkon Strandenes   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data;
1989a0502c6SHåkon Strandenes 
1999a0502c6SHåkon Strandenes   PetscFunctionBegin;
2009a0502c6SHåkon Strandenes   hdf5->spoutput = flg;
2019a0502c6SHåkon Strandenes   PetscFunctionReturn(0);
2029a0502c6SHåkon Strandenes }
2039a0502c6SHåkon Strandenes 
204df863907SAlex Fikl /*@
2059a0502c6SHåkon Strandenes      PetscViewerHDF5SetSPOutput - Data is written to disk in single precision even if PETSc is
2069a0502c6SHåkon Strandenes        compiled with double precision PetscReal.
2079a0502c6SHåkon Strandenes 
2089a0502c6SHåkon Strandenes     Logically Collective on PetscViewer
2099a0502c6SHåkon Strandenes 
2109a0502c6SHåkon Strandenes   Input Parameters:
2119a0502c6SHåkon Strandenes +  viewer - the PetscViewer; if it is not hdf5 then this command is ignored
2129a0502c6SHåkon Strandenes -  flg - if PETSC_TRUE the data will be written to disk with single precision
2139a0502c6SHåkon Strandenes 
2149a0502c6SHåkon Strandenes   Options Database:
2159a0502c6SHåkon Strandenes .  -viewer_hdf5_sp_output - turns on (true) or off (false) output in single precision
2169a0502c6SHåkon Strandenes 
21795452b02SPatrick Sanan   Notes:
21895452b02SPatrick Sanan     Setting this option does not make any difference if PETSc is compiled with single precision
2199a0502c6SHåkon Strandenes          in the first place. It does not affect reading datasets (HDF5 handle this internally).
2209a0502c6SHåkon Strandenes 
2219a0502c6SHåkon Strandenes   Level: intermediate
2229a0502c6SHåkon Strandenes 
223db781477SPatrick Sanan .seealso: `PetscViewerFileSetMode()`, `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerBinaryOpen()`,
224db781477SPatrick Sanan           `PetscReal`
2259a0502c6SHåkon Strandenes 
2269a0502c6SHåkon Strandenes @*/
2279371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5SetSPOutput(PetscViewer viewer, PetscBool flg) {
2289a0502c6SHåkon Strandenes   PetscFunctionBegin;
2299a0502c6SHåkon Strandenes   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
230cac4c232SBarry Smith   PetscTryMethod(viewer, "PetscViewerHDF5SetSPOutput_C", (PetscViewer, PetscBool), (viewer, flg));
2319a0502c6SHåkon Strandenes   PetscFunctionReturn(0);
2329a0502c6SHåkon Strandenes }
2339a0502c6SHåkon Strandenes 
234df863907SAlex Fikl /*@
2359a0502c6SHåkon Strandenes      PetscViewerHDF5GetSPOutput - Data is written to disk in single precision even if PETSc is
2369a0502c6SHåkon Strandenes        compiled with double precision PetscReal.
2379a0502c6SHåkon Strandenes 
2389a0502c6SHåkon Strandenes     Logically Collective on PetscViewer
2399a0502c6SHåkon Strandenes 
2409a0502c6SHåkon Strandenes   Input Parameter:
2419a0502c6SHåkon Strandenes .  viewer - the PetscViewer, must be of type HDF5
2429a0502c6SHåkon Strandenes 
2439a0502c6SHåkon Strandenes   Output Parameter:
2449a0502c6SHåkon Strandenes .  flg - if PETSC_TRUE the data will be written to disk with single precision
2459a0502c6SHåkon Strandenes 
24695452b02SPatrick Sanan   Notes:
24795452b02SPatrick Sanan     Setting this option does not make any difference if PETSc is compiled with single precision
2489a0502c6SHåkon Strandenes          in the first place. It does not affect reading datasets (HDF5 handle this internally).
2499a0502c6SHåkon Strandenes 
2509a0502c6SHåkon Strandenes   Level: intermediate
2519a0502c6SHåkon Strandenes 
252db781477SPatrick Sanan .seealso: `PetscViewerFileSetMode()`, `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerBinaryOpen()`,
253db781477SPatrick Sanan           `PetscReal`
2549a0502c6SHåkon Strandenes 
2559a0502c6SHåkon Strandenes @*/
2569371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5GetSPOutput(PetscViewer viewer, PetscBool *flg) {
2579a0502c6SHåkon Strandenes   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data;
2589a0502c6SHåkon Strandenes 
2599a0502c6SHåkon Strandenes   PetscFunctionBegin;
2609a0502c6SHåkon Strandenes   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
2619a0502c6SHåkon Strandenes   *flg = hdf5->spoutput;
2629a0502c6SHåkon Strandenes   PetscFunctionReturn(0);
2639a0502c6SHåkon Strandenes }
2649a0502c6SHåkon Strandenes 
2659371c9d4SSatish Balay static PetscErrorCode PetscViewerHDF5SetCollective_HDF5(PetscViewer viewer, PetscBool flg) {
266ee8b9732SVaclav Hapla   PetscFunctionBegin;
267ee8b9732SVaclav Hapla   /* H5FD_MPIO_COLLECTIVE is wrong in hdf5 1.10.2, and is the same as H5FD_MPIO_INDEPENDENT in earlier versions
268ee8b9732SVaclav Hapla      - see e.g. https://gitlab.cosma.dur.ac.uk/swift/swiftsim/issues/431 */
269c796909eSBarry Smith #if H5_VERSION_GE(1, 10, 3) && defined(H5_HAVE_PARALLEL)
270ee8b9732SVaclav Hapla   {
271ee8b9732SVaclav Hapla     PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data;
272792fecdfSBarry Smith     PetscCallHDF5(H5Pset_dxpl_mpio, (hdf5->dxpl_id, flg ? H5FD_MPIO_COLLECTIVE : H5FD_MPIO_INDEPENDENT));
273ee8b9732SVaclav Hapla   }
274ee8b9732SVaclav Hapla #else
2759566063dSJacob Faibussowitsch   if (flg) PetscCall(PetscPrintf(PetscObjectComm((PetscObject)viewer), "Warning: PetscViewerHDF5SetCollective(viewer,PETSC_TRUE) is ignored for HDF5 versions prior to 1.10.3 or if built without MPI support\n"));
276ee8b9732SVaclav Hapla #endif
277ee8b9732SVaclav Hapla   PetscFunctionReturn(0);
278ee8b9732SVaclav Hapla }
279ee8b9732SVaclav Hapla 
280ee8b9732SVaclav Hapla /*@
281ee8b9732SVaclav Hapla   PetscViewerHDF5SetCollective - Use collective MPI-IO transfer mode for HDF5 reads and writes.
282ee8b9732SVaclav Hapla 
283ee8b9732SVaclav Hapla   Logically Collective; flg must contain common value
284ee8b9732SVaclav Hapla 
285ee8b9732SVaclav Hapla   Input Parameters:
286ee8b9732SVaclav Hapla + viewer - the PetscViewer; if it is not hdf5 then this command is ignored
287ee8b9732SVaclav Hapla - flg - PETSC_TRUE for collective mode; PETSC_FALSE for independent mode (default)
288ee8b9732SVaclav Hapla 
289ee8b9732SVaclav Hapla   Options Database:
290ee8b9732SVaclav Hapla . -viewer_hdf5_collective - turns on (true) or off (false) collective transfers
291ee8b9732SVaclav Hapla 
292ee8b9732SVaclav Hapla   Notes:
293ee8b9732SVaclav Hapla   Collective mode gives the MPI-IO layer underneath HDF5 a chance to do some additional collective optimizations and hence can perform better.
29453effed7SBarry Smith   However, this works correctly only since HDF5 1.10.3 and if HDF5 is installed for MPI; hence, we ignore this setting for older versions.
295ee8b9732SVaclav Hapla 
296ee8b9732SVaclav Hapla   Developer notes:
297ee8b9732SVaclav Hapla   In the HDF5 layer, PETSC_TRUE / PETSC_FALSE means H5Pset_dxpl_mpio() is called with H5FD_MPIO_COLLECTIVE / H5FD_MPIO_INDEPENDENT, respectively.
298ee8b9732SVaclav Hapla   This in turn means use of MPI_File_{read,write}_all /  MPI_File_{read,write} in the MPI-IO layer, respectively.
299ee8b9732SVaclav Hapla   See HDF5 documentation and MPI-IO documentation for details.
300ee8b9732SVaclav Hapla 
301ee8b9732SVaclav Hapla   Level: intermediate
302ee8b9732SVaclav Hapla 
303db781477SPatrick Sanan .seealso: `PetscViewerHDF5GetCollective()`, `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerHDF5Open()`
304ee8b9732SVaclav Hapla 
305ee8b9732SVaclav Hapla @*/
3069371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5SetCollective(PetscViewer viewer, PetscBool flg) {
307ee8b9732SVaclav Hapla   PetscFunctionBegin;
308ee8b9732SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
309ee8b9732SVaclav Hapla   PetscValidLogicalCollectiveBool(viewer, flg, 2);
310cac4c232SBarry Smith   PetscTryMethod(viewer, "PetscViewerHDF5SetCollective_C", (PetscViewer, PetscBool), (viewer, flg));
311ee8b9732SVaclav Hapla   PetscFunctionReturn(0);
312ee8b9732SVaclav Hapla }
313ee8b9732SVaclav Hapla 
3149371c9d4SSatish Balay static PetscErrorCode PetscViewerHDF5GetCollective_HDF5(PetscViewer viewer, PetscBool *flg) {
315c796909eSBarry Smith #if defined(H5_HAVE_PARALLEL)
316ee8b9732SVaclav Hapla   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data;
317ee8b9732SVaclav Hapla   H5FD_mpio_xfer_t  mode;
318c796909eSBarry Smith #endif
319ee8b9732SVaclav Hapla 
320ee8b9732SVaclav Hapla   PetscFunctionBegin;
321c796909eSBarry Smith #if !defined(H5_HAVE_PARALLEL)
322c796909eSBarry Smith   *flg = PETSC_FALSE;
323c796909eSBarry Smith #else
324792fecdfSBarry Smith   PetscCallHDF5(H5Pget_dxpl_mpio, (hdf5->dxpl_id, &mode));
325ee8b9732SVaclav Hapla   *flg = (mode == H5FD_MPIO_COLLECTIVE) ? PETSC_TRUE : PETSC_FALSE;
326c796909eSBarry Smith #endif
327ee8b9732SVaclav Hapla   PetscFunctionReturn(0);
328ee8b9732SVaclav Hapla }
329ee8b9732SVaclav Hapla 
330ee8b9732SVaclav Hapla /*@
331ee8b9732SVaclav Hapla   PetscViewerHDF5GetCollective - Return flag whether collective MPI-IO transfer mode is used for HDF5 reads and writes.
332ee8b9732SVaclav Hapla 
333ee8b9732SVaclav Hapla   Not Collective
334ee8b9732SVaclav Hapla 
335ee8b9732SVaclav Hapla   Input Parameters:
336ee8b9732SVaclav Hapla . viewer - the HDF5 PetscViewer
337ee8b9732SVaclav Hapla 
338ee8b9732SVaclav Hapla   Output Parameters:
339ee8b9732SVaclav Hapla . flg - the flag
340ee8b9732SVaclav Hapla 
341ee8b9732SVaclav Hapla   Level: intermediate
342ee8b9732SVaclav Hapla 
343ee8b9732SVaclav Hapla   Notes:
344c796909eSBarry Smith   This setting works correctly only since HDF5 1.10.3 and if HDF5 was installed for MPI. For older versions, PETSC_FALSE will be always returned.
345ee8b9732SVaclav Hapla   For more details, see PetscViewerHDF5SetCollective().
346ee8b9732SVaclav Hapla 
347db781477SPatrick Sanan .seealso: `PetscViewerHDF5SetCollective()`, `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerHDF5Open()`
348ee8b9732SVaclav Hapla 
349ee8b9732SVaclav Hapla @*/
3509371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5GetCollective(PetscViewer viewer, PetscBool *flg) {
351ee8b9732SVaclav Hapla   PetscFunctionBegin;
352ee8b9732SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
353534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
354ee8b9732SVaclav Hapla 
355cac4c232SBarry Smith   PetscUseMethod(viewer, "PetscViewerHDF5GetCollective_C", (PetscViewer, PetscBool *), (viewer, flg));
356ee8b9732SVaclav Hapla   PetscFunctionReturn(0);
357ee8b9732SVaclav Hapla }
358ee8b9732SVaclav Hapla 
3599371c9d4SSatish Balay static PetscErrorCode PetscViewerFileSetName_HDF5(PetscViewer viewer, const char name[]) {
3605c6c1daeSBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data;
3615c6c1daeSBarry Smith   hid_t             plist_id;
3625c6c1daeSBarry Smith 
3635c6c1daeSBarry Smith   PetscFunctionBegin;
364792fecdfSBarry Smith   if (hdf5->file_id) PetscCallHDF5(H5Fclose, (hdf5->file_id));
3659566063dSJacob Faibussowitsch   if (hdf5->filename) PetscCall(PetscFree(hdf5->filename));
3669566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(name, &hdf5->filename));
3675c6c1daeSBarry Smith   /* Set up file access property list with parallel I/O access */
368792fecdfSBarry Smith   PetscCallHDF5Return(plist_id, H5Pcreate, (H5P_FILE_ACCESS));
369c796909eSBarry Smith #if defined(H5_HAVE_PARALLEL)
370792fecdfSBarry Smith   PetscCallHDF5(H5Pset_fapl_mpio, (plist_id, PetscObjectComm((PetscObject)viewer), MPI_INFO_NULL));
371c796909eSBarry Smith #endif
3725c6c1daeSBarry Smith   /* Create or open the file collectively */
3735c6c1daeSBarry Smith   switch (hdf5->btype) {
3745c6c1daeSBarry Smith   case FILE_MODE_READ:
3758a2871f6SBarry Smith     if (PetscDefined(USE_DEBUG)) {
3768a2871f6SBarry Smith       PetscMPIInt rank;
3778a2871f6SBarry Smith       PetscBool   flg;
3788a2871f6SBarry Smith 
3799566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
3808a2871f6SBarry Smith       if (rank == 0) {
3819566063dSJacob Faibussowitsch         PetscCall(PetscTestFile(hdf5->filename, 'r', &flg));
3825f80ce2aSJacob Faibussowitsch         PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "File %s requested for reading does not exist", hdf5->filename);
3838a2871f6SBarry Smith       }
3849566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Barrier(PetscObjectComm((PetscObject)viewer)));
3858a2871f6SBarry Smith     }
386792fecdfSBarry Smith     PetscCallHDF5Return(hdf5->file_id, H5Fopen, (name, H5F_ACC_RDONLY, plist_id));
3875c6c1daeSBarry Smith     break;
3885c6c1daeSBarry Smith   case FILE_MODE_APPEND:
3899371c9d4SSatish Balay   case FILE_MODE_UPDATE: {
3907e4fd573SVaclav Hapla     PetscBool flg;
3919566063dSJacob Faibussowitsch     PetscCall(PetscTestFile(hdf5->filename, 'r', &flg));
392792fecdfSBarry Smith     if (flg) PetscCallHDF5Return(hdf5->file_id, H5Fopen, (name, H5F_ACC_RDWR, plist_id));
393792fecdfSBarry Smith     else PetscCallHDF5Return(hdf5->file_id, H5Fcreate, (name, H5F_ACC_EXCL, H5P_DEFAULT, plist_id));
3945c6c1daeSBarry Smith     break;
3957e4fd573SVaclav Hapla   }
3969371c9d4SSatish Balay   case FILE_MODE_WRITE: PetscCallHDF5Return(hdf5->file_id, H5Fcreate, (name, H5F_ACC_TRUNC, H5P_DEFAULT, plist_id)); break;
3979371c9d4SSatish Balay   case FILE_MODE_UNDEFINED: SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_ORDER, "Must call PetscViewerFileSetMode() before PetscViewerFileSetName()");
3989371c9d4SSatish Balay   default: SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Unsupported file mode %s", PetscFileModes[hdf5->btype]);
3995c6c1daeSBarry Smith   }
4005f80ce2aSJacob Faibussowitsch   PetscCheck(hdf5->file_id >= 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "H5Fcreate failed for %s", name);
401792fecdfSBarry Smith   PetscCallHDF5(H5Pclose, (plist_id));
4025c6c1daeSBarry Smith   PetscFunctionReturn(0);
4035c6c1daeSBarry Smith }
4045c6c1daeSBarry Smith 
4059371c9d4SSatish Balay static PetscErrorCode PetscViewerFileGetName_HDF5(PetscViewer viewer, const char **name) {
406d1232d7fSToby Isaac   PetscViewer_HDF5 *vhdf5 = (PetscViewer_HDF5 *)viewer->data;
407d1232d7fSToby Isaac 
408d1232d7fSToby Isaac   PetscFunctionBegin;
409d1232d7fSToby Isaac   *name = vhdf5->filename;
410d1232d7fSToby Isaac   PetscFunctionReturn(0);
411d1232d7fSToby Isaac }
412d1232d7fSToby Isaac 
4139371c9d4SSatish Balay static PetscErrorCode PetscViewerSetUp_HDF5(PetscViewer viewer) {
4145dc64a97SVaclav Hapla   /*
415b723ab35SVaclav Hapla   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5*) viewer->data;
4165dc64a97SVaclav Hapla   */
417b723ab35SVaclav Hapla 
418b723ab35SVaclav Hapla   PetscFunctionBegin;
419b723ab35SVaclav Hapla   PetscFunctionReturn(0);
420b723ab35SVaclav Hapla }
421b723ab35SVaclav Hapla 
4229371c9d4SSatish Balay static PetscErrorCode PetscViewerHDF5SetDefaultTimestepping_HDF5(PetscViewer viewer, PetscBool flg) {
42319a20e4cSMatthew G. Knepley   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data;
42419a20e4cSMatthew G. Knepley 
42519a20e4cSMatthew G. Knepley   PetscFunctionBegin;
42619a20e4cSMatthew G. Knepley   hdf5->defTimestepping = flg;
42719a20e4cSMatthew G. Knepley   PetscFunctionReturn(0);
42819a20e4cSMatthew G. Knepley }
42919a20e4cSMatthew G. Knepley 
4309371c9d4SSatish Balay static PetscErrorCode PetscViewerHDF5GetDefaultTimestepping_HDF5(PetscViewer viewer, PetscBool *flg) {
43119a20e4cSMatthew G. Knepley   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data;
43219a20e4cSMatthew G. Knepley 
43319a20e4cSMatthew G. Knepley   PetscFunctionBegin;
43419a20e4cSMatthew G. Knepley   *flg = hdf5->defTimestepping;
43519a20e4cSMatthew G. Knepley   PetscFunctionReturn(0);
43619a20e4cSMatthew G. Knepley }
43719a20e4cSMatthew G. Knepley 
43819a20e4cSMatthew G. Knepley /*@
43919a20e4cSMatthew G. Knepley   PetscViewerHDF5SetDefaultTimestepping - Set the flag for default timestepping
44019a20e4cSMatthew G. Knepley 
44119a20e4cSMatthew G. Knepley   Logically Collective on PetscViewer
44219a20e4cSMatthew G. Knepley 
44319a20e4cSMatthew G. Knepley   Input Parameters:
44419a20e4cSMatthew G. Knepley + viewer - the PetscViewer; if it is not hdf5 then this command is ignored
44519a20e4cSMatthew G. Knepley - flg    - if PETSC_TRUE we will assume that timestepping is on
44619a20e4cSMatthew G. Knepley 
44719a20e4cSMatthew G. Knepley   Options Database:
44819a20e4cSMatthew G. Knepley . -viewer_hdf5_default_timestepping - turns on (true) or off (false) default timestepping
44919a20e4cSMatthew G. Knepley 
45019a20e4cSMatthew G. Knepley   Notes:
45119a20e4cSMatthew G. Knepley   If the timestepping attribute is not found for an object, then the default timestepping is used
45219a20e4cSMatthew G. Knepley 
45319a20e4cSMatthew G. Knepley   Level: intermediate
45419a20e4cSMatthew G. Knepley 
455db781477SPatrick Sanan .seealso: `PetscViewerHDF5GetDefaultTimestepping()`, `PetscViewerHDF5PushTimestepping()`, `PetscViewerHDF5GetTimestep()`
45619a20e4cSMatthew G. Knepley @*/
4579371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5SetDefaultTimestepping(PetscViewer viewer, PetscBool flg) {
45819a20e4cSMatthew G. Knepley   PetscFunctionBegin;
45919a20e4cSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
460cac4c232SBarry Smith   PetscTryMethod(viewer, "PetscViewerHDF5SetDefaultTimestepping_C", (PetscViewer, PetscBool), (viewer, flg));
46119a20e4cSMatthew G. Knepley   PetscFunctionReturn(0);
46219a20e4cSMatthew G. Knepley }
46319a20e4cSMatthew G. Knepley 
46419a20e4cSMatthew G. Knepley /*@
46519a20e4cSMatthew G. Knepley   PetscViewerHDF5GetDefaultTimestepping - Get the flag for default timestepping
46619a20e4cSMatthew G. Knepley 
46719a20e4cSMatthew G. Knepley   Not collective
46819a20e4cSMatthew G. Knepley 
46919a20e4cSMatthew G. Knepley   Input Parameter:
47019a20e4cSMatthew G. Knepley . viewer - the PetscViewer
47119a20e4cSMatthew G. Knepley 
47219a20e4cSMatthew G. Knepley   Output Parameter:
47319a20e4cSMatthew G. Knepley . flg    - if PETSC_TRUE we will assume that timestepping is on
47419a20e4cSMatthew G. Knepley 
47519a20e4cSMatthew G. Knepley   Notes:
47619a20e4cSMatthew G. Knepley   If the timestepping attribute is not found for an object, then the default timestepping is used
47719a20e4cSMatthew G. Knepley 
47819a20e4cSMatthew G. Knepley   Level: intermediate
47919a20e4cSMatthew G. Knepley 
480db781477SPatrick Sanan .seealso: `PetscViewerHDF5SetDefaultTimestepping()`, `PetscViewerHDF5PushTimestepping()`, `PetscViewerHDF5GetTimestep()`
48119a20e4cSMatthew G. Knepley @*/
4829371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5GetDefaultTimestepping(PetscViewer viewer, PetscBool *flg) {
48319a20e4cSMatthew G. Knepley   PetscFunctionBegin;
48419a20e4cSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
485cac4c232SBarry Smith   PetscUseMethod(viewer, "PetscViewerHDF5GetDefaultTimestepping_C", (PetscViewer, PetscBool *), (viewer, flg));
48619a20e4cSMatthew G. Knepley   PetscFunctionReturn(0);
48719a20e4cSMatthew G. Knepley }
48819a20e4cSMatthew G. Knepley 
4898556b5ebSBarry Smith /*MC
4908556b5ebSBarry Smith    PETSCVIEWERHDF5 - A viewer that writes to an HDF5 file
4918556b5ebSBarry Smith 
492db781477SPatrick Sanan .seealso: `PetscViewerHDF5Open()`, `PetscViewerStringSPrintf()`, `PetscViewerSocketOpen()`, `PetscViewerDrawOpen()`, `PETSCVIEWERSOCKET`,
493db781477SPatrick Sanan           `PetscViewerCreate()`, `PetscViewerASCIIOpen()`, `PetscViewerBinaryOpen()`, `PETSCVIEWERBINARY`, `PETSCVIEWERDRAW`, `PETSCVIEWERSTRING`,
494db781477SPatrick Sanan           `PetscViewerMatlabOpen()`, `VecView()`, `DMView()`, `PetscViewerMatlabPutArray()`, `PETSCVIEWERASCII`, `PETSCVIEWERMATLAB`,
495db781477SPatrick Sanan           `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `PetscViewerFormat`, `PetscViewerType`, `PetscViewerSetType()`
4968556b5ebSBarry Smith 
4971b266c99SBarry Smith   Level: beginner
4988556b5ebSBarry Smith M*/
499d1232d7fSToby Isaac 
5009371c9d4SSatish Balay PETSC_EXTERN PetscErrorCode PetscViewerCreate_HDF5(PetscViewer v) {
5015c6c1daeSBarry Smith   PetscViewer_HDF5 *hdf5;
5025c6c1daeSBarry Smith 
5035c6c1daeSBarry Smith   PetscFunctionBegin;
50499335c34SVaclav Hapla #if !defined(H5_HAVE_PARALLEL)
50599335c34SVaclav Hapla   {
50699335c34SVaclav Hapla     PetscMPIInt size;
5079566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)v), &size));
5085f80ce2aSJacob Faibussowitsch     PetscCheck(size <= 1, PetscObjectComm((PetscObject)v), PETSC_ERR_SUP, "Cannot use parallel HDF5 viewer since the given HDF5 does not support parallel I/O (H5_HAVE_PARALLEL is unset)");
50999335c34SVaclav Hapla   }
51099335c34SVaclav Hapla #endif
51199335c34SVaclav Hapla 
5129566063dSJacob Faibussowitsch   PetscCall(PetscNewLog(v, &hdf5));
5135c6c1daeSBarry Smith 
5145c6c1daeSBarry Smith   v->data                = (void *)hdf5;
5155c6c1daeSBarry Smith   v->ops->destroy        = PetscViewerDestroy_HDF5;
51682ea9b62SBarry Smith   v->ops->setfromoptions = PetscViewerSetFromOptions_HDF5;
517b723ab35SVaclav Hapla   v->ops->setup          = PetscViewerSetUp_HDF5;
5181b793a25SVaclav Hapla   v->ops->view           = PetscViewerView_HDF5;
5196226335aSVaclav Hapla   v->ops->flush          = PetscViewerFlush_HDF5;
5207e4fd573SVaclav Hapla   hdf5->btype            = FILE_MODE_UNDEFINED;
521908793a3SLisandro Dalcin   hdf5->filename         = NULL;
5225c6c1daeSBarry Smith   hdf5->timestep         = -1;
5230298fd71SBarry Smith   hdf5->groups           = NULL;
5245c6c1daeSBarry Smith 
525792fecdfSBarry Smith   PetscCallHDF5Return(hdf5->dxpl_id, H5Pcreate, (H5P_DATASET_XFER));
5269c5072fbSVaclav Hapla 
5279566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetName_C", PetscViewerFileSetName_HDF5));
5289566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetName_C", PetscViewerFileGetName_HDF5));
5299566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileSetMode_C", PetscViewerFileSetMode_HDF5));
5309566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerFileGetMode_C", PetscViewerFileGetMode_HDF5));
5319566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerHDF5SetBaseDimension2_C", PetscViewerHDF5SetBaseDimension2_HDF5));
5329566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerHDF5SetSPOutput_C", PetscViewerHDF5SetSPOutput_HDF5));
5339566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerHDF5SetCollective_C", PetscViewerHDF5SetCollective_HDF5));
5349566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerHDF5GetCollective_C", PetscViewerHDF5GetCollective_HDF5));
5359566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerHDF5GetDefaultTimestepping_C", PetscViewerHDF5GetDefaultTimestepping_HDF5));
5369566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerHDF5SetDefaultTimestepping_C", PetscViewerHDF5SetDefaultTimestepping_HDF5));
5375c6c1daeSBarry Smith   PetscFunctionReturn(0);
5385c6c1daeSBarry Smith }
5395c6c1daeSBarry Smith 
5405c6c1daeSBarry Smith /*@C
5415c6c1daeSBarry Smith    PetscViewerHDF5Open - Opens a file for HDF5 input/output.
5425c6c1daeSBarry Smith 
543d083f849SBarry Smith    Collective
5445c6c1daeSBarry Smith 
5455c6c1daeSBarry Smith    Input Parameters:
5465c6c1daeSBarry Smith +  comm - MPI communicator
5475c6c1daeSBarry Smith .  name - name of file
5485c6c1daeSBarry Smith -  type - type of file
5495c6c1daeSBarry Smith 
5505c6c1daeSBarry Smith    Output Parameter:
5515c6c1daeSBarry Smith .  hdf5v - PetscViewer for HDF5 input/output to use with the specified file
5525c6c1daeSBarry Smith 
55382ea9b62SBarry Smith   Options Database:
554a2b725a8SWilliam Gropp +  -viewer_hdf5_base_dimension2 - turns on (true) or off (false) using a dimension of 2 in the HDF5 file even if the bs/dof of the vector is 1
555a2b725a8SWilliam Gropp -  -viewer_hdf5_sp_output - forces (if true) the viewer to write data in single precision independent on the precision of PetscReal
55682ea9b62SBarry Smith 
5575c6c1daeSBarry Smith    Level: beginner
5585c6c1daeSBarry Smith 
5597e4fd573SVaclav Hapla    Notes:
5607e4fd573SVaclav Hapla    Reading is always available, regardless of the mode. Available modes are
5617e4fd573SVaclav Hapla +  FILE_MODE_READ - open existing HDF5 file for read only access, fail if file does not exist [H5Fopen() with H5F_ACC_RDONLY]
5627e4fd573SVaclav Hapla .  FILE_MODE_WRITE - if file exists, fully overwrite it, else create new HDF5 file [H5FcreateH5Fcreate() with H5F_ACC_TRUNC]
5637e4fd573SVaclav Hapla .  FILE_MODE_APPEND - if file exists, keep existing contents [H5Fopen() with H5F_ACC_RDWR], else create new HDF5 file [H5FcreateH5Fcreate() with H5F_ACC_EXCL]
5647e4fd573SVaclav Hapla -  FILE_MODE_UPDATE - same as FILE_MODE_APPEND
5657e4fd573SVaclav Hapla 
5667e4fd573SVaclav Hapla    In case of FILE_MODE_APPEND / FILE_MODE_UPDATE, any stored object (dataset, attribute) can be selectively ovewritten if the same fully qualified name (/group/path/to/object) is specified.
5677e4fd573SVaclav Hapla 
5685c6c1daeSBarry Smith    This PetscViewer should be destroyed with PetscViewerDestroy().
5695c6c1daeSBarry Smith 
570db781477SPatrick Sanan .seealso: `PetscViewerASCIIOpen()`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`, `PetscViewerHDF5SetBaseDimension2()`,
571db781477SPatrick Sanan           `PetscViewerHDF5SetSPOutput()`, `PetscViewerHDF5GetBaseDimension2()`, `VecView()`, `MatView()`, `VecLoad()`,
572db781477SPatrick Sanan           `MatLoad()`, `PetscFileMode`, `PetscViewer`, `PetscViewerSetType()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetName()`
5735c6c1daeSBarry Smith @*/
5749371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5Open(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *hdf5v) {
5755c6c1daeSBarry Smith   PetscFunctionBegin;
5769566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, hdf5v));
5779566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(*hdf5v, PETSCVIEWERHDF5));
5789566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetMode(*hdf5v, type));
5799566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetName(*hdf5v, name));
5809566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetFromOptions(*hdf5v));
5815c6c1daeSBarry Smith   PetscFunctionReturn(0);
5825c6c1daeSBarry Smith }
5835c6c1daeSBarry Smith 
5845c6c1daeSBarry Smith /*@C
5855c6c1daeSBarry Smith   PetscViewerHDF5GetFileId - Retrieve the file id, this file ID then can be used in direct HDF5 calls
5865c6c1daeSBarry Smith 
5875c6c1daeSBarry Smith   Not collective
5885c6c1daeSBarry Smith 
5895c6c1daeSBarry Smith   Input Parameter:
5905c6c1daeSBarry Smith . viewer - the PetscViewer
5915c6c1daeSBarry Smith 
5925c6c1daeSBarry Smith   Output Parameter:
5935c6c1daeSBarry Smith . file_id - The file id
5945c6c1daeSBarry Smith 
5955c6c1daeSBarry Smith   Level: intermediate
5965c6c1daeSBarry Smith 
597db781477SPatrick Sanan .seealso: `PetscViewerHDF5Open()`
5985c6c1daeSBarry Smith @*/
5999371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5GetFileId(PetscViewer viewer, hid_t *file_id) {
6005c6c1daeSBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data;
6015c6c1daeSBarry Smith 
6025c6c1daeSBarry Smith   PetscFunctionBegin;
6035c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
6045c6c1daeSBarry Smith   if (file_id) *file_id = hdf5->file_id;
6055c6c1daeSBarry Smith   PetscFunctionReturn(0);
6065c6c1daeSBarry Smith }
6075c6c1daeSBarry Smith 
6085c6c1daeSBarry Smith /*@C
6095c6c1daeSBarry Smith   PetscViewerHDF5PushGroup - Set the current HDF5 group for output
6105c6c1daeSBarry Smith 
6115c6c1daeSBarry Smith   Not collective
6125c6c1daeSBarry Smith 
6135c6c1daeSBarry Smith   Input Parameters:
6145c6c1daeSBarry Smith + viewer - the PetscViewer
6155c6c1daeSBarry Smith - name - The group name
6165c6c1daeSBarry Smith 
6175c6c1daeSBarry Smith   Level: intermediate
6185c6c1daeSBarry Smith 
6192e361344SVaclav Hapla   Notes:
6202e361344SVaclav Hapla   This is designed to mnemonically resemble the Unix cd command.
6212e361344SVaclav Hapla   + If name begins with '/', it is interpreted as an absolute path fully replacing current group, otherwise it is taken as relative to the current group.
6222e361344SVaclav Hapla   . NULL, empty string, or any sequence of all slashes (e.g. "///") is interpreted as the root group "/".
6232e361344SVaclav Hapla   - "." means the current group is pushed again.
6242e361344SVaclav Hapla 
6252e361344SVaclav Hapla   Example:
6262e361344SVaclav Hapla   Suppose the current group is "/a".
6272e361344SVaclav Hapla   + If name is NULL, empty string, or a sequence of all slashes (e.g. "///"), then the new group will be "/".
6282e361344SVaclav Hapla   . If name is ".", then the new group will be "/a".
6292e361344SVaclav Hapla   . If name is "b", then the new group will be "/a/b".
6302e361344SVaclav Hapla   - If name is "/b", then the new group will be "/b".
6312e361344SVaclav Hapla 
6322e361344SVaclav Hapla   Developer Notes:
6332e361344SVaclav Hapla   The root group "/" is internally stored as NULL.
634820fc2d1SVaclav Hapla 
635c2e3fba1SPatrick Sanan .seealso: `PetscViewerHDF5Open()`, `PetscViewerHDF5PopGroup()`, `PetscViewerHDF5GetGroup()`, `PetscViewerHDF5OpenGroup()`
6365c6c1daeSBarry Smith @*/
6379371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5PushGroup(PetscViewer viewer, const char name[]) {
6385c6c1daeSBarry Smith   PetscViewer_HDF5         *hdf5 = (PetscViewer_HDF5 *)viewer->data;
6397d964842SVaclav Hapla   PetscViewerHDF5GroupList *groupNode;
6402e361344SVaclav Hapla   size_t                    i, len;
6412e361344SVaclav Hapla   char                      buf[PETSC_MAX_PATH_LEN];
6422e361344SVaclav Hapla   const char               *gname;
6435c6c1daeSBarry Smith 
6445c6c1daeSBarry Smith   PetscFunctionBegin;
6455c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
646820fc2d1SVaclav Hapla   if (name) PetscValidCharPointer(name, 2);
6479566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(name, &len));
6482e361344SVaclav Hapla   gname = NULL;
6492e361344SVaclav Hapla   if (len) {
6502e361344SVaclav Hapla     if (len == 1 && name[0] == '.') {
6512e361344SVaclav Hapla       /* use current name */
6522e361344SVaclav Hapla       gname = (hdf5->groups && hdf5->groups->name) ? hdf5->groups->name : NULL;
6532e361344SVaclav Hapla     } else if (name[0] == '/') {
6542e361344SVaclav Hapla       /* absolute */
6552e361344SVaclav Hapla       for (i = 1; i < len; i++) {
6562e361344SVaclav Hapla         if (name[i] != '/') {
6572e361344SVaclav Hapla           gname = name;
6582e361344SVaclav Hapla           break;
6592e361344SVaclav Hapla         }
6602e361344SVaclav Hapla       }
6612e361344SVaclav Hapla     } else {
6622e361344SVaclav Hapla       /* relative */
6632e361344SVaclav Hapla       const char *parent = (hdf5->groups && hdf5->groups->name) ? hdf5->groups->name : "";
6649566063dSJacob Faibussowitsch       PetscCall(PetscSNPrintf(buf, sizeof(buf), "%s/%s", parent, name));
6652e361344SVaclav Hapla       gname = buf;
6662e361344SVaclav Hapla     }
6672e361344SVaclav Hapla   }
6689566063dSJacob Faibussowitsch   PetscCall(PetscNew(&groupNode));
6699566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(gname, (char **)&groupNode->name));
6705c6c1daeSBarry Smith   groupNode->next = hdf5->groups;
6715c6c1daeSBarry Smith   hdf5->groups    = groupNode;
6725c6c1daeSBarry Smith   PetscFunctionReturn(0);
6735c6c1daeSBarry Smith }
6745c6c1daeSBarry Smith 
6751ad4d0e4SMatthew G. Knepley /*@C
6761ad4d0e4SMatthew G. Knepley   PetscViewerHDF5PushGroupRelative - Set the current HDF5 group for output, appending the path in the argument
6771ad4d0e4SMatthew G. Knepley 
6781ad4d0e4SMatthew G. Knepley   Not collective
6791ad4d0e4SMatthew G. Knepley 
6801ad4d0e4SMatthew G. Knepley   Input Parameters:
6811ad4d0e4SMatthew G. Knepley + viewer - the PetscViewer
6821ad4d0e4SMatthew G. Knepley - name - The group name to append
6831ad4d0e4SMatthew G. Knepley 
6841ad4d0e4SMatthew G. Knepley   Level: intermediate
6851ad4d0e4SMatthew G. Knepley 
6861ad4d0e4SMatthew G. Knepley   Notes:
6871ad4d0e4SMatthew G. Knepley   This is designed to mnemonically resemble the Unix cd command.
6881ad4d0e4SMatthew G. Knepley   + If name begins with '/', it is interpreted as an absolute path fully replacing current group, otherwise it is taken as relative to the current group.
6891ad4d0e4SMatthew G. Knepley   . NULL, empty string, or any sequence of all slashes (e.g. "///") is interpreted as the root group "/".
6901ad4d0e4SMatthew G. Knepley   - "." means the current group is pushed again.
6911ad4d0e4SMatthew G. Knepley 
6921ad4d0e4SMatthew G. Knepley   Example:
6931ad4d0e4SMatthew G. Knepley   Suppose the current group is "/a".
6941ad4d0e4SMatthew G. Knepley   + If name is NULL, empty string, or a sequence of all slashes (e.g. "///"), then the new group will be "/".
6951ad4d0e4SMatthew G. Knepley   . If name is ".", then the new group will be "/a".
6961ad4d0e4SMatthew G. Knepley   . If name is "b", then the new group will be "/a/b".
6971ad4d0e4SMatthew G. Knepley   - If name is "/b", then the new group will be "/b".
6981ad4d0e4SMatthew G. Knepley 
6991ad4d0e4SMatthew G. Knepley   Developer Notes:
7001ad4d0e4SMatthew G. Knepley   The root group "/" is internally stored as NULL.
7011ad4d0e4SMatthew G. Knepley 
7021ad4d0e4SMatthew G. Knepley .seealso: `PetscViewerHDF5PushGroup()`, `PetscViewerHDF5PopGroup()`, `PetscViewerHDF5GetGroup()`, `PetscViewerHDF5OpenGroup()`, `PetscViewerHDF5Open()`
7031ad4d0e4SMatthew G. Knepley @*/
7049371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5PushGroupRelative(PetscViewer viewer, const char name[]) {
7051ad4d0e4SMatthew G. Knepley   PetscViewer_HDF5         *hdf5 = (PetscViewer_HDF5 *)viewer->data;
7061ad4d0e4SMatthew G. Knepley   PetscViewerHDF5GroupList *groupNode;
7071ad4d0e4SMatthew G. Knepley   char                      groupname[PETSC_MAX_PATH_LEN];
7081ad4d0e4SMatthew G. Knepley 
7091ad4d0e4SMatthew G. Knepley   PetscFunctionBegin;
7101ad4d0e4SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
7111ad4d0e4SMatthew G. Knepley   if (name) PetscValidCharPointer(name, 2);
7121ad4d0e4SMatthew G. Knepley   if (name && name[0]) {
7131ad4d0e4SMatthew G. Knepley     size_t i, len;
7141ad4d0e4SMatthew G. Knepley     PetscCall(PetscStrlen(name, &len));
7159371c9d4SSatish Balay     for (i = 0; i < len; i++)
7169371c9d4SSatish Balay       if (name[i] != '/') break;
7171ad4d0e4SMatthew G. Knepley     if (i == len) name = NULL;
7181ad4d0e4SMatthew G. Knepley   } else name = NULL;
7191ad4d0e4SMatthew G. Knepley   PetscCall(PetscNew(&groupNode));
7201ad4d0e4SMatthew G. Knepley   PetscCall(PetscStrncpy(groupname, hdf5->groups->name, sizeof(groupname)));
7211ad4d0e4SMatthew G. Knepley   if (name[0] != '/') PetscCall(PetscStrlcat(groupname, "/", sizeof(groupname)));
7221ad4d0e4SMatthew G. Knepley   PetscCall(PetscStrlcat(groupname, name, sizeof(groupname)));
7231ad4d0e4SMatthew G. Knepley   PetscCall(PetscStrallocpy(groupname, (char **)&groupNode->name));
7241ad4d0e4SMatthew G. Knepley   groupNode->next = hdf5->groups;
7251ad4d0e4SMatthew G. Knepley   hdf5->groups    = groupNode;
7261ad4d0e4SMatthew G. Knepley   PetscFunctionReturn(0);
7271ad4d0e4SMatthew G. Knepley }
7281ad4d0e4SMatthew G. Knepley 
7293ef9c667SSatish Balay /*@
7305c6c1daeSBarry Smith   PetscViewerHDF5PopGroup - Return the current HDF5 group for output to the previous value
7315c6c1daeSBarry Smith 
7325c6c1daeSBarry Smith   Not collective
7335c6c1daeSBarry Smith 
7345c6c1daeSBarry Smith   Input Parameter:
7355c6c1daeSBarry Smith . viewer - the PetscViewer
7365c6c1daeSBarry Smith 
7375c6c1daeSBarry Smith   Level: intermediate
7385c6c1daeSBarry Smith 
739c2e3fba1SPatrick Sanan .seealso: `PetscViewerHDF5Open()`, `PetscViewerHDF5PushGroup()`, `PetscViewerHDF5GetGroup()`, `PetscViewerHDF5OpenGroup()`
7405c6c1daeSBarry Smith @*/
7419371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5PopGroup(PetscViewer viewer) {
7425c6c1daeSBarry Smith   PetscViewer_HDF5         *hdf5 = (PetscViewer_HDF5 *)viewer->data;
7437d964842SVaclav Hapla   PetscViewerHDF5GroupList *groupNode;
7445c6c1daeSBarry Smith 
7455c6c1daeSBarry Smith   PetscFunctionBegin;
7465c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
7475f80ce2aSJacob Faibussowitsch   PetscCheck(hdf5->groups, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "HDF5 group stack is empty, cannot pop");
7485c6c1daeSBarry Smith   groupNode    = hdf5->groups;
7495c6c1daeSBarry Smith   hdf5->groups = hdf5->groups->next;
7509566063dSJacob Faibussowitsch   PetscCall(PetscFree(groupNode->name));
7519566063dSJacob Faibussowitsch   PetscCall(PetscFree(groupNode));
7525c6c1daeSBarry Smith   PetscFunctionReturn(0);
7535c6c1daeSBarry Smith }
7545c6c1daeSBarry Smith 
7555c6c1daeSBarry Smith /*@C
756874270d9SVaclav Hapla   PetscViewerHDF5GetGroup - Get the current HDF5 group name (full path), set with PetscViewerHDF5PushGroup()/PetscViewerHDF5PopGroup().
757874270d9SVaclav Hapla   If none has been assigned, returns NULL.
7585c6c1daeSBarry Smith 
7595c6c1daeSBarry Smith   Not collective
7605c6c1daeSBarry Smith 
7615c6c1daeSBarry Smith   Input Parameter:
7625c6c1daeSBarry Smith . viewer - the PetscViewer
7635c6c1daeSBarry Smith 
7645c6c1daeSBarry Smith   Output Parameter:
7655c6c1daeSBarry Smith . name - The group name
7665c6c1daeSBarry Smith 
7675c6c1daeSBarry Smith   Level: intermediate
7685c6c1daeSBarry Smith 
769c2e3fba1SPatrick Sanan .seealso: `PetscViewerHDF5Open()`, `PetscViewerHDF5PushGroup()`, `PetscViewerHDF5PopGroup()`, `PetscViewerHDF5OpenGroup()`
7705c6c1daeSBarry Smith @*/
7719371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5GetGroup(PetscViewer viewer, const char *name[]) {
7725c6c1daeSBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data;
7735c6c1daeSBarry Smith 
7745c6c1daeSBarry Smith   PetscFunctionBegin;
7755c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
776c959eef4SJed Brown   PetscValidPointer(name, 2);
777a297a907SKarl Rupp   if (hdf5->groups) *name = hdf5->groups->name;
7780298fd71SBarry Smith   else *name = NULL;
7795c6c1daeSBarry Smith   PetscFunctionReturn(0);
7805c6c1daeSBarry Smith }
7815c6c1daeSBarry Smith 
7828c557b5aSVaclav Hapla /*@
783874270d9SVaclav Hapla   PetscViewerHDF5OpenGroup - Open the HDF5 group with the name (full path) returned by PetscViewerHDF5GetGroup(),
784874270d9SVaclav Hapla   and return this group's ID and file ID.
785874270d9SVaclav Hapla   If PetscViewerHDF5GetGroup() yields NULL, then group ID is file ID.
786874270d9SVaclav Hapla 
787874270d9SVaclav Hapla   Not collective
788874270d9SVaclav Hapla 
789874270d9SVaclav Hapla   Input Parameter:
790874270d9SVaclav Hapla . viewer - the PetscViewer
791874270d9SVaclav Hapla 
792d8d19677SJose E. Roman   Output Parameters:
793874270d9SVaclav Hapla + fileId - The HDF5 file ID
794874270d9SVaclav Hapla - groupId - The HDF5 group ID
795874270d9SVaclav Hapla 
796e74616beSVaclav Hapla   Notes:
797e74616beSVaclav Hapla   If the viewer is writable, the group is created if it doesn't exist yet.
798e74616beSVaclav Hapla 
799874270d9SVaclav Hapla   Level: intermediate
800874270d9SVaclav Hapla 
801c2e3fba1SPatrick Sanan .seealso: `PetscViewerHDF5Open()`, `PetscViewerHDF5PushGroup()`, `PetscViewerHDF5PopGroup()`, `PetscViewerHDF5GetGroup()`
802874270d9SVaclav Hapla @*/
8039371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5OpenGroup(PetscViewer viewer, hid_t *fileId, hid_t *groupId) {
80490e03537SVaclav Hapla   hid_t       file_id;
80590e03537SVaclav Hapla   H5O_type_t  type;
80676d59af2SVaclav Hapla   const char *groupName = NULL, *fileName = NULL;
80776d59af2SVaclav Hapla   PetscBool   writable, has;
80854dbf706SVaclav Hapla 
80954dbf706SVaclav Hapla   PetscFunctionBegin;
8109566063dSJacob Faibussowitsch   PetscCall(PetscViewerWritable(viewer, &writable));
8119566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5GetFileId(viewer, &file_id));
8129566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileGetName(viewer, &fileName));
8139566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5GetGroup(viewer, &groupName));
8149566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5Traverse_Internal(viewer, groupName, writable, &has, &type));
81576d59af2SVaclav Hapla   if (!has) {
8165f80ce2aSJacob Faibussowitsch     PetscCheck(writable, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Group %s does not exist and file %s is not open for writing", groupName, fileName);
817f7d195e4SLawrence Mitchell     SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_LIB, "HDF5 failed to create group %s although file %s is open for writing", groupName, fileName);
81876d59af2SVaclav Hapla   }
8195f80ce2aSJacob Faibussowitsch   PetscCheck(type == H5O_TYPE_GROUP, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Path %s in file %s resolves to something which is not a group", groupName, fileName);
820792fecdfSBarry Smith   PetscCallHDF5Return(*groupId, H5Gopen2, (file_id, groupName ? groupName : "/", H5P_DEFAULT));
82154dbf706SVaclav Hapla   *fileId = file_id;
82254dbf706SVaclav Hapla   PetscFunctionReturn(0);
82354dbf706SVaclav Hapla }
82454dbf706SVaclav Hapla 
8253ef9c667SSatish Balay /*@
826d7dd068bSVaclav Hapla   PetscViewerHDF5PushTimestepping - Activate timestepping mode for subsequent HDF5 reading and writing.
8275c6c1daeSBarry Smith 
8285c6c1daeSBarry Smith   Not collective
8295c6c1daeSBarry Smith 
8305c6c1daeSBarry Smith   Input Parameter:
8315c6c1daeSBarry Smith . viewer - the PetscViewer
8325c6c1daeSBarry Smith 
8335c6c1daeSBarry Smith   Level: intermediate
8345c6c1daeSBarry Smith 
835d7dd068bSVaclav Hapla   Notes:
836d7dd068bSVaclav Hapla   On first PetscViewerHDF5PushTimestepping(), the initial time step is set to 0.
837d7dd068bSVaclav Hapla   Next timesteps can then be set using PetscViewerHDF5IncrementTimestep() or PetscViewerHDF5SetTimestep().
838d7dd068bSVaclav Hapla   Current timestep value determines which timestep is read from or written to any dataset on the next HDF5 I/O operation [e.g. VecView()].
839d7dd068bSVaclav Hapla   Use PetscViewerHDF5PopTimestepping() to deactivate timestepping mode; calling it by the end of the program is NOT mandatory.
840d7dd068bSVaclav Hapla   Current timestep is remembered between PetscViewerHDF5PopTimestepping() and the next PetscViewerHDF5PushTimestepping().
841d7dd068bSVaclav Hapla 
842d7dd068bSVaclav Hapla   If a dataset was stored with timestepping, it can be loaded only in the timestepping mode again.
843d7dd068bSVaclav Hapla   Loading a timestepped dataset with timestepping disabled, or vice-versa results in an error.
844d7dd068bSVaclav Hapla 
845d7dd068bSVaclav Hapla   Developer notes:
846d7dd068bSVaclav Hapla   Timestepped HDF5 dataset has an extra dimension and attribute "timestepping" set to true.
847d7dd068bSVaclav Hapla 
848db781477SPatrick Sanan .seealso: `PetscViewerHDF5Open()`, `PetscViewerHDF5PopTimestepping()`, `PetscViewerHDF5IsTimestepping()`, `PetscViewerHDF5SetTimestep()`, `PetscViewerHDF5IncrementTimestep()`, `PetscViewerHDF5GetTimestep()`
849d7dd068bSVaclav Hapla @*/
8509371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5PushTimestepping(PetscViewer viewer) {
851d7dd068bSVaclav Hapla   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data;
852d7dd068bSVaclav Hapla 
853d7dd068bSVaclav Hapla   PetscFunctionBegin;
854d7dd068bSVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
8555f80ce2aSJacob Faibussowitsch   PetscCheck(!hdf5->timestepping, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Timestepping is already pushed");
856d7dd068bSVaclav Hapla   hdf5->timestepping = PETSC_TRUE;
857d7dd068bSVaclav Hapla   if (hdf5->timestep < 0) hdf5->timestep = 0;
858d7dd068bSVaclav Hapla   PetscFunctionReturn(0);
859d7dd068bSVaclav Hapla }
860d7dd068bSVaclav Hapla 
861d7dd068bSVaclav Hapla /*@
862d7dd068bSVaclav Hapla   PetscViewerHDF5PopTimestepping - Deactivate timestepping mode for subsequent HDF5 reading and writing.
863d7dd068bSVaclav Hapla 
864d7dd068bSVaclav Hapla   Not collective
865d7dd068bSVaclav Hapla 
866d7dd068bSVaclav Hapla   Input Parameter:
867d7dd068bSVaclav Hapla . viewer - the PetscViewer
868d7dd068bSVaclav Hapla 
869d7dd068bSVaclav Hapla   Level: intermediate
870d7dd068bSVaclav Hapla 
871d7dd068bSVaclav Hapla   Notes:
872d7dd068bSVaclav Hapla   See PetscViewerHDF5PushTimestepping() for details.
873d7dd068bSVaclav Hapla 
874db781477SPatrick Sanan .seealso: `PetscViewerHDF5Open()`, `PetscViewerHDF5PushTimestepping()`, `PetscViewerHDF5IsTimestepping()`, `PetscViewerHDF5SetTimestep()`, `PetscViewerHDF5IncrementTimestep()`, `PetscViewerHDF5GetTimestep()`
875d7dd068bSVaclav Hapla @*/
8769371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5PopTimestepping(PetscViewer viewer) {
877d7dd068bSVaclav Hapla   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data;
878d7dd068bSVaclav Hapla 
879d7dd068bSVaclav Hapla   PetscFunctionBegin;
880d7dd068bSVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
8815f80ce2aSJacob Faibussowitsch   PetscCheck(hdf5->timestepping, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Timestepping has not been pushed yet. Call PetscViewerHDF5PushTimestepping() first");
882d7dd068bSVaclav Hapla   hdf5->timestepping = PETSC_FALSE;
883d7dd068bSVaclav Hapla   PetscFunctionReturn(0);
884d7dd068bSVaclav Hapla }
885d7dd068bSVaclav Hapla 
886d7dd068bSVaclav Hapla /*@
887d7dd068bSVaclav Hapla   PetscViewerHDF5IsTimestepping - Ask the viewer whether it is in timestepping mode currently.
888d7dd068bSVaclav Hapla 
889d7dd068bSVaclav Hapla   Not collective
890d7dd068bSVaclav Hapla 
891d7dd068bSVaclav Hapla   Input Parameter:
892d7dd068bSVaclav Hapla . viewer - the PetscViewer
893d7dd068bSVaclav Hapla 
894d7dd068bSVaclav Hapla   Output Parameter:
895d7dd068bSVaclav Hapla . flg - is timestepping active?
896d7dd068bSVaclav Hapla 
897d7dd068bSVaclav Hapla   Level: intermediate
898d7dd068bSVaclav Hapla 
899d7dd068bSVaclav Hapla   Notes:
900d7dd068bSVaclav Hapla   See PetscViewerHDF5PushTimestepping() for details.
901d7dd068bSVaclav Hapla 
902db781477SPatrick Sanan .seealso: `PetscViewerHDF5Open()`, `PetscViewerHDF5PushTimestepping()`, `PetscViewerHDF5PopTimestepping()`, `PetscViewerHDF5SetTimestep()`, `PetscViewerHDF5IncrementTimestep()`, `PetscViewerHDF5GetTimestep()`
903d7dd068bSVaclav Hapla @*/
9049371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5IsTimestepping(PetscViewer viewer, PetscBool *flg) {
905d7dd068bSVaclav Hapla   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data;
906d7dd068bSVaclav Hapla 
907d7dd068bSVaclav Hapla   PetscFunctionBegin;
908d7dd068bSVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
909d7dd068bSVaclav Hapla   *flg = hdf5->timestepping;
910d7dd068bSVaclav Hapla   PetscFunctionReturn(0);
911d7dd068bSVaclav Hapla }
912d7dd068bSVaclav Hapla 
913d7dd068bSVaclav Hapla /*@
914d7dd068bSVaclav Hapla   PetscViewerHDF5IncrementTimestep - Increments current timestep for the HDF5 output. Fields are stacked in time.
915d7dd068bSVaclav Hapla 
916d7dd068bSVaclav Hapla   Not collective
917d7dd068bSVaclav Hapla 
918d7dd068bSVaclav Hapla   Input Parameter:
919d7dd068bSVaclav Hapla . viewer - the PetscViewer
920d7dd068bSVaclav Hapla 
921d7dd068bSVaclav Hapla   Level: intermediate
922d7dd068bSVaclav Hapla 
923d7dd068bSVaclav Hapla   Notes:
924d7dd068bSVaclav Hapla   This can be called only if the viewer is in timestepping mode. See PetscViewerHDF5PushTimestepping() for details.
925d7dd068bSVaclav Hapla 
926db781477SPatrick Sanan .seealso: `PetscViewerHDF5Open()`, `PetscViewerHDF5PushTimestepping()`, `PetscViewerHDF5SetTimestep()`, `PetscViewerHDF5GetTimestep()`
9275c6c1daeSBarry Smith @*/
9289371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5IncrementTimestep(PetscViewer viewer) {
9295c6c1daeSBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data;
9305c6c1daeSBarry Smith 
9315c6c1daeSBarry Smith   PetscFunctionBegin;
9325c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
9335f80ce2aSJacob Faibussowitsch   PetscCheck(hdf5->timestepping, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Timestepping has not been pushed yet. Call PetscViewerHDF5PushTimestepping() first");
9345c6c1daeSBarry Smith   ++hdf5->timestep;
9355c6c1daeSBarry Smith   PetscFunctionReturn(0);
9365c6c1daeSBarry Smith }
9375c6c1daeSBarry Smith 
9383ef9c667SSatish Balay /*@
939d7dd068bSVaclav Hapla   PetscViewerHDF5SetTimestep - Set the current timestep for the HDF5 output. Fields are stacked in time.
9405c6c1daeSBarry Smith 
941d7dd068bSVaclav Hapla   Logically collective
9425c6c1daeSBarry Smith 
9435c6c1daeSBarry Smith   Input Parameters:
9445c6c1daeSBarry Smith + viewer - the PetscViewer
945d7dd068bSVaclav Hapla - timestep - The timestep
9465c6c1daeSBarry Smith 
9475c6c1daeSBarry Smith   Level: intermediate
9485c6c1daeSBarry Smith 
949d7dd068bSVaclav Hapla   Notes:
950d7dd068bSVaclav Hapla   This can be called only if the viewer is in timestepping mode. See PetscViewerHDF5PushTimestepping() for details.
951d7dd068bSVaclav Hapla 
952db781477SPatrick Sanan .seealso: `PetscViewerHDF5Open()`, `PetscViewerHDF5PushTimestepping()`, `PetscViewerHDF5IncrementTimestep()`, `PetscViewerHDF5GetTimestep()`
9535c6c1daeSBarry Smith @*/
9549371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5SetTimestep(PetscViewer viewer, PetscInt timestep) {
9555c6c1daeSBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data;
9565c6c1daeSBarry Smith 
9575c6c1daeSBarry Smith   PetscFunctionBegin;
9585c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
959d7dd068bSVaclav Hapla   PetscValidLogicalCollectiveInt(viewer, timestep, 2);
9605f80ce2aSJacob Faibussowitsch   PetscCheck(timestep >= 0, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Timestep %" PetscInt_FMT " is negative", timestep);
9615f80ce2aSJacob Faibussowitsch   PetscCheck(hdf5->timestepping, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Timestepping has not been pushed yet. Call PetscViewerHDF5PushTimestepping() first");
9625c6c1daeSBarry Smith   hdf5->timestep = timestep;
9635c6c1daeSBarry Smith   PetscFunctionReturn(0);
9645c6c1daeSBarry Smith }
9655c6c1daeSBarry Smith 
9663ef9c667SSatish Balay /*@
9675c6c1daeSBarry Smith   PetscViewerHDF5GetTimestep - Get the current timestep for the HDF5 output. Fields are stacked in time.
9685c6c1daeSBarry Smith 
9695c6c1daeSBarry Smith   Not collective
9705c6c1daeSBarry Smith 
9715c6c1daeSBarry Smith   Input Parameter:
9725c6c1daeSBarry Smith . viewer - the PetscViewer
9735c6c1daeSBarry Smith 
9745c6c1daeSBarry Smith   Output Parameter:
975d7dd068bSVaclav Hapla . timestep - The timestep
9765c6c1daeSBarry Smith 
9775c6c1daeSBarry Smith   Level: intermediate
9785c6c1daeSBarry Smith 
979d7dd068bSVaclav Hapla   Notes:
980d7dd068bSVaclav Hapla   This can be called only if the viewer is in the timestepping mode. See PetscViewerHDF5PushTimestepping() for details.
981d7dd068bSVaclav Hapla 
982db781477SPatrick Sanan .seealso: `PetscViewerHDF5Open()`, `PetscViewerHDF5PushTimestepping()`, `PetscViewerHDF5IncrementTimestep()`, `PetscViewerHDF5SetTimestep()`
9835c6c1daeSBarry Smith @*/
9849371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5GetTimestep(PetscViewer viewer, PetscInt *timestep) {
9855c6c1daeSBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *)viewer->data;
9865c6c1daeSBarry Smith 
9875c6c1daeSBarry Smith   PetscFunctionBegin;
9885c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
989d7dd068bSVaclav Hapla   PetscValidIntPointer(timestep, 2);
9905f80ce2aSJacob Faibussowitsch   PetscCheck(hdf5->timestepping, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Timestepping has not been pushed yet. Call PetscViewerHDF5PushTimestepping() first");
9915c6c1daeSBarry Smith   *timestep = hdf5->timestep;
9925c6c1daeSBarry Smith   PetscFunctionReturn(0);
9935c6c1daeSBarry Smith }
9945c6c1daeSBarry Smith 
99536bce6e8SMatthew G. Knepley /*@C
99636bce6e8SMatthew G. Knepley   PetscDataTypeToHDF5DataType - Converts the PETSc name of a datatype to its HDF5 name.
99736bce6e8SMatthew G. Knepley 
99836bce6e8SMatthew G. Knepley   Not collective
99936bce6e8SMatthew G. Knepley 
100036bce6e8SMatthew G. Knepley   Input Parameter:
100136bce6e8SMatthew G. Knepley . ptype - the PETSc datatype name (for example PETSC_DOUBLE)
100236bce6e8SMatthew G. Knepley 
100336bce6e8SMatthew G. Knepley   Output Parameter:
100436bce6e8SMatthew G. Knepley . mtype - the MPI datatype (for example MPI_DOUBLE, ...)
100536bce6e8SMatthew G. Knepley 
100636bce6e8SMatthew G. Knepley   Level: advanced
100736bce6e8SMatthew G. Knepley 
1008db781477SPatrick Sanan .seealso: `PetscDataType`, `PetscHDF5DataTypeToPetscDataType()`
100936bce6e8SMatthew G. Knepley @*/
10109371c9d4SSatish Balay PetscErrorCode PetscDataTypeToHDF5DataType(PetscDataType ptype, hid_t *htype) {
101136bce6e8SMatthew G. Knepley   PetscFunctionBegin;
101236bce6e8SMatthew G. Knepley   if (ptype == PETSC_INT)
101336bce6e8SMatthew G. Knepley #if defined(PETSC_USE_64BIT_INDICES)
101436bce6e8SMatthew G. Knepley     *htype = H5T_NATIVE_LLONG;
101536bce6e8SMatthew G. Knepley #else
101636bce6e8SMatthew G. Knepley     *htype = H5T_NATIVE_INT;
101736bce6e8SMatthew G. Knepley #endif
101836bce6e8SMatthew G. Knepley   else if (ptype == PETSC_DOUBLE) *htype = H5T_NATIVE_DOUBLE;
101936bce6e8SMatthew G. Knepley   else if (ptype == PETSC_LONG) *htype = H5T_NATIVE_LONG;
102036bce6e8SMatthew G. Knepley   else if (ptype == PETSC_SHORT) *htype = H5T_NATIVE_SHORT;
1021c9450970SBarry Smith   else if (ptype == PETSC_ENUM) *htype = H5T_NATIVE_INT;
1022de3ba262SVaclav Hapla   else if (ptype == PETSC_BOOL) *htype = H5T_NATIVE_INT;
102336bce6e8SMatthew G. Knepley   else if (ptype == PETSC_FLOAT) *htype = H5T_NATIVE_FLOAT;
102436bce6e8SMatthew G. Knepley   else if (ptype == PETSC_CHAR) *htype = H5T_NATIVE_CHAR;
102536bce6e8SMatthew G. Knepley   else if (ptype == PETSC_BIT_LOGICAL) *htype = H5T_NATIVE_UCHAR;
10267e97c476SMatthew G. Knepley   else if (ptype == PETSC_STRING) *htype = H5Tcopy(H5T_C_S1);
102736bce6e8SMatthew G. Knepley   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported PETSc datatype");
102836bce6e8SMatthew G. Knepley   PetscFunctionReturn(0);
102936bce6e8SMatthew G. Knepley }
103036bce6e8SMatthew G. Knepley 
103136bce6e8SMatthew G. Knepley /*@C
103236bce6e8SMatthew G. Knepley   PetscHDF5DataTypeToPetscDataType - Finds the PETSc name of a datatype from its HDF5 name
103336bce6e8SMatthew G. Knepley 
103436bce6e8SMatthew G. Knepley   Not collective
103536bce6e8SMatthew G. Knepley 
103636bce6e8SMatthew G. Knepley   Input Parameter:
103736bce6e8SMatthew G. Knepley . htype - the HDF5 datatype (for example H5T_NATIVE_DOUBLE, ...)
103836bce6e8SMatthew G. Knepley 
103936bce6e8SMatthew G. Knepley   Output Parameter:
104036bce6e8SMatthew G. Knepley . ptype - the PETSc datatype name (for example PETSC_DOUBLE)
104136bce6e8SMatthew G. Knepley 
104236bce6e8SMatthew G. Knepley   Level: advanced
104336bce6e8SMatthew G. Knepley 
1044db781477SPatrick Sanan .seealso: `PetscDataType`, `PetscHDF5DataTypeToPetscDataType()`
104536bce6e8SMatthew G. Knepley @*/
10469371c9d4SSatish Balay PetscErrorCode PetscHDF5DataTypeToPetscDataType(hid_t htype, PetscDataType *ptype) {
104736bce6e8SMatthew G. Knepley   PetscFunctionBegin;
104836bce6e8SMatthew G. Knepley #if defined(PETSC_USE_64BIT_INDICES)
104936bce6e8SMatthew G. Knepley   if (htype == H5T_NATIVE_INT) *ptype = PETSC_LONG;
105036bce6e8SMatthew G. Knepley   else if (htype == H5T_NATIVE_LLONG) *ptype = PETSC_INT;
105136bce6e8SMatthew G. Knepley #else
105236bce6e8SMatthew G. Knepley   if (htype == H5T_NATIVE_INT) *ptype = PETSC_INT;
105336bce6e8SMatthew G. Knepley #endif
105436bce6e8SMatthew G. Knepley   else if (htype == H5T_NATIVE_DOUBLE) *ptype = PETSC_DOUBLE;
105536bce6e8SMatthew G. Knepley   else if (htype == H5T_NATIVE_LONG) *ptype = PETSC_LONG;
105636bce6e8SMatthew G. Knepley   else if (htype == H5T_NATIVE_SHORT) *ptype = PETSC_SHORT;
105736bce6e8SMatthew G. Knepley   else if (htype == H5T_NATIVE_FLOAT) *ptype = PETSC_FLOAT;
105836bce6e8SMatthew G. Knepley   else if (htype == H5T_NATIVE_CHAR) *ptype = PETSC_CHAR;
105936bce6e8SMatthew G. Knepley   else if (htype == H5T_NATIVE_UCHAR) *ptype = PETSC_CHAR;
10607e97c476SMatthew G. Knepley   else if (htype == H5T_C_S1) *ptype = PETSC_STRING;
106136bce6e8SMatthew G. Knepley   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported HDF5 datatype");
106236bce6e8SMatthew G. Knepley   PetscFunctionReturn(0);
106336bce6e8SMatthew G. Knepley }
106436bce6e8SMatthew G. Knepley 
1065df863907SAlex Fikl /*@C
1066b8ee85a1SVaclav Hapla  PetscViewerHDF5WriteAttribute - Write an attribute
106736bce6e8SMatthew G. Knepley 
1068343a20b2SVaclav Hapla   Collective
1069343a20b2SVaclav Hapla 
107036bce6e8SMatthew G. Knepley   Input Parameters:
107136bce6e8SMatthew G. Knepley + viewer - The HDF5 viewer
10724302210dSVaclav Hapla . parent - The parent dataset/group name
107336bce6e8SMatthew G. Knepley . name   - The attribute name
107436bce6e8SMatthew G. Knepley . datatype - The attribute type
107536bce6e8SMatthew G. Knepley - value    - The attribute value
107636bce6e8SMatthew G. Knepley 
107736bce6e8SMatthew G. Knepley   Level: advanced
107836bce6e8SMatthew G. Knepley 
10794302210dSVaclav Hapla   Notes:
1080343a20b2SVaclav Hapla   If parent starts with '/', it is taken as an absolute path overriding currently pushed group, else parent is relative to the current pushed group. NULL means the current pushed group.
10814302210dSVaclav Hapla 
1082c2e3fba1SPatrick Sanan .seealso: `PetscViewerHDF5Open()`, `PetscViewerHDF5WriteObjectAttribute()`, `PetscViewerHDF5ReadAttribute()`, `PetscViewerHDF5HasAttribute()`, `PetscViewerHDF5PushGroup()`, `PetscViewerHDF5PopGroup()`, `PetscViewerHDF5GetGroup()`
108336bce6e8SMatthew G. Knepley @*/
10849371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5WriteAttribute(PetscViewer viewer, const char parent[], const char name[], PetscDataType datatype, const void *value) {
10854302210dSVaclav Hapla   char     *parentAbsPath;
108660568592SMatthew G. Knepley   hid_t     h5, dataspace, obj, attribute, dtype;
1087080f144cSVaclav Hapla   PetscBool has;
108836bce6e8SMatthew G. Knepley 
108936bce6e8SMatthew G. Knepley   PetscFunctionBegin;
10905cdeb108SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
10914302210dSVaclav Hapla   if (parent) PetscValidCharPointer(parent, 2);
1092c57a1a9aSVaclav Hapla   PetscValidCharPointer(name, 3);
10934302210dSVaclav Hapla   PetscValidLogicalCollectiveEnum(viewer, datatype, 4);
1094b7e81f0fSVaclav Hapla   PetscValidPointer(value, 5);
10959566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5GetAbsolutePath_Internal(viewer, parent, &parentAbsPath));
10969566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5Traverse_Internal(viewer, parentAbsPath, PETSC_TRUE, NULL, NULL));
10979566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5HasAttribute_Internal(viewer, parentAbsPath, name, &has));
10989566063dSJacob Faibussowitsch   PetscCall(PetscDataTypeToHDF5DataType(datatype, &dtype));
10997e97c476SMatthew G. Knepley   if (datatype == PETSC_STRING) {
11007e97c476SMatthew G. Knepley     size_t len;
11019566063dSJacob Faibussowitsch     PetscCall(PetscStrlen((const char *)value, &len));
1102792fecdfSBarry Smith     PetscCallHDF5(H5Tset_size, (dtype, len + 1));
11037e97c476SMatthew G. Knepley   }
11049566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5GetFileId(viewer, &h5));
1105792fecdfSBarry Smith   PetscCallHDF5Return(dataspace, H5Screate, (H5S_SCALAR));
1106792fecdfSBarry Smith   PetscCallHDF5Return(obj, H5Oopen, (h5, parentAbsPath, H5P_DEFAULT));
1107080f144cSVaclav Hapla   if (has) {
1108792fecdfSBarry Smith     PetscCallHDF5Return(attribute, H5Aopen_name, (obj, name));
1109080f144cSVaclav Hapla   } else {
1110792fecdfSBarry Smith     PetscCallHDF5Return(attribute, H5Acreate2, (obj, name, dtype, dataspace, H5P_DEFAULT, H5P_DEFAULT));
1111080f144cSVaclav Hapla   }
1112792fecdfSBarry Smith   PetscCallHDF5(H5Awrite, (attribute, dtype, value));
1113792fecdfSBarry Smith   if (datatype == PETSC_STRING) PetscCallHDF5(H5Tclose, (dtype));
1114792fecdfSBarry Smith   PetscCallHDF5(H5Aclose, (attribute));
1115792fecdfSBarry Smith   PetscCallHDF5(H5Oclose, (obj));
1116792fecdfSBarry Smith   PetscCallHDF5(H5Sclose, (dataspace));
11179566063dSJacob Faibussowitsch   PetscCall(PetscFree(parentAbsPath));
111836bce6e8SMatthew G. Knepley   PetscFunctionReturn(0);
111936bce6e8SMatthew G. Knepley }
112036bce6e8SMatthew G. Knepley 
1121df863907SAlex Fikl /*@C
1122577e0e04SVaclav Hapla  PetscViewerHDF5WriteObjectAttribute - Write an attribute to the dataset matching the given PetscObject by name
1123577e0e04SVaclav Hapla 
1124343a20b2SVaclav Hapla   Collective
1125343a20b2SVaclav Hapla 
1126577e0e04SVaclav Hapla   Input Parameters:
1127577e0e04SVaclav Hapla + viewer   - The HDF5 viewer
1128577e0e04SVaclav Hapla . obj      - The object whose name is used to lookup the parent dataset, relative to the current group.
1129577e0e04SVaclav Hapla . name     - The attribute name
1130577e0e04SVaclav Hapla . datatype - The attribute type
1131577e0e04SVaclav Hapla - value    - The attribute value
1132577e0e04SVaclav Hapla 
1133577e0e04SVaclav Hapla   Notes:
1134343a20b2SVaclav Hapla   This fails if the path current_group/object_name doesn't resolve to a dataset (the path doesn't exist or is not a dataset).
1135577e0e04SVaclav Hapla   You might want to check first if it does using PetscViewerHDF5HasObject().
1136577e0e04SVaclav Hapla 
1137577e0e04SVaclav Hapla   Level: advanced
1138577e0e04SVaclav Hapla 
1139c2e3fba1SPatrick Sanan .seealso: `PetscViewerHDF5Open()`, `PetscViewerHDF5WriteAttribute()`, `PetscViewerHDF5ReadObjectAttribute()`, `PetscViewerHDF5HasObjectAttribute()`, `PetscViewerHDF5HasObject()`, `PetscViewerHDF5PushGroup()`, `PetscViewerHDF5PopGroup()`, `PetscViewerHDF5GetGroup()`
1140577e0e04SVaclav Hapla @*/
11419371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5WriteObjectAttribute(PetscViewer viewer, PetscObject obj, const char name[], PetscDataType datatype, const void *value) {
1142577e0e04SVaclav Hapla   PetscFunctionBegin;
1143577e0e04SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
1144577e0e04SVaclav Hapla   PetscValidHeader(obj, 2);
1145577e0e04SVaclav Hapla   PetscValidCharPointer(name, 3);
1146b7e81f0fSVaclav Hapla   PetscValidPointer(value, 5);
11479566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5CheckNamedObject_Internal(viewer, obj));
11489566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5WriteAttribute(viewer, obj->name, name, datatype, value));
1149577e0e04SVaclav Hapla   PetscFunctionReturn(0);
1150577e0e04SVaclav Hapla }
1151577e0e04SVaclav Hapla 
1152577e0e04SVaclav Hapla /*@C
1153b8ee85a1SVaclav Hapla  PetscViewerHDF5ReadAttribute - Read an attribute
1154ad0c61feSMatthew G. Knepley 
1155343a20b2SVaclav Hapla   Collective
1156343a20b2SVaclav Hapla 
1157ad0c61feSMatthew G. Knepley   Input Parameters:
1158ad0c61feSMatthew G. Knepley + viewer - The HDF5 viewer
11594302210dSVaclav Hapla . parent - The parent dataset/group name
1160ad0c61feSMatthew G. Knepley . name   - The attribute name
1161a2d6be1bSVaclav Hapla . datatype - The attribute type
1162a2d6be1bSVaclav Hapla - defaultValue - The pointer to the default value
1163ad0c61feSMatthew G. Knepley 
1164ad0c61feSMatthew G. Knepley   Output Parameter:
1165a2d6be1bSVaclav Hapla . value    - The pointer to the read HDF5 attribute value
1166ad0c61feSMatthew G. Knepley 
1167a2d6be1bSVaclav Hapla   Notes:
1168a2d6be1bSVaclav Hapla   If defaultValue is NULL and the attribute is not found, an error occurs.
1169a2d6be1bSVaclav Hapla   If defaultValue is not NULL and the attribute is not found, defaultValue is copied to value.
1170a2d6be1bSVaclav Hapla   The pointers defaultValue and value can be the same; for instance
1171a2d6be1bSVaclav Hapla $  flg = PETSC_FALSE;
11729566063dSJacob Faibussowitsch $  PetscCall(PetscViewerHDF5ReadAttribute(viewer,name,"attr",PETSC_BOOL,&flg,&flg));
1173a2d6be1bSVaclav Hapla   is valid, but make sure the default value is initialized.
1174a2d6be1bSVaclav Hapla 
1175a2d6be1bSVaclav Hapla   If the datatype is PETSC_STRING, the output string is newly allocated so one must PetscFree() it when no longer needed.
1176708d4cb3SBarry Smith 
1177343a20b2SVaclav Hapla   If parent starts with '/', it is taken as an absolute path overriding currently pushed group, else parent is relative to the current pushed group. NULL means the current pushed group.
1178ad0c61feSMatthew G. Knepley 
1179343a20b2SVaclav Hapla   Level: advanced
11804302210dSVaclav Hapla 
1181c2e3fba1SPatrick Sanan .seealso: `PetscViewerHDF5Open()`, `PetscViewerHDF5ReadObjectAttribute()`, `PetscViewerHDF5WriteAttribute()`, `PetscViewerHDF5HasAttribute()`, `PetscViewerHDF5HasObject()`, `PetscViewerHDF5PushGroup()`, `PetscViewerHDF5PopGroup()`, `PetscViewerHDF5GetGroup()`
1182ad0c61feSMatthew G. Knepley @*/
11839371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5ReadAttribute(PetscViewer viewer, const char parent[], const char name[], PetscDataType datatype, const void *defaultValue, void *value) {
11844302210dSVaclav Hapla   char     *parentAbsPath;
1185a2d6be1bSVaclav Hapla   hid_t     h5, obj, attribute, dtype;
1186969748fdSVaclav Hapla   PetscBool has;
1187ad0c61feSMatthew G. Knepley 
1188ad0c61feSMatthew G. Knepley   PetscFunctionBegin;
11895cdeb108SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
11904302210dSVaclav Hapla   if (parent) PetscValidCharPointer(parent, 2);
1191c57a1a9aSVaclav Hapla   PetscValidCharPointer(name, 3);
1192a2d6be1bSVaclav Hapla   if (defaultValue) PetscValidPointer(defaultValue, 5);
1193a2d6be1bSVaclav Hapla   PetscValidPointer(value, 6);
11949566063dSJacob Faibussowitsch   PetscCall(PetscDataTypeToHDF5DataType(datatype, &dtype));
11959566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5GetAbsolutePath_Internal(viewer, parent, &parentAbsPath));
11969566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5Traverse_Internal(viewer, parentAbsPath, PETSC_FALSE, &has, NULL));
11979566063dSJacob Faibussowitsch   if (has) PetscCall(PetscViewerHDF5HasAttribute_Internal(viewer, parentAbsPath, name, &has));
1198a2d6be1bSVaclav Hapla   if (!has) {
1199a2d6be1bSVaclav Hapla     if (defaultValue) {
1200a2d6be1bSVaclav Hapla       if (defaultValue != value) {
1201a2d6be1bSVaclav Hapla         if (datatype == PETSC_STRING) {
12029566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(*(char **)defaultValue, (char **)value));
1203a2d6be1bSVaclav Hapla         } else {
1204a2d6be1bSVaclav Hapla           size_t len;
1205792fecdfSBarry Smith           PetscCallHDF5ReturnNoCheck(len, H5Tget_size, (dtype));
12069566063dSJacob Faibussowitsch           PetscCall(PetscMemcpy(value, defaultValue, len));
1207a2d6be1bSVaclav Hapla         }
1208a2d6be1bSVaclav Hapla       }
12099566063dSJacob Faibussowitsch       PetscCall(PetscFree(parentAbsPath));
1210a2d6be1bSVaclav Hapla       PetscFunctionReturn(0);
121198921bdaSJacob Faibussowitsch     } else SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Attribute %s/%s does not exist and default value not provided", parentAbsPath, name);
1212a2d6be1bSVaclav Hapla   }
12139566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5GetFileId(viewer, &h5));
1214792fecdfSBarry Smith   PetscCallHDF5Return(obj, H5Oopen, (h5, parentAbsPath, H5P_DEFAULT));
1215792fecdfSBarry Smith   PetscCallHDF5Return(attribute, H5Aopen_name, (obj, name));
1216f0b58479SMatthew G. Knepley   if (datatype == PETSC_STRING) {
1217f0b58479SMatthew G. Knepley     size_t len;
1218a2d6be1bSVaclav Hapla     hid_t  atype;
1219792fecdfSBarry Smith     PetscCallHDF5Return(atype, H5Aget_type, (attribute));
1220792fecdfSBarry Smith     PetscCallHDF5ReturnNoCheck(len, H5Tget_size, (atype));
12219566063dSJacob Faibussowitsch     PetscCall(PetscMalloc((len + 1) * sizeof(char), value));
1222792fecdfSBarry Smith     PetscCallHDF5(H5Tset_size, (dtype, len + 1));
1223792fecdfSBarry Smith     PetscCallHDF5(H5Aread, (attribute, dtype, *(char **)value));
1224708d4cb3SBarry Smith   } else {
1225792fecdfSBarry Smith     PetscCallHDF5(H5Aread, (attribute, dtype, value));
1226708d4cb3SBarry Smith   }
1227792fecdfSBarry Smith   PetscCallHDF5(H5Aclose, (attribute));
1228e90c5075SMarek Pecha   /* H5Oclose can be used to close groups, datasets, or committed datatypes */
1229792fecdfSBarry Smith   PetscCallHDF5(H5Oclose, (obj));
12309566063dSJacob Faibussowitsch   PetscCall(PetscFree(parentAbsPath));
1231ad0c61feSMatthew G. Knepley   PetscFunctionReturn(0);
1232ad0c61feSMatthew G. Knepley }
1233ad0c61feSMatthew G. Knepley 
1234577e0e04SVaclav Hapla /*@C
1235577e0e04SVaclav Hapla  PetscViewerHDF5ReadObjectAttribute - Read an attribute from the dataset matching the given PetscObject by name
1236577e0e04SVaclav Hapla 
1237343a20b2SVaclav Hapla   Collective
1238343a20b2SVaclav Hapla 
1239577e0e04SVaclav Hapla   Input Parameters:
1240577e0e04SVaclav Hapla + viewer   - The HDF5 viewer
1241577e0e04SVaclav Hapla . obj      - The object whose name is used to lookup the parent dataset, relative to the current group.
1242577e0e04SVaclav Hapla . name     - The attribute name
1243577e0e04SVaclav Hapla - datatype - The attribute type
1244577e0e04SVaclav Hapla 
1245577e0e04SVaclav Hapla   Output Parameter:
1246577e0e04SVaclav Hapla . value    - The attribute value
1247577e0e04SVaclav Hapla 
1248577e0e04SVaclav Hapla   Notes:
1249577e0e04SVaclav Hapla   This fails if current_group/object_name doesn't resolve to a dataset (the path doesn't exist or is not a dataset).
1250577e0e04SVaclav Hapla   You might want to check first if it does using PetscViewerHDF5HasObject().
1251577e0e04SVaclav Hapla 
1252577e0e04SVaclav Hapla   Level: advanced
1253577e0e04SVaclav Hapla 
1254c2e3fba1SPatrick Sanan .seealso: `PetscViewerHDF5Open()`, `PetscViewerHDF5ReadAttribute()` `PetscViewerHDF5WriteObjectAttribute()`, `PetscViewerHDF5HasObjectAttribute()`, `PetscViewerHDF5PushGroup()`, `PetscViewerHDF5PopGroup()`, `PetscViewerHDF5GetGroup()`
1255577e0e04SVaclav Hapla @*/
12569371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5ReadObjectAttribute(PetscViewer viewer, PetscObject obj, const char name[], PetscDataType datatype, void *defaultValue, void *value) {
1257577e0e04SVaclav Hapla   PetscFunctionBegin;
1258577e0e04SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
1259577e0e04SVaclav Hapla   PetscValidHeader(obj, 2);
1260577e0e04SVaclav Hapla   PetscValidCharPointer(name, 3);
1261064a246eSJacob Faibussowitsch   PetscValidPointer(value, 6);
12629566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5CheckNamedObject_Internal(viewer, obj));
12639566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5ReadAttribute(viewer, obj->name, name, datatype, defaultValue, value));
1264577e0e04SVaclav Hapla   PetscFunctionReturn(0);
1265577e0e04SVaclav Hapla }
1266577e0e04SVaclav Hapla 
12679371c9d4SSatish Balay static inline PetscErrorCode PetscViewerHDF5Traverse_Inner_Internal(hid_t h5, const char name[], PetscBool createGroup, PetscBool *exists_) {
1268a75c3fd4SVaclav Hapla   htri_t exists;
1269a75c3fd4SVaclav Hapla   hid_t  group;
1270a75c3fd4SVaclav Hapla 
1271a75c3fd4SVaclav Hapla   PetscFunctionBegin;
1272792fecdfSBarry Smith   PetscCallHDF5Return(exists, H5Lexists, (h5, name, H5P_DEFAULT));
1273792fecdfSBarry Smith   if (exists) PetscCallHDF5Return(exists, H5Oexists_by_name, (h5, name, H5P_DEFAULT));
1274a75c3fd4SVaclav Hapla   if (!exists && createGroup) {
1275792fecdfSBarry Smith     PetscCallHDF5Return(group, H5Gcreate2, (h5, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT));
1276792fecdfSBarry Smith     PetscCallHDF5(H5Gclose, (group));
1277a75c3fd4SVaclav Hapla     exists = PETSC_TRUE;
1278a75c3fd4SVaclav Hapla   }
1279a75c3fd4SVaclav Hapla   *exists_ = (PetscBool)exists;
1280a75c3fd4SVaclav Hapla   PetscFunctionReturn(0);
1281a75c3fd4SVaclav Hapla }
1282a75c3fd4SVaclav Hapla 
12839371c9d4SSatish Balay static PetscErrorCode PetscViewerHDF5Traverse_Internal(PetscViewer viewer, const char name[], PetscBool createGroup, PetscBool *has, H5O_type_t *otype) {
128490e03537SVaclav Hapla   const char rootGroupName[] = "/";
12855cdeb108SMatthew G. Knepley   hid_t      h5;
1286e5bf9ebcSVaclav Hapla   PetscBool  exists = PETSC_FALSE;
128715b861d2SVaclav Hapla   PetscInt   i;
128815b861d2SVaclav Hapla   int        n;
128985688ae2SVaclav Hapla   char     **hierarchy;
129085688ae2SVaclav Hapla   char       buf[PETSC_MAX_PATH_LEN] = "";
12915cdeb108SMatthew G. Knepley 
12925cdeb108SMatthew G. Knepley   PetscFunctionBegin;
12935cdeb108SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
129490e03537SVaclav Hapla   if (name) PetscValidCharPointer(name, 2);
129590e03537SVaclav Hapla   else name = rootGroupName;
1296ccd66a83SVaclav Hapla   if (has) {
1297064a246eSJacob Faibussowitsch     PetscValidBoolPointer(has, 4);
12985cdeb108SMatthew G. Knepley     *has = PETSC_FALSE;
1299ccd66a83SVaclav Hapla   }
1300ccd66a83SVaclav Hapla   if (otype) {
1301064a246eSJacob Faibussowitsch     PetscValidIntPointer(otype, 5);
130256cc0592SVaclav Hapla     *otype = H5O_TYPE_UNKNOWN;
1303ccd66a83SVaclav Hapla   }
13049566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5GetFileId(viewer, &h5));
130585688ae2SVaclav Hapla 
130685688ae2SVaclav Hapla   /*
130785688ae2SVaclav Hapla      Unfortunately, H5Oexists_by_name() fails if any object in hierarchy is missing.
130885688ae2SVaclav Hapla      Hence, each of them needs to be tested separately:
130985688ae2SVaclav Hapla      1) whether it's a valid link
131085688ae2SVaclav Hapla      2) whether this link resolves to an object
131185688ae2SVaclav Hapla      See H5Oexists_by_name() documentation.
131285688ae2SVaclav Hapla   */
13139566063dSJacob Faibussowitsch   PetscCall(PetscStrToArray(name, '/', &n, &hierarchy));
131485688ae2SVaclav Hapla   if (!n) {
131585688ae2SVaclav Hapla     /*  Assume group "/" always exists in accordance with HDF5 >= 1.10.0. See H5Lexists() documentation. */
1316ccd66a83SVaclav Hapla     if (has) *has = PETSC_TRUE;
1317ccd66a83SVaclav Hapla     if (otype) *otype = H5O_TYPE_GROUP;
13189566063dSJacob Faibussowitsch     PetscCall(PetscStrToArrayDestroy(n, hierarchy));
131985688ae2SVaclav Hapla     PetscFunctionReturn(0);
132085688ae2SVaclav Hapla   }
132185688ae2SVaclav Hapla   for (i = 0; i < n; i++) {
13229566063dSJacob Faibussowitsch     PetscCall(PetscStrcat(buf, "/"));
13239566063dSJacob Faibussowitsch     PetscCall(PetscStrcat(buf, hierarchy[i]));
13249566063dSJacob Faibussowitsch     PetscCall(PetscViewerHDF5Traverse_Inner_Internal(h5, buf, createGroup, &exists));
1325a75c3fd4SVaclav Hapla     if (!exists) break;
132685688ae2SVaclav Hapla   }
13279566063dSJacob Faibussowitsch   PetscCall(PetscStrToArrayDestroy(n, hierarchy));
132885688ae2SVaclav Hapla 
132985688ae2SVaclav Hapla   /* If the object exists, get its type */
1330ccd66a83SVaclav Hapla   if (exists && otype) {
13315cdeb108SMatthew G. Knepley     H5O_info_t info;
13325cdeb108SMatthew G. Knepley 
133376276748SVaclav Hapla     /* We could use H5Iget_type() here but that would require opening the object. This way we only need its name. */
1334792fecdfSBarry Smith     PetscCallHDF5(H5Oget_info_by_name, (h5, name, &info, H5P_DEFAULT));
133556cc0592SVaclav Hapla     *otype = info.type;
13365cdeb108SMatthew G. Knepley   }
1337ccd66a83SVaclav Hapla   if (has) *has = exists;
13385cdeb108SMatthew G. Knepley   PetscFunctionReturn(0);
13395cdeb108SMatthew G. Knepley }
13405cdeb108SMatthew G. Knepley 
13414302210dSVaclav Hapla /*@C
13420a9f38efSVaclav Hapla  PetscViewerHDF5HasGroup - Check whether the current (pushed) group exists in the HDF5 file
13430a9f38efSVaclav Hapla 
1344343a20b2SVaclav Hapla   Collective
1345343a20b2SVaclav Hapla 
13460a9f38efSVaclav Hapla   Input Parameters:
13474302210dSVaclav Hapla + viewer - The HDF5 viewer
13484302210dSVaclav Hapla - path - The group path
13490a9f38efSVaclav Hapla 
13500a9f38efSVaclav Hapla   Output Parameter:
13510a9f38efSVaclav Hapla . has - Flag for group existence
13520a9f38efSVaclav Hapla 
13530a9f38efSVaclav Hapla   Level: advanced
13540a9f38efSVaclav Hapla 
13554302210dSVaclav Hapla   Notes:
1356785c443eSVaclav Hapla   If path starts with '/', it is taken as an absolute path overriding currently pushed group, else path is relative to the current pushed group.
1357785c443eSVaclav Hapla   So NULL or empty path means the current pushed group.
1358343a20b2SVaclav Hapla   If path exists but is not a group, PETSC_FALSE is returned.
13594302210dSVaclav Hapla 
1360db781477SPatrick Sanan .seealso: `PetscViewerHDF5HasAttribute()`, `PetscViewerHDF5HasDataset()`, `PetscViewerHDF5PushGroup()`, `PetscViewerHDF5PopGroup()`, `PetscViewerHDF5GetGroup()`, `PetscViewerHDF5OpenGroup()`
13610a9f38efSVaclav Hapla @*/
13629371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5HasGroup(PetscViewer viewer, const char path[], PetscBool *has) {
13630a9f38efSVaclav Hapla   H5O_type_t type;
13644302210dSVaclav Hapla   char      *abspath;
13650a9f38efSVaclav Hapla 
13660a9f38efSVaclav Hapla   PetscFunctionBegin;
13670a9f38efSVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
13684302210dSVaclav Hapla   if (path) PetscValidCharPointer(path, 2);
13694302210dSVaclav Hapla   PetscValidBoolPointer(has, 3);
13709566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5GetAbsolutePath_Internal(viewer, path, &abspath));
13719566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5Traverse_Internal(viewer, abspath, PETSC_FALSE, NULL, &type));
13724302210dSVaclav Hapla   *has = (PetscBool)(type == H5O_TYPE_GROUP);
13739566063dSJacob Faibussowitsch   PetscCall(PetscFree(abspath));
13740a9f38efSVaclav Hapla   PetscFunctionReturn(0);
13750a9f38efSVaclav Hapla }
13760a9f38efSVaclav Hapla 
137789e0ef10SVaclav Hapla /*@C
137889e0ef10SVaclav Hapla  PetscViewerHDF5HasDataset - Check whether a given dataset exists in the HDF5 file
137989e0ef10SVaclav Hapla 
1380343a20b2SVaclav Hapla   Collective
1381343a20b2SVaclav Hapla 
138289e0ef10SVaclav Hapla   Input Parameters:
138389e0ef10SVaclav Hapla + viewer - The HDF5 viewer
138489e0ef10SVaclav Hapla - path - The dataset path
138589e0ef10SVaclav Hapla 
138689e0ef10SVaclav Hapla   Output Parameter:
138789e0ef10SVaclav Hapla . has - Flag whether dataset exists
138889e0ef10SVaclav Hapla 
138989e0ef10SVaclav Hapla   Level: advanced
139089e0ef10SVaclav Hapla 
139189e0ef10SVaclav Hapla   Notes:
1392343a20b2SVaclav Hapla   If path starts with '/', it is taken as an absolute path overriding currently pushed group, else path is relative to the current pushed group.
139389e0ef10SVaclav Hapla   If path is NULL or empty, has is set to PETSC_FALSE.
1394343a20b2SVaclav Hapla   If path exists but is not a dataset, has is set to PETSC_FALSE as well.
139589e0ef10SVaclav Hapla 
1396db781477SPatrick Sanan .seealso: `PetscViewerHDF5HasObject()`, `PetscViewerHDF5HasAttribute()`, `PetscViewerHDF5HasGroup()`, `PetscViewerHDF5PushGroup()`, `PetscViewerHDF5PopGroup()`, `PetscViewerHDF5GetGroup()`
139789e0ef10SVaclav Hapla @*/
13989371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5HasDataset(PetscViewer viewer, const char path[], PetscBool *has) {
139989e0ef10SVaclav Hapla   H5O_type_t type;
140089e0ef10SVaclav Hapla   char      *abspath;
140189e0ef10SVaclav Hapla 
140289e0ef10SVaclav Hapla   PetscFunctionBegin;
140389e0ef10SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
140489e0ef10SVaclav Hapla   if (path) PetscValidCharPointer(path, 2);
140589e0ef10SVaclav Hapla   PetscValidBoolPointer(has, 3);
14069566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5GetAbsolutePath_Internal(viewer, path, &abspath));
14079566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5Traverse_Internal(viewer, abspath, PETSC_FALSE, NULL, &type));
140889e0ef10SVaclav Hapla   *has = (PetscBool)(type == H5O_TYPE_DATASET);
14099566063dSJacob Faibussowitsch   PetscCall(PetscFree(abspath));
141089e0ef10SVaclav Hapla   PetscFunctionReturn(0);
141189e0ef10SVaclav Hapla }
141289e0ef10SVaclav Hapla 
14130a9f38efSVaclav Hapla /*@
1414e3f143f7SVaclav Hapla  PetscViewerHDF5HasObject - Check whether a dataset with the same name as given object exists in the HDF5 file under current group
1415ecce1506SVaclav Hapla 
1416343a20b2SVaclav Hapla   Collective
1417343a20b2SVaclav Hapla 
1418ecce1506SVaclav Hapla   Input Parameters:
1419ecce1506SVaclav Hapla + viewer - The HDF5 viewer
1420ecce1506SVaclav Hapla - obj    - The named object
1421ecce1506SVaclav Hapla 
1422ecce1506SVaclav Hapla   Output Parameter:
142389e0ef10SVaclav Hapla . has    - Flag for dataset existence
1424ecce1506SVaclav Hapla 
1425e3f143f7SVaclav Hapla   Notes:
142689e0ef10SVaclav Hapla   If the object is unnamed, an error occurs.
1427343a20b2SVaclav Hapla   If the path current_group/object_name exists but is not a dataset, has is set to PETSC_FALSE as well.
1428e3f143f7SVaclav Hapla 
1429ecce1506SVaclav Hapla   Level: advanced
1430ecce1506SVaclav Hapla 
1431db781477SPatrick Sanan .seealso: `PetscViewerHDF5Open()`, `PetscViewerHDF5HasDataset()`, `PetscViewerHDF5HasAttribute()`, `PetscViewerHDF5PushGroup()`, `PetscViewerHDF5PopGroup()`, `PetscViewerHDF5GetGroup()`
1432ecce1506SVaclav Hapla @*/
14339371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5HasObject(PetscViewer viewer, PetscObject obj, PetscBool *has) {
143489e0ef10SVaclav Hapla   size_t len;
1435ecce1506SVaclav Hapla 
1436ecce1506SVaclav Hapla   PetscFunctionBegin;
1437c57a1a9aSVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
1438c57a1a9aSVaclav Hapla   PetscValidHeader(obj, 2);
14394302210dSVaclav Hapla   PetscValidBoolPointer(has, 3);
14409566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(obj->name, &len));
14415f80ce2aSJacob Faibussowitsch   PetscCheck(len, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONG, "Object must be named");
14429566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5HasDataset(viewer, obj->name, has));
1443ecce1506SVaclav Hapla   PetscFunctionReturn(0);
1444ecce1506SVaclav Hapla }
1445ecce1506SVaclav Hapla 
1446df863907SAlex Fikl /*@C
1447ece83b6cSVaclav Hapla  PetscViewerHDF5HasAttribute - Check whether an attribute exists
1448e7bdbf8eSMatthew G. Knepley 
1449343a20b2SVaclav Hapla   Collective
1450343a20b2SVaclav Hapla 
1451e7bdbf8eSMatthew G. Knepley   Input Parameters:
1452e7bdbf8eSMatthew G. Knepley + viewer - The HDF5 viewer
14534302210dSVaclav Hapla . parent - The parent dataset/group name
1454e7bdbf8eSMatthew G. Knepley - name   - The attribute name
1455e7bdbf8eSMatthew G. Knepley 
1456e7bdbf8eSMatthew G. Knepley   Output Parameter:
1457e7bdbf8eSMatthew G. Knepley . has    - Flag for attribute existence
1458e7bdbf8eSMatthew G. Knepley 
1459e7bdbf8eSMatthew G. Knepley   Level: advanced
1460e7bdbf8eSMatthew G. Knepley 
14614302210dSVaclav Hapla   Notes:
1462343a20b2SVaclav Hapla   If parent starts with '/', it is taken as an absolute path overriding currently pushed group, else parent is relative to the current pushed group. NULL means the current pushed group.
14634302210dSVaclav Hapla 
1464c2e3fba1SPatrick Sanan .seealso: `PetscViewerHDF5Open()`, `PetscViewerHDF5HasObjectAttribute()`, `PetscViewerHDF5WriteAttribute()`, `PetscViewerHDF5ReadAttribute()`, `PetscViewerHDF5PushGroup()`, `PetscViewerHDF5PopGroup()`, `PetscViewerHDF5GetGroup()`
1465e7bdbf8eSMatthew G. Knepley @*/
14669371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5HasAttribute(PetscViewer viewer, const char parent[], const char name[], PetscBool *has) {
14674302210dSVaclav Hapla   char *parentAbsPath;
1468e7bdbf8eSMatthew G. Knepley 
1469e7bdbf8eSMatthew G. Knepley   PetscFunctionBegin;
14705cdeb108SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
14714302210dSVaclav Hapla   if (parent) PetscValidCharPointer(parent, 2);
1472c57a1a9aSVaclav Hapla   PetscValidCharPointer(name, 3);
14734302210dSVaclav Hapla   PetscValidBoolPointer(has, 4);
14749566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5GetAbsolutePath_Internal(viewer, parent, &parentAbsPath));
14759566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5Traverse_Internal(viewer, parentAbsPath, PETSC_FALSE, has, NULL));
14769566063dSJacob Faibussowitsch   if (*has) PetscCall(PetscViewerHDF5HasAttribute_Internal(viewer, parentAbsPath, name, has));
14779566063dSJacob Faibussowitsch   PetscCall(PetscFree(parentAbsPath));
147806db490cSVaclav Hapla   PetscFunctionReturn(0);
147906db490cSVaclav Hapla }
148006db490cSVaclav Hapla 
1481577e0e04SVaclav Hapla /*@C
1482577e0e04SVaclav Hapla  PetscViewerHDF5HasObjectAttribute - Check whether an attribute is attached to the dataset matching the given PetscObject by name
1483577e0e04SVaclav Hapla 
1484343a20b2SVaclav Hapla   Collective
1485343a20b2SVaclav Hapla 
1486577e0e04SVaclav Hapla   Input Parameters:
1487577e0e04SVaclav Hapla + viewer - The HDF5 viewer
1488577e0e04SVaclav Hapla . obj    - The object whose name is used to lookup the parent dataset, relative to the current group.
1489577e0e04SVaclav Hapla - name   - The attribute name
1490577e0e04SVaclav Hapla 
1491577e0e04SVaclav Hapla   Output Parameter:
1492577e0e04SVaclav Hapla . has    - Flag for attribute existence
1493577e0e04SVaclav Hapla 
1494577e0e04SVaclav Hapla   Notes:
1495577e0e04SVaclav Hapla   This fails if current_group/object_name doesn't resolve to a dataset (the path doesn't exist or is not a dataset).
1496577e0e04SVaclav Hapla   You might want to check first if it does using PetscViewerHDF5HasObject().
1497577e0e04SVaclav Hapla 
1498577e0e04SVaclav Hapla   Level: advanced
1499577e0e04SVaclav Hapla 
1500c2e3fba1SPatrick Sanan .seealso: `PetscViewerHDF5Open()`, `PetscViewerHDF5HasAttribute()`, `PetscViewerHDF5WriteObjectAttribute()`, `PetscViewerHDF5ReadObjectAttribute()`, `PetscViewerHDF5HasObject()`, `PetscViewerHDF5PushGroup()`, `PetscViewerHDF5PopGroup()`, `PetscViewerHDF5GetGroup()`
1501577e0e04SVaclav Hapla @*/
15029371c9d4SSatish Balay PetscErrorCode PetscViewerHDF5HasObjectAttribute(PetscViewer viewer, PetscObject obj, const char name[], PetscBool *has) {
1503577e0e04SVaclav Hapla   PetscFunctionBegin;
1504577e0e04SVaclav Hapla   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
1505577e0e04SVaclav Hapla   PetscValidHeader(obj, 2);
1506577e0e04SVaclav Hapla   PetscValidCharPointer(name, 3);
15074302210dSVaclav Hapla   PetscValidBoolPointer(has, 4);
15089566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5CheckNamedObject_Internal(viewer, obj));
15099566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5HasAttribute(viewer, obj->name, name, has));
1510577e0e04SVaclav Hapla   PetscFunctionReturn(0);
1511577e0e04SVaclav Hapla }
1512577e0e04SVaclav Hapla 
15139371c9d4SSatish Balay static PetscErrorCode PetscViewerHDF5HasAttribute_Internal(PetscViewer viewer, const char parent[], const char name[], PetscBool *has) {
151406db490cSVaclav Hapla   hid_t  h5;
151506db490cSVaclav Hapla   htri_t hhas;
151606db490cSVaclav Hapla 
151706db490cSVaclav Hapla   PetscFunctionBegin;
15189566063dSJacob Faibussowitsch   PetscCall(PetscViewerHDF5GetFileId(viewer, &h5));
1519792fecdfSBarry Smith   PetscCallHDF5Return(hhas, H5Aexists_by_name, (h5, parent, name, H5P_DEFAULT));
15205cdeb108SMatthew G. Knepley   *has = hhas ? PETSC_TRUE : PETSC_FALSE;
1521e7bdbf8eSMatthew G. Knepley   PetscFunctionReturn(0);
1522e7bdbf8eSMatthew G. Knepley }
1523e7bdbf8eSMatthew G. Knepley 
1524a75e6a4aSMatthew G. Knepley /*
1525a75e6a4aSMatthew G. Knepley   The variable Petsc_Viewer_HDF5_keyval is used to indicate an MPI attribute that
1526a75e6a4aSMatthew G. Knepley   is attached to a communicator, in this case the attribute is a PetscViewer.
1527a75e6a4aSMatthew G. Knepley */
1528d4c7638eSBarry Smith PetscMPIInt Petsc_Viewer_HDF5_keyval = MPI_KEYVAL_INVALID;
1529a75e6a4aSMatthew G. Knepley 
1530a75e6a4aSMatthew G. Knepley /*@C
1531a75e6a4aSMatthew G. Knepley   PETSC_VIEWER_HDF5_ - Creates an HDF5 PetscViewer shared by all processors in a communicator.
1532a75e6a4aSMatthew G. Knepley 
1533d083f849SBarry Smith   Collective
1534a75e6a4aSMatthew G. Knepley 
1535a75e6a4aSMatthew G. Knepley   Input Parameter:
1536a75e6a4aSMatthew G. Knepley . comm - the MPI communicator to share the HDF5 PetscViewer
1537a75e6a4aSMatthew G. Knepley 
1538a75e6a4aSMatthew G. Knepley   Level: intermediate
1539a75e6a4aSMatthew G. Knepley 
1540a75e6a4aSMatthew G. Knepley   Options Database Keys:
154110699b91SBarry Smith . -viewer_hdf5_filename <name> - name of the HDF5 file
1542a75e6a4aSMatthew G. Knepley 
1543a75e6a4aSMatthew G. Knepley   Environmental variables:
154410699b91SBarry Smith . PETSC_VIEWER_HDF5_FILENAME - name of the HDF5 file
1545a75e6a4aSMatthew G. Knepley 
1546a75e6a4aSMatthew G. Knepley   Notes:
1547a75e6a4aSMatthew G. Knepley   Unlike almost all other PETSc routines, PETSC_VIEWER_HDF5_ does not return
1548a75e6a4aSMatthew G. Knepley   an error code.  The HDF5 PetscViewer is usually used in the form
1549a75e6a4aSMatthew G. Knepley $       XXXView(XXX object, PETSC_VIEWER_HDF5_(comm));
1550a75e6a4aSMatthew G. Knepley 
1551db781477SPatrick Sanan .seealso: `PetscViewerHDF5Open()`, `PetscViewerCreate()`, `PetscViewerDestroy()`
1552a75e6a4aSMatthew G. Knepley @*/
15539371c9d4SSatish Balay PetscViewer PETSC_VIEWER_HDF5_(MPI_Comm comm) {
1554a75e6a4aSMatthew G. Knepley   PetscErrorCode ierr;
1555a75e6a4aSMatthew G. Knepley   PetscBool      flg;
1556a75e6a4aSMatthew G. Knepley   PetscViewer    viewer;
1557a75e6a4aSMatthew G. Knepley   char           fname[PETSC_MAX_PATH_LEN];
1558a75e6a4aSMatthew G. Knepley   MPI_Comm       ncomm;
1559a75e6a4aSMatthew G. Knepley 
1560a75e6a4aSMatthew G. Knepley   PetscFunctionBegin;
15619371c9d4SSatish Balay   ierr = PetscCommDuplicate(comm, &ncomm, NULL);
15629371c9d4SSatish Balay   if (ierr) {
15639371c9d4SSatish Balay     PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_HDF5_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
15649371c9d4SSatish Balay     PetscFunctionReturn(NULL);
15659371c9d4SSatish Balay   }
1566a75e6a4aSMatthew G. Knepley   if (Petsc_Viewer_HDF5_keyval == MPI_KEYVAL_INVALID) {
1567908793a3SLisandro Dalcin     ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, MPI_COMM_NULL_DELETE_FN, &Petsc_Viewer_HDF5_keyval, NULL);
15689371c9d4SSatish Balay     if (ierr) {
15699371c9d4SSatish Balay       PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_HDF5_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
15709371c9d4SSatish Balay       PetscFunctionReturn(NULL);
15719371c9d4SSatish Balay     }
1572a75e6a4aSMatthew G. Knepley   }
157347435625SJed Brown   ierr = MPI_Comm_get_attr(ncomm, Petsc_Viewer_HDF5_keyval, (void **)&viewer, (int *)&flg);
15749371c9d4SSatish Balay   if (ierr) {
15759371c9d4SSatish Balay     PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_HDF5_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
15769371c9d4SSatish Balay     PetscFunctionReturn(NULL);
15779371c9d4SSatish Balay   }
1578a75e6a4aSMatthew G. Knepley   if (!flg) { /* PetscViewer not yet created */
1579a75e6a4aSMatthew G. Knepley     ierr = PetscOptionsGetenv(ncomm, "PETSC_VIEWER_HDF5_FILENAME", fname, PETSC_MAX_PATH_LEN, &flg);
15809371c9d4SSatish Balay     if (ierr) {
15819371c9d4SSatish Balay       PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_HDF5_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_REPEAT, " ");
15829371c9d4SSatish Balay       PetscFunctionReturn(NULL);
15839371c9d4SSatish Balay     }
1584a75e6a4aSMatthew G. Knepley     if (!flg) {
1585a75e6a4aSMatthew G. Knepley       ierr = PetscStrcpy(fname, "output.h5");
15869371c9d4SSatish Balay       if (ierr) {
15879371c9d4SSatish Balay         PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_HDF5_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_REPEAT, " ");
15889371c9d4SSatish Balay         PetscFunctionReturn(NULL);
15899371c9d4SSatish Balay       }
1590a75e6a4aSMatthew G. Knepley     }
1591a75e6a4aSMatthew G. Knepley     ierr = PetscViewerHDF5Open(ncomm, fname, FILE_MODE_WRITE, &viewer);
15929371c9d4SSatish Balay     if (ierr) {
15939371c9d4SSatish Balay       PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_HDF5_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_REPEAT, " ");
15949371c9d4SSatish Balay       PetscFunctionReturn(NULL);
15959371c9d4SSatish Balay     }
1596a75e6a4aSMatthew G. Knepley     ierr = PetscObjectRegisterDestroy((PetscObject)viewer);
15979371c9d4SSatish Balay     if (ierr) {
15989371c9d4SSatish Balay       PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_HDF5_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_REPEAT, " ");
15999371c9d4SSatish Balay       PetscFunctionReturn(NULL);
16009371c9d4SSatish Balay     }
160147435625SJed Brown     ierr = MPI_Comm_set_attr(ncomm, Petsc_Viewer_HDF5_keyval, (void *)viewer);
16029371c9d4SSatish Balay     if (ierr) {
16039371c9d4SSatish Balay       PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_HDF5_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
16049371c9d4SSatish Balay       PetscFunctionReturn(NULL);
16059371c9d4SSatish Balay     }
1606a75e6a4aSMatthew G. Knepley   }
1607a75e6a4aSMatthew G. Knepley   ierr = PetscCommDestroy(&ncomm);
16089371c9d4SSatish Balay   if (ierr) {
16099371c9d4SSatish Balay     PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_HDF5_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_REPEAT, " ");
16109371c9d4SSatish Balay     PetscFunctionReturn(NULL);
16119371c9d4SSatish Balay   }
1612a75e6a4aSMatthew G. Knepley   PetscFunctionReturn(viewer);
1613a75e6a4aSMatthew G. Knepley }
1614