CellBasedSimulationArchiver.hpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 #ifndef CELLBASEDSIMULATIONARCHIVER_HPP_
00030 #define CELLBASEDSIMULATIONARCHIVER_HPP_
00031 
00032 // Must be included before any other serialization headers
00033 #include "CheckpointArchiveTypes.hpp"
00034 
00035 #include <string>
00036 #include <iostream>
00037 
00038 #include "OutputFileHandler.hpp"
00039 #include "SimulationTime.hpp"
00040 #include "CellwiseData.hpp"
00041 #include "ArchiveLocationInfo.hpp"
00042 #include "ArchiveOpener.hpp"
00043 #include "FileFinder.hpp"
00044 
00050 template<unsigned DIM, class SIM>
00051 class CellBasedSimulationArchiver
00052 {
00053 public:
00054 
00063     static SIM* Load(const std::string& rArchiveDirectory, const double& rTimeStamp);
00064 
00077     static void Save(SIM* pSim);
00078 };
00079 
00080 
00081 template<unsigned DIM, class SIM>
00082 SIM* CellBasedSimulationArchiver<DIM, SIM>::Load(const std::string& rArchiveDirectory, const double& rTimeStamp)
00083 {
00092     std::ostringstream time_stamp;
00093     time_stamp << rTimeStamp;
00094     std::string archive_filename = "cell_population_sim_at_time_" + time_stamp.str() + ".arch";
00095     std::string mesh_filename = "mesh_" + time_stamp.str();
00096     FileFinder archive_dir(rArchiveDirectory + "/archive/", RelativeTo::ChasteTestOutput);
00097     ArchiveLocationInfo::SetMeshPathname(archive_dir, mesh_filename);
00098 
00099     // Create an input archive
00100     ArchiveOpener<boost::archive::text_iarchive, std::ifstream> arch_opener(archive_dir, archive_filename);
00101     boost::archive::text_iarchive* p_arch = arch_opener.GetCommonArchive();
00102 
00103     // Load any data that isn't the simulation itself, mainly singletons
00104     // - simulation time
00105     SimulationTime* p_simulation_time = SimulationTime::Instance();
00106     assert(p_simulation_time->IsStartTimeSetUp());
00107     (*p_arch) & *p_simulation_time;
00108 
00109     // - CellwiseData (if used)
00110     bool archive_cellwise_data;
00111     (*p_arch) & archive_cellwise_data;
00112     if (archive_cellwise_data)
00113     {
00114         CellwiseData<DIM>* p_cellwise_data = CellwiseData<DIM>::Instance();
00115         (*p_arch) & *p_cellwise_data;
00116     }
00117 
00118     // Load the simulation
00119     SIM* p_sim;
00120     (*p_arch) >> p_sim;
00121     return p_sim;
00122 }
00123 
00124 template<unsigned DIM, class SIM>
00125 void CellBasedSimulationArchiver<DIM, SIM>::Save(SIM* pSim)
00126 {
00127     // Get the simulation time as a string
00128     const SimulationTime* p_sim_time = SimulationTime::Instance();
00129     assert(p_sim_time->IsStartTimeSetUp());
00130     std::ostringstream time_stamp;
00131     time_stamp << p_sim_time->GetTime();
00132 
00133     // Set up folder and filename of archive
00134     FileFinder archive_dir(pSim->GetOutputDirectory() + "/archive/", RelativeTo::ChasteTestOutput);
00135     std::string archive_filename = "cell_population_sim_at_time_" + time_stamp.str() + ".arch";
00136     ArchiveLocationInfo::SetMeshFilename(std::string("mesh_") + time_stamp.str());
00137 
00138     // Create output archive
00139     ArchiveOpener<boost::archive::text_oarchive, std::ofstream> arch_opener(archive_dir, archive_filename);
00140     boost::archive::text_oarchive* p_arch = arch_opener.GetCommonArchive();
00141 
00142     // Save the simulation.  We save the time directly first to maintain its
00143     // singleton-ness on load.
00144     (*p_arch) << *p_sim_time;
00145 
00146     // Archive the CellwiseData if it is used
00147     bool archive_cellwise_data = CellwiseData<DIM>::Instance()->IsSetUp();
00148     (*p_arch) & archive_cellwise_data;
00149     if (archive_cellwise_data)
00150     {
00151         CellwiseData<DIM>* p_cellwise_data = CellwiseData<DIM>::Instance();
00152         (*p_arch) & *p_cellwise_data;
00153     }
00154 
00155     // Archive the simulation itself
00156     (*p_arch) & pSim; // const-ness would be a pain here
00157 }
00158 
00159 #endif /*CELLBASEDSIMULATIONARCHIVER_HPP_*/

Generated on Mon Apr 18 11:35:28 2011 for Chaste by  doxygen 1.5.5