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 void AbstractParameterisedSystem<VECTOR>::SetStateVariables(const VECTOR& rStateVariables)
00093 {
00094     if ( mNumberOfStateVariables != GetVectorSize(rStateVariables) )
00095     {
00096         EXCEPTION("The size of the passed in vector must be that of the number of state variables.");
00097     }
00098     CreateVectorIfEmpty(mStateVariables, mNumberOfStateVariables);
00099     for (unsigned i=0; i<mNumberOfStateVariables; i++)
00100     {
00101         SetVectorComponent(mStateVariables, i, GetVectorComponent(rStateVariables, i));
00102     }
00103 }
00104 
00105 template<typename VECTOR>
00106 double AbstractParameterisedSystem<VECTOR>::GetStateVariable(unsigned index) const
00107 {
00108     if (index >= mNumberOfStateVariables)
00109     {
00110         EXCEPTION("The index passed in must be less than the number of state variables.");
00111     }
00112     return GetVectorComponent(mStateVariables, index);
00113 }
00114 
00115 template<typename VECTOR>
00116 double AbstractParameterisedSystem<VECTOR>::GetStateVariable(const std::string& rName) const
00117 {
00118     return GetStateVariable(GetStateVariableIndex(rName));
00119 }
00120 
00121 template<typename VECTOR>
00122 void AbstractParameterisedSystem<VECTOR>::SetStateVariable(unsigned index, double newValue)
00123 {
00124     if ( mNumberOfStateVariables <= index )
00125     {
00126         EXCEPTION("The index passed in must be less than the number of state variables.");
00127     }
00128     SetVectorComponent(mStateVariables, index, newValue);
00129 }
00130 
00131 template<typename VECTOR>
00132 void AbstractParameterisedSystem<VECTOR>::SetStateVariable(const std::string& rName, double newValue)
00133 {
00134     SetStateVariable(GetStateVariableIndex(rName), newValue);
00135 }
00136 
00137 template<typename VECTOR>
00138 const std::vector<std::string>& AbstractParameterisedSystem<VECTOR>::rGetStateVariableNames() const
00139 {
00140     assert(mpSystemInfo);
00141     return mpSystemInfo->rGetStateVariableNames();
00142 }
00143 
00144 template<typename VECTOR>
00145 const std::vector<std::string>& AbstractParameterisedSystem<VECTOR>::rGetStateVariableUnits() const
00146 {
00147     assert(mpSystemInfo);
00148     return mpSystemInfo->rGetStateVariableUnits();
00149 }
00150 
00151 template<typename VECTOR>
00152 unsigned AbstractParameterisedSystem<VECTOR>::GetStateVariableIndex(const std::string& rName) const
00153 {
00154     assert(mpSystemInfo);
00155     return mpSystemInfo->GetStateVariableIndex(rName);
00156 }
00157 
00158 template<typename VECTOR>
00159 bool AbstractParameterisedSystem<VECTOR>::HasStateVariable(const std::string& rName) const
00160 {
00161     assert(mpSystemInfo);
00162     return mpSystemInfo->HasStateVariable(rName);
00163 }
00164 
00165 template<typename VECTOR>
00166 std::string AbstractParameterisedSystem<VECTOR>::GetStateVariableUnits(unsigned index) const
00167 {
00168     assert(mpSystemInfo);
00169     return mpSystemInfo->GetStateVariableUnits(index);
00170 }
00171 
00172 //
00173 // Initial condition methods
00174 //
00175 
00176 template<typename VECTOR>
00177 void AbstractParameterisedSystem<VECTOR>::SetDefaultInitialConditions(const VECTOR& rInitialConditions)
00178 {
00179     if (GetVectorSize(rInitialConditions) != mNumberOfStateVariables)
00180     {
00181         EXCEPTION("The number of initial conditions must be that of the number of state variables.");
00182     }
00183     assert(mpSystemInfo);
00184     std::vector<double> inits;
00185     CopyToStdVector(rInitialConditions, inits);
00186     mpSystemInfo->SetDefaultInitialConditions(inits);
00187 }
00188 
00189 template<typename VECTOR>
00190 void AbstractParameterisedSystem<VECTOR>::SetDefaultInitialCondition(unsigned index, double initialCondition)
00191 {
00192     if (index >= mNumberOfStateVariables)
00193     {
00194         EXCEPTION("Index is greater than the number of state variables.");
00195     }
00196     assert(mpSystemInfo);
00197     mpSystemInfo->SetDefaultInitialCondition(index, initialCondition);
00198 }
00199 
00200 template<typename VECTOR>
00201 VECTOR AbstractParameterisedSystem<VECTOR>::GetInitialConditions() const
00202 {
00203     assert(mpSystemInfo);
00204     VECTOR v;
00205     InitialiseEmptyVector(v);
00206     CreateVectorIfEmpty(v, mNumberOfStateVariables);
00207     CopyFromStdVector(mpSystemInfo->GetInitialConditions(), v);
00208     return v;
00209 }
00210 
00211 template<typename VECTOR>
00212 void AbstractParameterisedSystem<VECTOR>::ResetToInitialConditions()
00213 {
00214     VECTOR inits = GetInitialConditions();
00215     SetStateVariables(inits);
00216     DeleteVector(inits);
00217 }
00218 
00219 //
00220 // Parameter methods
00221 //
00222 
00223 template<typename VECTOR>
00224 unsigned AbstractParameterisedSystem<VECTOR>::GetNumberOfParameters() const
00225 {
00226     return GetVectorSize(mParameters);
00227 }
00228 
00229 template<typename VECTOR>
00230 double AbstractParameterisedSystem<VECTOR>::GetParameter(unsigned index) const
00231 {
00232     if (index >= GetVectorSize(mParameters))
00233     {
00234         EXCEPTION("The index passed in must be less than the number of parameters.");
00235     }
00236     return GetVectorComponent(mParameters, index);
00237 }
00238 
00239 template<typename VECTOR>
00240 void AbstractParameterisedSystem<VECTOR>::SetParameter(unsigned index, double value)
00241 {
00242     if (index >= GetVectorSize(mParameters))
00243     {
00244         EXCEPTION("The index passed in must be less than the number of parameters.");
00245     }
00246     SetVectorComponent(mParameters, index, value);
00247 }
00248 
00249 template<typename VECTOR>
00250 void AbstractParameterisedSystem<VECTOR>::SetParameter(const std::string& rName, double value)
00251 {
00252     SetVectorComponent(mParameters, GetParameterIndex(rName), value);
00253 }
00254 
00255 template<typename VECTOR>
00256 double AbstractParameterisedSystem<VECTOR>::GetParameter(const std::string& rName) const
00257 {
00258     return GetParameter(GetParameterIndex(rName));
00259 }
00260 
00261 template<typename VECTOR>
00262 const std::vector<std::string>& AbstractParameterisedSystem<VECTOR>::rGetParameterNames() const
00263 {
00264     assert(mpSystemInfo);
00265     return mpSystemInfo->rGetParameterNames();
00266 }
00267 
00268 template<typename VECTOR>
00269 const std::vector<std::string>& AbstractParameterisedSystem<VECTOR>::rGetParameterUnits() const
00270 {
00271     assert(mpSystemInfo);
00272     return mpSystemInfo->rGetParameterUnits();
00273 }
00274 
00275 template<typename VECTOR>
00276 unsigned AbstractParameterisedSystem<VECTOR>::GetParameterIndex(const std::string& rName) const
00277 {
00278     assert(mpSystemInfo);
00279     return mpSystemInfo->GetParameterIndex(rName);
00280 }
00281 
00282 template<typename VECTOR>
00283 bool AbstractParameterisedSystem<VECTOR>::HasParameter(const std::string& rName) const
00284 {
00285     assert(mpSystemInfo);
00286     return mpSystemInfo->HasParameter(rName);
00287 }
00288 
00289 template<typename VECTOR>
00290 std::string AbstractParameterisedSystem<VECTOR>::GetParameterUnits(unsigned index) const
00291 {
00292     assert(mpSystemInfo);
00293     return mpSystemInfo->GetParameterUnits(index);
00294 }
00295 
00296 //
00297 // "Any variable" methods
00298 //
00299 
00300 template<typename VECTOR>
00301 double AbstractParameterisedSystem<VECTOR>::GetAnyVariable(unsigned index, double time,
00302                                                            VECTOR* pDerivedQuantities)
00303 {
00304     if (index < mNumberOfStateVariables)
00305     {
00306         return GetVectorComponent(mStateVariables, index);
00307     }
00308     else if (index - mNumberOfStateVariables < GetVectorSize(mParameters))
00309     {
00310         return GetVectorComponent(mParameters, index - mNumberOfStateVariables);
00311     }
00312     else
00313     {
00314         unsigned offset = mNumberOfStateVariables + GetVectorSize(mParameters);
00315         if (index - offset < GetNumberOfDerivedQuantities())
00316         {
00317             VECTOR dqs;
00318             if (pDerivedQuantities == NULL)
00319             {
00320                 dqs = ComputeDerivedQuantitiesFromCurrentState(time);
00321                 pDerivedQuantities = &dqs;
00322             }
00323             double value = GetVectorComponent(*pDerivedQuantities, index - offset);
00324             if (pDerivedQuantities == &dqs)
00325             {
00326                 DeleteVector(dqs);
00327             }
00328             return value;
00329         }
00330         else
00331         {
00332             EXCEPTION("Invalid index passed to GetAnyVariable.");
00333         }
00334     }
00335 }
00336 
00337 template<typename VECTOR>
00338 double AbstractParameterisedSystem<VECTOR>::GetAnyVariable(const std::string& rName,
00339                                                            double time,
00340                                                            VECTOR* pDerivedQuantities)
00341 {
00342     return GetAnyVariable(GetAnyVariableIndex(rName), time, pDerivedQuantities);
00343 }
00344 
00345 template<typename VECTOR>
00346 unsigned AbstractParameterisedSystem<VECTOR>::GetAnyVariableIndex(const std::string& rName) const
00347 {
00348     assert(mpSystemInfo);
00349     return mpSystemInfo->GetAnyVariableIndex(rName);
00350 }
00351 
00352 template<typename VECTOR>
00353 bool AbstractParameterisedSystem<VECTOR>::HasAnyVariable(const std::string& rName) const
00354 {
00355     assert(mpSystemInfo);
00356     return mpSystemInfo->HasAnyVariable(rName);
00357 }
00358 
00359 template<typename VECTOR>
00360 void AbstractParameterisedSystem<VECTOR>::SetAnyVariable(unsigned index, double value)
00361 {
00362     if (index < mNumberOfStateVariables)
00363     {
00364         SetVectorComponent(mStateVariables, index, value);
00365     }
00366     else if (index - mNumberOfStateVariables < GetVectorSize(mParameters))
00367     {
00368         SetVectorComponent(mParameters, index - mNumberOfStateVariables, value);
00369     }
00370     else
00371     {
00372         EXCEPTION("Cannot set the value of a derived quantity, or invalid index.");
00373     }
00374 }
00375 
00376 template<typename VECTOR>
00377 void AbstractParameterisedSystem<VECTOR>::SetAnyVariable(const std::string& rName, double value)
00378 {
00379     SetAnyVariable(GetAnyVariableIndex(rName), value);
00380 }
00381 
00382 template<typename VECTOR>
00383 std::string AbstractParameterisedSystem<VECTOR>::GetAnyVariableUnits(unsigned index) const
00384 {
00385     assert(mpSystemInfo);
00386     return mpSystemInfo->GetAnyVariableUnits(index);
00387 }
00388 
00389 template<typename VECTOR>
00390 std::string AbstractParameterisedSystem<VECTOR>::GetAnyVariableUnits(const std::string& rName) const
00391 {
00392     return GetAnyVariableUnits(GetAnyVariableIndex(rName));
00393 }
00394 
00395 //
00396 // "Derived quantities" methods
00397 //
00398 
00399 template<typename VECTOR>
00400 unsigned AbstractParameterisedSystem<VECTOR>::GetNumberOfDerivedQuantities() const
00401 {
00402     assert(mpSystemInfo);
00403     return mpSystemInfo->rGetDerivedQuantityNames().size();
00404 }
00405 
00406 template<typename VECTOR>
00407 VECTOR AbstractParameterisedSystem<VECTOR>::ComputeDerivedQuantities(double time,
00408                                                                      const VECTOR& rState)
00409 {
00410     EXCEPTION("This ODE system does not define derived quantities.");
00411 }
00412 
00413 template<typename VECTOR>
00414 VECTOR AbstractParameterisedSystem<VECTOR>::ComputeDerivedQuantitiesFromCurrentState(double time)
00415 {
00416     return this->ComputeDerivedQuantities(time, mStateVariables);
00417 }
00418 
00419 template<typename VECTOR>
00420 const std::vector<std::string>& AbstractParameterisedSystem<VECTOR>::rGetDerivedQuantityNames() const
00421 {
00422     assert(mpSystemInfo);
00423     return mpSystemInfo->rGetDerivedQuantityNames();
00424 }
00425 
00426 template<typename VECTOR>
00427 const std::vector<std::string>& AbstractParameterisedSystem<VECTOR>::rGetDerivedQuantityUnits() const
00428 {
00429     assert(mpSystemInfo);
00430     return mpSystemInfo->rGetDerivedQuantityUnits();
00431 }
00432 
00433 template<typename VECTOR>
00434 unsigned AbstractParameterisedSystem<VECTOR>::GetDerivedQuantityIndex(const std::string& rName) const
00435 {
00436     assert(mpSystemInfo);
00437     return mpSystemInfo->GetDerivedQuantityIndex(rName);
00438 }
00439 
00440 template<typename VECTOR>
00441 bool AbstractParameterisedSystem<VECTOR>::HasDerivedQuantity(const std::string& rName) const
00442 {
00443     assert(mpSystemInfo);
00444     return mpSystemInfo->HasDerivedQuantity(rName);
00445 }
00446 
00447 template<typename VECTOR>
00448 std::string AbstractParameterisedSystem<VECTOR>::GetDerivedQuantityUnits(unsigned index) const
00449 {
00450     assert(mpSystemInfo);
00451     return mpSystemInfo->GetDerivedQuantityUnits(index);
00452 }
00453 
00454 
00455 template<typename VECTOR>
00456 unsigned AbstractParameterisedSystem<VECTOR>::GetNumberOfAttributes() const
00457 {
00458     assert(mpSystemInfo);
00459     return mpSystemInfo->GetNumberOfAttributes();
00460 }
00461 
00462 template<typename VECTOR>
00463 bool AbstractParameterisedSystem<VECTOR>::HasAttribute(const std::string& rName) const
00464 {
00465     assert(mpSystemInfo);
00466     return mpSystemInfo->HasAttribute(rName);
00467 }
00468 
00469 template<typename VECTOR>
00470 double AbstractParameterisedSystem<VECTOR>::GetAttribute(const std::string& rName) const
00471 {
00472     assert(mpSystemInfo);
00473     return mpSystemInfo->GetAttribute(rName);
00474 }
00475 
00476 
00477 
00479 // Explicit instantiation
00481 
00482 template class AbstractParameterisedSystem<std::vector<double> >;
00483 #ifdef CHASTE_CVODE
00484 template class AbstractParameterisedSystem<N_Vector>;
00485 #endif

Generated on Tue May 31 14:31:48 2011 for Chaste by  doxygen 1.5.5