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