CryptSimulationArchiver.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 CRYPTSIMULATIONARCHIVER_HPP_
00030 #define CRYPTSIMULATIONARCHIVER_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 "WntConcentration.hpp"
00041 #include "ArchiveLocationInfo.hpp"
00042 #include "ArchiveOpener.hpp"
00043 #include "FileFinder.hpp"
00044 
00053 template<unsigned DIM, class SIM>
00054 class CryptSimulationArchiver
00055 {
00056 public:
00057 
00066     static SIM* Load(const std::string& rArchiveDirectory, const double& rTimeStamp);
00067 
00080     static void Save(SIM* pSim);
00081 };
00082 
00083 template<unsigned DIM, class SIM>
00084 SIM* CryptSimulationArchiver<DIM, SIM>::Load(const std::string& rArchiveDirectory, const double& rTimeStamp)
00085 {
00094     std::ostringstream time_stamp;
00095     time_stamp << rTimeStamp;
00096     std::string archive_filename = "cell_population_sim_at_time_" + time_stamp.str() + ".arch";
00097     std::string mesh_filename = "mesh_" + time_stamp.str();
00098     FileFinder archive_dir(rArchiveDirectory + "/archive/", RelativeTo::ChasteTestOutput);
00099     ArchiveLocationInfo::SetMeshPathname(archive_dir, mesh_filename);
00100 
00101     // Create an input archive
00102     ArchiveOpener<boost::archive::text_iarchive, std::ifstream> arch_opener(archive_dir, archive_filename);
00103     boost::archive::text_iarchive* p_arch = arch_opener.GetCommonArchive();
00104 
00105     // Load any data that isn't the simulation itself, mainly singletons
00106     // - simulation time
00107     SimulationTime* p_simulation_time = SimulationTime::Instance();
00108     assert(p_simulation_time->IsStartTimeSetUp());
00109     (*p_arch) & *p_simulation_time;
00110 
00111     // - Wnt concentration (if used)
00112     bool archive_wnt;
00113     (*p_arch) & archive_wnt;
00114     if (archive_wnt)
00115     {
00116         WntConcentration<DIM>* p_wnt = WntConcentration<DIM>::Instance();
00117         (*p_arch) & *p_wnt;
00118     }
00119 
00120     // Load the simulation
00121     SIM* p_sim;
00122     (*p_arch) >> p_sim;
00123     return p_sim;
00124 }
00125 
00126 template<unsigned DIM, class SIM>
00127 void CryptSimulationArchiver<DIM, SIM>::Save(SIM* pSim)
00128 {
00129     // Get the simulation time as a string
00130     const SimulationTime* p_sim_time = SimulationTime::Instance();
00131     assert(p_sim_time->IsStartTimeSetUp());
00132     std::ostringstream time_stamp;
00133     time_stamp << p_sim_time->GetTime();
00134 
00135     // Set up folder and filename of archive
00136     FileFinder archive_dir(pSim->GetOutputDirectory() + "/archive/", RelativeTo::ChasteTestOutput);
00137     std::string archive_filename = "cell_population_sim_at_time_" + time_stamp.str() + ".arch";
00138     ArchiveLocationInfo::SetMeshFilename(std::string("mesh_") + time_stamp.str());
00139 
00140     // Create output archive
00141     ArchiveOpener<boost::archive::text_oarchive, std::ofstream> arch_opener(archive_dir, archive_filename);
00142     boost::archive::text_oarchive* p_arch = arch_opener.GetCommonArchive();
00143 
00144     // Save the simulation.  We save the time directly first to maintain its
00145     // singleton-ness on load.
00146     (*p_arch) << *p_sim_time;
00147 
00148     // Archive the Wnt concentration if it's used
00149     bool archive_wnt = WntConcentration<DIM>::Instance()->IsWntSetUp();
00150     (*p_arch) & archive_wnt;
00151     if (archive_wnt)
00152     {
00153         WntConcentration<DIM>* p_wnt = WntConcentration<DIM>::Instance();
00154         (*p_arch) & *p_wnt;
00155     }
00156 
00157     // Archive the simulation itself
00158     (*p_arch) & pSim; // const-ness would be a pain here
00159 }
00160 
00161 #endif /*CRYPTSIMULATIONARCHIVER_HPP_*/

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