36 #include "AbstractCardiacProblem.hpp"
38 #include "GenericMeshReader.hpp"
40 #include "HeartConfig.hpp"
41 #include "HeartEventHandler.hpp"
42 #include "TimeStepper.hpp"
44 #include "DistributedVector.hpp"
45 #include "ProgressReporter.hpp"
46 #include "LinearSystem.hpp"
47 #include "PostProcessingWriter.hpp"
48 #include "Hdf5ToMeshalyzerConverter.hpp"
49 #include "Hdf5ToCmguiConverter.hpp"
50 #include "Hdf5ToVtkConverter.hpp"
53 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
57 mAllocatedMemoryForMesh(false),
60 mpCardiacTissue(NULL),
62 mpCellFactory(pCellFactory),
66 mpTimeAdaptivityController(NULL),
68 mUseHdf5DataWriterCache(false),
69 mHdf5DataWriterChunkSizeAndAlignment(0)
74 EXCEPTION(
"AbstractCardiacProblem: Please supply a cell factory pointer to your cardiac problem constructor.");
79 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
84 mAllocatedMemoryForMesh(false),
87 mVoltageColumnId(UINT_MAX),
88 mTimeColumnId(UINT_MAX),
89 mNodeColumnId(UINT_MAX),
90 mpCardiacTissue(NULL),
96 mpTimeAdaptivityController(NULL),
98 mUseHdf5DataWriterCache(false),
99 mHdf5DataWriterChunkSizeAndAlignment(0)
103 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
106 delete mpCardiacTissue;
112 if (mAllocatedMemoryForMesh)
118 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
126 WARNING(
"Using a non-distributed mesh in a parallel simulation is not a good idea.");
136 CreateMeshFromHeartConfig();
137 std::shared_ptr<AbstractMeshReader<ELEMENT_DIM, SPACE_DIM> > p_mesh_reader
139 mpMesh->ConstructFromMeshReader(*p_mesh_reader);
143 CreateMeshFromHeartConfig();
151 c_vector<double, 1> fibre_length;
153 mpMesh->ConstructRegularSlabMesh(inter_node_space, fibre_length[0]);
158 c_vector<double, 2> sheet_dimensions;
160 mpMesh->ConstructRegularSlabMesh(inter_node_space, sheet_dimensions[0], sheet_dimensions[1]);
165 c_vector<double, 3> slab_dimensions;
167 mpMesh->ConstructRegularSlabMesh(inter_node_space, slab_dimensions[0], slab_dimensions[1], slab_dimensions[2]);
179 mAllocatedMemoryForMesh =
true;
183 EXCEPTION(std::string(
"No mesh given: define it in XML parameters file or call SetMesh()\n") + e.
GetShortMessage());
186 mpCellFactory->SetMesh( mpMesh );
194 mpCellFactory->FillInCellularTransmuralAreas();
197 delete mpCardiacTissue;
198 mpCardiacTissue = CreateCardiacTissue();
218 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
224 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
227 this->mpBoundaryConditionsContainer = pBcc;
230 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
233 if (mpCardiacTissue == NULL)
235 EXCEPTION(
"Cardiac tissue is null, Initialise() probably hasn't been called");
239 EXCEPTION(
"End time should be in the future");
245 EXCEPTION(
"Either explicitly specify not to print output (call PrintOutput(false)) or specify the output directory and filename prefix");
260 if (fabs(end_time - pde_time*round(end_time/pde_time)) > 1e-10)
262 EXCEPTION(
"PDE timestep does not seem to divide end time - check parameters");
266 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
270 Vec initial_condition = p_factory->
CreateVec(PROBLEM_DIM);
272 std::vector<DistributedVector::Stripe> stripe;
273 stripe.reserve(PROBLEM_DIM);
275 for (
unsigned i=0; i<PROBLEM_DIM; i++)
284 stripe[0][index] = mpCardiacTissue->GetCardiacCell(index.Global)->GetVoltage();
287 stripe[1][index] = 0;
293 return initial_condition;
296 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
303 assert(mpMesh == NULL);
304 assert(pMesh != NULL);
305 mAllocatedMemoryForMesh =
false;
309 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
312 mPrintOutput = printOutput;
315 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
318 mWriteInfo = writeInfo;
321 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
327 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
330 return mpMesh->GetDistributedVectorFactory()->CreateDistributedVector(mSolution);
333 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
339 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
346 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
349 if (mpCardiacTissue == NULL)
351 EXCEPTION(
"Tissue not yet set up, you may need to call Initialise() before GetTissue().");
353 return mpCardiacTissue;
356 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
364 mpTimeAdaptivityController = pController;
368 mpTimeAdaptivityController = NULL;
372 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
377 std::vector<double> additional_stopping_times;
378 SetUpAdditionalStoppingTimes(additional_stopping_times);
384 additional_stopping_times);
389 if (!mpBoundaryConditionsContainer)
393 for (
unsigned problem_index=0; problem_index<PROBLEM_DIM; problem_index++)
395 mpDefaultBoundaryConditionsContainer->DefineZeroNeumannOnMeshBoundary(mpMesh, problem_index);
397 mpBoundaryConditionsContainer = mpDefaultBoundaryConditionsContainer;
400 assert(mpSolver==NULL);
401 mpSolver = CreateSolver();
404 Vec initial_condition;
407 initial_condition = mSolution;
411 initial_condition = CreateInitialCondition();
414 std::string progress_reporter_dir;
419 bool extending_file =
false;
422 extending_file = InitialiseWriter();
429 if (mSolution != initial_condition)
449 if (!(mSolution && extending_file))
451 WriteOneStep(stepper.
GetTime(), initial_condition);
452 mpWriter->AdvanceAlongUnlimitedDimension();
460 progress_reporter_dir =
"";
462 BOOST_FOREACH(boost::shared_ptr<AbstractOutputModifier> p_output_modifier, mOutputModifiers)
464 p_output_modifier->InitialiseAtStart(this->mpMesh->GetDistributedVectorFactory());
465 p_output_modifier->ProcessSolutionAtTimeStep(stepper.
GetTime(), initial_condition, PROBLEM_DIM);
477 progress_reporter.
Update(mCurrentTime);
480 if (mpTimeAdaptivityController)
482 mpSolver->SetTimeAdaptivityController(mpTimeAdaptivityController);
489 mpSolver->SetInitialCondition( initial_condition );
491 AtBeginningOfTimestep(stepper.
GetTime());
497 mSolution = mpSolver->Solve();
515 if (initial_condition != mSolution)
530 CloseFilesAndPostProcess();
541 initial_condition = mSolution;
545 mCurrentTime = stepper.
GetTime();
555 BOOST_FOREACH(boost::shared_ptr<AbstractOutputModifier> p_output_modifier, mOutputModifiers)
557 p_output_modifier->ProcessSolutionAtTimeStep(stepper.
GetTime(), mSolution, PROBLEM_DIM);
563 WriteOneStep(stepper.
GetTime(), mSolution);
565 mpWriter->AdvanceAlongUnlimitedDimension();
572 OnEndOfTimestep(stepper.
GetTime());
581 BOOST_FOREACH(boost::shared_ptr<AbstractOutputModifier> p_output_modifier, mOutputModifiers)
583 p_output_modifier->FinaliseAtEnd();
585 CloseFilesAndPostProcess();
589 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
622 mHdf5DataWriterChunkSizeAndAlignment);
633 if (mNodesToOutput.empty())
686 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
691 if (mNodesToOutput.empty() )
694 mpWriter->DefineFixedDimension( mpMesh->GetNumNodes() );
699 mpWriter->DefineFixedDimension( mNodesToOutput, mpMesh->GetNumNodes() );
702 mVoltageColumnId = mpWriter->DefineVariable(
"V",
"mV");
709 mpWriter->DefineUnlimitedDimension(
"Time",
"msecs", stepper.
EstimateTimeSteps()+1);
713 mVoltageColumnId = mpWriter->GetVariableByName(
"V");
717 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
720 mExtraVariablesId.clear();
725 std::vector<std::string> output_variables;
727 const unsigned num_vars = output_variables.size();
728 mExtraVariablesId.reserve(num_vars);
731 for (
unsigned var_index=0; var_index<num_vars; var_index++)
734 std::string var_name = output_variables[var_index];
740 column_id = this->mpWriter->GetVariableByName(var_name);
746 column_id = this->mpWriter->DefineVariable(var_name,
"unknown_units");
750 mExtraVariablesId.push_back(column_id);
755 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
759 std::vector<std::string> output_variables;
760 unsigned num_vars = mExtraVariablesId.size();
765 assert(output_variables.size() == num_vars);
768 for (
unsigned var_index=0; var_index<num_vars; var_index++)
771 Vec variable_data = this->mpMesh->GetDistributedVectorFactory()->CreateVec();
772 DistributedVector distributed_var_data = this->mpMesh->GetDistributedVectorFactory()->CreateDistributedVector(variable_data);
776 index!= distributed_var_data.
End();
784 distributed_var_data[index] = 0.0;
789 distributed_var_data[index] = this->mpCardiacTissue->GetCardiacCell(index.Global)->
790 GetAnyVariable(output_variables[var_index], mCurrentTime);
793 distributed_var_data.
Restore();
796 this->mpWriter->PutVector(mExtraVariablesId[var_index], variable_data);
802 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
805 bool extend_file = (mSolution != NULL);
830 if (times.back() > mCurrentTime)
833 " with results from time = " << mCurrentTime <<
834 ", but it already contains results up to time = " << times.back() <<
"."
835 " Calling HeartConfig::Instance()->SetOutputDirectory() before Solve() will direct results elsewhere.");
840 mpWriter =
new Hdf5DataWriter(*mpMesh->GetDistributedVectorFactory(),
846 mUseHdf5DataWriterCache);
855 if (!extend_file && mHdf5DataWriterChunkSizeAndAlignment)
858 mpWriter->SetAlignment(mHdf5DataWriterChunkSizeAndAlignment);
862 DefineWriterColumns(extend_file);
867 bool success = mpWriter->ApplyPermutation(mpMesh->rGetNodePermutation(),
true);
868 if (success ==
false)
877 mpWriter->EndDefineMode();
883 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
886 mUseHdf5DataWriterCache = useCache;
889 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
892 mHdf5DataWriterChunkSizeAndAlignment = size;
895 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
898 mNodesToOutput = nodesToOutput;
901 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
906 EXCEPTION(
"Data reader invalid as data writer cannot be initialised");
911 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
917 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM,
unsigned PROBLEM_DIM>
void WritePostProcessingFiles()
std::string GetOutputFilenamePrefix() const
double GetSimulationDuration() const
static bool IsRegionBath(HeartRegionType regionId)
void PrintOutput(bool rPrintOutput)
void Update(double currentTime)
std::string GetAbsolutePath() const
DistributedVector CreateDistributedVector(Vec vec, bool readOnly=false)
void Write(bool useArchiveLocationInfo=false, std::string subfolderName="output")
void GetSlabDimensions(c_vector< double, 3 > &slabDimensions) const
#define EXCEPTION(message)
static void BeginEvent(unsigned event)
virtual Vec CreateInitialCondition()
virtual void CreateMeshFromHeartConfig()
AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > & rGetMesh()
void SetOutputUsingOriginalNodeOrdering(bool useOriginal)
void GetOutputVariables(std::vector< std::string > &rOutputVariables) const
DistributedTetrahedralMeshPartitionType::type GetMeshPartitioning() const
virtual void PreSolveChecks()
double GetPdeTimeStep() const
std::string GetMeshName() const
void GetFibreLength(c_vector< double, 1 > &fibreLength) const
void SetTargetChunkSize(hsize_t targetSize)
void AdvanceOneTimeStep()
void CloseFilesAndPostProcess()
void SetHdf5DataWriterTargetChunkSizeAndAlignment(hsize_t size)
void GetSheetDimensions(c_vector< double, 2 > &sheetDimensions) const
Hdf5DataReader GetDataReader()
void SetUseTimeAdaptivityController(bool useAdaptivity, AbstractTimeAdaptivityController *pController=NULL)
AbstractCardiacTissue< ELEMENT_DIM, SPACE_DIM > * GetTissue()
std::vector< double > GetUnlimitedDimensionValues()
std::vector< unsigned > mNodesToOutput
std::string GetSubdirectory()
unsigned EstimateTimeSteps() const
void SetMesh(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > *pMesh)
double GetInterNodeSpace() const
void SetUseHdf5DataWriterCache(bool useCache=true)
AbstractCardiacCellFactory< ELEMENT_DIM, SPACE_DIM > * mpCellFactory
virtual bool GetHasBath()
void SetOutputNodes(std::vector< unsigned > &rNodesToOutput)
virtual void DefineWriterColumns(bool extending)
virtual ~AbstractCardiacProblem()
void SetBoundaryConditionsContainer(BccType pBcc)
virtual void SetElectrodes()
bool GetVisualizeWithMeshalyzer() const
std::string GetOutputDirectory() const
static void EndEvent(unsigned event)
static std::string GetChasteTestOutputDirectory()
double GetNextTime() const
void SetWriteInfo(bool writeInfo=true)
void WriteExtraVariablesOneStep()
DistributedVector GetSolutionDistributedVector()
std::string GetShortMessage() const
static HeartConfig * Instance()
void DefineExtraVariablesWriterColumns(bool extending)