VPTissue Reference Manual
StartupFileHdf5.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
21 
22 #include "session/SimSession.h"
23 #include "fileformats/Hdf5File.h"
24 #include "util/misc/Exception.h"
26 
27 namespace SimPT_Shell {
28 namespace Ws {
29 
30 using namespace std;
31 using namespace boost::property_tree;
32 using namespace SimPT_Sim::Util;
33 using namespace SimShell::Ws;
34 
36  : StartupFileBase(path),
37  m_reverse_index_initialized(false)
38 {
39 }
40 
41 shared_ptr<SimShell::Session::ISession>
42 StartupFileHdf5::CreateSession(shared_ptr<IProject> proj, std::shared_ptr<IWorkspace> ws) const
43 {
44  shared_ptr<Session::ISession> session;
45  auto prefs = MergedPreferences::Create(ws, proj);
46  try {
47  Hdf5File tissue_file(m_path);
48  // may throw
49  SimPT_Sim::SimState sim_state = tissue_file.Read();
50  session = make_shared<Session::SimSession>(prefs, sim_state);
51  }
52  catch (exception& e) {
53  throw Exception("Could not open \"" + m_path + "\": " + e.what());
54  }
55 
56  return session;
57 }
58 
60 {
61  if (!m_reverse_index_initialized)
62  InitializeReverseIndex();
63  try {
64  Hdf5File tissue_file(m_path);
65  SimPT_Sim::SimState result = tissue_file.Read(m_reverse_index[timestep]);
66  tissue_file.Close();
67  return result;
68  } catch (exception& e) {
69  throw Exception("GetSimState(): Could not open \"" + m_path + "\": " + e.what());
70  }
71 }
72 
73 vector<int> StartupFileHdf5::GetTimeSteps() const
74 {
75  vector<int> result;
76  try {
77  Hdf5File tissue_file(m_path);
78  for (auto& pair : tissue_file.TimeSteps()) {
79  result.push_back(pair.first);
80  }
81  } catch (exception& e) {
82  throw Exception("GetTimeSteps(): Could not open \"" + m_path + "\": " + e.what());
83  }
84  return result;
85 }
86 
87 void StartupFileHdf5::InitializeReverseIndex() const
88 {
89  int i = 0;
90  for (int t : GetTimeSteps()) {
91  m_reverse_index[t] = i++;
92  }
93  m_reverse_index_initialized = true;
94 }
95 
96 } // namespace
97 } // namespace
98 
Interface for Hdf5 tissue file format.
STL namespace.
Namespace for miscellaneous utilities.
Definition: PTreeFile.cpp:44
virtual std::vector< int > GetTimeSteps() const
Namespace for SimPT shell package.
Definition: Client.cpp:50
StartupFileHdf5(const std::string &path)
Constructor.
Extremely simple Exception root class.
Definition: Exception.h:28
Interface for VirtualLeaf::Hdf5File.
Contains facilities for storing the simulation Mesh to HDF5 files.
Definition: Hdf5File.h:38
std::vector< std::pair< int, double > > TimeSteps()
Reads the time steps contained in the HDF5 file.
Definition: Hdf5File.cpp:1096
Namespace for generic workspace classes.
Definition: SimSession.h:32
virtual std::shared_ptr< SimShell::Session::ISession > CreateSession(std::shared_ptr< SimShell::Ws::IProject > proj, std::shared_ptr< SimShell::Ws::IWorkspace > ws) const
Interface for MergedPreferences.
bool Close()
Closes the HDF5 file associated with exporter, dissociates it from this exporter object and returns t...
Definition: Hdf5File.cpp:318
Interfaces for simulator session.
Contains the state of the whole Simulator at a given simulation step.
Definition: SimState.h:33
virtual SimPT_Sim::SimState GetSimState(int timestep) const
Base class representing the file types used to initiate a session.
Header file for Exception class.