VPTissue Reference Manual
Hdf5File.h
Go to the documentation of this file.
1 #ifndef HDF5FILE_H_INCLUDED
2 #define HDF5FILE_H_INCLUDED
3 /*
4  * Copyright 2011-2016 Universiteit Antwerpen
5  *
6  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
7  * the European Commission - subsequent versions of the EUPL (the "Licence");
8  * You may not use this work except in compliance with the Licence.
9  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the Licence is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the Licence for the specific language governing
15  * permissions and limitations under the Licence.
16  */
22 #include "sim/SimState.h"
23 
24 #ifdef USE_HDF5
25 #include <list>
26 #include <string>
27 #include <vector>
28 #include <hdf5.h>
29 #include <boost/property_tree/ptree.hpp>
30 
31 namespace SimPT_Sim {
32  class Sim;
33 }
34 
35 namespace SimPT_Shell {
36 
38 class Hdf5File
39 {
40 public:
43  Hdf5File();
44 
48  Hdf5File(const std::string& file_path);
49 
51  virtual ~Hdf5File();
52 
57  bool Close();
58 
60  static std::string GetFileExtension();
61 
63  std::string GetFilePath();
64 
66  static bool HDF5Available() { return true; }
67 
70  bool IsOpen();
71 
75  bool Open(const std::string& file_path);
76 
77  // TODO: document!
78  SimPT_Sim::SimState Read(int step_idx = -1);
79 
82  std::vector<std::pair<int, double>> TimeSteps();
83 
87  void Write(std::shared_ptr<const SimPT_Sim::Sim> sim);
88 
89 private:
91  void ReadBPNodes(hid_t loc_id, SimPT_Sim::MeshState& mstate);
92 
94  void ReadBPWalls(hid_t loc_id, SimPT_Sim::MeshState& mstate);
95 
97  void ReadCells(hid_t loc_id, SimPT_Sim::MeshState& mstate);
98 
100  void ReadNodes(hid_t loc_id, SimPT_Sim::MeshState& mstate);
101 
103  boost::property_tree::ptree ReadParameters(hid_t loc_id);
104 
107  boost::property_tree::ptree ReadPtreeFromStringAttribute(hid_t loc_id, const std::string& attr_name);
108 
110  void ReadWalls(hid_t loc_id, SimPT_Sim::MeshState& mstate);
111 
112  void WriteBPNodes(hid_t loc_id, const SimPT_Sim::MeshState& mstate);
113 
114  void WriteBPWalls(hid_t loc_id, const SimPT_Sim::MeshState& mstate);
115 
124  void WriteCells(hid_t loc_id, const SimPT_Sim::MeshState& mstate);
125 
134  void WriteMesh(hid_t loc_id, const SimPT_Sim::MeshState& mstate);
135 
144  void WriteNodes(hid_t loc_id, const SimPT_Sim::MeshState& mstate);
145 
153  void WriteParameters(hid_t loc_id, const boost::property_tree::ptree& params);
154 
163  void WritePtreeToStringAttribute(hid_t loc_id, const std::string& attr_name, boost::property_tree::ptree const& pt);
164 
170  void WriteState(const SimPT_Sim::SimState& s);
171 
180  void WriteWalls(hid_t loc_id, const SimPT_Sim::MeshState& mstate);
181 
182 private:
183  hid_t m_file_id;
184  std::string m_file_path;
185 };
186 
187 } // end namespace VirtualLeaf
188 
189 // Fallback code in case libhdf5 is not available on this platform
190 #else
191 
192 #include "bio/Mesh.h"
193 #include <iostream>
194 #include <string>
195 
196 #pragma GCC diagnostic ignored "-Wunused-parameter"
197 
198 namespace SimPT_Sim {
199  class Sim;
200 }
201 
202 namespace SimPT_Shell {
203 
204 class Hdf5File
205 {
206 public:
207  Hdf5File()
208  {
209  std::cout << "[HDF5] Warning: HDF5 must be enabled at compile time." << std::endl;
210  }
211  Hdf5File(std::string const&)
212  {
213  std::cout << "[HDF5] Warning: HDF5 must be enabled at compile time." << std::endl;
214  }
215  virtual ~Hdf5File() {}
216 
217  bool Close() { return true; }
218 
219  std::string static GetFileExtension() {return "h5";}
220 
221  std::string GetFilePath() {return "";}
222 
223  static bool HDF5Available() { return false; }
224 
225  bool IsOpen() { return true; }
226 
227  bool Open(const std::string& ) { return true; }
228 
229  void Write(std::shared_ptr<const SimPT_Sim::Sim> ) {}
230 
231  SimPT_Sim::SimState Read(int step = -1) {return SimPT_Sim::SimState(); }
232 
233  std::vector<std::pair<int, double>> TimeSteps() { return {}; }
234 };
235 
236 } // namespace
237 
238 #endif /* USE_HDF5 */
239 
240 #endif // end_of_include guard
241 
void Write(std::shared_ptr< const SimPT_Sim::Sim > sim)
Writes the mesh state to the HDF5 file currently associated with exporter object. ...
Definition: Hdf5File.cpp:1134
Namespace for SimPT shell package.
Definition: Client.cpp:50
Namespace for the core simulator.
Contains facilities for storing the simulation Mesh to HDF5 files.
Definition: Hdf5File.h:38
Header for SimState.
std::vector< std::pair< int, double > > TimeSteps()
Reads the time steps contained in the HDF5 file.
Definition: Hdf5File.cpp:1096
static bool HDF5Available()
To avoid littering the code with preprocessor conditionals.
Definition: Hdf5File.h:66
bool IsOpen()
Returns true if exporter is associated with a HDF5 file that has been successfully opened...
Definition: Hdf5File.cpp:339
virtual ~Hdf5File()
Cleans up HDF5 storage: makes sure files are closed properly.
Definition: Hdf5File.cpp:311
bool Close()
Closes the HDF5 file associated with exporter, dissociates it from this exporter object and returns t...
Definition: Hdf5File.cpp:318
Contains the state of the whole Simulator at a given simulation step.
Definition: SimState.h:33
bool Open(const std::string &file_path)
Creates an HDF5 file and binds the exporter object to it.
Definition: Hdf5File.cpp:344
Contains the state of the whole Mesh at a given simulation step.
Definition: MeshState.h:41
Hdf5File()
Creates an empty HDF5 mesh exporter with no file association.
Definition: Hdf5File.cpp:301
std::string GetFilePath()
Return file path for HDF5 file associated with the exporter object, if any.
Definition: Hdf5File.cpp:334
static std::string GetFileExtension()
Return file extension for HDF5 format.
Definition: Hdf5File.cpp:329
Interface for Mesh.