CardiacSimulation.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 "CardiacSimulationArchiver.hpp"
00037 #include "CardiacSimulation.hpp"
00038
00039
00040 boost::shared_ptr<AbstractUntemplatedCardiacProblem> CardiacSimulation::GetSavedProblem()
00041 {
00042 return mSavedProblem;
00043 }
00044
00045 std::string CardiacSimulation::BoolToString(bool yesNo)
00046 {
00047 std::string result;
00048 if (yesNo)
00049 {
00050 result = "yes";
00051 }
00052 else
00053 {
00054 result = "no";
00055 }
00056 return result;
00057 }
00058
00059 void CardiacSimulation::CreateResumeXmlFile(const std::string& rOutputDirectory, const std::string& rArchiveDirectory)
00060 {
00061 OutputFileHandler handler(rOutputDirectory, false);
00062 if (PetscTools::AmMaster())
00063 {
00064 out_stream p_file = handler.OpenOutputFile("ResumeParameters.xml");
00065 (*p_file) << "<?xml version='1.0' encoding='UTF-8'?>" << std::endl;
00066 (*p_file) << "<ChasteParameters xmlns='https://chaste.comlab.ox.ac.uk/nss/parameters/3_0' "
00067 << "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
00068 << "xsi:schemaLocation='https://chaste.comlab.ox.ac.uk/nss/parameters/3_0 ChasteParameters_3_0.xsd'>" << std::endl;
00069 (*p_file) << std::endl;
00070 (*p_file) << " <ResumeSimulation>" << std::endl;
00071 (*p_file) << " <ArchiveDirectory relative_to='this_file'>" << rArchiveDirectory << "</ArchiveDirectory>" << std::endl;
00072 (*p_file) << " <SpaceDimension>" << HeartConfig::Instance()->GetSpaceDimension() << "</SpaceDimension>" << std::endl;
00073 (*p_file) << " <SimulationDuration unit='ms'>0.0</SimulationDuration> <!-- Edit with new simulation duration. Please "
00074 << "note that the simulation does not restart at t=0 but at the time where the checkpoint was created.-->" << std::endl;
00075 (*p_file) << " <Domain>" << HeartConfig::Instance()->GetDomain() << "</Domain>" << std::endl;
00076 (*p_file) << " <CheckpointSimulation timestep='" << HeartConfig::Instance()->GetCheckpointTimestep()
00077 << "' unit='ms' max_checkpoints_on_disk='" << HeartConfig::Instance()->GetMaxCheckpointsOnDisk()
00078 << "'/> <!-- This is optional; if not given, the loaded simulation will NOT itself be checkpointed -->" << std::endl;
00079 (*p_file) << " <OutputVisualizer meshalyzer='" << BoolToString(HeartConfig::Instance()->GetVisualizeWithMeshalyzer())
00080 << "' vtk='" << BoolToString(HeartConfig::Instance()->GetVisualizeWithVtk())
00081 << "' parallel_vtk='" << BoolToString(HeartConfig::Instance()->GetVisualizeWithParallelVtk())
00082 << "' cmgui='" << BoolToString(HeartConfig::Instance()->GetVisualizeWithCmgui()) << "'/>" << std::endl;
00083 (*p_file) << " </ResumeSimulation>" << std::endl;
00084 (*p_file) << std::endl;
00085 (*p_file) << " <!-- These elements must exist, but their contents are ignored -->" << std::endl;
00086 (*p_file) << " <Physiological/>" << std::endl;
00087 (*p_file) << " <Numerical/>" << std::endl;
00088 (*p_file) << "</ChasteParameters>" << std::endl;
00089 p_file->close();
00090 }
00091
00092 TRY_IF_MASTER(HeartConfig::Instance()->CopySchema(handler.GetOutputDirectoryFullPath()));
00093 }
00094
00095 CardiacSimulation::CardiacSimulation(std::string parameterFileName,
00096 bool writeProvenanceInfo,
00097 bool saveProblemInstance)
00098 : mSaveProblemInstance(saveProblemInstance)
00099 {
00100
00101 if (parameterFileName == "")
00102 {
00103 EXCEPTION("No XML file name given");
00104 }
00105 ReadParametersFromFile(parameterFileName);
00106 Run();
00107 HeartEventHandler::Headings();
00108 HeartEventHandler::Report();
00109 if (writeProvenanceInfo)
00110 {
00111 ExecutableSupport::SetOutputDirectory(HeartConfig::Instance()->GetOutputDirectory());
00112 ExecutableSupport::WriteProvenanceInfoFile();
00113 ExecutableSupport::WriteMachineInfoFile("machine_info");
00114 }
00115 }
00116
00117 void CardiacSimulation::ReadParametersFromFile(std::string parameterFileName)
00118 {
00119
00120 HeartConfig::Reset();
00121 try
00122 {
00123
00124 HeartConfig::Instance()->SetUseFixedSchemaLocation(true);
00125 HeartConfig::Instance()->SetParametersFile(parameterFileName);
00126 }
00127 catch (Exception& e)
00128 {
00129 if (e.CheckShortMessageContains("Missing file parsing configuration") == "")
00130 {
00131
00132 HeartConfig::Reset();
00133 HeartConfig::Instance()->SetUseFixedSchemaLocation(false);
00134 HeartConfig::Instance()->SetParametersFile(parameterFileName);
00135 }
00136 else
00137 {
00138 throw e;
00139 }
00140 }
00141 }
00142
00143
00144 #define DOMAIN_CASE(VALUE, CLASS, DIM) \
00145 case VALUE: \
00146 { \
00147 CreateAndRun<CLASS<DIM>, DIM>(); \
00148 break; \
00149 }
00150
00151 #define DOMAIN_SWITCH(DIM) \
00152 switch (HeartConfig::Instance()->GetDomain()) \
00153 { \
00154 DOMAIN_CASE(cp::domain_type::Mono, MonodomainProblem, DIM) \
00155 DOMAIN_CASE(cp::domain_type::Bi, BidomainProblem, DIM) \
00156 DOMAIN_CASE(cp::domain_type::BiWithBath, BidomainWithBathProblem, DIM) \
00157 default: \
00158 NEVER_REACHED; \
00159 } \
00160 break
00161
00162
00163
00164 void CardiacSimulation::Run()
00165 {
00166 switch (HeartConfig::Instance()->GetSpaceDimension())
00167 {
00168 case 3:
00169 {
00170 DOMAIN_SWITCH(3);
00171 }
00172 case 2:
00173 {
00174 DOMAIN_SWITCH(2);
00175 }
00176 case 1:
00177 {
00178 DOMAIN_SWITCH(1);
00179 }
00180 default:
00181
00182 EXCEPTION("Space dimension not supported: should be 1, 2 or 3");
00183 }
00184 }
00185
00186
00187 #undef DOMAIN_SWITCH
00188 #undef DOMAIN_CASE
00189