xref: /petsc/src/sys/classes/viewer/impls/hdf5/hdf5v.c (revision e7bdbf8ee2d7e05ba48565a98608913b85d23ec9)
15c6c1daeSBarry Smith #include <petsc-private/viewerimpl.h>    /*I   "petscsys.h"   I*/
2d70abbfaSBarry Smith #include <petscviewerhdf5.h>    /*I   "petscviewerhdf5.h"   I*/
35c6c1daeSBarry Smith 
45c6c1daeSBarry Smith typedef struct GroupList {
55c6c1daeSBarry Smith   const char       *name;
65c6c1daeSBarry Smith   struct GroupList *next;
75c6c1daeSBarry Smith } GroupList;
85c6c1daeSBarry Smith 
95c6c1daeSBarry Smith typedef struct {
105c6c1daeSBarry Smith   char          *filename;
115c6c1daeSBarry Smith   PetscFileMode btype;
125c6c1daeSBarry Smith   hid_t         file_id;
135c6c1daeSBarry Smith   PetscInt      timestep;
145c6c1daeSBarry Smith   GroupList     *groups;
155c6c1daeSBarry Smith } PetscViewer_HDF5;
165c6c1daeSBarry Smith 
175c6c1daeSBarry Smith #undef __FUNCT__
185c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileClose_HDF5"
195c6c1daeSBarry Smith static PetscErrorCode PetscViewerFileClose_HDF5(PetscViewer viewer)
205c6c1daeSBarry Smith {
215c6c1daeSBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5*)viewer->data;
225c6c1daeSBarry Smith   PetscErrorCode   ierr;
235c6c1daeSBarry Smith 
245c6c1daeSBarry Smith   PetscFunctionBegin;
255c6c1daeSBarry Smith   ierr = PetscFree(hdf5->filename);CHKERRQ(ierr);
26a297a907SKarl Rupp   if (hdf5->file_id) H5Fclose(hdf5->file_id);
275c6c1daeSBarry Smith   PetscFunctionReturn(0);
285c6c1daeSBarry Smith }
295c6c1daeSBarry Smith 
305c6c1daeSBarry Smith #undef __FUNCT__
315c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerDestroy_HDF5"
325c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroy_HDF5(PetscViewer viewer)
335c6c1daeSBarry Smith {
345c6c1daeSBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5*) viewer->data;
355c6c1daeSBarry Smith   PetscErrorCode   ierr;
365c6c1daeSBarry Smith 
375c6c1daeSBarry Smith   PetscFunctionBegin;
385c6c1daeSBarry Smith   ierr = PetscViewerFileClose_HDF5(viewer);CHKERRQ(ierr);
395c6c1daeSBarry Smith   while (hdf5->groups) {
405c6c1daeSBarry Smith     GroupList *tmp = hdf5->groups->next;
415c6c1daeSBarry Smith 
425c6c1daeSBarry Smith     ierr         = PetscFree(hdf5->groups->name);CHKERRQ(ierr);
435c6c1daeSBarry Smith     ierr         = PetscFree(hdf5->groups);CHKERRQ(ierr);
445c6c1daeSBarry Smith     hdf5->groups = tmp;
455c6c1daeSBarry Smith   }
465c6c1daeSBarry Smith   ierr = PetscFree(hdf5);CHKERRQ(ierr);
470b062f91SJed Brown   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C",NULL);CHKERRQ(ierr);
480b062f91SJed Brown   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetMode_C",NULL);CHKERRQ(ierr);
495c6c1daeSBarry Smith   PetscFunctionReturn(0);
505c6c1daeSBarry Smith }
515c6c1daeSBarry Smith 
525c6c1daeSBarry Smith #undef __FUNCT__
535c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetMode_HDF5"
545c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetMode_HDF5(PetscViewer viewer, PetscFileMode type)
555c6c1daeSBarry Smith {
565c6c1daeSBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5*) viewer->data;
575c6c1daeSBarry Smith 
585c6c1daeSBarry Smith   PetscFunctionBegin;
595c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
605c6c1daeSBarry Smith   hdf5->btype = type;
615c6c1daeSBarry Smith   PetscFunctionReturn(0);
625c6c1daeSBarry Smith }
635c6c1daeSBarry Smith 
645c6c1daeSBarry Smith #undef __FUNCT__
655c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetName_HDF5"
665c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetName_HDF5(PetscViewer viewer, const char name[])
675c6c1daeSBarry Smith {
685c6c1daeSBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5*) viewer->data;
695c6c1daeSBarry Smith #if defined(PETSC_HAVE_H5PSET_FAPL_MPIO)
705c6c1daeSBarry Smith   MPI_Info          info = MPI_INFO_NULL;
715c6c1daeSBarry Smith #endif
725c6c1daeSBarry Smith   hid_t             plist_id;
735c6c1daeSBarry Smith   herr_t            herr;
745c6c1daeSBarry Smith   PetscErrorCode    ierr;
755c6c1daeSBarry Smith 
765c6c1daeSBarry Smith   PetscFunctionBegin;
775c6c1daeSBarry Smith   ierr = PetscStrallocpy(name, &hdf5->filename);CHKERRQ(ierr);
785c6c1daeSBarry Smith   /* Set up file access property list with parallel I/O access */
795c6c1daeSBarry Smith   plist_id = H5Pcreate(H5P_FILE_ACCESS);
805c6c1daeSBarry Smith #if defined(PETSC_HAVE_H5PSET_FAPL_MPIO)
81ce94432eSBarry Smith   herr = H5Pset_fapl_mpio(plist_id, PetscObjectComm((PetscObject)viewer), info);CHKERRQ(herr);
825c6c1daeSBarry Smith #endif
835c6c1daeSBarry Smith   /* Create or open the file collectively */
845c6c1daeSBarry Smith   switch (hdf5->btype) {
855c6c1daeSBarry Smith   case FILE_MODE_READ:
865c6c1daeSBarry Smith     hdf5->file_id = H5Fopen(name, H5F_ACC_RDONLY, plist_id);
875c6c1daeSBarry Smith     break;
885c6c1daeSBarry Smith   case FILE_MODE_APPEND:
895c6c1daeSBarry Smith     hdf5->file_id = H5Fopen(name, H5F_ACC_RDWR, plist_id);
905c6c1daeSBarry Smith     break;
915c6c1daeSBarry Smith   case FILE_MODE_WRITE:
925c6c1daeSBarry Smith     hdf5->file_id = H5Fcreate(name, H5F_ACC_TRUNC, H5P_DEFAULT, plist_id);
935c6c1daeSBarry Smith     break;
945c6c1daeSBarry Smith   default:
955c6c1daeSBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER, "Must call PetscViewerFileSetMode() before PetscViewerFileSetName()");
965c6c1daeSBarry Smith   }
975c6c1daeSBarry Smith   if (hdf5->file_id < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB, "H5Fcreate failed for %s", name);
985c6c1daeSBarry Smith   H5Pclose(plist_id);
995c6c1daeSBarry Smith   PetscFunctionReturn(0);
1005c6c1daeSBarry Smith }
1015c6c1daeSBarry Smith 
1025c6c1daeSBarry Smith #undef __FUNCT__
1035c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerCreate_HDF5"
1048cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_HDF5(PetscViewer v)
1055c6c1daeSBarry Smith {
1065c6c1daeSBarry Smith   PetscViewer_HDF5 *hdf5;
1075c6c1daeSBarry Smith   PetscErrorCode   ierr;
1085c6c1daeSBarry Smith 
1095c6c1daeSBarry Smith   PetscFunctionBegin;
110b00a9115SJed Brown   ierr = PetscNewLog(v,&hdf5);CHKERRQ(ierr);
1115c6c1daeSBarry Smith 
1125c6c1daeSBarry Smith   v->data         = (void*) hdf5;
1135c6c1daeSBarry Smith   v->ops->destroy = PetscViewerDestroy_HDF5;
1145c6c1daeSBarry Smith   v->ops->flush   = 0;
1155c6c1daeSBarry Smith   hdf5->btype     = (PetscFileMode) -1;
1165c6c1daeSBarry Smith   hdf5->filename  = 0;
1175c6c1daeSBarry Smith   hdf5->timestep  = -1;
1180298fd71SBarry Smith   hdf5->groups    = NULL;
1195c6c1daeSBarry Smith 
1200b062f91SJed Brown   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetName_C",PetscViewerFileSetName_HDF5);CHKERRQ(ierr);
1210b062f91SJed Brown   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_HDF5);CHKERRQ(ierr);
1225c6c1daeSBarry Smith   PetscFunctionReturn(0);
1235c6c1daeSBarry Smith }
1245c6c1daeSBarry Smith 
1255c6c1daeSBarry Smith #undef __FUNCT__
1265c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerHDF5Open"
1275c6c1daeSBarry Smith /*@C
1285c6c1daeSBarry Smith    PetscViewerHDF5Open - Opens a file for HDF5 input/output.
1295c6c1daeSBarry Smith 
1305c6c1daeSBarry Smith    Collective on MPI_Comm
1315c6c1daeSBarry Smith 
1325c6c1daeSBarry Smith    Input Parameters:
1335c6c1daeSBarry Smith +  comm - MPI communicator
1345c6c1daeSBarry Smith .  name - name of file
1355c6c1daeSBarry Smith -  type - type of file
1365c6c1daeSBarry Smith $    FILE_MODE_WRITE - create new file for binary output
1375c6c1daeSBarry Smith $    FILE_MODE_READ - open existing file for binary input
1385c6c1daeSBarry Smith $    FILE_MODE_APPEND - open existing file for binary output
1395c6c1daeSBarry Smith 
1405c6c1daeSBarry Smith    Output Parameter:
1415c6c1daeSBarry Smith .  hdf5v - PetscViewer for HDF5 input/output to use with the specified file
1425c6c1daeSBarry Smith 
1435c6c1daeSBarry Smith    Level: beginner
1445c6c1daeSBarry Smith 
1455c6c1daeSBarry Smith    Note:
1465c6c1daeSBarry Smith    This PetscViewer should be destroyed with PetscViewerDestroy().
1475c6c1daeSBarry Smith 
1485c6c1daeSBarry Smith    Concepts: HDF5 files
1495c6c1daeSBarry Smith    Concepts: PetscViewerHDF5^creating
1505c6c1daeSBarry Smith 
1515c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
1525c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(),
1535c6c1daeSBarry Smith           PetscFileMode, PetscViewer
1545c6c1daeSBarry Smith @*/
1555c6c1daeSBarry Smith PetscErrorCode  PetscViewerHDF5Open(MPI_Comm comm, const char name[], PetscFileMode type, PetscViewer *hdf5v)
1565c6c1daeSBarry Smith {
1575c6c1daeSBarry Smith   PetscErrorCode ierr;
1585c6c1daeSBarry Smith 
1595c6c1daeSBarry Smith   PetscFunctionBegin;
1605c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm, hdf5v);CHKERRQ(ierr);
1615c6c1daeSBarry Smith   ierr = PetscViewerSetType(*hdf5v, PETSCVIEWERHDF5);CHKERRQ(ierr);
1625c6c1daeSBarry Smith   ierr = PetscViewerFileSetMode(*hdf5v, type);CHKERRQ(ierr);
1635c6c1daeSBarry Smith   ierr = PetscViewerFileSetName(*hdf5v, name);CHKERRQ(ierr);
1645c6c1daeSBarry Smith   PetscFunctionReturn(0);
1655c6c1daeSBarry Smith }
1665c6c1daeSBarry Smith 
1675c6c1daeSBarry Smith #undef __FUNCT__
1685c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerHDF5GetFileId"
1695c6c1daeSBarry Smith /*@C
1705c6c1daeSBarry Smith   PetscViewerHDF5GetFileId - Retrieve the file id, this file ID then can be used in direct HDF5 calls
1715c6c1daeSBarry Smith 
1725c6c1daeSBarry Smith   Not collective
1735c6c1daeSBarry Smith 
1745c6c1daeSBarry Smith   Input Parameter:
1755c6c1daeSBarry Smith . viewer - the PetscViewer
1765c6c1daeSBarry Smith 
1775c6c1daeSBarry Smith   Output Parameter:
1785c6c1daeSBarry Smith . file_id - The file id
1795c6c1daeSBarry Smith 
1805c6c1daeSBarry Smith   Level: intermediate
1815c6c1daeSBarry Smith 
1825c6c1daeSBarry Smith .seealso: PetscViewerHDF5Open()
1835c6c1daeSBarry Smith @*/
1845c6c1daeSBarry Smith PetscErrorCode  PetscViewerHDF5GetFileId(PetscViewer viewer, hid_t *file_id)
1855c6c1daeSBarry Smith {
1865c6c1daeSBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5*) viewer->data;
1875c6c1daeSBarry Smith 
1885c6c1daeSBarry Smith   PetscFunctionBegin;
1895c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
1905c6c1daeSBarry Smith   if (file_id) *file_id = hdf5->file_id;
1915c6c1daeSBarry Smith   PetscFunctionReturn(0);
1925c6c1daeSBarry Smith }
1935c6c1daeSBarry Smith 
1945c6c1daeSBarry Smith #undef __FUNCT__
1955c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerHDF5PushGroup"
1965c6c1daeSBarry Smith /*@C
1975c6c1daeSBarry Smith   PetscViewerHDF5PushGroup - Set the current HDF5 group for output
1985c6c1daeSBarry Smith 
1995c6c1daeSBarry Smith   Not collective
2005c6c1daeSBarry Smith 
2015c6c1daeSBarry Smith   Input Parameters:
2025c6c1daeSBarry Smith + viewer - the PetscViewer
2035c6c1daeSBarry Smith - name - The group name
2045c6c1daeSBarry Smith 
2055c6c1daeSBarry Smith   Level: intermediate
2065c6c1daeSBarry Smith 
2075c6c1daeSBarry Smith .seealso: PetscViewerHDF5Open(),PetscViewerHDF5PopGroup(),PetscViewerHDF5GetGroup()
2085c6c1daeSBarry Smith @*/
2095c6c1daeSBarry Smith PetscErrorCode  PetscViewerHDF5PushGroup(PetscViewer viewer, const char *name)
2105c6c1daeSBarry Smith {
2115c6c1daeSBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5*) viewer->data;
2125c6c1daeSBarry Smith   GroupList        *groupNode;
2135c6c1daeSBarry Smith   PetscErrorCode   ierr;
2145c6c1daeSBarry Smith 
2155c6c1daeSBarry Smith   PetscFunctionBegin;
2165c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2175c6c1daeSBarry Smith   PetscValidCharPointer(name,2);
2185c6c1daeSBarry Smith   ierr = PetscMalloc(sizeof(GroupList), &groupNode);CHKERRQ(ierr);
2195c6c1daeSBarry Smith   ierr = PetscStrallocpy(name, (char**) &groupNode->name);CHKERRQ(ierr);
220a297a907SKarl Rupp 
2215c6c1daeSBarry Smith   groupNode->next = hdf5->groups;
2225c6c1daeSBarry Smith   hdf5->groups    = groupNode;
2235c6c1daeSBarry Smith   PetscFunctionReturn(0);
2245c6c1daeSBarry Smith }
2255c6c1daeSBarry Smith 
2265c6c1daeSBarry Smith #undef __FUNCT__
2275c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerHDF5PopGroup"
2283ef9c667SSatish Balay /*@
2295c6c1daeSBarry Smith   PetscViewerHDF5PopGroup - Return the current HDF5 group for output to the previous value
2305c6c1daeSBarry Smith 
2315c6c1daeSBarry Smith   Not collective
2325c6c1daeSBarry Smith 
2335c6c1daeSBarry Smith   Input Parameter:
2345c6c1daeSBarry Smith . viewer - the PetscViewer
2355c6c1daeSBarry Smith 
2365c6c1daeSBarry Smith   Level: intermediate
2375c6c1daeSBarry Smith 
2385c6c1daeSBarry Smith .seealso: PetscViewerHDF5Open(),PetscViewerHDF5PushGroup(),PetscViewerHDF5GetGroup()
2395c6c1daeSBarry Smith @*/
2405c6c1daeSBarry Smith PetscErrorCode  PetscViewerHDF5PopGroup(PetscViewer viewer)
2415c6c1daeSBarry Smith {
2425c6c1daeSBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5*) viewer->data;
2435c6c1daeSBarry Smith   GroupList        *groupNode;
2445c6c1daeSBarry Smith   PetscErrorCode   ierr;
2455c6c1daeSBarry Smith 
2465c6c1daeSBarry Smith   PetscFunctionBegin;
2475c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
24882f516ccSBarry Smith   if (!hdf5->groups) SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "HDF5 group stack is empty, cannot pop");
2495c6c1daeSBarry Smith   groupNode    = hdf5->groups;
2505c6c1daeSBarry Smith   hdf5->groups = hdf5->groups->next;
2515c6c1daeSBarry Smith   ierr         = PetscFree(groupNode->name);CHKERRQ(ierr);
2525c6c1daeSBarry Smith   ierr         = PetscFree(groupNode);CHKERRQ(ierr);
2535c6c1daeSBarry Smith   PetscFunctionReturn(0);
2545c6c1daeSBarry Smith }
2555c6c1daeSBarry Smith 
2565c6c1daeSBarry Smith #undef __FUNCT__
2575c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerHDF5GetGroup"
2585c6c1daeSBarry Smith /*@C
2590298fd71SBarry Smith   PetscViewerHDF5GetGroup - Get the current HDF5 group for output. If none has been assigned, returns NULL.
2605c6c1daeSBarry Smith 
2615c6c1daeSBarry Smith   Not collective
2625c6c1daeSBarry Smith 
2635c6c1daeSBarry Smith   Input Parameter:
2645c6c1daeSBarry Smith . viewer - the PetscViewer
2655c6c1daeSBarry Smith 
2665c6c1daeSBarry Smith   Output Parameter:
2675c6c1daeSBarry Smith . name - The group name
2685c6c1daeSBarry Smith 
2695c6c1daeSBarry Smith   Level: intermediate
2705c6c1daeSBarry Smith 
2715c6c1daeSBarry Smith .seealso: PetscViewerHDF5Open(),PetscViewerHDF5PushGroup(),PetscViewerHDF5PopGroup()
2725c6c1daeSBarry Smith @*/
2735c6c1daeSBarry Smith PetscErrorCode  PetscViewerHDF5GetGroup(PetscViewer viewer, const char **name)
2745c6c1daeSBarry Smith {
2755c6c1daeSBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5 *) viewer->data;
2765c6c1daeSBarry Smith 
2775c6c1daeSBarry Smith   PetscFunctionBegin;
2785c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2795c6c1daeSBarry Smith   PetscValidCharPointer(name,2);
280a297a907SKarl Rupp   if (hdf5->groups) *name = hdf5->groups->name;
2810298fd71SBarry Smith   else *name = NULL;
2825c6c1daeSBarry Smith   PetscFunctionReturn(0);
2835c6c1daeSBarry Smith }
2845c6c1daeSBarry Smith 
2855c6c1daeSBarry Smith #undef __FUNCT__
2865c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerHDF5IncrementTimestep"
2873ef9c667SSatish Balay /*@
2885c6c1daeSBarry Smith   PetscViewerHDF5IncrementTimestep - Increments the current timestep for the HDF5 output. Fields are stacked in time.
2895c6c1daeSBarry Smith 
2905c6c1daeSBarry Smith   Not collective
2915c6c1daeSBarry Smith 
2925c6c1daeSBarry Smith   Input Parameter:
2935c6c1daeSBarry Smith . viewer - the PetscViewer
2945c6c1daeSBarry Smith 
2955c6c1daeSBarry Smith   Level: intermediate
2965c6c1daeSBarry Smith 
2975c6c1daeSBarry Smith .seealso: PetscViewerHDF5Open(), PetscViewerHDF5SetTimestep(), PetscViewerHDF5GetTimestep()
2985c6c1daeSBarry Smith @*/
2995c6c1daeSBarry Smith PetscErrorCode PetscViewerHDF5IncrementTimestep(PetscViewer viewer)
3005c6c1daeSBarry Smith {
3015c6c1daeSBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5*) viewer->data;
3025c6c1daeSBarry Smith 
3035c6c1daeSBarry Smith   PetscFunctionBegin;
3045c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
3055c6c1daeSBarry Smith   ++hdf5->timestep;
3065c6c1daeSBarry Smith   PetscFunctionReturn(0);
3075c6c1daeSBarry Smith }
3085c6c1daeSBarry Smith 
3095c6c1daeSBarry Smith #undef __FUNCT__
3105c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerHDF5SetTimestep"
3113ef9c667SSatish Balay /*@
3125c6c1daeSBarry Smith   PetscViewerHDF5SetTimestep - Set the current timestep for the HDF5 output. Fields are stacked in time. A timestep
3135c6c1daeSBarry Smith   of -1 disables blocking with timesteps.
3145c6c1daeSBarry Smith 
3155c6c1daeSBarry Smith   Not collective
3165c6c1daeSBarry Smith 
3175c6c1daeSBarry Smith   Input Parameters:
3185c6c1daeSBarry Smith + viewer - the PetscViewer
3195c6c1daeSBarry Smith - timestep - The timestep number
3205c6c1daeSBarry Smith 
3215c6c1daeSBarry Smith   Level: intermediate
3225c6c1daeSBarry Smith 
3235c6c1daeSBarry Smith .seealso: PetscViewerHDF5Open(), PetscViewerHDF5IncrementTimestep(), PetscViewerHDF5GetTimestep()
3245c6c1daeSBarry Smith @*/
3255c6c1daeSBarry Smith PetscErrorCode  PetscViewerHDF5SetTimestep(PetscViewer viewer, PetscInt timestep)
3265c6c1daeSBarry Smith {
3275c6c1daeSBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5*) viewer->data;
3285c6c1daeSBarry Smith 
3295c6c1daeSBarry Smith   PetscFunctionBegin;
3305c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
3315c6c1daeSBarry Smith   hdf5->timestep = timestep;
3325c6c1daeSBarry Smith   PetscFunctionReturn(0);
3335c6c1daeSBarry Smith }
3345c6c1daeSBarry Smith 
3355c6c1daeSBarry Smith #undef __FUNCT__
3365c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerHDF5GetTimestep"
3373ef9c667SSatish Balay /*@
3385c6c1daeSBarry Smith   PetscViewerHDF5GetTimestep - Get the current timestep for the HDF5 output. Fields are stacked in time.
3395c6c1daeSBarry Smith 
3405c6c1daeSBarry Smith   Not collective
3415c6c1daeSBarry Smith 
3425c6c1daeSBarry Smith   Input Parameter:
3435c6c1daeSBarry Smith . viewer - the PetscViewer
3445c6c1daeSBarry Smith 
3455c6c1daeSBarry Smith   Output Parameter:
3465c6c1daeSBarry Smith . timestep - The timestep number
3475c6c1daeSBarry Smith 
3485c6c1daeSBarry Smith   Level: intermediate
3495c6c1daeSBarry Smith 
3505c6c1daeSBarry Smith .seealso: PetscViewerHDF5Open(), PetscViewerHDF5IncrementTimestep(), PetscViewerHDF5SetTimestep()
3515c6c1daeSBarry Smith @*/
3525c6c1daeSBarry Smith PetscErrorCode  PetscViewerHDF5GetTimestep(PetscViewer viewer, PetscInt *timestep)
3535c6c1daeSBarry Smith {
3545c6c1daeSBarry Smith   PetscViewer_HDF5 *hdf5 = (PetscViewer_HDF5*) viewer->data;
3555c6c1daeSBarry Smith 
3565c6c1daeSBarry Smith   PetscFunctionBegin;
3575c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
3585c6c1daeSBarry Smith   PetscValidPointer(timestep,2);
3595c6c1daeSBarry Smith   *timestep = hdf5->timestep;
3605c6c1daeSBarry Smith   PetscFunctionReturn(0);
3615c6c1daeSBarry Smith }
3625c6c1daeSBarry Smith 
363a75e6a4aSMatthew G. Knepley 
36436bce6e8SMatthew G. Knepley #undef __FUNCT__
36536bce6e8SMatthew G. Knepley #define __FUNCT__ "PetscDataTypeToHDF5DataType"
36636bce6e8SMatthew G. Knepley /*@C
36736bce6e8SMatthew G. Knepley   PetscDataTypeToHDF5DataType - Converts the PETSc name of a datatype to its HDF5 name.
36836bce6e8SMatthew G. Knepley 
36936bce6e8SMatthew G. Knepley   Not collective
37036bce6e8SMatthew G. Knepley 
37136bce6e8SMatthew G. Knepley   Input Parameter:
37236bce6e8SMatthew G. Knepley . ptype - the PETSc datatype name (for example PETSC_DOUBLE)
37336bce6e8SMatthew G. Knepley 
37436bce6e8SMatthew G. Knepley   Output Parameter:
37536bce6e8SMatthew G. Knepley . mtype - the MPI datatype (for example MPI_DOUBLE, ...)
37636bce6e8SMatthew G. Knepley 
37736bce6e8SMatthew G. Knepley   Level: advanced
37836bce6e8SMatthew G. Knepley 
37936bce6e8SMatthew G. Knepley .seealso: PetscDataType, PetscHDF5DataTypeToPetscDataType()
38036bce6e8SMatthew G. Knepley @*/
38136bce6e8SMatthew G. Knepley PetscErrorCode PetscDataTypeToHDF5DataType(PetscDataType ptype, hid_t *htype)
38236bce6e8SMatthew G. Knepley {
38336bce6e8SMatthew G. Knepley   PetscFunctionBegin;
38436bce6e8SMatthew G. Knepley   if (ptype == PETSC_INT)
38536bce6e8SMatthew G. Knepley #if defined(PETSC_USE_64BIT_INDICES)
38636bce6e8SMatthew G. Knepley                                        *htype = H5T_NATIVE_LLONG;
38736bce6e8SMatthew G. Knepley #else
38836bce6e8SMatthew G. Knepley                                        *htype = H5T_NATIVE_INT;
38936bce6e8SMatthew G. Knepley #endif
39036bce6e8SMatthew G. Knepley   else if (ptype == PETSC_DOUBLE)      *htype = H5T_NATIVE_DOUBLE;
39136bce6e8SMatthew G. Knepley   else if (ptype == PETSC_LONG)        *htype = H5T_NATIVE_LONG;
39236bce6e8SMatthew G. Knepley   else if (ptype == PETSC_SHORT)       *htype = H5T_NATIVE_SHORT;
39336bce6e8SMatthew G. Knepley   else if (ptype == PETSC_ENUM)        *htype = H5T_NATIVE_DOUBLE;
39436bce6e8SMatthew G. Knepley   else if (ptype == PETSC_BOOL)        *htype = H5T_NATIVE_DOUBLE;
39536bce6e8SMatthew G. Knepley   else if (ptype == PETSC_FLOAT)       *htype = H5T_NATIVE_FLOAT;
39636bce6e8SMatthew G. Knepley   else if (ptype == PETSC_CHAR)        *htype = H5T_NATIVE_CHAR;
39736bce6e8SMatthew G. Knepley   else if (ptype == PETSC_BIT_LOGICAL) *htype = H5T_NATIVE_UCHAR;
3987e97c476SMatthew G. Knepley   else if (ptype == PETSC_STRING)      *htype = H5Tcopy(H5T_C_S1);
39936bce6e8SMatthew G. Knepley   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported PETSc datatype");
40036bce6e8SMatthew G. Knepley   PetscFunctionReturn(0);
40136bce6e8SMatthew G. Knepley }
40236bce6e8SMatthew G. Knepley 
40336bce6e8SMatthew G. Knepley #undef __FUNCT__
40436bce6e8SMatthew G. Knepley #define __FUNCT__ "PetscHDF5DataTypeToPetscDataType"
40536bce6e8SMatthew G. Knepley /*@C
40636bce6e8SMatthew G. Knepley   PetscHDF5DataTypeToPetscDataType - Finds the PETSc name of a datatype from its HDF5 name
40736bce6e8SMatthew G. Knepley 
40836bce6e8SMatthew G. Knepley   Not collective
40936bce6e8SMatthew G. Knepley 
41036bce6e8SMatthew G. Knepley   Input Parameter:
41136bce6e8SMatthew G. Knepley . htype - the HDF5 datatype (for example H5T_NATIVE_DOUBLE, ...)
41236bce6e8SMatthew G. Knepley 
41336bce6e8SMatthew G. Knepley   Output Parameter:
41436bce6e8SMatthew G. Knepley . ptype - the PETSc datatype name (for example PETSC_DOUBLE)
41536bce6e8SMatthew G. Knepley 
41636bce6e8SMatthew G. Knepley   Level: advanced
41736bce6e8SMatthew G. Knepley 
41836bce6e8SMatthew G. Knepley .seealso: PetscDataType, PetscHDF5DataTypeToPetscDataType()
41936bce6e8SMatthew G. Knepley @*/
42036bce6e8SMatthew G. Knepley PetscErrorCode PetscHDF5DataTypeToPetscDataType(hid_t htype, PetscDataType *ptype)
42136bce6e8SMatthew G. Knepley {
42236bce6e8SMatthew G. Knepley   PetscFunctionBegin;
42336bce6e8SMatthew G. Knepley #if defined(PETSC_USE_64BIT_INDICES)
42436bce6e8SMatthew G. Knepley   if      (htype == H5T_NATIVE_INT)    *ptype = PETSC_LONG;
42536bce6e8SMatthew G. Knepley   else if (htype == H5T_NATIVE_LLONG)  *ptype = PETSC_INT;
42636bce6e8SMatthew G. Knepley #else
42736bce6e8SMatthew G. Knepley   if      (htype == H5T_NATIVE_INT)    *ptype = PETSC_INT;
42836bce6e8SMatthew G. Knepley #endif
42936bce6e8SMatthew G. Knepley   else if (htype == H5T_NATIVE_DOUBLE) *ptype = PETSC_DOUBLE;
43036bce6e8SMatthew G. Knepley   else if (htype == H5T_NATIVE_LONG)   *ptype = PETSC_LONG;
43136bce6e8SMatthew G. Knepley   else if (htype == H5T_NATIVE_SHORT)  *ptype = PETSC_SHORT;
43236bce6e8SMatthew G. Knepley   else if (htype == H5T_NATIVE_FLOAT)  *ptype = PETSC_FLOAT;
43336bce6e8SMatthew G. Knepley   else if (htype == H5T_NATIVE_CHAR)   *ptype = PETSC_CHAR;
43436bce6e8SMatthew G. Knepley   else if (htype == H5T_NATIVE_UCHAR)  *ptype = PETSC_CHAR;
4357e97c476SMatthew G. Knepley   else if (htype == H5T_C_S1)          *ptype = PETSC_STRING;
43636bce6e8SMatthew G. Knepley   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported HDF5 datatype");
43736bce6e8SMatthew G. Knepley   PetscFunctionReturn(0);
43836bce6e8SMatthew G. Knepley }
43936bce6e8SMatthew G. Knepley 
44036bce6e8SMatthew G. Knepley #undef __FUNCT__
44136bce6e8SMatthew G. Knepley #define __FUNCT__ "PetscViewerHDF5WriteAttribute"
44236bce6e8SMatthew G. Knepley /*@
44336bce6e8SMatthew G. Knepley  PetscViewerHDF5WriteAttribute - Write a scalar attribute
44436bce6e8SMatthew G. Knepley 
44536bce6e8SMatthew G. Knepley   Input Parameters:
44636bce6e8SMatthew G. Knepley + viewer - The HDF5 viewer
44736bce6e8SMatthew G. Knepley . parent - The parent name
44836bce6e8SMatthew G. Knepley . name   - The attribute name
44936bce6e8SMatthew G. Knepley . datatype - The attribute type
45036bce6e8SMatthew G. Knepley - value    - The attribute value
45136bce6e8SMatthew G. Knepley 
45236bce6e8SMatthew G. Knepley   Level: advanced
45336bce6e8SMatthew G. Knepley 
454*e7bdbf8eSMatthew G. Knepley .seealso: PetscViewerHDF5Open(), PetscViewerHDF5ReadAttribute(), PetscViewerHDF5HasAttribute()
45536bce6e8SMatthew G. Knepley @*/
45636bce6e8SMatthew G. Knepley PetscErrorCode PetscViewerHDF5WriteAttribute(PetscViewer viewer, const char parent[], const char name[], PetscDataType datatype, const void *value)
45736bce6e8SMatthew G. Knepley {
45836bce6e8SMatthew G. Knepley   hid_t          h5, dataspace, dataset, attribute, dtype, status;
45936bce6e8SMatthew G. Knepley   PetscErrorCode ierr;
46036bce6e8SMatthew G. Knepley 
46136bce6e8SMatthew G. Knepley   PetscFunctionBegin;
46236bce6e8SMatthew G. Knepley   PetscValidPointer(parent, 2);
46336bce6e8SMatthew G. Knepley   PetscValidPointer(name, 3);
46436bce6e8SMatthew G. Knepley   PetscValidPointer(value, 4);
46536bce6e8SMatthew G. Knepley   ierr = PetscDataTypeToHDF5DataType(datatype, &dtype);CHKERRQ(ierr);
4667e97c476SMatthew G. Knepley   if (datatype == PETSC_STRING) {
4677e97c476SMatthew G. Knepley     size_t len;
4687e97c476SMatthew G. Knepley     ierr = PetscStrlen((const char *) value, &len);CHKERRQ(ierr);
4697e97c476SMatthew G. Knepley     status = H5Tset_size(dtype, len+1);CHKERRQ(status);
4707e97c476SMatthew G. Knepley   }
47136bce6e8SMatthew G. Knepley   ierr = PetscViewerHDF5GetFileId(viewer, &h5);CHKERRQ(ierr);
47236bce6e8SMatthew G. Knepley   dataspace = H5Screate(H5S_SCALAR);
47336bce6e8SMatthew G. Knepley   if (dataspace < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Could not create dataspace for attribute %s of %s", name, parent);
47436bce6e8SMatthew G. Knepley #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800)
47536bce6e8SMatthew G. Knepley   dataset = H5Dopen2(h5, parent, H5P_DEFAULT);
47636bce6e8SMatthew G. Knepley   if (dataset < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Could not open parent dataset for attribute %s of %s", name, parent);
47736bce6e8SMatthew G. Knepley   attribute = H5Acreate2(dataset, name, dtype, dataspace, H5P_DEFAULT, H5P_DEFAULT);
47836bce6e8SMatthew G. Knepley #else
47936bce6e8SMatthew G. Knepley   dataset = H5Dopen(h5, parent);
48036bce6e8SMatthew G. Knepley   if (dataset < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Could not open parent dataset for attribute %s of %s", name, parent);
48136bce6e8SMatthew G. Knepley   attribute = H5Acreate(dataset, name, dtype, dataspace, H5P_DEFAULT);
48236bce6e8SMatthew G. Knepley #endif
48336bce6e8SMatthew G. Knepley   if (attribute < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Could not create attribute %s of %s", name, parent);
48436bce6e8SMatthew G. Knepley   status = H5Awrite(attribute, dtype, value);CHKERRQ(status);
4857e97c476SMatthew G. Knepley   if (datatype == PETSC_STRING) {status = H5Tclose(dtype);CHKERRQ(status);}
48636bce6e8SMatthew G. Knepley   status = H5Aclose(attribute);CHKERRQ(status);
48736bce6e8SMatthew G. Knepley   status = H5Dclose(dataset);CHKERRQ(status);
48836bce6e8SMatthew G. Knepley   status = H5Sclose(dataspace);CHKERRQ(status);
48936bce6e8SMatthew G. Knepley   PetscFunctionReturn(0);
49036bce6e8SMatthew G. Knepley }
49136bce6e8SMatthew G. Knepley 
492ad0c61feSMatthew G. Knepley #undef __FUNCT__
493ad0c61feSMatthew G. Knepley #define __FUNCT__ "PetscViewerHDF5ReadAttribute"
494ad0c61feSMatthew G. Knepley /*@
495ad0c61feSMatthew G. Knepley  PetscViewerHDF5ReadAttribute - Read a scalar attribute
496ad0c61feSMatthew G. Knepley 
497ad0c61feSMatthew G. Knepley   Input Parameters:
498ad0c61feSMatthew G. Knepley + viewer - The HDF5 viewer
499ad0c61feSMatthew G. Knepley . parent - The parent name
500ad0c61feSMatthew G. Knepley . name   - The attribute name
501ad0c61feSMatthew G. Knepley - datatype - The attribute type
502ad0c61feSMatthew G. Knepley 
503ad0c61feSMatthew G. Knepley   Output Parameter:
504ad0c61feSMatthew G. Knepley . value    - The attribute value
505ad0c61feSMatthew G. Knepley 
506ad0c61feSMatthew G. Knepley   Level: advanced
507ad0c61feSMatthew G. Knepley 
508*e7bdbf8eSMatthew G. Knepley .seealso: PetscViewerHDF5Open(), PetscViewerHDF5WriteAttribute(), PetscViewerHDF5HasAttribute()
509ad0c61feSMatthew G. Knepley @*/
510ad0c61feSMatthew G. Knepley PetscErrorCode PetscViewerHDF5ReadAttribute(PetscViewer viewer, const char parent[], const char name[], PetscDataType datatype, void *value)
511ad0c61feSMatthew G. Knepley {
512ad0c61feSMatthew G. Knepley   hid_t          h5, dataspace, dataset, attribute, dtype, status;
513ad0c61feSMatthew G. Knepley   PetscErrorCode ierr;
514ad0c61feSMatthew G. Knepley 
515ad0c61feSMatthew G. Knepley   PetscFunctionBegin;
516ad0c61feSMatthew G. Knepley   PetscValidPointer(parent, 2);
517ad0c61feSMatthew G. Knepley   PetscValidPointer(name, 3);
518ad0c61feSMatthew G. Knepley   PetscValidPointer(value, 4);
519ad0c61feSMatthew G. Knepley   ierr = PetscDataTypeToHDF5DataType(datatype, &dtype);CHKERRQ(ierr);
520ad0c61feSMatthew G. Knepley   ierr = PetscViewerHDF5GetFileId(viewer, &h5);CHKERRQ(ierr);
521ad0c61feSMatthew G. Knepley   dataspace = H5Screate(H5S_SCALAR);
522ad0c61feSMatthew G. Knepley   if (dataspace < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Could not create dataspace for attribute %s of %s", name, parent);
523ad0c61feSMatthew G. Knepley #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800)
524ad0c61feSMatthew G. Knepley   dataset = H5Dopen2(h5, parent, H5P_DEFAULT);
525ad0c61feSMatthew G. Knepley   if (dataset < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Could not open parent dataset for attribute %s of %s", name, parent);
526ad0c61feSMatthew G. Knepley #else
527ad0c61feSMatthew G. Knepley   dataset = H5Dopen(h5, parent);
528ad0c61feSMatthew G. Knepley   if (dataset < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Could not open parent dataset for attribute %s of %s", name, parent);
529ad0c61feSMatthew G. Knepley #endif
530ad0c61feSMatthew G. Knepley   attribute = H5Aopen_name(dataset, name);
531*e7bdbf8eSMatthew G. Knepley   if (attribute < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Could not open attribute %s of %s", name, parent);
532ad0c61feSMatthew G. Knepley   status = H5Aread(attribute, dtype, value);CHKERRQ(status);
533ad0c61feSMatthew G. Knepley   status = H5Aclose(attribute);CHKERRQ(status);
534ad0c61feSMatthew G. Knepley   status = H5Dclose(dataset);CHKERRQ(status);
535ad0c61feSMatthew G. Knepley   status = H5Sclose(dataspace);CHKERRQ(status);
536ad0c61feSMatthew G. Knepley   PetscFunctionReturn(0);
537ad0c61feSMatthew G. Knepley }
538ad0c61feSMatthew G. Knepley 
539*e7bdbf8eSMatthew G. Knepley #undef __FUNCT__
540*e7bdbf8eSMatthew G. Knepley #define __FUNCT__ "PetscViewerHDF5HasAttribute"
541*e7bdbf8eSMatthew G. Knepley /*@
542*e7bdbf8eSMatthew G. Knepley  PetscViewerHDF5HasAttribute - Check whether a scalar attribute exists
543*e7bdbf8eSMatthew G. Knepley 
544*e7bdbf8eSMatthew G. Knepley   Input Parameters:
545*e7bdbf8eSMatthew G. Knepley + viewer - The HDF5 viewer
546*e7bdbf8eSMatthew G. Knepley . parent - The parent name
547*e7bdbf8eSMatthew G. Knepley - name   - The attribute name
548*e7bdbf8eSMatthew G. Knepley 
549*e7bdbf8eSMatthew G. Knepley   Output Parameter:
550*e7bdbf8eSMatthew G. Knepley . has    - Flag for attribute existence
551*e7bdbf8eSMatthew G. Knepley 
552*e7bdbf8eSMatthew G. Knepley   Level: advanced
553*e7bdbf8eSMatthew G. Knepley 
554*e7bdbf8eSMatthew G. Knepley .seealso: PetscViewerHDF5Open(), PetscViewerHDF5WriteAttribute(), PetscViewerHDF5ReadAttribute()
555*e7bdbf8eSMatthew G. Knepley @*/
556*e7bdbf8eSMatthew G. Knepley PetscErrorCode PetscViewerHDF5HasAttribute(PetscViewer viewer, const char parent[], const char name[], PetscBool *has)
557*e7bdbf8eSMatthew G. Knepley {
558*e7bdbf8eSMatthew G. Knepley   hid_t          h5, dataset, attribute, status;
559*e7bdbf8eSMatthew G. Knepley   PetscErrorCode ierr;
560*e7bdbf8eSMatthew G. Knepley 
561*e7bdbf8eSMatthew G. Knepley   PetscFunctionBegin;
562*e7bdbf8eSMatthew G. Knepley   PetscValidPointer(parent, 2);
563*e7bdbf8eSMatthew G. Knepley   PetscValidPointer(name, 3);
564*e7bdbf8eSMatthew G. Knepley   PetscValidPointer(has, 4);
565*e7bdbf8eSMatthew G. Knepley   *has = PETSC_FALSE;
566*e7bdbf8eSMatthew G. Knepley   ierr = PetscViewerHDF5GetFileId(viewer, &h5);CHKERRQ(ierr);
567*e7bdbf8eSMatthew G. Knepley #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800)
568*e7bdbf8eSMatthew G. Knepley   dataset = H5Dopen2(h5, parent, H5P_DEFAULT);
569*e7bdbf8eSMatthew G. Knepley   if (dataset < 0) PetscFunctionReturn(0);
570*e7bdbf8eSMatthew G. Knepley #else
571*e7bdbf8eSMatthew G. Knepley   dataset = H5Dopen(h5, parent);
572*e7bdbf8eSMatthew G. Knepley   if (dataset < 0) PetscFunctionReturn(0);
573*e7bdbf8eSMatthew G. Knepley #endif
574*e7bdbf8eSMatthew G. Knepley   attribute = H5Aopen_name(dataset, name);
575*e7bdbf8eSMatthew G. Knepley   if (attribute < 0) {status = H5Dclose(dataset);CHKERRQ(status); PetscFunctionReturn(0);}
576*e7bdbf8eSMatthew G. Knepley   status = H5Aclose(attribute);CHKERRQ(status);
577*e7bdbf8eSMatthew G. Knepley   status = H5Dclose(dataset);CHKERRQ(status);
578*e7bdbf8eSMatthew G. Knepley   *has = PETSC_TRUE;
579*e7bdbf8eSMatthew G. Knepley   PetscFunctionReturn(0);
580*e7bdbf8eSMatthew G. Knepley }
581*e7bdbf8eSMatthew G. Knepley 
582a75e6a4aSMatthew G. Knepley /*
583a75e6a4aSMatthew G. Knepley   The variable Petsc_Viewer_HDF5_keyval is used to indicate an MPI attribute that
584a75e6a4aSMatthew G. Knepley   is attached to a communicator, in this case the attribute is a PetscViewer.
585a75e6a4aSMatthew G. Knepley */
586a75e6a4aSMatthew G. Knepley static int Petsc_Viewer_HDF5_keyval = MPI_KEYVAL_INVALID;
587a75e6a4aSMatthew G. Knepley 
588a75e6a4aSMatthew G. Knepley #undef __FUNCT__
589a75e6a4aSMatthew G. Knepley #define __FUNCT__ "PETSC_VIEWER_HDF5_"
590a75e6a4aSMatthew G. Knepley /*@C
591a75e6a4aSMatthew G. Knepley   PETSC_VIEWER_HDF5_ - Creates an HDF5 PetscViewer shared by all processors in a communicator.
592a75e6a4aSMatthew G. Knepley 
593a75e6a4aSMatthew G. Knepley   Collective on MPI_Comm
594a75e6a4aSMatthew G. Knepley 
595a75e6a4aSMatthew G. Knepley   Input Parameter:
596a75e6a4aSMatthew G. Knepley . comm - the MPI communicator to share the HDF5 PetscViewer
597a75e6a4aSMatthew G. Knepley 
598a75e6a4aSMatthew G. Knepley   Level: intermediate
599a75e6a4aSMatthew G. Knepley 
600a75e6a4aSMatthew G. Knepley   Options Database Keys:
601a75e6a4aSMatthew G. Knepley . -viewer_hdf5_filename <name>
602a75e6a4aSMatthew G. Knepley 
603a75e6a4aSMatthew G. Knepley   Environmental variables:
604a75e6a4aSMatthew G. Knepley . PETSC_VIEWER_HDF5_FILENAME
605a75e6a4aSMatthew G. Knepley 
606a75e6a4aSMatthew G. Knepley   Notes:
607a75e6a4aSMatthew G. Knepley   Unlike almost all other PETSc routines, PETSC_VIEWER_HDF5_ does not return
608a75e6a4aSMatthew G. Knepley   an error code.  The HDF5 PetscViewer is usually used in the form
609a75e6a4aSMatthew G. Knepley $       XXXView(XXX object, PETSC_VIEWER_HDF5_(comm));
610a75e6a4aSMatthew G. Knepley 
611a75e6a4aSMatthew G. Knepley .seealso: PetscViewerHDF5Open(), PetscViewerCreate(), PetscViewerDestroy()
612a75e6a4aSMatthew G. Knepley @*/
613a75e6a4aSMatthew G. Knepley PetscViewer PETSC_VIEWER_HDF5_(MPI_Comm comm)
614a75e6a4aSMatthew G. Knepley {
615a75e6a4aSMatthew G. Knepley   PetscErrorCode ierr;
616a75e6a4aSMatthew G. Knepley   PetscBool      flg;
617a75e6a4aSMatthew G. Knepley   PetscViewer    viewer;
618a75e6a4aSMatthew G. Knepley   char           fname[PETSC_MAX_PATH_LEN];
619a75e6a4aSMatthew G. Knepley   MPI_Comm       ncomm;
620a75e6a4aSMatthew G. Knepley 
621a75e6a4aSMatthew G. Knepley   PetscFunctionBegin;
622a75e6a4aSMatthew G. Knepley   ierr = PetscCommDuplicate(comm,&ncomm,NULL);if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_HDF5_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
623a75e6a4aSMatthew G. Knepley   if (Petsc_Viewer_HDF5_keyval == MPI_KEYVAL_INVALID) {
624a75e6a4aSMatthew G. Knepley     ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_HDF5_keyval,0);
625a75e6a4aSMatthew G. Knepley     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_HDF5_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
626a75e6a4aSMatthew G. Knepley   }
627a75e6a4aSMatthew G. Knepley   ierr = MPI_Attr_get(ncomm,Petsc_Viewer_HDF5_keyval,(void**)&viewer,(int*)&flg);
628a75e6a4aSMatthew G. Knepley   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_HDF5_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
629a75e6a4aSMatthew G. Knepley   if (!flg) { /* PetscViewer not yet created */
630a75e6a4aSMatthew G. Knepley     ierr = PetscOptionsGetenv(ncomm,"PETSC_VIEWER_HDF5_FILENAME",fname,PETSC_MAX_PATH_LEN,&flg);
631a75e6a4aSMatthew G. Knepley     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_HDF5_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
632a75e6a4aSMatthew G. Knepley     if (!flg) {
633a75e6a4aSMatthew G. Knepley       ierr = PetscStrcpy(fname,"output.h5");
634a75e6a4aSMatthew G. Knepley       if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_HDF5_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
635a75e6a4aSMatthew G. Knepley     }
636a75e6a4aSMatthew G. Knepley     ierr = PetscViewerHDF5Open(ncomm,fname,FILE_MODE_WRITE,&viewer);
637a75e6a4aSMatthew G. Knepley     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_HDF5_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
638a75e6a4aSMatthew G. Knepley     ierr = PetscObjectRegisterDestroy((PetscObject)viewer);
639a75e6a4aSMatthew G. Knepley     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_HDF5_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
640a75e6a4aSMatthew G. Knepley     ierr = MPI_Attr_put(ncomm,Petsc_Viewer_HDF5_keyval,(void*)viewer);
641a75e6a4aSMatthew G. Knepley     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_HDF5_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
642a75e6a4aSMatthew G. Knepley   }
643a75e6a4aSMatthew G. Knepley   ierr = PetscCommDestroy(&ncomm);
644a75e6a4aSMatthew G. Knepley   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_HDF5_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
645a75e6a4aSMatthew G. Knepley   PetscFunctionReturn(viewer);
646a75e6a4aSMatthew G. Knepley }
64736bce6e8SMatthew G. Knepley 
6485c6c1daeSBarry Smith #if defined(oldhdf4stuff)
6495c6c1daeSBarry Smith #undef __FUNCT__
6505c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerHDF5WriteSDS"
6515c6c1daeSBarry Smith PetscErrorCode  PetscViewerHDF5WriteSDS(PetscViewer viewer, float *xf, int d, int *dims,int bs)
6525c6c1daeSBarry Smith {
6535c6c1daeSBarry Smith   int              i;
6545c6c1daeSBarry Smith   PetscViewer_HDF5 *vhdf5 = (PetscViewer_HDF5*)viewer->data;
6555c6c1daeSBarry Smith   int32            sds_id,zero32[3],dims32[3];
6565c6c1daeSBarry Smith 
6575c6c1daeSBarry Smith   PetscFunctionBegin;
6585c6c1daeSBarry Smith   for (i = 0; i < d; i++) {
6595c6c1daeSBarry Smith     zero32[i] = 0;
6605c6c1daeSBarry Smith     dims32[i] = dims[i];
6615c6c1daeSBarry Smith   }
6625c6c1daeSBarry Smith   sds_id = SDcreate(vhdf5->sd_id, "Vec", DFNT_FLOAT32, d, dims32);
6635c6c1daeSBarry Smith   if (sds_id < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"SDcreate failed");
6645c6c1daeSBarry Smith   SDwritedata(sds_id, zero32, 0, dims32, xf);
6655c6c1daeSBarry Smith   SDendaccess(sds_id);
6665c6c1daeSBarry Smith   PetscFunctionReturn(0);
6675c6c1daeSBarry Smith }
6685c6c1daeSBarry Smith 
6695c6c1daeSBarry Smith #endif
670