CardiacSimulation.hpp

00001 /*
00002 
00003 Copyright (c) 2005-2015, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 // (HeartConfig::Instance()->IsSimulationResumed())
00107         {
00108             p_problem.reset(CardiacSimulationArchiver<Problem>::Load(HeartConfig::Instance()->GetArchivedSimulationDir()));
00109             // Any changes to parameters that normally only take effect at problem creation time...
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                 // Overwrite any previous stimuli if new ones are defined
00119                 cell_factory.SetCellIntracellularStimulus(p_tissue->GetCardiacCell(node_global_index), node_global_index);
00120                 // Modify cell model parameters
00121                 cell_factory.SetCellParameters(p_tissue->GetCardiacCell(node_global_index), node_global_index);
00122             }
00123         }
00124 
00125         if (HeartConfig::Instance()->GetCheckpointSimulation())
00126         {
00127             // Create the checkpoints directory and set up a fifo queue of subdirectory names
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                 // Solve checkpoint timestep
00135                 HeartConfig::Instance()->SetSimulationDuration(checkpoint_stepper.GetNextTime());
00136                 p_problem->Solve();
00137 
00138                 // Create directory that will contain archive and partial results for this checkpoint timestep.
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                 // Archive simulation (in a subdirectory of checkpoint_dir_basename).
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                 // Put a copy of the partial results aside (in a subdirectory of checkpoint_dir_basename).
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                 // Create an XML file to help in resuming
00157                 CreateResumeXmlFile(checkpoint_dir_basename, archive_foldername.str());
00158 
00159                 // Advance time stepper
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 /*CARDIACSIMULATION_HPP_*/

Generated by  doxygen 1.6.2