Chaste  Release::2017.1
ImplicitCardiacMechanicsSolver.cpp
1 /*
2 
3 Copyright (c) 2005-2017, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #include "ImplicitCardiacMechanicsSolver.hpp"
37 
38 template<class ELASTICITY_SOLVER,unsigned DIM>
40  QuadraticMesh<DIM>& rQuadMesh,
41  ElectroMechanicsProblemDefinition<DIM>& rProblemDefinition,
42  std::string outputDirectory)
44  rProblemDefinition,
45  outputDirectory)
46 {
47 
48 }
49 
50 template<class ELASTICITY_SOLVER,unsigned DIM>
52 {
53 }
54 
55 
56 template<class ELASTICITY_SOLVER,unsigned DIM>
57 void ImplicitCardiacMechanicsSolver<ELASTICITY_SOLVER,DIM>::Solve(double time, double nextTime, double odeTimestep)
58 {
59  // set the times, which are used in AssembleOnElement
60  assert(time < nextTime);
61  this->mCurrentTime = time;
62  this->mNextTime = nextTime;
63  this->mOdeTimestep = odeTimestep;
64 
65  // solve
66  ELASTICITY_SOLVER::Solve();
67 
68  // assemble residual again (to solve the cell models implicitly again
69  // using the correct value of the deformation x (in case this wasn't the
70  // last thing that was done
71  if (this->GetNumNewtonIterations() > 0)
72  {
73  this->AssembleSystem(true,false);
74  }
75 
76  // now update state variables, and set lambda at last timestep. Note
77  // stretches were set in AssembleOnElement
78  for (std::map<unsigned,DataAtQuadraturePoint>::iterator iter = this->mQuadPointToDataAtQuadPointMap.begin();
79  iter != this->mQuadPointToDataAtQuadPointMap.end();
80  iter++)
81  {
82  AbstractContractionModel* p_contraction_model = iter->second.ContractionModel;
83  iter->second.StretchLastTimeStep = iter->second.Stretch;
84  p_contraction_model->UpdateStateVariables();
85  }
86 }
87 
88 template<class ELASTICITY_SOLVER,unsigned DIM>
90  unsigned currentQuadPointGlobalIndex,
91  bool assembleJacobian,
92  double& rActiveTension,
93  double& rDerivActiveTensionWrtLambda,
94  double& rDerivActiveTensionWrtDLambdaDt)
95 {
96  // The iterator should be pointing to the right place (note: it is incremented at the end of this method)
97  // This iterator is used so that we don't have to search the map
98  assert(this->mMapIterator->first==currentQuadPointGlobalIndex);
99 
100  DataAtQuadraturePoint& r_data_at_quad_point = this->mMapIterator->second;
101 
102  // save this fibre stretch
103  r_data_at_quad_point.Stretch = currentFibreStretch;
104 
105  // compute dlam/dt
106  double dlam_dt = (currentFibreStretch-r_data_at_quad_point.StretchLastTimeStep)/(this->mNextTime-this->mCurrentTime);
107 
108  // Set this stretch and stretch rate on contraction model
109  AbstractContractionModel* p_contraction_model = r_data_at_quad_point.ContractionModel;
110  p_contraction_model->SetStretchAndStretchRate(currentFibreStretch, dlam_dt);
111 
112  // Call RunDoNotUpdate() on the contraction model to solve it using this stretch, and get the active tension
113  try
114  {
115  p_contraction_model->RunDoNotUpdate(this->mCurrentTime,this->mNextTime,this->mOdeTimestep);
116  rActiveTension = p_contraction_model->GetNextActiveTension();
117  }
118  // LCOV_EXCL_START
119  catch (Exception&)
120  {
121  // if this failed during assembling the Jacobian this is a fatal error.
122  if (assembleJacobian)
123  {
124  // probably shouldn't be able to get here
125  EXCEPTION("Failure in solving contraction models using current stretches for assembling Jacobian");
126  }
127  // if this failed during assembling the residual, the stretches are too large, so we just
128  // set the active tension to infinity so that the residual will be infinite
129  rActiveTension = DBL_MAX;
130  std::cout << "WARNING: could not solve contraction model with this stretch and stretch rate. "
131  << "Setting active tension to infinity (DBL_MAX) so that the residual(-norm) is also infinite\n" << std::flush;
133  return;
134  }
135  // LCOV_EXCL_STOP
136 
137  // if assembling the Jacobian, numerically evaluate dTa/dlam & dTa/d(lamdot)
138  if (assembleJacobian)
139  {
140  // get active tension for (lam+h,dlamdt)
141  double h1 = std::max(1e-6, currentFibreStretch/100);
142  p_contraction_model->SetStretchAndStretchRate(currentFibreStretch+h1, dlam_dt);
143  p_contraction_model->RunDoNotUpdate(this->mCurrentTime,this->mNextTime,this->mOdeTimestep);
144  double active_tension_at_lam_plus_h = p_contraction_model->GetNextActiveTension();
145 
146  // get active tension for (lam,dlamdt+h)
147  double h2 = std::max(1e-6, dlam_dt/100);
148  p_contraction_model->SetStretchAndStretchRate(currentFibreStretch, dlam_dt+h2);
149  p_contraction_model->RunDoNotUpdate(this->mCurrentTime,this->mNextTime,this->mOdeTimestep);
150  double active_tension_at_dlamdt_plus_h = p_contraction_model->GetNextActiveTension();
151 
152  rDerivActiveTensionWrtLambda = (active_tension_at_lam_plus_h - rActiveTension)/h1;
153  rDerivActiveTensionWrtDLambdaDt = (active_tension_at_dlamdt_plus_h - rActiveTension)/h2;
154  }
155 
156  // Increment the iterator
157  this->mMapIterator++;
158  if (this->mMapIterator==this->mQuadPointToDataAtQuadPointMap.end())
159  {
160  this->mMapIterator = this->mQuadPointToDataAtQuadPointMap.begin();
161  }
162 }
163 
164 // Explicit instantiation
ImplicitCardiacMechanicsSolver(QuadraticMesh< DIM > &rQuadMesh, ElectroMechanicsProblemDefinition< DIM > &rProblemDefinition, std::string outputDirectory)
virtual void UpdateStateVariables()=0
void GetActiveTensionAndTensionDerivs(double currentFibreStretch, unsigned currentQuadPointGlobalIndex, bool assembleJacobian, double &rActiveTension, double &rDerivActiveTensionWrtLambda, double &rDerivActiveTensionWrtDLambdaDt)
#define EXCEPTION(message)
Definition: Exception.hpp:143
std::map< unsigned, DataAtQuadraturePoint >::iterator mMapIterator
std::map< unsigned, DataAtQuadraturePoint > mQuadPointToDataAtQuadPointMap
virtual void SetStretchAndStretchRate(double stretch, double stretchRate)=0
#define NEVER_REACHED
Definition: Exception.hpp:206
AbstractContractionModel * ContractionModel
virtual void RunDoNotUpdate(double startTime, double endTime, double timeStep)=0
virtual double GetNextActiveTension()=0
void Solve(double time, double nextTime, double odeTimestep)