This tutorial is automatically generated from the file trunk/heart/test/tutorials/TestCardiacCheckpointingAndRestartingTutorial.hpp at revision r11233. Note that the code is given in full at the bottom of the page.
Checkpointing and restarting cardiac simulations
In this tutorial we show how to save and reload cardiac simulations
CardiacSimulationArchiver has to be included. Archiving includes often have to be included first.
#include <cxxtest/TestSuite.h> #include "CardiacSimulationArchiver.hpp" #include "BidomainProblem.hpp" #include "LuoRudy1991.hpp" #include "PetscSetupAndFinalize.hpp" #include "PlaneStimulusCellFactory.hpp" class TestCardiacCheckpointingAndRestartingTutorial : public CxxTest::TestSuite { public:
First, the checkpointing test.
void TestCheckpointing() throw(Exception) {
We set up exactly the same simulation as in UserTutorials/AnotherBidomainSimulation
HeartConfig::Instance()->Reset(); PlaneStimulusCellFactory<CellLuoRudy1991FromCellML,2> cell_factory(-2000000); HeartConfig::Instance()->SetSimulationDuration(5.0); //ms HeartConfig::Instance()->SetOutputDirectory("BidomainCheckpointingTutorial"); HeartConfig::Instance()->SetOutputFilenamePrefix("results"); HeartConfig::Instance()->SetMeshFileName("mesh/test/data/2D_0_to_1mm_800_elements", cp::media_type::Orthotropic); double scale = 2; HeartConfig::Instance()->SetIntracellularConductivities(Create_c_vector(1.75*scale, 0.19*scale)); BidomainProblem<2> bidomain_problem( &cell_factory ); bidomain_problem.Initialise(); bidomain_problem.Solve();
To save the entire simulation, use the CardiacSimulationArchiver class, as shown in the following. Note the BidomainProblem<2> as the template parameter. The output directory is relative to CHASTE_TEST_OUTPUT.
CardiacSimulationArchiver<BidomainProblem<2> >::Save(bidomain_problem, "BidomainCheckpointingTutorial/saved_simulation"); }
This is how to restart the test.
void TestRestarting() throw(Exception) {
To restart from the saved simulation directory we use the CardiacSimulationArchiver class, as shown in the following. Note the BidomainProblem<2> as the template parameter again. The dimension (2) must match the one given in the saved archive directory. The output directory is again relative to CHASTE_TEST_OUTPUT.
BidomainProblem<2>* p_bidomain_problem = CardiacSimulationArchiver<BidomainProblem<2> >::Load("BidomainCheckpointingTutorial/saved_simulation");
The simulation duration has to be amended. Note that the duration is always given with respect to the origin of the first solve. This means that we are running from t=5 ms (the end of the previous simulation) to t=10 ms. The output files are concatenated so that they appear to be made by a single simulation running from t=0 ms to t=10 ms.
HeartConfig::Instance()->SetSimulationDuration(10); //ms
The point of checkpointing and restarting is that there may be something which we want to change during the course of experiment. Here we change the conductivity.
HeartConfig::Instance()->SetIntracellularConductivities(Create_c_vector(3.0, 0.3)); p_bidomain_problem->Solve(); } };
Notes
- Note that making a checkpoint does add a significant overhead at present, in particular because the mesh is written out to disk at each checkpoint. This is to ensure that each checkpoint directory contains everything needed to resume the simulation. In particular, the mesh written out will be in permuted form if it was partitioned for a parallel simulation.
- Meshes written in checkpoints use a binary form of the Triangle/Tetgen mesh format. This makes checkpoints significantly smaller but will cause portability problems if checkpoints are moved between little-endian systems (e.g. x86) and big-endian systems (e.g. PowerPC).
- Checkpoints may be resumed on any number of processes — you are not restricted to the number on which it was saved. However, the mesh will not be re-partitioned if loaded on a different number of processes, so the parallel efficiency of the simulation may be significantly reduced in this case.
- Resuming a checkpoint will attempt to extend the original results h5 file, if present, so that the file contains the complete simulation results. If this file does not exist, a new file will be created to contain just the results from the resume point.
- When checkpointing, the progress_status.txt file only reports the percentage of time to go until the next checkpoint, not until the end of the simulation. This makes it slightly less useful; however, the presence of the checkpoint directories (1ms, 2ms, etc.) provides overall progress information instead.
Code
The full code is given below
File name TestCardiacCheckpointingAndRestartingTutorial.hpp
#include <cxxtest/TestSuite.h> #include "CardiacSimulationArchiver.hpp" #include "BidomainProblem.hpp" #include "LuoRudy1991.hpp" #include "PetscSetupAndFinalize.hpp" #include "PlaneStimulusCellFactory.hpp" class TestCardiacCheckpointingAndRestartingTutorial : public CxxTest::TestSuite { public: void TestCheckpointing() throw(Exception) { HeartConfig::Instance()->Reset(); PlaneStimulusCellFactory<CellLuoRudy1991FromCellML,2> cell_factory(-2000000); HeartConfig::Instance()->SetSimulationDuration(5.0); //ms HeartConfig::Instance()->SetOutputDirectory("BidomainCheckpointingTutorial"); HeartConfig::Instance()->SetOutputFilenamePrefix("results"); HeartConfig::Instance()->SetMeshFileName("mesh/test/data/2D_0_to_1mm_800_elements", cp::media_type::Orthotropic); double scale = 2; HeartConfig::Instance()->SetIntracellularConductivities(Create_c_vector(1.75*scale, 0.19*scale)); BidomainProblem<2> bidomain_problem( &cell_factory ); bidomain_problem.Initialise(); bidomain_problem.Solve(); CardiacSimulationArchiver<BidomainProblem<2> >::Save(bidomain_problem, "BidomainCheckpointingTutorial/saved_simulation"); } void TestRestarting() throw(Exception) { BidomainProblem<2>* p_bidomain_problem = CardiacSimulationArchiver<BidomainProblem<2> >::Load("BidomainCheckpointingTutorial/saved_simulation"); HeartConfig::Instance()->SetSimulationDuration(10); //ms HeartConfig::Instance()->SetIntracellularConductivities(Create_c_vector(3.0, 0.3)); p_bidomain_problem->Solve(); } };