AbstractParameterisedSystem.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
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 <cassert>
00030 
00031 #include "AbstractParameterisedSystem.hpp"
00032 
00033 #include "Exception.hpp"
00034 #include "VectorHelperFunctions.hpp"
00035 
00036 #ifdef CHASTE_CVODE
00037 // CVODE headers
00038 #include <nvector/nvector_serial.h>
00039 #endif
00040 
00041 template<typename VECTOR>
00042 AbstractParameterisedSystem<VECTOR>::AbstractParameterisedSystem(unsigned numberOfStateVariables)
00043     : mNumberOfStateVariables(numberOfStateVariables)
00044 {
00045     InitialiseEmptyVector(mParameters);
00046     InitialiseEmptyVector(mStateVariables);
00047 }
00048 
00049 template<typename VECTOR>
00050 AbstractParameterisedSystem<VECTOR>::~AbstractParameterisedSystem()
00051 {
00052 }
00053 
00054 template<typename VECTOR>
00055 boost::shared_ptr<const AbstractOdeSystemInformation> AbstractParameterisedSystem<VECTOR>::GetSystemInformation() const
00056 {
00057     assert(mpSystemInfo);
00058     return mpSystemInfo;
00059 }
00060 
00061 
00062 template<typename VECTOR>
00063 std::string AbstractParameterisedSystem<VECTOR>::GetSystemName() const
00064 {
00065     assert(mpSystemInfo);
00066     return mpSystemInfo->GetSystemName();
00067 }
00068 
00069 //
00070 // State variable methods
00071 //
00072 
00073 template<typename VECTOR>
00074 unsigned AbstractParameterisedSystem<VECTOR>::GetNumberOfStateVariables() const
00075 {
00076     return mNumberOfStateVariables;
00077 }
00078 
00079 template<typename VECTOR>
00080 VECTOR& AbstractParameterisedSystem<VECTOR>::rGetStateVariables()
00081 {
00082     return mStateVariables;
00083 }
00084 
00085 template<typename VECTOR>
00086 VECTOR AbstractParameterisedSystem<VECTOR>::GetStateVariables()
00087 {
00088     return CopyVector(mStateVariables);
00089 }
00090 
00091 template<typename VECTOR>
00092 double AbstractParameterisedSystem<VECTOR>::GetStateVariable(unsigned index) const
00093 {
00094     if (index >= mNumberOfStateVariables)
00095     {
00096         EXCEPTION("The index passed in must be less than the number of state variables.");
00097     }
00098     return GetVectorComponent(mStateVariables, index);
00099 }
00100 
00101 template<typename VECTOR>
00102 double AbstractParameterisedSystem<VECTOR>::GetStateVariable(const std::string& rName) const
00103 {
00104     return GetStateVariable(GetStateVariableIndex(rName));
00105 }
00106 
00107 template<typename VECTOR>
00108 void AbstractParameterisedSystem<VECTOR>::SetStateVariable(unsigned index, double newValue)
00109 {
00110     if ( mNumberOfStateVariables <= index )
00111     {
00112         EXCEPTION("The index passed in must be less than the number of state variables.");
00113     }
00114     SetVectorComponent(mStateVariables, index, newValue);
00115 }
00116 
00117 template<typename VECTOR>
00118 void AbstractParameterisedSystem<VECTOR>::SetStateVariable(const std::string& rName, double newValue)
00119 {
00120     SetStateVariable(GetStateVariableIndex(rName), newValue);
00121 }
00122 
00123 template<typename VECTOR>
00124 const std::vector<std::string>& AbstractParameterisedSystem<VECTOR>::rGetStateVariableNames() const
00125 {
00126     assert(mpSystemInfo);
00127     return mpSystemInfo->rGetStateVariableNames();
00128 }
00129 
00130 template<typename VECTOR>
00131 const std::vector<std::string>& AbstractParameterisedSystem<VECTOR>::rGetStateVariableUnits() const
00132 {
00133     assert(mpSystemInfo);
00134     return mpSystemInfo->rGetStateVariableUnits();
00135 }
00136 
00137 template<typename VECTOR>
00138 unsigned AbstractParameterisedSystem<VECTOR>::GetStateVariableIndex(const std::string& rName) const
00139 {
00140     assert(mpSystemInfo);
00141     return mpSystemInfo->GetStateVariableIndex(rName);
00142 }
00143 
00144 template<typename VECTOR>
00145 bool AbstractParameterisedSystem<VECTOR>::HasStateVariable(const std::string& rName) const
00146 {
00147     assert(mpSystemInfo);
00148     return mpSystemInfo->HasStateVariable(rName);
00149 }
00150 
00151 template<typename VECTOR>
00152 std::string AbstractParameterisedSystem<VECTOR>::GetStateVariableUnits(unsigned index) const
00153 {
00154     assert(mpSystemInfo);
00155     return mpSystemInfo->GetStateVariableUnits(index);
00156 }
00157 
00158 //
00159 // Parameter methods
00160 //
00161 
00162 template<typename VECTOR>
00163 unsigned AbstractParameterisedSystem<VECTOR>::GetNumberOfParameters() const
00164 {
00165     return GetVectorSize(mParameters);
00166 }
00167 
00168 template<typename VECTOR>
00169 double AbstractParameterisedSystem<VECTOR>::GetParameter(unsigned index) const
00170 {
00171     if (index >= GetVectorSize(mParameters))
00172     {
00173         EXCEPTION("The index passed in must be less than the number of parameters.");
00174     }
00175     return GetVectorComponent(mParameters, index);
00176 }
00177 
00178 template<typename VECTOR>
00179 void AbstractParameterisedSystem<VECTOR>::SetParameter(unsigned index, double value)
00180 {
00181     if (index >= GetVectorSize(mParameters))
00182     {
00183         EXCEPTION("The index passed in must be less than the number of parameters.");
00184     }
00185     SetVectorComponent(mParameters, index, value);
00186 }
00187 
00188 template<typename VECTOR>
00189 void AbstractParameterisedSystem<VECTOR>::SetParameter(const std::string& rName, double value)
00190 {
00191     SetVectorComponent(mParameters, GetParameterIndex(rName), value);
00192 }
00193 
00194 template<typename VECTOR>
00195 double AbstractParameterisedSystem<VECTOR>::GetParameter(const std::string& rName) const
00196 {
00197     return GetParameter(GetParameterIndex(rName));
00198 }
00199 
00200 template<typename VECTOR>
00201 const std::vector<std::string>& AbstractParameterisedSystem<VECTOR>::rGetParameterNames() const
00202 {
00203     assert(mpSystemInfo);
00204     return mpSystemInfo->rGetParameterNames();
00205 }
00206 
00207 template<typename VECTOR>
00208 const std::vector<std::string>& AbstractParameterisedSystem<VECTOR>::rGetParameterUnits() const
00209 {
00210     assert(mpSystemInfo);
00211     return mpSystemInfo->rGetParameterUnits();
00212 }
00213 
00214 template<typename VECTOR>
00215 unsigned AbstractParameterisedSystem<VECTOR>::GetParameterIndex(const std::string& rName) const
00216 {
00217     assert(mpSystemInfo);
00218     return mpSystemInfo->GetParameterIndex(rName);
00219 }
00220 
00221 template<typename VECTOR>
00222 bool AbstractParameterisedSystem<VECTOR>::HasParameter(const std::string& rName) const
00223 {
00224     assert(mpSystemInfo);
00225     return mpSystemInfo->HasParameter(rName);
00226 }
00227 
00228 template<typename VECTOR>
00229 std::string AbstractParameterisedSystem<VECTOR>::GetParameterUnits(unsigned index) const
00230 {
00231     assert(mpSystemInfo);
00232     return mpSystemInfo->GetParameterUnits(index);
00233 }
00234 
00235 //
00236 // "Any variable" methods
00237 //
00238 
00239 template<typename VECTOR>
00240 double AbstractParameterisedSystem<VECTOR>::GetAnyVariable(unsigned index, double time,
00241                                                            VECTOR* pDerivedQuantities)
00242 {
00243     if (index < mNumberOfStateVariables)
00244     {
00245         return GetVectorComponent(mStateVariables, index);
00246     }
00247     else if (index - mNumberOfStateVariables < GetVectorSize(mParameters))
00248     {
00249         return GetVectorComponent(mParameters, index - mNumberOfStateVariables);
00250     }
00251     else
00252     {
00253         unsigned offset = mNumberOfStateVariables + GetVectorSize(mParameters);
00254         if (index - offset < GetNumberOfDerivedQuantities())
00255         {
00256             VECTOR dqs;
00257             if (pDerivedQuantities == NULL)
00258             {
00259                 dqs = ComputeDerivedQuantitiesFromCurrentState(time);
00260                 pDerivedQuantities = &dqs;
00261             }
00262             double value = GetVectorComponent(*pDerivedQuantities, index - offset);
00263             if (pDerivedQuantities == &dqs)
00264             {
00265                 DeleteVector(dqs);
00266             }
00267             return value;
00268         }
00269         else
00270         {
00271             EXCEPTION("Invalid index passed to GetAnyVariable.");
00272         }
00273     }
00274 }
00275 
00276 template<typename VECTOR>
00277 double AbstractParameterisedSystem<VECTOR>::GetAnyVariable(const std::string& rName,
00278                                                            double time,
00279                                                            VECTOR* pDerivedQuantities)
00280 {
00281     return GetAnyVariable(GetAnyVariableIndex(rName), time, pDerivedQuantities);
00282 }
00283 
00284 template<typename VECTOR>
00285 unsigned AbstractParameterisedSystem<VECTOR>::GetAnyVariableIndex(const std::string& rName) const
00286 {
00287     assert(mpSystemInfo);
00288     return mpSystemInfo->GetAnyVariableIndex(rName);
00289 }
00290 
00291 template<typename VECTOR>
00292 bool AbstractParameterisedSystem<VECTOR>::HasAnyVariable(const std::string& rName) const
00293 {
00294     assert(mpSystemInfo);
00295     return mpSystemInfo->HasAnyVariable(rName);
00296 }
00297 
00298 template<typename VECTOR>
00299 void AbstractParameterisedSystem<VECTOR>::SetAnyVariable(unsigned index, double value)
00300 {
00301     if (index < mNumberOfStateVariables)
00302     {
00303         SetVectorComponent(mStateVariables, index, value);
00304     }
00305     else if (index - mNumberOfStateVariables < GetVectorSize(mParameters))
00306     {
00307         SetVectorComponent(mParameters, index - mNumberOfStateVariables, value);
00308     }
00309     else
00310     {
00311         EXCEPTION("Cannot set the value of a derived quantity, or invalid index.");
00312     }
00313 }
00314 
00315 template<typename VECTOR>
00316 void AbstractParameterisedSystem<VECTOR>::SetAnyVariable(const std::string& rName, double value)
00317 {
00318     SetAnyVariable(GetAnyVariableIndex(rName), value);
00319 }
00320 
00321 template<typename VECTOR>
00322 std::string AbstractParameterisedSystem<VECTOR>::GetAnyVariableUnits(unsigned index) const
00323 {
00324     assert(mpSystemInfo);
00325     return mpSystemInfo->GetAnyVariableUnits(index);
00326 }
00327 
00328 template<typename VECTOR>
00329 std::string AbstractParameterisedSystem<VECTOR>::GetAnyVariableUnits(const std::string& rName) const
00330 {
00331     return GetAnyVariableUnits(GetAnyVariableIndex(rName));
00332 }
00333 
00334 //
00335 // "Derived quantities" methods
00336 //
00337 
00338 template<typename VECTOR>
00339 unsigned AbstractParameterisedSystem<VECTOR>::GetNumberOfDerivedQuantities() const
00340 {
00341     assert(mpSystemInfo);
00342     return mpSystemInfo->rGetDerivedQuantityNames().size();
00343 }
00344 
00345 template<typename VECTOR>
00346 VECTOR AbstractParameterisedSystem<VECTOR>::ComputeDerivedQuantities(double time,
00347                                                                      const VECTOR& rState)
00348 {
00349     EXCEPTION("This ODE system does not define derived quantities.");
00350 }
00351 
00352 template<typename VECTOR>
00353 VECTOR AbstractParameterisedSystem<VECTOR>::ComputeDerivedQuantitiesFromCurrentState(double time)
00354 {
00355     return this->ComputeDerivedQuantities(time, mStateVariables);
00356 }
00357 
00358 template<typename VECTOR>
00359 const std::vector<std::string>& AbstractParameterisedSystem<VECTOR>::rGetDerivedQuantityNames() const
00360 {
00361     assert(mpSystemInfo);
00362     return mpSystemInfo->rGetDerivedQuantityNames();
00363 }
00364 
00365 template<typename VECTOR>
00366 const std::vector<std::string>& AbstractParameterisedSystem<VECTOR>::rGetDerivedQuantityUnits() const
00367 {
00368     assert(mpSystemInfo);
00369     return mpSystemInfo->rGetDerivedQuantityUnits();
00370 }
00371 
00372 template<typename VECTOR>
00373 unsigned AbstractParameterisedSystem<VECTOR>::GetDerivedQuantityIndex(const std::string& rName) const
00374 {
00375     assert(mpSystemInfo);
00376     return mpSystemInfo->GetDerivedQuantityIndex(rName);
00377 }
00378 
00379 template<typename VECTOR>
00380 bool AbstractParameterisedSystem<VECTOR>::HasDerivedQuantity(const std::string& rName) const
00381 {
00382     assert(mpSystemInfo);
00383     return mpSystemInfo->HasDerivedQuantity(rName);
00384 }
00385 
00386 template<typename VECTOR>
00387 std::string AbstractParameterisedSystem<VECTOR>::GetDerivedQuantityUnits(unsigned index) const
00388 {
00389     assert(mpSystemInfo);
00390     return mpSystemInfo->GetDerivedQuantityUnits(index);
00391 }
00392 
00393 
00394 template<typename VECTOR>
00395 unsigned AbstractParameterisedSystem<VECTOR>::GetNumberOfAttributes() const
00396 {
00397     assert(mpSystemInfo);
00398     return mpSystemInfo->GetNumberOfAttributes();
00399 }
00400 
00401 template<typename VECTOR>
00402 bool AbstractParameterisedSystem<VECTOR>::HasAttribute(const std::string& rName) const
00403 {
00404     assert(mpSystemInfo);
00405     return mpSystemInfo->HasAttribute(rName);
00406 }
00407 
00408 template<typename VECTOR>
00409 double AbstractParameterisedSystem<VECTOR>::GetAttribute(const std::string& rName) const
00410 {
00411     assert(mpSystemInfo);
00412     return mpSystemInfo->GetAttribute(rName);
00413 }
00414 
00415 
00416 
00418 // Explicit instantiation
00420 
00421 template class AbstractParameterisedSystem<std::vector<double> >;
00422 #ifdef CHASTE_CVODE
00423 template class AbstractParameterisedSystem<N_Vector>;
00424 #endif

Generated on Mon Apr 18 11:35:35 2011 for Chaste by  doxygen 1.5.5