Chaste  Release::2017.1
CardiacSimulation.cpp
1 /*
2 
3 Copyright (c) 2005-2017, 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 "CardiacSimulationArchiver.hpp" // Must go first
37 #include "CardiacSimulation.hpp"
38 
39 
40 boost::shared_ptr<AbstractUntemplatedCardiacProblem> CardiacSimulation::GetSavedProblem()
41 {
42  return mSavedProblem;
43 }
44 
45 std::string CardiacSimulation::BoolToString(bool yesNo)
46 {
47  std::string result;
48  if (yesNo)
49  {
50  result = "yes";
51  }
52  else
53  {
54  result = "no";
55  }
56  return result;
57 }
58 
59 void CardiacSimulation::CreateResumeXmlFile(const std::string& rOutputDirectory, const std::string& rArchiveDirectory)
60 {
61  OutputFileHandler handler(rOutputDirectory, false);
63  {
64  out_stream p_file = handler.OpenOutputFile("ResumeParameters.xml");
65  (*p_file) << "<?xml version='1.0' encoding='UTF-8'?>" << std::endl;
66  (*p_file) << "<ChasteParameters xmlns='https://chaste.comlab.ox.ac.uk/nss/parameters/3_0' "
67  << "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
68  << "xsi:schemaLocation='https://chaste.comlab.ox.ac.uk/nss/parameters/3_0 ChasteParameters_3_0.xsd'>" << std::endl;
69  (*p_file) << std::endl;
70  (*p_file) << " <ResumeSimulation>" << std::endl;
71  (*p_file) << " <ArchiveDirectory relative_to='this_file'>" << rArchiveDirectory << "</ArchiveDirectory>" << std::endl;
72  (*p_file) << " <SpaceDimension>" << HeartConfig::Instance()->GetSpaceDimension() << "</SpaceDimension>" << std::endl;
73  (*p_file) << " <SimulationDuration unit='ms'>0.0</SimulationDuration> <!-- Edit with new simulation duration. Please "
74  << "note that the simulation does not restart at t=0 but at the time where the checkpoint was created.-->" << std::endl;
75  (*p_file) << " <Domain>" << HeartConfig::Instance()->GetDomain() << "</Domain>" << std::endl;
76  (*p_file) << " <CheckpointSimulation timestep='" << HeartConfig::Instance()->GetCheckpointTimestep()
77  << "' unit='ms' max_checkpoints_on_disk='" << HeartConfig::Instance()->GetMaxCheckpointsOnDisk()
78  << "'/> <!-- This is optional; if not given, the loaded simulation will NOT itself be checkpointed -->" << std::endl;
79  (*p_file) << " <OutputVisualizer meshalyzer='" << BoolToString(HeartConfig::Instance()->GetVisualizeWithMeshalyzer())
80  << "' vtk='" << BoolToString(HeartConfig::Instance()->GetVisualizeWithVtk())
81  << "' parallel_vtk='" << BoolToString(HeartConfig::Instance()->GetVisualizeWithParallelVtk())
82  << "' cmgui='" << BoolToString(HeartConfig::Instance()->GetVisualizeWithCmgui()) << "'/>" << std::endl;
83  (*p_file) << " </ResumeSimulation>" << std::endl;
84  (*p_file) << std::endl;
85  (*p_file) << " <!-- These elements must exist, but their contents are ignored -->" << std::endl;
86  (*p_file) << " <Physiological/>" << std::endl;
87  (*p_file) << " <Numerical/>" << std::endl;
88  (*p_file) << "</ChasteParameters>" << std::endl;
89  p_file->close();
90  }
91 
93 }
94 
95 CardiacSimulation::CardiacSimulation(std::string parameterFileName,
96  bool writeProvenanceInfo,
97  bool saveProblemInstance)
98  : mSaveProblemInstance(saveProblemInstance)
99 {
100  // If we have been passed an XML file then parse the XML file, otherwise throw
101  if (parameterFileName == "")
102  {
103  EXCEPTION("No XML file name given");
104  }
105  ReadParametersFromFile(parameterFileName);
106  Run();
109  if (writeProvenanceInfo)
110  {
114  }
115 }
116 
117 void CardiacSimulation::ReadParametersFromFile(std::string parameterFileName)
118 {
119  // Ensure the singleton is in a clean state
121  try
122  {
123  // Try the hardcoded schema location first
125  HeartConfig::Instance()->SetParametersFile(parameterFileName);
126  }
127  catch (Exception& e)
128  {
129  if (e.CheckShortMessageContains("Missing file parsing configuration") == "")
130  {
131  // Try using the schema location given in the XML
134  HeartConfig::Instance()->SetParametersFile(parameterFileName);
135  }
136  else
137  {
138  throw e;
139  }
140  }
141 }
142 
143 
144 #define DOMAIN_CASE(VALUE, CLASS, DIM) \
145  case VALUE: \
146  { \
147  CreateAndRun<CLASS<DIM>, DIM>(); \
148  break; \
149  }
150 
151 #define DOMAIN_SWITCH(DIM) \
152  switch (HeartConfig::Instance()->GetDomain()) \
153  { \
154  DOMAIN_CASE(cp::domain_type::Mono, MonodomainProblem, DIM) \
155  DOMAIN_CASE(cp::domain_type::Bi, BidomainProblem, DIM) \
156  DOMAIN_CASE(cp::domain_type::BiWithBath, BidomainWithBathProblem, DIM) \
157  default: \
158  NEVER_REACHED; \
159  } \
160  break
161 // Note that if the domain is not set correctly then the XML parser will have picked it up before now!
162 // Missing semi-colon after break so we can put it after the macro call.
163 
165 {
166  switch (HeartConfig::Instance()->GetSpaceDimension())
167  {
168  case 3:
169  {
170  DOMAIN_SWITCH(3);
171  }
172  case 2:
173  {
174  DOMAIN_SWITCH(2);
175  }
176  case 1:
177  {
178  DOMAIN_SWITCH(1);
179  }
180  default:
181  // We could check for this too in the XML Schema...
182  EXCEPTION("Space dimension not supported: should be 1, 2 or 3");
183  }
184 }
185 
186 // These aren't needed externally
187 #undef DOMAIN_SWITCH
188 #undef DOMAIN_CASE
189 
CardiacSimulation(std::string parameterFileName, bool writeProvenanceInfo=false, bool saveProblemInstance=false)
double GetCheckpointTimestep() const
#define EXCEPTION(message)
Definition: Exception.hpp:143
static bool AmMaster()
Definition: PetscTools.cpp:120
boost::shared_ptr< AbstractUntemplatedCardiacProblem > mSavedProblem
void SetUseFixedSchemaLocation(bool useFixedSchemaLocation)
std::string GetOutputDirectoryFullPath() const
static void WriteProvenanceInfoFile()
cp::domain_type GetDomain() const
out_stream OpenOutputFile(const std::string &rFileName, std::ios_base::openmode mode=std::ios::out|std::ios::trunc) const
unsigned GetSpaceDimension() const
unsigned GetMaxCheckpointsOnDisk() const
std::string BoolToString(bool yesNo)
void CreateResumeXmlFile(const std::string &rOutputDirectory, const std::string &rArchiveDirectory)
void SetParametersFile(const std::string &rFileName)
boost::shared_ptr< AbstractUntemplatedCardiacProblem > GetSavedProblem()
std::string CheckShortMessageContains(std::string expected) const
Definition: Exception.cpp:103
static void Reset()
static void WriteMachineInfoFile(std::string fileBaseName)
#define TRY_IF_MASTER(method)
Definition: PetscTools.hpp:91
static HeartConfig * Instance()
void ReadParametersFromFile(std::string parameterFileName)
static void SetOutputDirectory(const std::string &rOutputDirectory)