Chaste  Release::3.4
ImplicitCardiacMechanicsSolver.cpp
1 /*
2 
3 Copyright (c) 2005-2016, 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 
89 
90 
91 template<class ELASTICITY_SOLVER,unsigned DIM>
93  unsigned currentQuadPointGlobalIndex,
94  bool assembleJacobian,
95  double& rActiveTension,
96  double& rDerivActiveTensionWrtLambda,
97  double& rDerivActiveTensionWrtDLambdaDt)
98 {
99  // The iterator should be pointing to the right place (note: it is incremented at the end of this method)
100  // This iterator is used so that we don't have to search the map
101  assert(this->mMapIterator->first==currentQuadPointGlobalIndex);
102 
103  DataAtQuadraturePoint& r_data_at_quad_point = this->mMapIterator->second;
104 
105  // save this fibre stretch
106  r_data_at_quad_point.Stretch = currentFibreStretch;
107 
108  // compute dlam/dt
109  double dlam_dt = (currentFibreStretch-r_data_at_quad_point.StretchLastTimeStep)/(this->mNextTime-this->mCurrentTime);
110 
111  // Set this stretch and stretch rate on contraction model
112  AbstractContractionModel* p_contraction_model = r_data_at_quad_point.ContractionModel;
113  p_contraction_model->SetStretchAndStretchRate(currentFibreStretch, dlam_dt);
114 
115  // Call RunDoNotUpdate() on the contraction model to solve it using this stretch, and get the active tension
116  try
117  {
118  p_contraction_model->RunDoNotUpdate(this->mCurrentTime,this->mNextTime,this->mOdeTimestep);
119  rActiveTension = p_contraction_model->GetNextActiveTension();
120  }
121  catch (Exception&)
122  {
123  #define COVERAGE_IGNORE
124  // if this failed during assembling the Jacobian this is a fatal error.
125  if(assembleJacobian)
126  {
127  // probably shouldn't be able to get here
128  EXCEPTION("Failure in solving contraction models using current stretches for assembling Jacobian");
129  }
130  // if this failed during assembling the residual, the stretches are too large, so we just
131  // set the active tension to infinity so that the residual will be infinite
132  rActiveTension = DBL_MAX;
133  std::cout << "WARNING: could not solve contraction model with this stretch and stretch rate. "
134  << "Setting active tension to infinity (DBL_MAX) so that the residual(-norm) is also infinite\n" << std::flush;
135  assert(0); // just to see if we ever get here, can be removed..
136  return;
137  #undef COVERAGE_IGNORE
138  }
139 
140  // if assembling the Jacobian, numerically evaluate dTa/dlam & dTa/d(lamdot)
141  if(assembleJacobian)
142  {
143  // get active tension for (lam+h,dlamdt)
144  double h1 = std::max(1e-6, currentFibreStretch/100);
145  p_contraction_model->SetStretchAndStretchRate(currentFibreStretch+h1, dlam_dt);
146  p_contraction_model->RunDoNotUpdate(this->mCurrentTime,this->mNextTime,this->mOdeTimestep);
147  double active_tension_at_lam_plus_h = p_contraction_model->GetNextActiveTension();
148 
149  // get active tension for (lam,dlamdt+h)
150  double h2 = std::max(1e-6, dlam_dt/100);
151  p_contraction_model->SetStretchAndStretchRate(currentFibreStretch, dlam_dt+h2);
152  p_contraction_model->RunDoNotUpdate(this->mCurrentTime,this->mNextTime,this->mOdeTimestep);
153  double active_tension_at_dlamdt_plus_h = p_contraction_model->GetNextActiveTension();
154 
155  rDerivActiveTensionWrtLambda = (active_tension_at_lam_plus_h - rActiveTension)/h1;
156  rDerivActiveTensionWrtDLambdaDt = (active_tension_at_dlamdt_plus_h - rActiveTension)/h2;
157  }
158 
159  // increment the iterator
160  this->mMapIterator++;
161  if(this->mMapIterator==this->mQuadPointToDataAtQuadPointMap.end())
162  {
163  this->mMapIterator = this->mQuadPointToDataAtQuadPointMap.begin();
164  }
165 
166 }
167 
168 
169 
174 
175 
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
virtual void SetStretchAndStretchRate(double stretch, double stretchRate)=0
AbstractContractionModel * ContractionModel
virtual void RunDoNotUpdate(double startTime, double endTime, double timeStep)=0
virtual double GetNextActiveTension()=0
void Solve(double time, double nextTime, double odeTimestep)