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