AbstractOdeSystem.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2009
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 #include <sstream>
00030 
00031 #include "AbstractOdeSystem.hpp"
00032 
00033 AbstractOdeSystem::AbstractOdeSystem(unsigned numberOfStateVariables)
00034     : mNumberOfStateVariables(numberOfStateVariables),
00035       mUseAnalyticJacobian(false)
00036 {
00037 }
00038 
00039 AbstractOdeSystem::~AbstractOdeSystem()
00040 {}
00041 
00042 bool AbstractOdeSystem::CalculateStoppingEvent(double time, const std::vector<double>& rY)
00043 {
00044     return false;
00045 }
00046 
00047 std::string AbstractOdeSystem::DumpState(const std::string& rMessage,
00048                                          std::vector<double> Y)
00049 {
00050     std::stringstream res;
00051     res << rMessage << "\nState:\n";
00052     if (Y.empty())
00053     {
00054         Y = rGetStateVariables();
00055     }
00056     const std::vector<std::string>& r_var_names = rGetVariableNames();
00057     assert(Y.size() == r_var_names.size());
00058     for (unsigned i=0; i<r_var_names.size(); i++)
00059     {
00060         res << "\t" << r_var_names[i] << ":" << Y[i] << "\n";
00061     }
00062     return res.str();
00063 }
00064 
00065 unsigned AbstractOdeSystem::GetNumberOfStateVariables() const
00066 {
00067     return mNumberOfStateVariables;
00068 }
00069 
00070 
00071 unsigned AbstractOdeSystem::GetNumberOfParameters() const
00072 {
00073     return mParameters.size();
00074 }
00075 
00076 double AbstractOdeSystem::GetParameter(unsigned index) const
00077 {
00078     return mParameters[index];
00079 }
00080 
00081 void AbstractOdeSystem::SetParameter(unsigned index, double value)
00082 {
00083     mParameters[index] = value;
00084 }
00085 
00086 const std::vector<std::string>& AbstractOdeSystem::rGetParameterNames() const
00087 {
00088     assert(mpSystemInfo);
00089     return mpSystemInfo->rGetParameterNames();
00090 }
00091 
00092 const std::vector<std::string>& AbstractOdeSystem::rGetParameterUnits() const
00093 {
00094     assert(mpSystemInfo);
00095     return mpSystemInfo->rGetParameterUnits();
00096 }
00097 
00098 
00099 void AbstractOdeSystem::SetInitialConditions(const std::vector<double>& rInitialConditions)
00100 {
00101     if (rInitialConditions.size() != mNumberOfStateVariables)
00102     {
00103         EXCEPTION("The number of initial conditions must be that of the number of state variables");
00104     }
00105     assert(mpSystemInfo);
00106     mpSystemInfo->SetInitialConditions(rInitialConditions);
00107 }
00108 
00109 void AbstractOdeSystem::SetInitialConditionsComponent(unsigned index, double initialCondition)
00110 {
00111     if (index >= mNumberOfStateVariables)
00112     {
00113         EXCEPTION("Index is greater than the number of state variables");
00114     }
00115     assert(mpSystemInfo);
00116     mpSystemInfo->SetInitialConditionsComponent(index, initialCondition);
00117 }
00118 
00119 std::vector<double> AbstractOdeSystem::GetInitialConditions() const
00120 {
00121     assert(mpSystemInfo);
00122     return mpSystemInfo->GetInitialConditions();
00123 }
00124 
00125 void AbstractOdeSystem::SetStateVariables(const std::vector<double>& rStateVariables)
00126 {
00127     if ( mNumberOfStateVariables != rStateVariables.size() )
00128     {
00129         EXCEPTION("The size of the passed in vector must be that of the number of state variables");
00130     }
00131     mStateVariables = rStateVariables;
00132 }
00133 
00134 void AbstractOdeSystem::SetStateVariable(unsigned stateVariable, double newValue)
00135 {
00136     if ( mNumberOfStateVariables <= stateVariable )
00137     {
00138         EXCEPTION("The index passed in must be less than the number of state variables");
00139     }
00140     mStateVariables[stateVariable] = newValue;
00141 }
00142 
00143 
00144 std::vector<double>& AbstractOdeSystem::rGetStateVariables()
00145 {
00146     return mStateVariables;
00147 }
00148 
00149 const std::vector<std::string>& AbstractOdeSystem::rGetVariableNames() const
00150 {
00151     assert(mpSystemInfo);
00152     return mpSystemInfo->rGetVariableNames();
00153 }
00154 
00155 const std::vector<std::string>& AbstractOdeSystem::rGetVariableUnits() const
00156 {
00157     assert(mpSystemInfo);
00158     return mpSystemInfo->rGetVariableUnits();
00159 }
00160 
00161 boost::shared_ptr<const AbstractOdeSystemInformation> AbstractOdeSystem::GetSystemInformation() const
00162 {
00163     assert(mpSystemInfo);
00164     return mpSystemInfo;
00165 }
00166 
00167 double AbstractOdeSystem::CalculateRootFunction(double time, const std::vector<double>& rY)
00168 {
00169     bool stop = CalculateStoppingEvent(time, rY);
00170     return stop ? 0.0 : 1.0;
00171 }
00172 
00173 bool AbstractOdeSystem::GetUseAnalyticJacobian()
00174 {
00175     return mUseAnalyticJacobian;
00176 }
00177 
00178 unsigned AbstractOdeSystem::GetStateVariableNumberByName(const std::string name)
00179 {
00180     assert(mpSystemInfo);
00181     return mpSystemInfo->GetStateVariableNumberByName(name);
00182 }
00183 
00184 double AbstractOdeSystem::GetStateVariableValueByNumber(unsigned varNumber) const
00185 {
00186     assert(varNumber < mNumberOfStateVariables);
00187     return mStateVariables[varNumber];
00188 }
00189 
00190 std::string AbstractOdeSystem::GetStateVariableUnitsByNumber(unsigned varNumber) const
00191 {
00192     assert(varNumber < mNumberOfStateVariables);
00193     assert(mpSystemInfo);
00194     return mpSystemInfo->GetStateVariableUnitsByNumber(varNumber);
00195 }

Generated on Tue Aug 4 16:10:24 2009 for Chaste by  doxygen 1.5.5