Chaste Release::3.1
AbstractCvodeCell.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 
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::SetTimestep(double maxDt)
00078 {
00079     mMaxDt = maxDt;
00080 }
00081 
00082 
00083 void AbstractCvodeCell::SolveAndUpdateState(double tStart, double tEnd)
00084 {
00085     if (mMaxDt == DOUBLE_UNSET)
00086     {
00087         SetTimestep(HeartConfig::Instance()->GetPrintingTimeStep());
00088     }
00089     Solve(tStart, tEnd, mMaxDt);
00090 }
00091 
00092 OdeSolution AbstractCvodeCell::Compute(double tStart, double tEnd, double tSamp)
00093 {
00094     if (tSamp == 0.0)
00095     {
00096         tSamp = HeartConfig::Instance()->GetPrintingTimeStep();
00097     }
00098     if (mMaxDt == DOUBLE_UNSET)
00099     {
00100         SetTimestep(tSamp);
00101     }
00102     return Solve(tStart, tEnd, mMaxDt, tSamp);
00103 }
00104 
00105 
00106 void AbstractCvodeCell::ComputeExceptVoltage(double tStart, double tEnd)
00107 {
00108     double saved_voltage = GetVoltage();
00109 
00110     AbstractCardiacCellInterface::SetVoltageDerivativeToZero(true);
00111     SolveAndUpdateState(tStart, tEnd);
00112     AbstractCardiacCellInterface::SetVoltageDerivativeToZero(false);
00113 
00114     SetVoltage(saved_voltage); // In case of naughty models
00115 
00116 #ifndef NDEBUG
00117     //Note that tests which rely on this throwing  (e.g. such-and-such a variable is out of range)
00118     //ought to be anotated with the NDEBUG macro
00119     VerifyStateVariables();
00120 #endif // NDEBUG
00121 }
00122 
00123 
00124 void AbstractCvodeCell::SetVoltageDerivativeToZero(bool clamp)
00125 {
00126     if (clamp != mSetVoltageDerivativeToZero)
00127     {
00128         ResetSolver();
00129     }
00130     AbstractCardiacCellInterface::SetVoltageDerivativeToZero(clamp);
00131 }
00132 
00133 unsigned AbstractCvodeCell::GetNumberOfStateVariables() const
00134 {
00135     return AbstractCvodeSystem::GetNumberOfStateVariables();
00136 }
00137 
00138 unsigned AbstractCvodeCell::GetNumberOfParameters() const
00139 {
00140     return AbstractCvodeSystem::GetNumberOfParameters();
00141 }
00142 
00143 std::vector<double> AbstractCvodeCell::GetStdVecStateVariables()
00144 {
00145     std::vector<double> state_variables(GetNumberOfStateVariables());
00146     CopyToStdVector(AbstractCvodeSystem::rGetStateVariables(), state_variables);
00147     return state_variables;
00148 }
00149 
00150 const std::vector<std::string>& AbstractCvodeCell::rGetStateVariableNames() const
00151 {
00152     return AbstractCvodeSystem::rGetStateVariableNames();
00153 }
00154 
00155 void AbstractCvodeCell::SetStateVariables(const std::vector<double>& rVariables)
00156 {
00157     N_Vector vars = MakeNVector(rVariables);
00158     AbstractCvodeSystem::SetStateVariables(vars);
00159     DeleteVector(vars);
00160 }
00161 
00162 void AbstractCvodeCell::SetStateVariables(const N_Vector& rVariables)
00163 {
00164     AbstractCvodeSystem::SetStateVariables(rVariables);
00165 }
00166 
00167 void AbstractCvodeCell::SetStateVariable(unsigned index, double newValue)
00168 {
00169     AbstractCvodeSystem::SetStateVariable(index, newValue);
00170 }
00171 
00172 void AbstractCvodeCell::SetStateVariable(const std::string& rName, double newValue)
00173 {
00174     AbstractCvodeSystem::SetStateVariable(rName, newValue);
00175 }
00176 
00177 double AbstractCvodeCell::GetAnyVariable(const std::string& rName, double time)
00178 {
00179     return AbstractCvodeSystem::GetAnyVariable(rName,time);
00180 }
00181 
00182 double AbstractCvodeCell::GetParameter(const std::string& rParameterName)
00183 {
00184     return AbstractCvodeSystem::GetParameter(rParameterName);
00185 }
00186 
00187 double AbstractCvodeCell::GetParameter(unsigned parameterIndex)
00188 {
00189     return AbstractCvodeSystem::GetParameter(parameterIndex);
00190 }
00191 
00192 void AbstractCvodeCell::SetParameter(const std::string& rParameterName, double value)
00193 {
00194     AbstractCvodeSystem::SetParameter(rParameterName,value);
00195 }
00196 
00197 void AbstractCvodeCell::SetParameter(unsigned parameterIndex, double value)
00198 {
00199     AbstractCvodeSystem::SetParameter(parameterIndex,value);
00200 }
00201 
00202 
00203 #endif // CHASTE_CVODE