CardiacSimulationArchiver.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <fstream>
00030
00031
00032 #include "CheckpointArchiveTypes.hpp"
00033 #include "CardiacSimulationArchiver.hpp"
00034
00035 #include "Exception.hpp"
00036 #include "ArchiveOpener.hpp"
00037 #include "OutputFileHandler.hpp"
00038 #include "ArchiveLocationInfo.hpp"
00039 #include "DistributedVectorFactory.hpp"
00040 #include "PetscTools.hpp"
00041 #include "FileFinder.hpp"
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 #include "MonodomainProblem.hpp"
00061 #include "BidomainProblem.hpp"
00062 #include "BidomainWithBathProblem.hpp"
00063 #include "HeartConfigRelatedCellFactory.hpp"
00064
00065
00066 template<class PROBLEM_CLASS>
00067 void CardiacSimulationArchiver<PROBLEM_CLASS>::Save(PROBLEM_CLASS& rSimulationToArchive,
00068 const std::string& rDirectory,
00069 bool clearDirectory)
00070 {
00071
00072 OutputFileHandler handler(rDirectory, clearDirectory);
00073
00074
00075
00076 {
00077
00078 FileFinder dir(rDirectory, RelativeTo::ChasteTestOutput);
00079 ArchiveOpener<boost::archive::text_oarchive, std::ofstream> archive_opener(dir, "archive.arch");
00080 boost::archive::text_oarchive* p_main_archive = archive_opener.GetCommonArchive();
00081
00082
00083 PROBLEM_CLASS* const p_simulation_to_archive = &rSimulationToArchive;
00084 (*p_main_archive) & p_simulation_to_archive;
00085 }
00086
00087
00088 if (PetscTools::AmMaster())
00089 {
00090 std::string info_path = handler.GetOutputDirectoryFullPath() + "archive.info";
00091 std::ofstream info_file(info_path.c_str());
00092 if (!info_file.is_open())
00093 {
00094
00095 PetscTools::ReplicateBool(true);
00096 EXCEPTION("Unable to open archive information file: " + info_path);
00097 }
00098 PetscTools::ReplicateBool(false);
00099 unsigned archive_version = 0;
00100 info_file << PetscTools::GetNumProcs() << " " << archive_version;
00101 }
00102 else
00103 {
00104 bool master_threw = PetscTools::ReplicateBool(false);
00105 if (master_threw)
00106 {
00107 EXCEPTION("Unable to open archive information file");
00108 }
00109 }
00110
00111 PetscTools::Barrier("CardiacSimulationArchiver::Save");
00112 }
00113
00114 template<class PROBLEM_CLASS>
00115 PROBLEM_CLASS* CardiacSimulationArchiver<PROBLEM_CLASS>::Load(const std::string& rDirectory)
00116 {
00117 FileFinder directory(rDirectory, RelativeTo::ChasteTestOutput);
00118 return CardiacSimulationArchiver<PROBLEM_CLASS>::Migrate(directory);
00119 }
00120
00121 template<class PROBLEM_CLASS>
00122 PROBLEM_CLASS* CardiacSimulationArchiver<PROBLEM_CLASS>::Load(const FileFinder& rDirectory)
00123 {
00124 return CardiacSimulationArchiver<PROBLEM_CLASS>::Migrate(rDirectory);
00125 }
00126
00127
00128 template<class PROBLEM_CLASS>
00129 PROBLEM_CLASS* CardiacSimulationArchiver<PROBLEM_CLASS>::Migrate(const FileFinder& rDirectory)
00130 {
00131
00132 std::string dir_path = rDirectory.GetAbsolutePath();
00133 if (!rDirectory.IsDir() || !rDirectory.Exists())
00134 {
00135 EXCEPTION("Checkpoint directory does not exist: " + dir_path);
00136 }
00137 assert(*(dir_path.end()-1) == '/');
00138
00139 std::string info_path = dir_path + "archive.info";
00140 std::ifstream info_file(info_path.c_str());
00141 if (!info_file.is_open())
00142 {
00143 EXCEPTION("Unable to open archive information file: " + info_path);
00144 }
00145 unsigned num_procs, archive_version;
00146 info_file >> num_procs >> archive_version;
00147
00148 PROBLEM_CLASS *p_unarchived_simulation;
00149
00150
00151
00152
00153 DistributedVectorFactory::SetCheckNumberOfProcessesOnLoad(false);
00154
00155 try
00156 {
00157
00158
00159
00160 unsigned initial_archive = num_procs == PetscTools::GetNumProcs() ? PetscTools::GetMyRank() : 0u;
00161
00162
00163
00164 ArchiveOpener<boost::archive::text_iarchive, std::ifstream> archive_opener(rDirectory, "archive.arch", initial_archive);
00165 boost::archive::text_iarchive* p_main_archive = archive_opener.GetCommonArchive();
00166 (*p_main_archive) >> p_unarchived_simulation;
00167
00168
00169 DistributedVectorFactory* p_factory = p_unarchived_simulation->rGetMesh().GetDistributedVectorFactory();
00170 assert(p_factory != NULL);
00171 unsigned original_num_procs = p_factory->GetOriginalFactory()->GetNumProcs();
00172 assert(original_num_procs == num_procs);
00173
00174
00175 for (unsigned archive_num=0; archive_num<original_num_procs; archive_num++)
00176 {
00177 if (archive_num != initial_archive)
00178 {
00179 std::string archive_path = ArchiveLocationInfo::GetProcessUniqueFilePath("archive.arch", archive_num);
00180 std::ifstream ifs(archive_path.c_str());
00181 boost::archive::text_iarchive archive(ifs);
00182 p_unarchived_simulation->LoadExtraArchive(archive, archive_version);
00183 }
00184 }
00185 }
00186 catch (Exception &e)
00187 {
00188 DistributedVectorFactory::SetCheckNumberOfProcessesOnLoad(true);
00189 throw e;
00190 }
00191
00192
00193 DistributedVectorFactory::SetCheckNumberOfProcessesOnLoad(true);
00194 return p_unarchived_simulation;
00195 }
00196
00197
00198
00199
00200
00201 template class CardiacSimulationArchiver<MonodomainProblem<1> >;
00202 template class CardiacSimulationArchiver<MonodomainProblem<2> >;
00203 template class CardiacSimulationArchiver<MonodomainProblem<3> >;
00204
00205 template class CardiacSimulationArchiver<BidomainProblem<1> >;
00206 template class CardiacSimulationArchiver<BidomainProblem<2> >;
00207 template class CardiacSimulationArchiver<BidomainProblem<3> >;
00208
00209 template class CardiacSimulationArchiver<BidomainWithBathProblem<1> >;
00210 template class CardiacSimulationArchiver<BidomainWithBathProblem<2> >;
00211 template class CardiacSimulationArchiver<BidomainWithBathProblem<3> >;