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 "MeshArchiveInfo.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 = test_output_directory + rArchiveDirectory + "/archive/mesh_" + time_stamp.str();
00111     MeshArchiveInfo::meshPathname = mesh_filename;
00112     //MeshArchiver::SetMeshPath(mesh_filename);
00113     return archive_filename;
00114 }
00115 
00116 template<unsigned DIM, class SIM>
00117 SIM* TissueSimulationArchiver<DIM, SIM>::Load(const std::string& rArchiveDirectory, const double& rTimeStamp)
00118 {
00119     std::string archive_filename = TissueSimulationArchiver<DIM, SIM>::GetArchivePathname(rArchiveDirectory, rTimeStamp);
00120 
00121     // Create an input archive
00122     std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
00123     boost::archive::text_iarchive input_arch(ifs);
00124 
00125     // Load any data that isn't the simulation itself, mainly singletons
00126     // - simulation time
00127     SimulationTime* p_simulation_time = SimulationTime::Instance();
00128     assert(p_simulation_time->IsStartTimeSetUp());
00129     input_arch & *p_simulation_time;
00130     // - Wnt concentration (if used)
00131     bool archive_wnt;
00132     input_arch & archive_wnt;
00133     if (archive_wnt)
00134     {
00135         WntConcentration* p_wnt = WntConcentration::Instance();
00136         input_arch & *p_wnt;
00137     }
00138     // - CellwiseData (if used)
00139     bool archive_cellwise_data;
00140     input_arch & archive_cellwise_data;
00141     if (archive_cellwise_data)
00142     {
00143         CellwiseData<DIM>* p_cellwise_data = CellwiseData<DIM>::Instance();
00144         input_arch & *p_cellwise_data;
00145     }
00146 
00147     // Load the simulation
00148     SIM* p_sim;
00149     input_arch >> p_sim;
00150 
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     std::string archive_filename = handler.GetOutputDirectoryFullPath() + "tissue_sim_at_time_" + time_stamp.str() + ".arch";
00169     std::string mesh_filename = std::string("mesh_") + time_stamp.str();
00170 
00171     // Write the mesh to file. Call Update() first to
00172     // ensure that the tissue is in a good state.
00173     pSim->rGetTissue().Update();
00174     if (pSim->rGetTissue().HasMesh())
00175     {
00176         pSim->rGetTissue().WriteMeshToFile(archive_directory, mesh_filename);
00177     }
00178 
00179     // Create a new archive
00180     std::ofstream ofs(archive_filename.c_str());
00181     boost::archive::text_oarchive output_arch(ofs);
00182 
00183     // Save the simulation.  We save the time directly first to maintain its
00184     // singleton-ness on load.
00185     output_arch << *p_sim_time;
00186 
00187     // Archive the Wnt concentration if it's used
00188     bool archive_wnt = WntConcentration::Instance()->IsWntSetUp();
00189     output_arch & archive_wnt;
00190     if (archive_wnt)
00191     {
00192         WntConcentration* p_wnt = WntConcentration::Instance();
00193         output_arch & *p_wnt;
00194     }
00195 
00196     // Archive the CellwiseData if it's used
00197     bool archive_cellwise_data = CellwiseData<DIM>::Instance()->IsSetUp();
00198     output_arch & archive_cellwise_data;
00199     if (archive_cellwise_data)
00200     {
00201         CellwiseData<DIM>* p_cellwise_data = CellwiseData<DIM>::Instance();
00202         output_arch & *p_cellwise_data;
00203     }
00204 
00205     // Archive the simulation itself
00206     output_arch & pSim; // const-ness would be a pain here
00207 }
00208 
00209 #endif /*TISSUESIMULATIONARCHIVER_HPP_*/

Generated on Wed Mar 18 12:51:50 2009 for Chaste by  doxygen 1.5.5