Chaste Commit::1fd4e48e3990e67db148bc1bc4cf6991a0049d0c
CellBasedSimulationArchiver.hpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, 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
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF 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
58template<unsigned ELEMENT_DIM, class SIM, unsigned SPACE_DIM=ELEMENT_DIM>
60{
61public:
62
72 static SIM* Load(const std::string& rArchiveDirectory, const double& rTimeStamp);
73
86 static void Save(SIM* pSim);
87};
88
89template<unsigned ELEMENT_DIM, class SIM, unsigned SPACE_DIM>
90SIM* 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
117template<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 void SetMeshPathname(const FileFinder &rDirectory, const std::string &rFilename)
Archive * GetCommonArchive()
static SIM * Load(const std::string &rArchiveDirectory, const double &rTimeStamp)
bool IsStartTimeSetUp() const
double GetTime() const
static SimulationTime * Instance()