Chaste Commit::675f9facbe008c5eacb9006feaeb6423206579ea
CellBasedSimulationArchiver.hpp
1/*
2
3Copyright (c) 2005-2025, 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
62template <unsigned ELEMENT_DIM, class SIM, unsigned SPACE_DIM = ELEMENT_DIM, class INPUT_ARCHIVE_TYPE = boost::archive::text_iarchive, class OUTPUT_ARCHIVE_TYPE = boost::archive::text_oarchive>
64{
65public:
75 static SIM* Load(const std::string& rArchiveDirectory, const double& rTimeStamp);
76
89 static void Save(SIM* pSim);
90};
91
92template <unsigned ELEMENT_DIM, class SIM, unsigned SPACE_DIM, class INPUT_ARCHIVE_TYPE, class OUTPUT_ARCHIVE_TYPE>
94{
103 std::ostringstream time_stamp;
104 time_stamp << rTimeStamp;
105 std::string archive_filename = "cell_population_sim_at_time_" + time_stamp.str() + ".arch";
106 std::string mesh_filename = "mesh_" + time_stamp.str();
107 FileFinder archive_dir(rArchiveDirectory + "/archive/", RelativeTo::ChasteTestOutput);
108 ArchiveLocationInfo::SetMeshPathname(archive_dir, mesh_filename);
109
110 // Create an input archive
111 ArchiveOpener<INPUT_ARCHIVE_TYPE, std::ifstream> arch_opener(archive_dir, archive_filename);
112 INPUT_ARCHIVE_TYPE* p_arch = arch_opener.GetCommonArchive();
113
114 // Load the simulation
115 SIM* p_sim;
116 (*p_arch) >> p_sim;
117 return p_sim;
118}
119
120template <unsigned ELEMENT_DIM, class SIM, unsigned SPACE_DIM, class INPUT_ARCHIVE_TYPE, class OUTPUT_ARCHIVE_TYPE>
122{
123 // Get the simulation time as a string
124 const SimulationTime* p_sim_time = SimulationTime::Instance();
125 assert(p_sim_time->IsStartTimeSetUp());
126 std::ostringstream time_stamp;
127 time_stamp << p_sim_time->GetTime();
128
129 // Set up folder and filename of archive
130 FileFinder archive_dir(pSim->GetOutputDirectory() + "/archive/", RelativeTo::ChasteTestOutput);
131 std::string archive_filename = "cell_population_sim_at_time_" + time_stamp.str() + ".arch";
132 ArchiveLocationInfo::SetMeshFilename(std::string("mesh_") + time_stamp.str());
133
134 // Create output archive
135 ArchiveOpener<OUTPUT_ARCHIVE_TYPE, std::ofstream> arch_opener(archive_dir, archive_filename);
136 OUTPUT_ARCHIVE_TYPE* p_arch = arch_opener.GetCommonArchive();
137
138 // Archive the simulation (const-ness would be a pain here)
139 (*p_arch) & pSim;
140}
141
142/*
143 * Specialization of the CellBasedSimulationArchiver for text based formats to reduce requirement to set both archive type template parameters
144 */
145template <unsigned ELEMENT_DIM, class SIM, unsigned SPACE_DIM = ELEMENT_DIM>
147
148/*
149 * Specialization of the CellBasedSimulationArchiver for binary based formats to reduce requirement to set both archive type template parameters
150 */
151template <unsigned ELEMENT_DIM, class SIM, unsigned SPACE_DIM = ELEMENT_DIM>
153
154#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()