TissueSimulationArchiver.hpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2009
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 #include <climits> // work around boost bug
00033 
00034 // Must be included before any other serialisation headers
00035 #include <boost/archive/text_oarchive.hpp>
00036 #include <boost/archive/text_iarchive.hpp>
00037 
00038 #include <string>
00039 #include <iostream>
00040 
00041 #include "OutputFileHandler.hpp"
00042 #include "SimulationTime.hpp"
00043 #include "WntConcentration.hpp"
00044 #include "CellwiseData.hpp"
00045 #include "ArchiveLocationInfo.hpp"
00046 
00052 template<unsigned DIM, class SIM>
00053 class TissueSimulationArchiver
00054 {
00055 public:
00056 
00065     static SIM* Load(const std::string& rArchiveDirectory, const double& rTimeStamp);
00066 
00079     static void Save(SIM* pSim);
00080 
00081 private:
00082 
00096     static std::string GetArchivePathname(const std::string& rArchiveDirectory, const double& rTimeStamp);
00097 };
00098 
00099 
00100 template<unsigned DIM, class SIM>
00101 std::string TissueSimulationArchiver<DIM, SIM>::GetArchivePathname(const std::string& rArchiveDirectory, const double& rTimeStamp)
00102 {
00103     // Find the right archive and mesh to load
00104     std::ostringstream time_stamp;
00105     time_stamp << rTimeStamp;
00106 
00107     std::string test_output_directory = OutputFileHandler::GetChasteTestOutputDirectory();
00108 
00109     std::string archive_filename = test_output_directory + rArchiveDirectory + "/archive/tissue_sim_at_time_"+time_stamp.str() +".arch";
00110     std::string mesh_filename = "mesh_" + time_stamp.str();
00111     ArchiveLocationInfo::SetMeshPathname(test_output_directory + rArchiveDirectory + "/archive/", mesh_filename);
00112     return archive_filename;
00113 }
00114 
00115 template<unsigned DIM, class SIM>
00116 SIM* TissueSimulationArchiver<DIM, SIM>::Load(const std::string& rArchiveDirectory, const double& rTimeStamp)
00117 {
00118     std::string archive_filename = TissueSimulationArchiver<DIM, SIM>::GetArchivePathname(rArchiveDirectory, rTimeStamp);
00119 
00120     // Create an input archive
00121     std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
00122     boost::archive::text_iarchive input_arch(ifs);
00123 
00124     // Load any data that isn't the simulation itself, mainly singletons
00125     // - simulation time
00126     SimulationTime *p_simulation_time = SimulationTime::Instance();
00127     assert(p_simulation_time->IsStartTimeSetUp());
00128     input_arch & *p_simulation_time;
00129 
00130     // - Wnt concentration (if used)
00131     bool archive_wnt;
00132     input_arch & archive_wnt;
00133     if (archive_wnt)
00134     {
00135         WntConcentration<DIM> *p_wnt = WntConcentration<DIM>::Instance();
00136         input_arch & *p_wnt;
00137     }
00138 
00139     // - CellwiseData (if used)
00140     bool archive_cellwise_data;
00141     input_arch & archive_cellwise_data;
00142     if (archive_cellwise_data)
00143     {
00144         CellwiseData<DIM> *p_cellwise_data = CellwiseData<DIM>::Instance();
00145         input_arch & *p_cellwise_data;
00146     }
00147 
00148     // Load the simulation
00149     SIM *p_sim;
00150     input_arch >> p_sim;
00151     return p_sim;
00152 }
00153 
00154 template<unsigned DIM, class SIM>
00155 void TissueSimulationArchiver<DIM, SIM>::Save(SIM* pSim)
00156 {
00157     // Get the simulation time as a string
00158     const SimulationTime *p_sim_time = SimulationTime::Instance();
00159     assert(p_sim_time->IsStartTimeSetUp());
00160     std::ostringstream time_stamp;
00161     time_stamp << p_sim_time->GetTime();
00162 
00163     // Create an output file handler in order to get the full path of the
00164     // archive directory.  Note the false is so the handler doesn't clean
00165     // the directory.
00166     std::string archive_directory = pSim->GetOutputDirectory() + "/archive/";
00167     OutputFileHandler handler(archive_directory, false);
00168     ArchiveLocationInfo::SetArchiveDirectory(handler.GetOutputDirectoryFullPath());
00169     std::string archive_filename = handler.GetOutputDirectoryFullPath() + "tissue_sim_at_time_" + time_stamp.str() + ".arch";
00170     ArchiveLocationInfo::SetMeshFilename(std::string("mesh_") + time_stamp.str());
00171 
00172     // Create a new archive
00173     std::ofstream ofs(archive_filename.c_str());
00174     boost::archive::text_oarchive output_arch(ofs);
00175 
00176     // Save the simulation.  We save the time directly first to maintain its
00177     // singleton-ness on load.
00178     output_arch << *p_sim_time;
00179 
00180     // Archive the Wnt concentration if it's used
00181     bool archive_wnt = WntConcentration<DIM>::Instance()->IsWntSetUp();
00182     output_arch & archive_wnt;
00183     if (archive_wnt)
00184     {
00185         WntConcentration<DIM> *p_wnt = WntConcentration<DIM>::Instance();
00186         output_arch & *p_wnt;
00187     }
00188 
00189     // Archive the CellwiseData if it's used
00190     bool archive_cellwise_data = CellwiseData<DIM>::Instance()->IsSetUp();
00191     output_arch & archive_cellwise_data;
00192     if (archive_cellwise_data)
00193     {
00194         CellwiseData<DIM> *p_cellwise_data = CellwiseData<DIM>::Instance();
00195         output_arch & *p_cellwise_data;
00196     }
00197 
00198     // Archive the simulation itself
00199     output_arch & pSim; // const-ness would be a pain here
00200 }
00201 
00202 #endif /*TISSUESIMULATIONARCHIVER_HPP_*/

Generated on Tue Aug 4 16:10:21 2009 for Chaste by  doxygen 1.5.5