36 #include "CardiacElectroMechanicsProblem.hpp" 38 #include "OutputFileHandler.hpp" 39 #include "ReplicatableVector.hpp" 40 #include "HeartConfig.hpp" 41 #include "LogFile.hpp" 42 #include "ChastePoint.hpp" 43 #include "Element.hpp" 44 #include "BoundaryConditionsContainer.hpp" 45 #include "AbstractDynamicLinearPdeSolver.hpp" 46 #include "TimeStepper.hpp" 47 #include "TrianglesMeshWriter.hpp" 48 #include "Hdf5ToMeshalyzerConverter.hpp" 49 #include "Hdf5ToCmguiConverter.hpp" 50 #include "MeshalyzerMeshWriter.hpp" 52 #include "ImplicitCardiacMechanicsSolver.hpp" 53 #include "ExplicitCardiacMechanicsSolver.hpp" 54 #include "CmguiDeformedSolutionsWriter.hpp" 55 #include "VoltageInterpolaterOntoMechanicsMesh.hpp" 56 #include "BidomainProblem.hpp" 59 template<
unsigned DIM,
unsigned ELEC_PROB_DIM>
62 assert(mIsWatchedLocation);
65 double min_dist = DBL_MAX;
67 for (
unsigned i=0; i<mpElectricsMesh->GetNumNodes(); i++)
69 double dist = norm_2(mWatchedLocation - mpElectricsMesh->GetNode(i)->rGetLocation());
79 c_vector<double,DIM> pos = mpElectricsMesh->GetNode(node_index)->rGetLocation();
84 std::cout <<
"ERROR: Could not find an electrics node very close to requested watched location - " 85 <<
"min distance was " << min_dist <<
" for node " << node_index
86 <<
" at location " << pos << std::flush;;
95 LOG(2,
"Chosen electrics node "<<node_index<<
" at location " << pos <<
" to be watched");
96 mWatchedElectricsNodeIndex = node_index;
102 c_vector<double,DIM> pos_at_min;
104 for (
unsigned i=0; i<mpMechanicsMesh->GetNumNodes(); i++)
106 c_vector<double,DIM> position = mpMechanicsMesh->GetNode(i)->rGetLocation();
108 double dist = norm_2(position-mWatchedLocation);
114 pos_at_min = position;
124 std::cout <<
"ERROR: Could not find a mechanics node very close to requested watched location - " 125 <<
"min distance was " << min_dist <<
" for node " << node_index
126 <<
" at location " << pos_at_min;
135 LOG(2,
"Chosen mechanics node "<<node_index<<
" at location " << pos <<
" to be watched");
136 mWatchedMechanicsNodeIndex = node_index;
143 template<
unsigned DIM,
unsigned ELEC_PROB_DIM>
146 assert(mIsWatchedLocation);
148 std::vector<c_vector<double,DIM> >& deformed_position = mpMechanicsSolver->rGetDeformedPosition();
152 double V = voltage_replicated[mWatchedElectricsNodeIndex];
159 *mpWatchedLocationFile << time <<
" ";
160 for (
unsigned i=0; i<DIM; i++)
162 *mpWatchedLocationFile << deformed_position[mWatchedMechanicsNodeIndex](i) <<
" ";
164 *mpWatchedLocationFile << V <<
"\n";
165 mpWatchedLocationFile->flush();
168 template<
unsigned DIM,
unsigned ELEC_PROB_DIM>
173 unsigned containing_mechanics_elem = mpMeshPair->rGetCoarseElementsForFineElementCentroids()[elementIndex];
174 c_matrix<double,DIM,DIM>& r_deformation_gradient = mDeformationGradientsForEachMechanicsElement[containing_mechanics_elem];
177 c_matrix<double,DIM,DIM> inv_F =
Inverse(r_deformation_gradient);
178 mModifiedConductivityTensor = prod(inv_F, rOriginalConductivity);
179 mModifiedConductivityTensor = prod(mModifiedConductivityTensor, trans(inv_F));
181 return mModifiedConductivityTensor;
188 template<
unsigned DIM,
unsigned PROBLEM_DIM>
205 template<
unsigned DIM>
218 if (problemType == MONODOMAIN)
222 EXCEPTION(
"The second template parameter should be 2 when a bidomain problem is chosen");
229 template<
unsigned DIM>
242 if (problemType == BIDOMAIN)
246 if (problemType == BIDOMAIN_WITH_BATH)
250 EXCEPTION(
"The second template parameter should be 1 when a monodomain problem is chosen");
255 template<
unsigned DIM,
unsigned ELEC_PROB_DIM>
257 CompressibilityType compressibilityType,
258 ElectricsProblemType electricsProblemType,
263 std::string outputDirectory)
264 : mCompressibilityType(compressibilityType),
265 mpCardiacMechSolver(NULL),
266 mpMechanicsSolver(NULL),
267 mpElectricsMesh(pElectricsMesh),
268 mpMechanicsMesh(pMechanicsMesh),
269 mpProblemDefinition(pProblemDefinition),
272 mNoElectricsOutput(false),
273 mIsWatchedLocation(false),
276 mNumTimestepsToOutputDeformationGradientsAndStress(
UNSIGNED_UNSET)
300 assert(pCellFactory != NULL);
303 if (electricsProblemType == BIDOMAIN_WITH_BATH)
326 template<
unsigned DIM,
unsigned ELEC_PROB_DIM>
344 template<
unsigned DIM,
unsigned ELEC_PROB_DIM>
357 EXCEPTION(
"Electrics PDE timestep does not divide mechanics solve timestep");
365 LOG(2, DIM <<
"d Implicit CardiacElectroMechanics Simulation:");
367 LOG(2,
"Contraction model ode timestep " <<
mpProblemDefinition->GetContractionModelOdeTimestep());
368 LOG(2,
"Output is written to " <<
mOutputDirectory <<
"/[deformation/electrics]");
371 LOG(2,
"Mechanics mesh has " <<
mpMechanicsMesh->GetNumNodes() <<
" nodes");
373 LOG(2,
"Initialising..");
465 mpMeshPair->ComputeCoarseElementsForFineNodes(
false);
473 mpMeshPair->ComputeCoarseElementsForFineElementCentroids(
false);
487 template<
unsigned DIM,
unsigned ELEC_PROB_DIM>
513 Vec electrics_solution=NULL;
518 unsigned counter = 0;
524 std::vector<std::string> variable_names;
534 WRITE_QUADRATIC_MESH);
535 variable_names.push_back(
"V");
536 if (ELEC_PROB_DIM==2)
538 variable_names.push_back(
"Phi_e");
541 std::vector<std::string> regions;
542 regions.push_back(
"tissue");
543 regions.push_back(
"bath");
544 p_cmgui_writer->SetRegionNames(regions);
547 p_cmgui_writer->SetAdditionalFieldNames(variable_names);
548 p_cmgui_writer->WriteInitialMesh(
"undeformed");
558 LOG(2,
"\nSolving for initial deformation");
560 if (verbose_during_solve)
562 std::cout <<
"\n\n ** Solving for initial deformation\n";
577 unsigned total_newton_iters = 0;
578 for (
unsigned index=1; index<=
mpProblemDefinition->GetNumIncrementsForInitialDeformation(); index++)
581 if (verbose_during_solve)
583 std::cout <<
" Increment " << index <<
" of " <<
mpProblemDefinition->GetNumIncrementsForInitialDeformation() <<
"\n";
598 LOG(2,
" Number of newton iterations = " << total_newton_iters);
601 unsigned mech_writer_counter = 0;
605 LOG(2,
" Writing output");
607 mpMechanicsSolver->WriteCurrentSpatialSolution(
"solution",
"nodes",mech_writer_counter);
631 mpMechanicsSolver->WriteCurrentStrains(DEFORMATION_GRADIENT_F,
"deformation_gradient",mech_writer_counter);
632 mpMechanicsSolver->WriteCurrentAverageElementStresses(
"second_PK",mech_writer_counter);
650 while (!stepper.IsTimeAtEnd())
652 LOG(2,
"\nCurrent time = " << stepper.GetTime());
654 if (verbose_during_solve)
657 std::cout <<
"\n\n ** Current time = " << stepper.GetTime() <<
"\n";
690 unsigned containing_elem =
mpMeshPair->rGetCoarseElementsForFineNodes()[global_index];
703 LOG(2,
" Solving electrics");
711 p_electrics_solver->
SetTimes(current_time, next_time);
714 electrics_solution = p_electrics_solver->
Solve();
716 PetscReal min_voltage, max_voltage;
717 VecMax(electrics_solution,PETSC_NULL,&max_voltage);
718 VecMin(electrics_solution,PETSC_NULL,&min_voltage);
721 LOG(2,
" minimum and maximum voltage is " << min_voltage <<
", "<<max_voltage);
729 initial_voltage = electrics_solution;
746 LOG(2,
" Interpolating Ca_I and voltage");
754 VecSetValue(calcium_data, node_index ,calcium_value, INSERT_VALUES);
764 for (
unsigned i=0; i<
mpMeshPair->rGetElementsAndWeights().size(); i++)
766 double interpolated_CaI = 0;
767 double interpolated_voltage = 0;
771 for (
unsigned node_index = 0; node_index<element.
GetNumNodes(); node_index++)
774 double CaI_at_node = calcium_repl[global_index];
775 interpolated_CaI += CaI_at_node*
mpMeshPair->rGetElementsAndWeights()[i].Weights(node_index);
777 interpolated_voltage += electrics_solution_repl[global_index*ELEC_PROB_DIM]*
mpMeshPair->rGetElementsAndWeights()[i].Weights(node_index);
801 LOG(2,
" Solving mechanics ");
838 LOG(2,
" Number of newton iterations = " <<
mpMechanicsSolver->GetNumNewtonIterations());
841 stepper.AdvanceOneTimeStep();
853 LOG(2,
" Writing output");
855 mech_writer_counter++;
857 mpMechanicsSolver->WriteCurrentSpatialSolution(
"solution",
"nodes",mech_writer_counter);
875 mpMechanicsSolver->WriteCurrentStrains(DEFORMATION_GRADIENT_F,
"deformation_gradient",mech_writer_counter);
876 mpMechanicsSolver->WriteCurrentAverageElementStresses(
"second_PK",mech_writer_counter);
930 p_cmgui_writer->WriteCmguiScript(
"",
"undeformed");
934 p_cmgui_writer->WriteCmguiScript(
"../../electrics/cmgui_output/voltage_mechanics_mesh",
"undeformed");
936 delete p_cmgui_writer;
940 delete p_electrics_solver;
945 template<
unsigned DIM,
unsigned ELEC_PROB_DIM>
949 for (
unsigned i=0; i<vec.size(); i++)
951 if (vec[i]>max) max=vec[i];
956 template<
unsigned DIM,
unsigned ELEC_PROB_DIM>
962 template<
unsigned DIM,
unsigned ELEC_PROB_DIM>
969 template<
unsigned DIM,
unsigned ELEC_PROB_DIM>
975 EXCEPTION(
"Timestep provided for SetOutputDeformationGradientsAndStress() is not a multiple of mechanics solve timestep");
979 template<
unsigned DIM,
unsigned ELEC_PROB_DIM>
AbstractCardiacMechanicsSolverInterface< DIM > * mpCardiacMechSolver
virtual void WriteOneStep(double time, Vec voltageVec)=0
virtual DistributedVectorFactory * GetDistributedVectorFactory()
unsigned mNumTimestepsToOutputDeformationGradientsAndStress
void Set(unsigned level, const std::string &rDirectory, const std::string &rFileName="log.txt")
std::string mDeformationOutputDirectory
virtual void PrepareForSolve()
Hdf5DataWriter * mpWriter
virtual void OnEndOfTimeStep(unsigned counter)
void SetOutputDirectory(const std::string &rOutputDirectory)
void WriteWatchedLocationData(double time, Vec voltage)
boost::numeric::ublas::c_matrix< T, 1, 1 > Inverse(const boost::numeric::ublas::c_matrix< T, 1, 1 > &rM)
unsigned GetNodeGlobalIndex(unsigned localIndex) const
#define EXCEPTION(message)
double Max(std::vector< double > &vec)
void SetTimeStep(double dt)
static void BeginEvent(unsigned event)
void SetInitialCondition(Vec initialCondition)
virtual Vec CreateInitialCondition()
Element< ELEMENT_DIM, SPACE_DIM > * GetElement(unsigned index) const
static AbstractCardiacProblem< DIM, DIM, 2u > * Create(ElectricsProblemType problemType, AbstractCardiacCellFactory< DIM > *pCellFactory)
QuadraticMesh< DIM > * mpMechanicsMesh
virtual unsigned GetNumNodes() const
std::vector< double > mInterpolatedVoltages
double GetPdeTimeStep() const
bool OptionExists(const std::string &rOption)
unsigned mNumElecTimestepsPerMechTimestep
FineCoarseMeshPair< DIM > * mpMeshPair
bool IsGlobalIndexLocal(unsigned globalIndex)
void SetTimes(double tStart, double tEnd)
std::vector< c_vector< double, DIM > > & rGetDeformedPosition()
out_stream OpenOutputFile(const std::string &rFileName, std::ios_base::openmode mode=std::ios::out|std::ios::trunc) const
CardiacElectroMechanicsProblem(CompressibilityType compressibilityType, ElectricsProblemType electricsProblemType, TetrahedralMesh< DIM, DIM > *pElectricsMesh, QuadraticMesh< DIM > *pMechanicsMesh, AbstractCardiacCellFactory< DIM > *pCellFactory, ElectroMechanicsProblemDefinition< DIM > *pProblemDefinition, std::string outputDirectory)
void SetNoElectricsOutput()
std::string mOutputDirectory
void DetermineWatchedNodes()
CompressibilityType mCompressibilityType
ElectroMechanicsProblemDefinition< DIM > * mpProblemDefinition
AbstractCardiacTissue< ELEMENT_DIM, SPACE_DIM > * GetTissue()
unsigned GetNumNodes() const
void AdvanceAlongUnlimitedDimension()
std::vector< double > mInterpolatedCalciumConcs
const unsigned UNSIGNED_UNSET
void SetOutputDeformationGradientsAndStress(double timestep)
std::vector< c_matrix< double, DIM, DIM > > mDeformationGradientsForEachMechanicsElement
static AbstractCardiacProblem< DIM, DIM, 1u > * Create(ElectricsProblemType problemType, AbstractCardiacCellFactory< DIM > *pCellFactory)
c_matrix< double, DIM, DIM > & rCalculateModifiedConductivityTensor(unsigned elementIndex, const c_matrix< double, DIM, DIM > &rOriginalConductivity, unsigned domainIndex)
void SetMesh(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > *pMesh)
out_stream mpWatchedLocationFile
void WriteHeader(std::string simulationType="")
virtual ~CardiacElectroMechanicsProblem()
AbstractNonlinearElasticitySolver< DIM > * mpMechanicsSolver
void SetBoundaryConditionsContainer(BccType pBcc)
AbstractCardiacProblem< DIM, DIM, ELEC_PROB_DIM > * mpElectricsProblem
void WriteElapsedTime(std::string pre="")
static CommandLineArguments * Instance()
std::string GetOutputDirectory() const
std::vector< double > mStretchesForEachMechanicsElement
static LogFile * Instance()
static void EndEvent(unsigned event)
virtual void WriteFilesUsingMesh(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > &rMesh, bool keepOriginalElementIndexing=true)
void SetMatrixIsNotAssembled()
virtual AbstractDynamicLinearPdeSolver< ELEMENT_DIM, SPACE_DIM, PROBLEM_DIM > * CreateSolver()=0
void SetPrintingTimeStep(double printingTimeStep)
void SetOutputFilenamePrefix(const std::string &rOutputFilenamePrefix)
static const int WRITE_EVERY_NTH_TIME
static HeartConfig * Instance()
static AbstractCardiacProblem< DIM, DIM, PROBLEM_DIM > * Create(ElectricsProblemType problemType, AbstractCardiacCellFactory< DIM > *pCellFactory)
TetrahedralMesh< DIM, DIM > * mpElectricsMesh
c_vector< double, DIM > mWatchedLocation
void SetWatchedPosition(c_vector< double, DIM > watchedLocation)