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 "ArchiveLocationInfo.hpp"
00041 #include "ArchiveOpener.hpp"
00042 #include "FileFinder.hpp"
00043 
00049 template<unsigned DIM, class SIM>
00050 class CellBasedSimulationArchiver
00051 {
00052 public:
00053 
00062     static SIM* Load(const std::string& rArchiveDirectory, const double& rTimeStamp);
00063 
00076     static void Save(SIM* pSim);
00077 };
00078 
00079 
00080 template<unsigned DIM, class SIM>
00081 SIM* CellBasedSimulationArchiver<DIM, SIM>::Load(const std::string& rArchiveDirectory, const double& rTimeStamp)
00082 {
00091     std::ostringstream time_stamp;
00092     time_stamp << rTimeStamp;
00093     std::string archive_filename = "cell_population_sim_at_time_" + time_stamp.str() + ".arch";
00094     std::string mesh_filename = "mesh_" + time_stamp.str();
00095     FileFinder archive_dir(rArchiveDirectory + "/archive/", RelativeTo::ChasteTestOutput);
00096     ArchiveLocationInfo::SetMeshPathname(archive_dir, mesh_filename);
00097 
00098     // Create an input archive
00099     ArchiveOpener<boost::archive::text_iarchive, std::ifstream> arch_opener(archive_dir, archive_filename);
00100     boost::archive::text_iarchive* p_arch = arch_opener.GetCommonArchive();
00101 
00102     // Load the simulation
00103     SIM* p_sim;
00104     (*p_arch) >> p_sim;
00105     return p_sim;
00106 }
00107 
00108 template<unsigned DIM, class SIM>
00109 void CellBasedSimulationArchiver<DIM, SIM>::Save(SIM* pSim)
00110 {
00111     // Get the simulation time as a string
00112     const SimulationTime* p_sim_time = SimulationTime::Instance();
00113     assert(p_sim_time->IsStartTimeSetUp());
00114     std::ostringstream time_stamp;
00115     time_stamp << p_sim_time->GetTime();
00116 
00117     // Set up folder and filename of archive
00118     FileFinder archive_dir(pSim->GetOutputDirectory() + "/archive/", RelativeTo::ChasteTestOutput);
00119     std::string archive_filename = "cell_population_sim_at_time_" + time_stamp.str() + ".arch";
00120     ArchiveLocationInfo::SetMeshFilename(std::string("mesh_") + time_stamp.str());
00121 
00122     // Create output archive
00123     ArchiveOpener<boost::archive::text_oarchive, std::ofstream> arch_opener(archive_dir, archive_filename);
00124     boost::archive::text_oarchive* p_arch = arch_opener.GetCommonArchive();
00125 
00126     // Archive the simulation (const-ness would be a pain here)
00127     (*p_arch) & pSim;
00128 }
00129 
00130 #endif /*CELLBASEDSIMULATIONARCHIVER_HPP_*/
Generated on Thu Dec 22 13:00:05 2011 for Chaste by  doxygen 1.6.3