Chaste  Release::2017.1
CellBasedSimulationArchiver.hpp
1 /*
2 
3 Copyright (c) 2005-2017, 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 #ifndef CELLBASEDSIMULATIONARCHIVER_HPP_
37 #define CELLBASEDSIMULATIONARCHIVER_HPP_
38 
39 // Must be included before any other serialization headers
41 
42 #include <string>
43 
44 // The headers below are needed by the templated implementation, which is in this header
45 #include <fstream>
46 #include <sstream>
47 
48 #include "ArchiveLocationInfo.hpp"
49 #include "ArchiveOpener.hpp"
50 #include "FileFinder.hpp"
51 #include "SimulationTime.hpp"
52 
58 template<unsigned ELEMENT_DIM, class SIM, unsigned SPACE_DIM=ELEMENT_DIM>
60 {
61 public:
62 
72  static SIM* Load(const std::string& rArchiveDirectory, const double& rTimeStamp);
73 
86  static void Save(SIM* pSim);
87 };
88 
89 template<unsigned ELEMENT_DIM, class SIM, unsigned SPACE_DIM>
90 SIM* CellBasedSimulationArchiver<ELEMENT_DIM, SIM, SPACE_DIM>::Load(const std::string& rArchiveDirectory, const double& rTimeStamp)
91 {
100  std::ostringstream time_stamp;
101  time_stamp << rTimeStamp;
102  std::string archive_filename = "cell_population_sim_at_time_" + time_stamp.str() + ".arch";
103  std::string mesh_filename = "mesh_" + time_stamp.str();
104  FileFinder archive_dir(rArchiveDirectory + "/archive/", RelativeTo::ChasteTestOutput);
105  ArchiveLocationInfo::SetMeshPathname(archive_dir, mesh_filename);
106 
107  // Create an input archive
108  ArchiveOpener<boost::archive::text_iarchive, std::ifstream> arch_opener(archive_dir, archive_filename);
109  boost::archive::text_iarchive* p_arch = arch_opener.GetCommonArchive();
110 
111  // Load the simulation
112  SIM* p_sim;
113  (*p_arch) >> p_sim;
114  return p_sim;
115 }
116 
117 template<unsigned ELEMENT_DIM, class SIM, unsigned SPACE_DIM>
119 {
120  // Get the simulation time as a string
121  const SimulationTime* p_sim_time = SimulationTime::Instance();
122  assert(p_sim_time->IsStartTimeSetUp());
123  std::ostringstream time_stamp;
124  time_stamp << p_sim_time->GetTime();
125 
126  // Set up folder and filename of archive
127  FileFinder archive_dir(pSim->GetOutputDirectory() + "/archive/", RelativeTo::ChasteTestOutput);
128  std::string archive_filename = "cell_population_sim_at_time_" + time_stamp.str() + ".arch";
129  ArchiveLocationInfo::SetMeshFilename(std::string("mesh_") + time_stamp.str());
130 
131  // Create output archive
132  ArchiveOpener<boost::archive::text_oarchive, std::ofstream> arch_opener(archive_dir, archive_filename);
133  boost::archive::text_oarchive* p_arch = arch_opener.GetCommonArchive();
134 
135  // Archive the simulation (const-ness would be a pain here)
136  (*p_arch) & pSim;
137 }
138 
139 #endif /*CELLBASEDSIMULATIONARCHIVER_HPP_*/
static void SetMeshFilename(const std::string &rFilename)
static SimulationTime * Instance()
Archive * GetCommonArchive()
double GetTime() const
bool IsStartTimeSetUp() const
static void SetMeshPathname(const FileFinder &rDirectory, const std::string &rFilename)
static SIM * Load(const std::string &rArchiveDirectory, const double &rTimeStamp)