Chaste Commit::1fd4e48e3990e67db148bc1bc4cf6991a0049d0c
AbstractHdf5Converter.cpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 * Neither the name of the University of Oxford nor the names of its
20 contributors may be used to endorse or promote products derived from this
21 software without specific prior written permission.
22
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36#include "AbstractHdf5Converter.hpp"
37#include "Version.hpp"
38
39
40/*
41 * Operator function to be called by H5Literate [HDF5 1.8.x] or H5Giterate [HDF5 1.6.x] (in TestListingDatasetsInAnHdf5File).
42 */
43herr_t op_func (hid_t loc_id,
44 const char *name,
45 const H5L_info_t *info,
46 void *operator_data);
47
48
49template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
51 const std::string& rFileBaseName,
53 const std::string& rSubdirectoryName,
54 unsigned precision)
55 : mrH5Folder(rInputDirectory),
56 mFileBaseName(rFileBaseName),
57 mOpenDatasetIndex(UNSIGNED_UNSET),
58 mpMesh(pMesh),
59 mRelativeSubdirectory(rSubdirectoryName),
60 mPrecision(precision)
61{
63
64 // Create new directory in which to store everything
66 mpOutputFileHandler = new OutputFileHandler(sub_directory, false);
67
69}
70
71template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
73{
74 // Note that we don't want the child processes to write info files
76 {
77 std::string time_info_filename;
78
79 // If the dataset is just "Data" then we will leave the original filename as it is (to avoid confusion!)
80 // If the dataset is a new variant like "Postprocessing" then we will put the dataset name in the output.
81 if (mDatasetNames[mOpenDatasetIndex]=="Data")
82 {
83 time_info_filename = mFileBaseName + "_times.info";
84 }
85 else
86 {
87 time_info_filename = mDatasetNames[mOpenDatasetIndex] + "_times.info";
88 }
89 out_stream p_file = mpOutputFileHandler->OpenOutputFile(time_info_filename);
90
91 std::vector<double> time_values = mpReader->GetUnlimitedDimensionValues();
92 unsigned num_timesteps = time_values.size();
93 double first_timestep = time_values.front();
94 double last_timestep = time_values.back();
95
96 double timestep = num_timesteps > 1 ? time_values[1] - time_values[0] : DOUBLE_UNSET;
97
98 *p_file << "Number of timesteps " << num_timesteps << std::endl;
99 *p_file << "timestep " << timestep << std::endl;
100 *p_file << "First timestep " << first_timestep << std::endl;
101 *p_file << "Last timestep " << last_timestep << std::endl;
103
104 p_file->close();
105 }
106}
107
108template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
113
114template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
116{
117 return mRelativeSubdirectory;
118}
119
120template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
122{
123 // If we are already at the end just return false.
124 if (mDatasetNames.size() == mOpenDatasetIndex+1u)
125 {
126 return false;
127 }
128
129 // If we haven't read anything yet, start at the beginning, otherwise increment by one.
130 if (mOpenDatasetIndex==UNSIGNED_UNSET)
131 {
132 mOpenDatasetIndex = 0u;
133 }
134 else
135 {
136 mOpenDatasetIndex++;
137 }
138
139 // Store directory, mesh and filenames and create the reader
140 mpReader.reset(new Hdf5DataReader(mrH5Folder, mFileBaseName, mDatasetNames[mOpenDatasetIndex]));
141
142 // Check the data file for basic validity
143 std::vector<std::string> variable_names = mpReader->GetVariableNames();
144 mNumVariables = variable_names.size();
145
146 if (mpReader->GetNumberOfRows() != mpMesh->GetNumNodes())
147 {
148 delete mpOutputFileHandler;
149 EXCEPTION("Mesh and HDF5 file have a different number of nodes");
150 }
151 WriteInfoFile();
152
153 return true;
154}
155
156template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
158 const std::string& rFileName)
159{
160 /*
161 * Open file.
162 */
163 std::string file_name = rH5Folder.GetAbsolutePath() + rFileName + ".h5";
164 hid_t file = H5Fopen(file_name.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
165
166 /*
167 * Begin HDF5 iteration, calls a method that populates mDatasetNames.
168 */
169 H5Literate(file, H5_INDEX_NAME, H5_ITER_NATIVE, nullptr, op_func, &mDatasetNames);
170
171 H5Fclose(file);
172
173 // Remove datasets that end in "_Unlimited", as these are paired up with other ones!
174 std::string ending = "_Unlimited";
175
176 // Strip off the independent variables from the list
177 std::vector<std::string>::iterator iter;
178 for (iter = mDatasetNames.begin(); iter != mDatasetNames.end(); )
179 {
180 // If the dataset name is "Time" OR ...
181 // it is longer than the ending we are looking for ("_Unlimited") ...
182 // ... AND it ends with the string we are looking for,
183 // then erase it.
184 if ((*(iter) == "Time") ||
185 ((iter->length() > ending.length()) &&
186 (0 == iter->compare(iter->length() - ending.length(), ending.length(), ending))))
187 {
188 iter = mDatasetNames.erase(iter);
189 }
190 else
191 {
192 ++iter;
193 }
194 }
195}
196
197/*
198 * HDF5 Operator function.
199 *
200 * Puts the name of the objects (in this case 'datasets')
201 * in an HDF5 file into a std::vector for us to use for
202 * iterating over the file.
203 *
204 * This was based on a couple of HDF5 example files.
205 */
206herr_t op_func (hid_t loc_id, const char *name,
207 const H5L_info_t *info,
208 void *operator_data)
209{
210 std::vector<std::string>* p_dataset_names = static_cast<std::vector< std::string > * >(operator_data);
211
212 /*
213 * Get type of the object and display its name and type.
214 * The name of the object is passed to this function by
215 * the Library.
216 */
217 H5O_info_t infobuf;
218 H5Oget_info_by_name (loc_id, name, &infobuf, H5P_DEFAULT);
219 switch (infobuf.type)
220 {
221// case H5O_TYPE_GROUP:
222// printf (" Group: %s\n", name);
223// break;
224 case H5O_TYPE_DATASET:
225 p_dataset_names->push_back(name);
226 break;
227// case H5O_TYPE_NAMED_DATATYPE:
228// printf (" Datatype: %s\n", name);
229// break;
230 default:
232 // If you ever do reach here, it means that an HDF5 file you are trying to convert contains
233 // something other than a 'Dataset', which is the usual data structure we write out in Chaste.
234 // The above commented out lines should help you figure out what it is, and how it got there.
235 }
236 return 0;
237}
238
239// Explicit instantiation
240template class AbstractHdf5Converter<1,1>;
241template class AbstractHdf5Converter<1,2>;
242template class AbstractHdf5Converter<2,2>;
243template class AbstractHdf5Converter<1,3>;
244template class AbstractHdf5Converter<2,3>;
245template class AbstractHdf5Converter<3,3>;
const double DOUBLE_UNSET
Definition Exception.hpp:57
#define EXCEPTION(message)
#define NEVER_REACHED
const unsigned UNSIGNED_UNSET
Definition Exception.hpp:53
OutputFileHandler * mpOutputFileHandler
AbstractHdf5Converter(const FileFinder &rInputDirectory, const std::string &rFileBaseName, AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > *pMesh, const std::string &rSubdirectoryName, unsigned precision)
void GenerateListOfDatasets(const FileFinder &rH5Folder, const std::string &rFileName)
static std::string GetProvenanceString()
std::string GetAbsolutePath() const
static bool AmMaster()