CardiacSimulation.hpp
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 #ifndef CARDIACSIMULATION_HPP_
00037 #define CARDIACSIMULATION_HPP_
00038
00039 #include <vector>
00040 #include <memory>
00041
00042 #include "UblasIncludes.hpp"
00043
00044 #include "AbstractCardiacProblem.hpp"
00045 #include "MonodomainProblem.hpp"
00046 #include "BidomainProblem.hpp"
00047 #include "BidomainWithBathProblem.hpp"
00048 #include "CardiacSimulationArchiver.hpp"
00049 #include "PetscTools.hpp"
00050 #include "TimeStepper.hpp"
00051 #include "Exception.hpp"
00052
00053 #include "HeartConfig.hpp"
00054 #include "HeartConfigRelatedCellFactory.hpp"
00055 #include "HeartFileFinder.hpp"
00056
00057 #include "TetrahedralMesh.hpp"
00058 #include "NonCachedTetrahedralMesh.hpp"
00059 #include "ChastePoint.hpp"
00060 #include "ChasteCuboid.hpp"
00061 #include "MeshalyzerMeshWriter.hpp"
00062 #include "TrianglesMeshWriter.hpp"
00063
00064 #include "OrthotropicConductivityTensors.hpp"
00065 #include "PostProcessingWriter.hpp"
00066
00067 #include "OutputDirectoryFifoQueue.hpp"
00068 #include "ExecutableSupport.hpp"
00069
00080 class CardiacSimulation
00081 {
00082 private:
00088 void ReadParametersFromFile(std::string parameterFileName);
00089
00094 template<class Problem, unsigned SPACE_DIM>
00095 void CreateAndRun()
00096 {
00097 std::auto_ptr<Problem> p_problem;
00098
00099 if (HeartConfig::Instance()->IsSimulationDefined())
00100 {
00101 HeartConfigRelatedCellFactory<SPACE_DIM> cell_factory;
00102 p_problem.reset(new Problem(&cell_factory));
00103
00104 p_problem->Initialise();
00105 }
00106 else
00107 {
00108 p_problem.reset(CardiacSimulationArchiver<Problem>::Load(HeartConfig::Instance()->GetArchivedSimulationDir()));
00109
00110 HeartConfigRelatedCellFactory<SPACE_DIM> cell_factory;
00111 cell_factory.SetMesh(&(p_problem->rGetMesh()));
00112 AbstractCardiacTissue<SPACE_DIM, SPACE_DIM>* p_tissue = p_problem->GetTissue();
00113 DistributedVectorFactory* p_vector_factory = p_problem->rGetMesh().GetDistributedVectorFactory();
00114 for (unsigned node_global_index = p_vector_factory->GetLow();
00115 node_global_index < p_vector_factory->GetHigh();
00116 node_global_index++)
00117 {
00118
00119 cell_factory.SetCellIntracellularStimulus(p_tissue->GetCardiacCell(node_global_index), node_global_index);
00120
00121 cell_factory.SetCellParameters(p_tissue->GetCardiacCell(node_global_index), node_global_index);
00122 }
00123 }
00124
00125 if (HeartConfig::Instance()->GetCheckpointSimulation())
00126 {
00127
00128 OutputDirectoryFifoQueue directory_queue(HeartConfig::Instance()->GetOutputDirectory() + "_checkpoints/",
00129 HeartConfig::Instance()->GetMaxCheckpointsOnDisk());
00130
00131 TimeStepper checkpoint_stepper(p_problem->GetCurrentTime(), HeartConfig::Instance()->GetSimulationDuration(), HeartConfig::Instance()->GetCheckpointTimestep());
00132 while ( !checkpoint_stepper.IsTimeAtEnd() )
00133 {
00134
00135 HeartConfig::Instance()->SetSimulationDuration(checkpoint_stepper.GetNextTime());
00136 p_problem->Solve();
00137
00138
00139 std::stringstream checkpoint_id;
00140 checkpoint_id << HeartConfig::Instance()->GetSimulationDuration() << "ms/";
00141 std::string checkpoint_dir_basename = directory_queue.CreateNextDir(checkpoint_id.str());
00142
00143
00144 std::stringstream archive_foldername;
00145 archive_foldername << HeartConfig::Instance()->GetOutputDirectory() << "_" << HeartConfig::Instance()->GetSimulationDuration() << "ms";
00146 CardiacSimulationArchiver<Problem>::Save(*(p_problem.get()), checkpoint_dir_basename + archive_foldername.str(), false);
00147
00148
00149 OutputFileHandler checkpoint_dir_basename_handler(checkpoint_dir_basename, false);
00150 OutputFileHandler partial_output_dir_handler(HeartConfig::Instance()->GetOutputDirectory(), false);
00151
00152 TRY_IF_MASTER(
00153 partial_output_dir_handler.FindFile("").CopyTo(checkpoint_dir_basename_handler.FindFile(""));
00154 );
00155
00156
00157 CreateResumeXmlFile(checkpoint_dir_basename, archive_foldername.str());
00158
00159
00160 checkpoint_stepper.AdvanceOneTimeStep();
00161 }
00162 }
00163 else
00164 {
00165 p_problem->Solve();
00166 }
00167 if (mSaveProblemInstance)
00168 {
00169 mSavedProblem = p_problem;
00170 }
00171 }
00172
00178 void Run();
00179
00189 void CreateResumeXmlFile(const std::string& rOutputDirectory, const std::string& rArchiveDirectory);
00190
00196 std::string BoolToString(bool yesNo);
00197 public:
00207 CardiacSimulation(std::string parameterFileName,
00208 bool writeProvenanceInfo=false,
00209 bool saveProblemInstance=false);
00210
00215 boost::shared_ptr<AbstractUntemplatedCardiacProblem> GetSavedProblem();
00216 private:
00218 bool mSaveProblemInstance;
00219
00221 boost::shared_ptr<AbstractUntemplatedCardiacProblem> mSavedProblem;
00222 };
00223
00224 #endif