Nektar++
Loading...
Searching...
No Matches
PtsIOHdf5.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2//
3// File: PtsIOHdf5.h
4//
5// For more information, please see: http://www.nektar.info/
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: I/O routines for points in HDF5 format
32//
33////////////////////////////////////////////////////////////////////////////////
34
35#ifndef NEKTAR_LIB_UTILITIES_BASIC_UTILS_PTSIOHDF5_H
36#define NEKTAR_LIB_UTILITIES_BASIC_UTILS_PTSIOHDF5_H
37
41
42#include <string>
43#include <vector>
44
46{
47
48class PtsIOHdf5;
49
50typedef std::shared_ptr<PtsIOHdf5> PtsIOHdf5SharedPtr;
51
52/**
53 * @brief Writes a time series of point data to a HDF5 file.
54 *
55 * The file is laid out as
56 *
57 * NEKTAR/
58 * COORDINATES/
59 * x - dataset, [npoints]
60 * y - dataset, [npoints]
61 * z - dataset, [npoints]
62 * TIME-DATA/
63 * <field1> - dataset, [ntimes x (npoints + 1)]
64 * ...
65 * <fieldN> - dataset, [ntimes x (npoints + 1)]
66 *
67 * The coordinates are fixed for the lifetime of the file and are written
68 * once. Each field is a single two-dimensional dataset, extensible along the
69 * time dimension, whose first column holds the time and whose remaining
70 * columns hold that field's value at each point. Storing the time in the
71 * datasets themselves means a row is self-describing and the whole time
72 * history of a field can be read with a single call.
73 */
75{
76public:
77 /// Opens @p fileName and prepares it to receive the fields described by
78 /// @p ptsField, discarding any data at or after @p time. @p expectedRows
79 /// is an optional hint at how many times will be written, used only to
80 /// choose a chunk size; zero means it is not known.
82 const std::string &fileName,
83 const NekDouble time,
84 const PtsFieldSharedPtr &ptsField,
85 const size_t expectedRows = 0);
86
88
89 /// Appends the field values held by @p ptsField at time @p time.
90 LIB_UTILITIES_EXPORT void Write(const PtsFieldSharedPtr &ptsField,
91 const NekDouble time);
92
93 /// Flushes and closes the file. Called by the destructor.
95
96private:
97 /// Target size of a single HDF5 chunk, in bytes. Chunks much smaller than
98 /// this make the chunk index dominate the file; chunks much larger risk
99 /// exceeding the default chunk cache.
100 static constexpr size_t TargetChunkBytes = 64 * 1024;
101
102 /// Fewest time rows in a chunk. A chunk holding only a handful of rows
103 /// costs more in index entries than it saves in allocated space.
104 static constexpr size_t MinChunkRows = 64;
105
106 size_t ChunkRows(const size_t rowLength, const size_t expectedRows) const;
108 const hsize_t row) const;
109 hsize_t FindRestartRow(const H5::DataSetSharedPtr &dataSet,
110 const hsize_t nRows, const NekDouble time) const;
111
113
114 /// Name of file
115 const std::string m_fileName;
116
117 /// True on the process that owns the file
118 bool m_isRoot = false;
119
120 /// Number of points, fixed when the file is opened
121 size_t m_nPts = 0;
122
123 /// Index of the next row to be written
124 hsize_t m_row = 0;
125
128 std::vector<H5::DataSetSharedPtr> m_fieldData;
129
130 /// Scratch space for one row: time followed by one value per point
131 std::vector<NekDouble> m_rowBuffer;
132};
133
134} // namespace Nektar::LibUtilities
135
136#endif
#define LIB_UTILITIES_EXPORT
Writes a time series of point data to a HDF5 file.
Definition PtsIOHdf5.h:75
bool m_isRoot
True on the process that owns the file.
Definition PtsIOHdf5.h:118
static constexpr size_t TargetChunkBytes
Target size of a single HDF5 chunk, in bytes. Chunks much smaller than this make the chunk index domi...
Definition PtsIOHdf5.h:100
std::vector< H5::DataSetSharedPtr > m_fieldData
Definition PtsIOHdf5.h:128
void Write(const PtsFieldSharedPtr &ptsField, const NekDouble time)
Appends the field values held by ptsField at time time.
void Close()
Flushes and closes the file. Called by the destructor.
LibUtilities::CommSharedPtr m_comm
Definition PtsIOHdf5.h:112
hsize_t FindRestartRow(const H5::DataSetSharedPtr &dataSet, const hsize_t nRows, const NekDouble time) const
Index of the first row whose time is at or after time.
static constexpr size_t MinChunkRows
Fewest time rows in a chunk. A chunk holding only a handful of rows costs more in index entries than ...
Definition PtsIOHdf5.h:104
hsize_t m_row
Index of the next row to be written.
Definition PtsIOHdf5.h:124
NekDouble ReadTime(const H5::DataSetSharedPtr &dataSet, const hsize_t row) const
Reads the time stored in the first column of row row.
const std::string m_fileName
Name of file.
Definition PtsIOHdf5.h:115
std::vector< NekDouble > m_rowBuffer
Scratch space for one row: time followed by one value per point.
Definition PtsIOHdf5.h:131
size_t ChunkRows(const size_t rowLength, const size_t expectedRows) const
Number of time rows in a single chunk.
size_t m_nPts
Number of points, fixed when the file is opened.
Definition PtsIOHdf5.h:121
H5::GroupSharedPtr m_timeGroup
Definition PtsIOHdf5.h:127
std::shared_ptr< File > FileSharedPtr
Definition H5.h:88
std::shared_ptr< Group > GroupSharedPtr
Definition FieldIOHdf5.h:48
std::shared_ptr< DataSet > DataSetSharedPtr
Definition H5.h:90
std::shared_ptr< PtsField > PtsFieldSharedPtr
Definition PtsField.h:184
std::shared_ptr< PtsIOHdf5 > PtsIOHdf5SharedPtr
Definition PtsIOHdf5.h:50
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition Comm.h:62