Chaste Release::3.1
AbstractCardiacCell.cpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 #include "AbstractCardiacCell.hpp"
00036 
00037 #include <cassert>
00038 #include <iostream>
00039 
00040 #include "HeartConfig.hpp"
00041 #include "Exception.hpp"
00042 
00043 AbstractCardiacCell::AbstractCardiacCell(boost::shared_ptr<AbstractIvpOdeSolver> pOdeSolver,
00044                                          unsigned numberOfStateVariables,
00045                                          unsigned voltageIndex,
00046                                          boost::shared_ptr<AbstractStimulusFunction> pIntracellularStimulus)
00047     : AbstractCardiacCellInterface(pOdeSolver, voltageIndex, pIntracellularStimulus),
00048       AbstractOdeSystem(numberOfStateVariables),
00049       mDt(HeartConfig::Instance()->GetOdeTimeStep())
00050 {
00051     // The second clause is to allow for FakeBathCell.
00052     assert(voltageIndex < mNumberOfStateVariables || mNumberOfStateVariables == 0);
00053 }
00054 
00055 AbstractCardiacCell::~AbstractCardiacCell()
00056 {
00057 }
00058 
00059 void AbstractCardiacCell::Init()
00060 {
00061     ResetToInitialConditions();
00062     mParameters.resize(rGetParameterNames().size());
00063 }
00064 
00065 void AbstractCardiacCell::SetTimestep(double dt)
00066 {
00067     mDt = dt;
00068 }
00069 
00070 void AbstractCardiacCell::SolveAndUpdateState(double tStart, double tEnd)
00071 {
00072     mpOdeSolver->SolveAndUpdateStateVariable(this, tStart, tEnd, mDt);
00073 }
00074 
00075 OdeSolution AbstractCardiacCell::Compute(double tStart, double tEnd, double tSamp)
00076 {
00077     if (tSamp < mDt)
00078     {
00079         tSamp = mDt;
00080     }
00081     return mpOdeSolver->Solve(this, rGetStateVariables(), tStart, tEnd, mDt, tSamp);
00082 }
00083 
00084 void AbstractCardiacCell::ComputeExceptVoltage(double tStart, double tEnd)
00085 {
00086     double saved_voltage = GetVoltage();
00087 
00088     SetVoltageDerivativeToZero(true);
00089     mpOdeSolver->SolveAndUpdateStateVariable(this, tStart, tEnd, mDt);
00090     SetVoltageDerivativeToZero(false);
00091 
00092     SetVoltage(saved_voltage); // In case of naughty models
00093 
00094 #ifndef NDEBUG
00095     //Note that tests which rely on this throwing  (e.g. such-and-such a variable is out of range)
00096     //ought to be anotated with the NDEBUG macro
00097     VerifyStateVariables();
00098 #endif // NDEBUG
00099 }
00100 
00101 void AbstractCardiacCell::SetVoltage(double voltage)
00102 {
00103     SetAnyVariable(mVoltageIndex, voltage);
00104     SetFixedVoltage(voltage);
00105 }
00106 
00107 double AbstractCardiacCell::GetVoltage()
00108 {
00109     return AbstractOdeSystem::GetAnyVariable(mVoltageIndex);
00110 }
00111 
00112 unsigned AbstractCardiacCell::GetNumberOfStateVariables() const
00113 {
00114     return AbstractOdeSystem::GetNumberOfStateVariables();
00115 }
00116 
00117 unsigned AbstractCardiacCell::GetNumberOfParameters() const
00118 {
00119     return AbstractOdeSystem::GetNumberOfParameters();
00120 }
00121 
00122 std::vector<double> AbstractCardiacCell::GetStdVecStateVariables()
00123 {
00124     return AbstractOdeSystem::GetStateVariables();
00125 }
00126 
00127 const std::vector<std::string>& AbstractCardiacCell::rGetStateVariableNames() const
00128 {
00129     return AbstractOdeSystem::rGetStateVariableNames();
00130 }
00131 
00132 void AbstractCardiacCell::SetStateVariables(const std::vector<double>& rVariables)
00133 {
00134     AbstractOdeSystem::SetStateVariables(rVariables);
00135 }
00136 
00137 void AbstractCardiacCell::SetStateVariable(unsigned index, double newValue)
00138 {
00139     AbstractOdeSystem::SetStateVariable(index, newValue);
00140 }
00141 
00142 void AbstractCardiacCell::SetStateVariable(const std::string& rName, double newValue)
00143 {
00144     AbstractOdeSystem::SetStateVariable(rName, newValue);
00145 }
00146 
00147 double AbstractCardiacCell::GetAnyVariable(const std::string& rName, double time)
00148 {
00149     return AbstractOdeSystem::GetAnyVariable(rName, time);
00150 }
00151 
00152 double AbstractCardiacCell::GetParameter(const std::string& rParameterName)
00153 {
00154     return AbstractOdeSystem::GetParameter(rParameterName);
00155 }
00156 
00157 double AbstractCardiacCell::GetParameter(unsigned parameterIndex)
00158 {
00159     return AbstractOdeSystem::GetParameter(parameterIndex);
00160 }
00161 
00162 void AbstractCardiacCell::SetParameter(const std::string& rParameterName, double value)
00163 {
00164     AbstractOdeSystem::SetParameter(rParameterName,value);
00165 }
00166 
00167 void AbstractCardiacCell::SetParameter(unsigned parameterIndex, double value)
00168 {
00169     AbstractOdeSystem::SetParameter(parameterIndex,value);
00170 }
00171 
00172 #include "LuoRudy1991.hpp"
00173 #include "LuoRudy1991BackwardEuler.hpp"
00174 void AbstractCardiacCell::CheckForArchiveFix()
00175 {
00176     if (dynamic_cast<CellLuoRudy1991FromCellML*>(this) || dynamic_cast<CellLuoRudy1991FromCellMLBackwardEuler*>(this))
00177     {
00178         // The LR91 model saved in previous Chaste versions had a different ordering of state variables...
00179         // Old is h, j, m, CaI, V, d, f, x
00180         // New is V, m, h, j, d, f, X, [Ca]
00181         assert(GetNumberOfStateVariables() == 8);
00182         unsigned var_index_map[8] = {2, 3, 1, 7, 0, 4, 5, 6};
00183         std::vector<double> old_state(this->mStateVariables);
00184         for (unsigned i=0; i<8; i++)
00185         {
00186             this->mStateVariables[var_index_map[i]] = old_state[i];
00187         }
00188         // It also didn't use to have parameters...
00189         this->mParameters.resize(this->rGetParameterNames().size());
00190         assert(this->mParameters.size() == 2u);
00191         this->mParameters[0] = 23.0;
00192         this->mParameters[1] = 0.282;
00193     }
00194 }
00195 
00196 
00197 /*
00198  *  METHODS NEEDED BY FAST CARDIAC CELLS
00199  */
00200 void AbstractCardiacCell::SetState(CellModelState state)
00201 {
00202     EXCEPTION("Non fast-slow cell model being used in a fast-slow problem.");
00203 }
00204 
00205 void AbstractCardiacCell::SetSlowValues(const std::vector<double> &rSlowValues)
00206 {
00207     EXCEPTION("Non fast-slow cell model being used in a fast-slow problem.");
00208 }
00209 
00210 void AbstractCardiacCell::GetSlowValues(std::vector<double>& rSlowValues)
00211 {
00212     EXCEPTION("Non fast-slow cell model being used in a fast-slow problem.");
00213 }
00214 
00215 bool AbstractCardiacCell::IsFastOnly()
00216 {
00217     EXCEPTION("Non fast-slow cell model being used in a fast-slow problem.");
00218 }
00219 
00220 unsigned AbstractCardiacCell::GetNumSlowValues()
00221 {
00222     EXCEPTION("Non fast-slow cell model being used in a fast-slow problem.");
00223 }
00224 
00225 void AbstractCardiacCell::AdjustOutOfRangeSlowValues(std::vector<double>& rSlowValues)
00226 {
00227     EXCEPTION("Non fast-slow cell model being used in a fast-slow problem.");
00228 }