Chaste  Release::3.4
CellBasedSimulationArchiver.hpp
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 #ifndef CELLBASEDSIMULATIONARCHIVER_HPP_
37 #define CELLBASEDSIMULATIONARCHIVER_HPP_
38 
39 // Must be included before any other serialization headers
41 
42 #include <string>
43 #include <iostream>
44 
45 #include "OutputFileHandler.hpp"
46 #include "SimulationTime.hpp"
47 #include "ArchiveLocationInfo.hpp"
48 #include "ArchiveOpener.hpp"
49 #include "FileFinder.hpp"
50 
56 template<unsigned ELEMENT_DIM, class SIM, unsigned SPACE_DIM=ELEMENT_DIM>
58 {
59 public:
60 
70  static SIM* Load(const std::string& rArchiveDirectory, const double& rTimeStamp);
71 
84  static void Save(SIM* pSim);
85 };
86 
87 template<unsigned ELEMENT_DIM, class SIM, unsigned SPACE_DIM>
88 SIM* CellBasedSimulationArchiver<ELEMENT_DIM, SIM, SPACE_DIM>::Load(const std::string& rArchiveDirectory, const double& rTimeStamp)
89 {
98  std::ostringstream time_stamp;
99  time_stamp << rTimeStamp;
100  std::string archive_filename = "cell_population_sim_at_time_" + time_stamp.str() + ".arch";
101  std::string mesh_filename = "mesh_" + time_stamp.str();
102  FileFinder archive_dir(rArchiveDirectory + "/archive/", RelativeTo::ChasteTestOutput);
103  ArchiveLocationInfo::SetMeshPathname(archive_dir, mesh_filename);
104 
105  // Create an input archive
106  ArchiveOpener<boost::archive::text_iarchive, std::ifstream> arch_opener(archive_dir, archive_filename);
107  boost::archive::text_iarchive* p_arch = arch_opener.GetCommonArchive();
108 
109  // Load the simulation
110  SIM* p_sim;
111  (*p_arch) >> p_sim;
112  return p_sim;
113 }
114 
115 template<unsigned ELEMENT_DIM, class SIM, unsigned SPACE_DIM>
117 {
118  // Get the simulation time as a string
119  const SimulationTime* p_sim_time = SimulationTime::Instance();
120  assert(p_sim_time->IsStartTimeSetUp());
121  std::ostringstream time_stamp;
122  time_stamp << p_sim_time->GetTime();
123 
124  // Set up folder and filename of archive
125  FileFinder archive_dir(pSim->GetOutputDirectory() + "/archive/", RelativeTo::ChasteTestOutput);
126  std::string archive_filename = "cell_population_sim_at_time_" + time_stamp.str() + ".arch";
127  ArchiveLocationInfo::SetMeshFilename(std::string("mesh_") + time_stamp.str());
128 
129  // Create output archive
130  ArchiveOpener<boost::archive::text_oarchive, std::ofstream> arch_opener(archive_dir, archive_filename);
131  boost::archive::text_oarchive* p_arch = arch_opener.GetCommonArchive();
132 
133  // Archive the simulation (const-ness would be a pain here)
134  (*p_arch) & pSim;
135 }
136 
137 #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)