TissueSimulationArchiver.hpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2010
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 TISSUESIMULATIONARCHIVER_HPP_
00030 #define TISSUESIMULATIONARCHIVER_HPP_
00031 
00032 // Must be included before any other serialisation 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 "CellwiseData.hpp"
00042 #include "ArchiveLocationInfo.hpp"
00043 #include "ArchiveOpener.hpp"
00044 
00050 template<unsigned DIM, class SIM>
00051 class TissueSimulationArchiver
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* TissueSimulationArchiver<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 = "tissue_sim_at_time_" + time_stamp.str() + ".arch";
00095     std::string mesh_filename = "mesh_" + time_stamp.str();
00096     ArchiveLocationInfo::SetMeshPathname(OutputFileHandler::GetChasteTestOutputDirectory()
00097                                          + rArchiveDirectory + "/archive/", mesh_filename);
00098 
00099     // Create an input archive
00100     ArchiveOpener<boost::archive::text_iarchive, std::ifstream> arch_opener(rArchiveDirectory + "/archive/", 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     // - Wnt concentration (if used)
00110     bool archive_wnt;
00111     (*p_arch) & archive_wnt;
00112     if (archive_wnt)
00113     {
00114         WntConcentration<DIM>* p_wnt = WntConcentration<DIM>::Instance();
00115         (*p_arch) & *p_wnt;
00116     }
00117 
00118     // - CellwiseData (if used)
00119     bool archive_cellwise_data;
00120     (*p_arch) & archive_cellwise_data;
00121     if (archive_cellwise_data)
00122     {
00123         CellwiseData<DIM>* p_cellwise_data = CellwiseData<DIM>::Instance();
00124         (*p_arch) & *p_cellwise_data;
00125     }
00126 
00127     // Load the simulation
00128     SIM* p_sim;
00129     (*p_arch) >> p_sim;
00130     return p_sim;
00131 }
00132 
00133 template<unsigned DIM, class SIM>
00134 void TissueSimulationArchiver<DIM, SIM>::Save(SIM* pSim)
00135 {
00136     // Get the simulation time as a string
00137     const SimulationTime* p_sim_time = SimulationTime::Instance();
00138     assert(p_sim_time->IsStartTimeSetUp());
00139     std::ostringstream time_stamp;
00140     time_stamp << p_sim_time->GetTime();
00141 
00142     // Set up folder and filename of archive
00143     std::string archive_directory = pSim->GetOutputDirectory() + "/archive/";
00144     std::string archive_filename = "tissue_sim_at_time_" + time_stamp.str() + ".arch";
00145     ArchiveLocationInfo::SetMeshFilename(std::string("mesh_") + time_stamp.str());
00146 
00147     // Create output archive
00148     ArchiveOpener<boost::archive::text_oarchive, std::ofstream> arch_opener(archive_directory, archive_filename);
00149     boost::archive::text_oarchive* p_arch = arch_opener.GetCommonArchive();
00150 
00151     // Save the simulation.  We save the time directly first to maintain its
00152     // singleton-ness on load.
00153     (*p_arch) << *p_sim_time;
00154 
00155     // Archive the Wnt concentration if it's used
00156     bool archive_wnt = WntConcentration<DIM>::Instance()->IsWntSetUp();
00157     (*p_arch) & archive_wnt;
00158     if (archive_wnt)
00159     {
00160         WntConcentration<DIM>* p_wnt = WntConcentration<DIM>::Instance();
00161         (*p_arch) & *p_wnt;
00162     }
00163 
00164     // Archive the CellwiseData if it's used
00165     bool archive_cellwise_data = CellwiseData<DIM>::Instance()->IsSetUp();
00166     (*p_arch) & archive_cellwise_data;
00167     if (archive_cellwise_data)
00168     {
00169         CellwiseData<DIM>* p_cellwise_data = CellwiseData<DIM>::Instance();
00170         (*p_arch) & *p_cellwise_data;
00171     }
00172 
00173     // Archive the simulation itself
00174     (*p_arch) & pSim; // const-ness would be a pain here
00175 }
00176 
00177 #endif /*TISSUESIMULATIONARCHIVER_HPP_*/

Generated by  doxygen 1.6.2