AbstractCvodeCell.cpp

00001 /*
00002 
00003 Copyright (c) 2005-2015, 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 
00036 #ifdef CHASTE_CVODE
00037 
00038 #include <sstream>
00039 #include <iostream>
00040 #include <cmath>
00041 
00042 #include "AbstractCvodeCell.hpp"
00043 #include "Exception.hpp"
00044 #include "HeartConfig.hpp"
00045 #include "VectorHelperFunctions.hpp"
00046 
00047 
00048 AbstractCvodeCell::AbstractCvodeCell(boost::shared_ptr<AbstractIvpOdeSolver> /* unused */,
00049                                      unsigned numberOfStateVariables,
00050                                      unsigned voltageIndex,
00051                                      boost::shared_ptr<AbstractStimulusFunction> pIntracellularStimulus)
00052     : AbstractCardiacCellInterface(boost::shared_ptr<AbstractIvpOdeSolver>(), voltageIndex, pIntracellularStimulus),
00053       AbstractCvodeSystem(numberOfStateVariables),
00054       mMaxDt(DOUBLE_UNSET)
00055 {
00056 }
00057 
00058 
00059 AbstractCvodeCell::~AbstractCvodeCell()
00060 {
00061 }
00062 
00063 double AbstractCvodeCell::GetVoltage()
00064 {
00065     assert(mStateVariables);
00066     return AbstractCvodeSystem::GetAnyVariable(mVoltageIndex);
00067 }
00068 
00069 void AbstractCvodeCell::SetVoltage(double voltage)
00070 {
00071     assert(mStateVariables);
00072     SetAnyVariable(mVoltageIndex, voltage);
00073     SetFixedVoltage(voltage);
00074 }
00075 
00076 
00077 void AbstractCvodeCell::SetMaxTimestep(double maxDt)
00078 {
00079     SetTimestep(maxDt); // Note: SetTimestep() sets the maximum timestep.
00080 }
00081 
00082 void AbstractCvodeCell::SetTimestep(double maxDt)
00083 {
00084     mMaxDt = maxDt;
00085 }
00086 
00087 double AbstractCvodeCell::GetTimestep()
00088 {
00089     return mMaxDt;
00090 }
00091 
00092 
00093 void AbstractCvodeCell::SolveAndUpdateState(double tStart, double tEnd)
00094 {
00095     if (mMaxDt == DOUBLE_UNSET)
00096     {
00097         SetTimestep(HeartConfig::Instance()->GetPrintingTimeStep());
00098     }
00099     Solve(tStart, tEnd, mMaxDt);
00100 }
00101 
00102 OdeSolution AbstractCvodeCell::Compute(double tStart, double tEnd, double tSamp)
00103 {
00104     if (tSamp == 0.0)
00105     {
00106         tSamp = HeartConfig::Instance()->GetPrintingTimeStep();
00107     }
00108     if (mMaxDt == DOUBLE_UNSET)
00109     {
00110         SetTimestep(tSamp);
00111     }
00112     return Solve(tStart, tEnd, mMaxDt, tSamp);
00113 }
00114 
00115 
00116 void AbstractCvodeCell::ComputeExceptVoltage(double tStart, double tEnd)
00117 {
00118     double saved_voltage = GetVoltage();
00119 
00120     AbstractCardiacCellInterface::SetVoltageDerivativeToZero(true);
00121     SolveAndUpdateState(tStart, tEnd);
00122     AbstractCardiacCellInterface::SetVoltageDerivativeToZero(false);
00123 
00124     SetVoltage(saved_voltage); // In case of naughty models
00125 
00126 #ifndef NDEBUG
00127     // Note that tests which rely on this throwing  (e.g. such-and-such a variable is out of range)
00128     // ought to be annotated with the NDEBUG macro
00129     VerifyStateVariables();
00130 #endif // NDEBUG
00131 }
00132 
00133 
00134 void AbstractCvodeCell::SetVoltageDerivativeToZero(bool clamp)
00135 {
00136     if (clamp != mSetVoltageDerivativeToZero)
00137     {
00138         ResetSolver();
00139     }
00140     AbstractCardiacCellInterface::SetVoltageDerivativeToZero(clamp);
00141 }
00142 
00143 unsigned AbstractCvodeCell::GetNumberOfStateVariables() const
00144 {
00145     return AbstractCvodeSystem::GetNumberOfStateVariables();
00146 }
00147 
00148 unsigned AbstractCvodeCell::GetNumberOfParameters() const
00149 {
00150     return AbstractCvodeSystem::GetNumberOfParameters();
00151 }
00152 
00153 std::vector<double> AbstractCvodeCell::GetStdVecStateVariables()
00154 {
00155     std::vector<double> state_variables(GetNumberOfStateVariables());
00156     CopyToStdVector(AbstractCvodeSystem::rGetStateVariables(), state_variables);
00157     return state_variables;
00158 }
00159 
00160 const std::vector<std::string>& AbstractCvodeCell::rGetStateVariableNames() const
00161 {
00162     return AbstractCvodeSystem::rGetStateVariableNames();
00163 }
00164 
00165 void AbstractCvodeCell::SetStateVariables(const std::vector<double>& rVariables)
00166 {
00167     N_Vector vars = MakeNVector(rVariables);
00168     AbstractCvodeSystem::SetStateVariables(vars);
00169     DeleteVector(vars);
00170 }
00171 
00172 void AbstractCvodeCell::SetStateVariables(const N_Vector& rVariables)
00173 {
00174     AbstractCvodeSystem::SetStateVariables(rVariables);
00175 }
00176 
00177 void AbstractCvodeCell::SetStateVariable(unsigned index, double newValue)
00178 {
00179     AbstractCvodeSystem::SetStateVariable(index, newValue);
00180 }
00181 
00182 void AbstractCvodeCell::SetStateVariable(const std::string& rName, double newValue)
00183 {
00184     AbstractCvodeSystem::SetStateVariable(rName, newValue);
00185 }
00186 
00187 double AbstractCvodeCell::GetAnyVariable(const std::string& rName, double time)
00188 {
00189     return AbstractCvodeSystem::GetAnyVariable(rName,time);
00190 }
00191 
00192 double AbstractCvodeCell::GetParameter(const std::string& rParameterName)
00193 {
00194     return AbstractCvodeSystem::GetParameter(rParameterName);
00195 }
00196 
00197 double AbstractCvodeCell::GetParameter(unsigned parameterIndex)
00198 {
00199     return AbstractCvodeSystem::GetParameter(parameterIndex);
00200 }
00201 
00202 void AbstractCvodeCell::SetParameter(const std::string& rParameterName, double value)
00203 {
00204     AbstractCvodeSystem::SetParameter(rParameterName,value);
00205 }
00206 
00207 #endif // CHASTE_CVODE

Generated by  doxygen 1.6.2