Chaste  Release::3.4
CardiacSimulationArchiver.cpp
1 /*
2 
3 Copyright (c) 2005-2016, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #include <fstream>
37 
38 // Must be included before any other serialization headers
40 #include "CardiacSimulationArchiver.hpp"
41 
42 #include "Exception.hpp"
43 #include "ArchiveOpener.hpp"
44 #include "OutputFileHandler.hpp"
45 #include "ArchiveLocationInfo.hpp"
46 #include "DistributedVectorFactory.hpp"
47 #include "PetscTools.hpp"
48 #include "FileFinder.hpp"
49 
50 #include "MonodomainProblem.hpp"
51 #include "BidomainProblem.hpp"
52 #include "BidomainWithBathProblem.hpp"
53 
54 template<class PROBLEM_CLASS>
55 void CardiacSimulationArchiver<PROBLEM_CLASS>::Save(PROBLEM_CLASS& rSimulationToArchive,
56  const std::string& rDirectory,
57  bool clearDirectory)
58 {
59  // Clear directory if requested (and make sure it exists)
60  OutputFileHandler handler(rDirectory, clearDirectory);
61 
62  // Nest the archive writing, so the ArchiveOpener goes out of scope before
63  // the method ends.
64  {
65  // Open the archive files
67  ArchiveOpener<boost::archive::text_oarchive, std::ofstream> archive_opener(dir, "archive.arch");
68  boost::archive::text_oarchive* p_main_archive = archive_opener.GetCommonArchive();
69 
70  // And save
71  PROBLEM_CLASS* const p_simulation_to_archive = &rSimulationToArchive;
72  (*p_main_archive) & p_simulation_to_archive;
73  }
74 
75  // Write the info file
77  {
78  std::string info_path = handler.GetOutputDirectoryFullPath() + "archive.info";
79  std::ofstream info_file(info_path.c_str());
80  if (!info_file.is_open())
81  {
82  // Avoid deadlock...
84  EXCEPTION("Unable to open archive information file: " + info_path);
85  }
87  unsigned archive_version = 0; // Note that Boost version numbers are per-class; this only needs to change if we change the Load/Save methods here
88  info_file << PetscTools::GetNumProcs() << " " << archive_version;
89  }
90  else
91  {
92  bool master_threw = PetscTools::ReplicateBool(false);
93  if (master_threw)
94  {
95  EXCEPTION("Unable to open archive information file");
96  }
97  }
98  // Make sure everything is written before any process continues.
99  PetscTools::Barrier("CardiacSimulationArchiver::Save");
100 }
101 
102 template<class PROBLEM_CLASS>
103 PROBLEM_CLASS* CardiacSimulationArchiver<PROBLEM_CLASS>::Load(const std::string& rDirectory)
104 {
105  FileFinder directory(rDirectory, RelativeTo::ChasteTestOutput);
107 }
108 
109 template<class PROBLEM_CLASS>
111 {
113 }
114 
115 
116 template<class PROBLEM_CLASS>
118 {
119  // Check the directory exists
120  std::string dir_path = rDirectory.GetAbsolutePath();
121  if (!rDirectory.IsDir() || !rDirectory.Exists())
122  {
123  EXCEPTION("Checkpoint directory does not exist: " + dir_path);
124  }
125  assert(*(dir_path.end()-1) == '/'); // Paranoia
126 
127  // Load the info file
128  std::string info_path = dir_path + "archive.info";
129  std::ifstream info_file(info_path.c_str());
130  if (!info_file.is_open())
131  {
132  EXCEPTION("Unable to open archive information file: " + info_path);
133  }
134  unsigned num_procs, archive_version;
135  info_file >> num_procs >> archive_version;
136 
137  PROBLEM_CLASS *p_unarchived_simulation;
138 
139  // Avoid the DistributedVectorFactory throwing a 'wrong number of processes' exception when loading,
140  // and make it get the original DistributedVectorFactory from the archive so we can compare against
141  // num_procs.
143  // Put what follows in a try-catch to make sure we reset this
144  try
145  {
146  // Figure out which process-specific archive to load first. If we're loading on the same number of
147  // processes, we must load our own one, or the mesh gets confused. Otherwise, start with 0 to make
148  // sure it exists.
149  unsigned initial_archive = num_procs == PetscTools::GetNumProcs() ? PetscTools::GetMyRank() : 0u;
150 
151  // Load the master and initial process-specific archive files.
152  // This will also set up ArchiveLocationInfo for us.
153  ArchiveOpener<boost::archive::text_iarchive, std::ifstream> archive_opener(rDirectory, "archive.arch", initial_archive);
154  boost::archive::text_iarchive* p_main_archive = archive_opener.GetCommonArchive();
155  (*p_main_archive) >> p_unarchived_simulation;
156 
157  // Work out how many more process-specific files to load
158  DistributedVectorFactory* p_factory = p_unarchived_simulation->rGetMesh().GetDistributedVectorFactory();
159  assert(p_factory != NULL);
160  unsigned original_num_procs = p_factory->GetOriginalFactory()->GetNumProcs();
161  assert(original_num_procs == num_procs); // Paranoia
162 
163  // Merge in the extra data
164  for (unsigned archive_num=0; archive_num<original_num_procs; archive_num++)
165  {
166  if (archive_num != initial_archive)
167  {
168  std::string archive_path = ArchiveLocationInfo::GetProcessUniqueFilePath("archive.arch", archive_num);
169  std::ifstream ifs(archive_path.c_str());
170  boost::archive::text_iarchive archive(ifs);
171  p_unarchived_simulation->LoadExtraArchive(archive, archive_version);
172  }
173  }
174  }
175  catch (Exception &e)
176  {
178  throw e;
179  }
180 
181  // Done.
183  return p_unarchived_simulation;
184 }
185 
186 //
187 // Explicit instantiation
188 //
189 
193 
197 
static void SetCheckNumberOfProcessesOnLoad(bool checkNumberOfProcessesOnLoad=true)
static bool ReplicateBool(bool flag)
Definition: PetscTools.cpp:186
static void Barrier(const std::string callerId="")
Definition: PetscTools.cpp:134
static std::string GetProcessUniqueFilePath(const std::string &rFileName, unsigned procId=PetscTools::GetMyRank())
std::string GetAbsolutePath() const
Definition: FileFinder.cpp:221
#define EXCEPTION(message)
Definition: Exception.hpp:143
Archive * GetCommonArchive()
static bool AmMaster()
Definition: PetscTools.cpp:120
static void Save(PROBLEM_CLASS &rSimulationToArchive, const std::string &rDirectory, bool clearDirectory=true)
DistributedVectorFactory * GetOriginalFactory()
std::string GetOutputDirectoryFullPath() const
static PROBLEM_CLASS * Load(const std::string &rDirectory)
bool IsDir() const
Definition: FileFinder.cpp:190
bool Exists() const
Definition: FileFinder.cpp:180
static PROBLEM_CLASS * Migrate(const FileFinder &rDirectory)
static unsigned GetNumProcs()
Definition: PetscTools.cpp:108
static unsigned GetMyRank()
Definition: PetscTools.cpp:114