Chaste  Release::2018.1
AbstractGeneralizedRushLarsenCardiacCell.cpp
1 /*
2 
3 Copyright (c) 2005-2018, 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 /*
37 Megan E. Marsh, Raymond J. Spiteri
38 Numerical Simulation Laboratory
39 University of Saskatchewan
40 December 2011
41 Partial support provided by research grants from the National
42 Science and Engineering Research Council (NSERC) of Canada
43 and the MITACS/Mprime Canadian Network of Centres of Excellence.
44 */
45 
46 #include "AbstractGeneralizedRushLarsenCardiacCell.hpp"
47 #include <cassert>
48 #include <cmath>
49 #include "Exception.hpp"
50 #include "OdeSolution.hpp"
51 #include "TimeStepper.hpp"
52 
54  unsigned voltageIndex,
55  boost::shared_ptr<AbstractStimulusFunction> pIntracellularStimulus)
56  : AbstractCardiacCell(boost::shared_ptr<AbstractIvpOdeSolver>(),
57  numberOfStateVariables,
58  voltageIndex,
59  pIntracellularStimulus),
60  mHasAnalyticJacobian(false)
61 {
62  mPartialF.resize(numberOfStateVariables);
63  mEvalF.resize(numberOfStateVariables);
64  mYInit.resize(numberOfStateVariables);
65 }
66 
68 {}
69 
70 OdeSolution AbstractGeneralizedRushLarsenCardiacCell::Compute(double tStart, double tEnd, double tSamp)
71 {
72  // Check length of time interval
73  if (tSamp < mDt)
74  {
75  tSamp = mDt;
76  }
77  const unsigned n_steps = (unsigned) floor((tEnd - tStart)/tSamp + 0.5);
78  assert(fabs(tStart+n_steps*tSamp - tEnd) < 1e-12);
79  const unsigned n_small_steps = (unsigned) floor(tSamp/mDt+0.5);
80  assert(fabs(mDt*n_small_steps - tSamp) < 1e-12);
81 
82  // Initialise solution store
83  OdeSolution solutions;
84  solutions.SetNumberOfTimeSteps(n_steps);
85  solutions.rGetSolutions().push_back(rGetStateVariables());
86  solutions.rGetTimes().push_back(tStart);
87  solutions.SetOdeSystemInformation(this->mpSystemInfo);
88 
89  // Loop over time
90  double v_old, v_new;
91  double& r_V = rGetStateVariables()[GetVoltageIndex()];
92  for (unsigned i=0; i<n_steps; i++)
93  {
94  double curr_time = tStart;
95  for (unsigned j=0; j<n_small_steps; j++)
96  {
97  curr_time = tStart + i*tSamp + j*mDt;
98  v_old = r_V;
100  v_new = r_V;
101  r_V = v_old;
102  ComputeOneStepExceptVoltage(curr_time);
103  r_V = v_new;
105  }
106 
107  // Update solutions
108  solutions.rGetSolutions().push_back(rGetStateVariables());
109  solutions.rGetTimes().push_back(curr_time+mDt);
110  }
111 
112  return solutions;
113 }
114 
116 {
118  TimeStepper stepper(tStart, tEnd, mDt);
119 
120  while (!stepper.IsTimeAtEnd())
121  {
123 
124 #ifndef NDEBUG
125  // Check gating variables are still in range
127 #endif // NDEBUG
128 
129  stepper.AdvanceOneTimeStep();
130  }
132 }
133 
135 {
136  TimeStepper stepper(tStart, tEnd, mDt);
137 
138  double v_old, v_new;
139  double& r_V = rGetStateVariables()[GetVoltageIndex()];
140  while (!stepper.IsTimeAtEnd())
141  {
142  v_old = r_V;
144  v_new = r_V;
145  r_V = v_old;
147  r_V = v_new;
149 
150  stepper.AdvanceOneTimeStep();
151  }
152 }
153 
155 {
156  return mHasAnalyticJacobian;
157 }
158 
160 {
161  if (!useNumericalJacobian)
162  {
163  EXCEPTION("Using analytic Jacobian terms for generalised Rush-Larsen is not yet supported.");
164  }
165 // if (!useNumericalJacobian && !mHasAnalyticJacobian)
166 // {
167 // EXCEPTION("Analytic Jacobian requested, but this ODE system doesn't have one. You can check this with HasAnalyticJacobian().");
168 // }
169  mUseAnalyticJacobian = !useNumericalJacobian;
170 }
std::vector< std::vector< double > > & rGetSolutions()
double GetTime() const
#define EXCEPTION(message)
Definition: Exception.hpp:143
void AdvanceOneTimeStep()
bool IsTimeAtEnd() const
void SetOdeSystemInformation(boost::shared_ptr< const AbstractOdeSystemInformation > pOdeSystemInfo)
Definition: OdeSolution.cpp:66
std::vector< double > & rGetTimes()
virtual void SetVoltageDerivativeToZero(bool clamp=true)
boost::shared_ptr< AbstractOdeSystemInformation > mpSystemInfo
AbstractGeneralizedRushLarsenCardiacCell(unsigned numberOfStateVariables, unsigned voltageIndex, boost::shared_ptr< AbstractStimulusFunction > pIntracellularStimulus)
virtual void UpdateTransmembranePotential(double time)=0
OdeSolution Compute(double tStart, double tEnd, double tSamp=0.0)
virtual void ComputeOneStepExceptVoltage(double time)=0
void SetNumberOfTimeSteps(unsigned numTimeSteps)
Definition: OdeSolution.cpp:58