Chaste Release::3.1
CellBasedSimulationArchiver.hpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #ifndef CELLBASEDSIMULATIONARCHIVER_HPP_
00037 #define CELLBASEDSIMULATIONARCHIVER_HPP_
00038 
00039 // Must be included before any other serialization headers
00040 #include "CheckpointArchiveTypes.hpp"
00041 
00042 #include <string>
00043 #include <iostream>
00044 
00045 #include "OutputFileHandler.hpp"
00046 #include "SimulationTime.hpp"
00047 #include "ArchiveLocationInfo.hpp"
00048 #include "ArchiveOpener.hpp"
00049 #include "FileFinder.hpp"
00050 
00056 template<unsigned DIM, class SIM>
00057 class CellBasedSimulationArchiver
00058 {
00059 public:
00060 
00069     static SIM* Load(const std::string& rArchiveDirectory, const double& rTimeStamp);
00070 
00083     static void Save(SIM* pSim);
00084 };
00085 
00086 
00087 template<unsigned DIM, class SIM>
00088 SIM* CellBasedSimulationArchiver<DIM, SIM>::Load(const std::string& rArchiveDirectory, const double& rTimeStamp)
00089 {
00098     std::ostringstream time_stamp;
00099     time_stamp << rTimeStamp;
00100     std::string archive_filename = "cell_population_sim_at_time_" + time_stamp.str() + ".arch";
00101     std::string mesh_filename = "mesh_" + time_stamp.str();
00102     FileFinder archive_dir(rArchiveDirectory + "/archive/", RelativeTo::ChasteTestOutput);
00103     ArchiveLocationInfo::SetMeshPathname(archive_dir, mesh_filename);
00104 
00105     // Create an input archive
00106     ArchiveOpener<boost::archive::text_iarchive, std::ifstream> arch_opener(archive_dir, archive_filename);
00107     boost::archive::text_iarchive* p_arch = arch_opener.GetCommonArchive();
00108 
00109     // Load the simulation
00110     SIM* p_sim;
00111     (*p_arch) >> p_sim;
00112     return p_sim;
00113 }
00114 
00115 template<unsigned DIM, class SIM>
00116 void CellBasedSimulationArchiver<DIM, SIM>::Save(SIM* pSim)
00117 {
00118     // Get the simulation time as a string
00119     const SimulationTime* p_sim_time = SimulationTime::Instance();
00120     assert(p_sim_time->IsStartTimeSetUp());
00121     std::ostringstream time_stamp;
00122     time_stamp << p_sim_time->GetTime();
00123 
00124     // Set up folder and filename of archive
00125     FileFinder archive_dir(pSim->GetOutputDirectory() + "/archive/", RelativeTo::ChasteTestOutput);
00126     std::string archive_filename = "cell_population_sim_at_time_" + time_stamp.str() + ".arch";
00127     ArchiveLocationInfo::SetMeshFilename(std::string("mesh_") + time_stamp.str());
00128 
00129     // Create output archive
00130     ArchiveOpener<boost::archive::text_oarchive, std::ofstream> arch_opener(archive_dir, archive_filename);
00131     boost::archive::text_oarchive* p_arch = arch_opener.GetCommonArchive();
00132 
00133     // Archive the simulation (const-ness would be a pain here)
00134     (*p_arch) & pSim;
00135 }
00136 
00137 #endif /*CELLBASEDSIMULATIONARCHIVER_HPP_*/