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 <sstream>
00030 #include <cassert>
00031 
00032 #include "AbstractParameterisedSystem.hpp"
00033 
00034 #include "Exception.hpp"
00035 #include "VectorHelperFunctions.hpp"
00036 
00037 
00038 AbstractUntemplatedParameterisedSystem::AbstractUntemplatedParameterisedSystem(unsigned numberOfStateVariables)
00039     : mNumberOfStateVariables(numberOfStateVariables)
00040 {
00041 }
00042 
00043 
00044 template<typename VECTOR>
00045 AbstractParameterisedSystem<VECTOR>::AbstractParameterisedSystem(unsigned numberOfStateVariables)
00046     : AbstractUntemplatedParameterisedSystem(numberOfStateVariables)
00047 {
00048     InitialiseEmptyVector(mParameters);
00049     InitialiseEmptyVector(mStateVariables);
00050 }
00051 
00052 
00053 AbstractUntemplatedParameterisedSystem::~AbstractUntemplatedParameterisedSystem()
00054 {
00055 }
00056 
00057 
00058 boost::shared_ptr<const AbstractOdeSystemInformation> AbstractUntemplatedParameterisedSystem::GetSystemInformation() const
00059 {
00060     assert(mpSystemInfo);
00061     return mpSystemInfo;
00062 }
00063 
00064 
00065 std::string AbstractUntemplatedParameterisedSystem::GetSystemName() const
00066 {
00067     return GetSystemInformation()->GetSystemName();
00068 }
00069 
00070 template<typename VECTOR>
00071 std::string AbstractParameterisedSystem<VECTOR>::DumpState(const std::string& message)
00072 {
00073     return GetStateMessage(message, mStateVariables);
00074 }
00075 
00076 template<typename VECTOR>
00077 std::string AbstractParameterisedSystem<VECTOR>::DumpState(const std::string& message,
00078                                                            VECTOR Y)
00079 {
00080     return GetStateMessage(message, Y);
00081 }
00082 
00083 template<typename VECTOR>
00084 std::string AbstractParameterisedSystem<VECTOR>::GetStateMessage(const std::string& message, VECTOR Y)
00085 {
00086     std::stringstream res;
00087     res << message << "\nState:\n";
00088     assert(rGetStateVariableNames().size()==GetVectorSize(Y));
00089     for (unsigned i=0; i<GetVectorSize(Y); i++)
00090     {
00091         res << "\t" << rGetStateVariableNames()[i] << ":" << GetVectorComponent(Y, i) << "\n";
00092     }
00093     return res.str();
00094 }
00095 
00096 //
00097 // State variable methods
00098 //
00099 
00100 unsigned AbstractUntemplatedParameterisedSystem::GetNumberOfStateVariables() const
00101 {
00102     return mNumberOfStateVariables;
00103 }
00104 
00105 template<typename VECTOR>
00106 VECTOR& AbstractParameterisedSystem<VECTOR>::rGetStateVariables()
00107 {
00108     return mStateVariables;
00109 }
00110 
00111 template<typename VECTOR>
00112 VECTOR AbstractParameterisedSystem<VECTOR>::GetStateVariables()
00113 {
00114     return CopyVector(mStateVariables);
00115 }
00116 
00117 template<typename VECTOR>
00118 void AbstractParameterisedSystem<VECTOR>::SetStateVariables(const VECTOR& rStateVariables)
00119 {
00120     if ( mNumberOfStateVariables != GetVectorSize(rStateVariables) )
00121     {
00122         EXCEPTION("The size of the passed in vector must be that of the number of state variables.");
00123     }
00124     CreateVectorIfEmpty(mStateVariables, mNumberOfStateVariables);
00125     for (unsigned i=0; i<mNumberOfStateVariables; i++)
00126     {
00127         SetVectorComponent(mStateVariables, i, GetVectorComponent(rStateVariables, i));
00128     }
00129 }
00130 
00131 template<typename VECTOR>
00132 double AbstractParameterisedSystem<VECTOR>::GetStateVariable(unsigned index) const
00133 {
00134     if (index >= mNumberOfStateVariables)
00135     {
00136         EXCEPTION("The index passed in must be less than the number of state variables.");
00137     }
00138     return GetVectorComponent(mStateVariables, index);
00139 }
00140 
00141 template<typename VECTOR>
00142 double AbstractParameterisedSystem<VECTOR>::GetStateVariable(const std::string& rName) const
00143 {
00144     return GetStateVariable(GetStateVariableIndex(rName));
00145 }
00146 
00147 template<typename VECTOR>
00148 void AbstractParameterisedSystem<VECTOR>::SetStateVariable(unsigned index, double newValue)
00149 {
00150     if ( mNumberOfStateVariables <= index )
00151     {
00152         EXCEPTION("The index passed in must be less than the number of state variables.");
00153     }
00154     SetVectorComponent(mStateVariables, index, newValue);
00155 }
00156 
00157 template<typename VECTOR>
00158 void AbstractParameterisedSystem<VECTOR>::SetStateVariable(const std::string& rName, double newValue)
00159 {
00160     SetStateVariable(GetStateVariableIndex(rName), newValue);
00161 }
00162 
00163 
00164 const std::vector<std::string>& AbstractUntemplatedParameterisedSystem::rGetStateVariableNames() const
00165 {
00166     return GetSystemInformation()->rGetStateVariableNames();
00167 }
00168 
00169 const std::vector<std::string>& AbstractUntemplatedParameterisedSystem::rGetStateVariableUnits() const
00170 {
00171     return GetSystemInformation()->rGetStateVariableUnits();
00172 }
00173 
00174 unsigned AbstractUntemplatedParameterisedSystem::GetStateVariableIndex(const std::string& rName) const
00175 {
00176     return GetSystemInformation()->GetStateVariableIndex(rName);
00177 }
00178 
00179 bool AbstractUntemplatedParameterisedSystem::HasStateVariable(const std::string& rName) const
00180 {
00181     return GetSystemInformation()->HasStateVariable(rName);
00182 }
00183 
00184 std::string AbstractUntemplatedParameterisedSystem::GetStateVariableUnits(unsigned index) const
00185 {
00186     return GetSystemInformation()->GetStateVariableUnits(index);
00187 }
00188 
00189 //
00190 // Initial condition methods
00191 //
00192 
00193 template<typename VECTOR>
00194 void AbstractParameterisedSystem<VECTOR>::SetDefaultInitialConditions(const VECTOR& rInitialConditions)
00195 {
00196     if (GetVectorSize(rInitialConditions) != mNumberOfStateVariables)
00197     {
00198         EXCEPTION("The number of initial conditions must be that of the number of state variables.");
00199     }
00200     assert(mpSystemInfo);
00201     std::vector<double> inits;
00202     CopyToStdVector(rInitialConditions, inits);
00203     mpSystemInfo->SetDefaultInitialConditions(inits);
00204 }
00205 
00206 template<typename VECTOR>
00207 void AbstractParameterisedSystem<VECTOR>::SetDefaultInitialCondition(unsigned index, double initialCondition)
00208 {
00209     if (index >= mNumberOfStateVariables)
00210     {
00211         EXCEPTION("Index is greater than the number of state variables.");
00212     }
00213     assert(mpSystemInfo);
00214     mpSystemInfo->SetDefaultInitialCondition(index, initialCondition);
00215 }
00216 
00217 template<typename VECTOR>
00218 VECTOR AbstractParameterisedSystem<VECTOR>::GetInitialConditions() const
00219 {
00220     assert(mpSystemInfo);
00221     VECTOR v;
00222     InitialiseEmptyVector(v);
00223     CreateVectorIfEmpty(v, mNumberOfStateVariables);
00224     CopyFromStdVector(mpSystemInfo->GetInitialConditions(), v);
00225     return v;
00226 }
00227 
00228 template<typename VECTOR>
00229 void AbstractParameterisedSystem<VECTOR>::ResetToInitialConditions()
00230 {
00231     VECTOR inits = GetInitialConditions();
00232     SetStateVariables(inits);
00233     DeleteVector(inits);
00234 }
00235 
00236 //
00237 // Parameter methods
00238 //
00239 
00240 unsigned AbstractUntemplatedParameterisedSystem::GetNumberOfParameters() const
00241 {
00242     return GetSystemInformation()->rGetParameterNames().size();
00243 }
00244 
00245 template<typename VECTOR>
00246 double AbstractParameterisedSystem<VECTOR>::GetParameter(unsigned index) const
00247 {
00248     if (index >= GetVectorSize(mParameters))
00249     {
00250         EXCEPTION("The index passed in must be less than the number of parameters.");
00251     }
00252     return GetVectorComponent(mParameters, index);
00253 }
00254 
00255 template<typename VECTOR>
00256 void AbstractParameterisedSystem<VECTOR>::SetParameter(unsigned index, double value)
00257 {
00258     if (index >= GetVectorSize(mParameters))
00259     {
00260         EXCEPTION("The index passed in must be less than the number of parameters.");
00261     }
00262     SetVectorComponent(mParameters, index, value);
00263 }
00264 
00265 template<typename VECTOR>
00266 void AbstractParameterisedSystem<VECTOR>::SetParameter(const std::string& rName, double value)
00267 {
00268     SetVectorComponent(mParameters, GetParameterIndex(rName), value);
00269 }
00270 
00271 template<typename VECTOR>
00272 double AbstractParameterisedSystem<VECTOR>::GetParameter(const std::string& rName) const
00273 {
00274     return GetParameter(GetParameterIndex(rName));
00275 }
00276 
00277 
00278 const std::vector<std::string>& AbstractUntemplatedParameterisedSystem::rGetParameterNames() const
00279 {
00280     return GetSystemInformation()->rGetParameterNames();
00281 }
00282 
00283 const std::vector<std::string>& AbstractUntemplatedParameterisedSystem::rGetParameterUnits() const
00284 {
00285     return GetSystemInformation()->rGetParameterUnits();
00286 }
00287 
00288 unsigned AbstractUntemplatedParameterisedSystem::GetParameterIndex(const std::string& rName) const
00289 {
00290     return GetSystemInformation()->GetParameterIndex(rName);
00291 }
00292 
00293 bool AbstractUntemplatedParameterisedSystem::HasParameter(const std::string& rName) const
00294 {
00295     return GetSystemInformation()->HasParameter(rName);
00296 }
00297 
00298 std::string AbstractUntemplatedParameterisedSystem::GetParameterUnits(unsigned index) const
00299 {
00300     return GetSystemInformation()->GetParameterUnits(index);
00301 }
00302 
00303 //
00304 // "Any variable" methods
00305 //
00306 
00307 template<typename VECTOR>
00308 double AbstractParameterisedSystem<VECTOR>::GetAnyVariable(unsigned index, double time,
00309                                                            VECTOR* pDerivedQuantities)
00310 {
00311     if (index < mNumberOfStateVariables)
00312     {
00313         return GetVectorComponent(mStateVariables, index);
00314     }
00315     else if (index - mNumberOfStateVariables < GetVectorSize(mParameters))
00316     {
00317         return GetVectorComponent(mParameters, index - mNumberOfStateVariables);
00318     }
00319     else
00320     {
00321         unsigned offset = mNumberOfStateVariables + GetVectorSize(mParameters);
00322         if (index - offset < GetNumberOfDerivedQuantities())
00323         {
00324             VECTOR dqs;
00325             if (pDerivedQuantities == NULL)
00326             {
00327                 dqs = ComputeDerivedQuantitiesFromCurrentState(time);
00328                 pDerivedQuantities = &dqs;
00329             }
00330             double value = GetVectorComponent(*pDerivedQuantities, index - offset);
00331             if (pDerivedQuantities == &dqs)
00332             {
00333                 DeleteVector(dqs);
00334             }
00335             return value;
00336         }
00337         else
00338         {
00339             EXCEPTION("Invalid index passed to GetAnyVariable.");
00340         }
00341     }
00342 }
00343 
00344 template<typename VECTOR>
00345 double AbstractParameterisedSystem<VECTOR>::GetAnyVariable(const std::string& rName,
00346                                                            double time,
00347                                                            VECTOR* pDerivedQuantities)
00348 {
00349     return GetAnyVariable(GetAnyVariableIndex(rName), time, pDerivedQuantities);
00350 }
00351 
00352 
00353 unsigned AbstractUntemplatedParameterisedSystem::GetAnyVariableIndex(const std::string& rName) const
00354 {
00355     return GetSystemInformation()->GetAnyVariableIndex(rName);
00356 }
00357 
00358 bool AbstractUntemplatedParameterisedSystem::HasAnyVariable(const std::string& rName) const
00359 {
00360     return GetSystemInformation()->HasAnyVariable(rName);
00361 }
00362 
00363 template<typename VECTOR>
00364 void AbstractParameterisedSystem<VECTOR>::SetAnyVariable(unsigned index, double value)
00365 {
00366     if (index < mNumberOfStateVariables)
00367     {
00368         SetVectorComponent(mStateVariables, index, value);
00369     }
00370     else if (index - mNumberOfStateVariables < GetVectorSize(mParameters))
00371     {
00372         SetVectorComponent(mParameters, index - mNumberOfStateVariables, value);
00373     }
00374     else
00375     {
00376         EXCEPTION("Cannot set the value of a derived quantity, or invalid index.");
00377     }
00378 }
00379 
00380 template<typename VECTOR>
00381 void AbstractParameterisedSystem<VECTOR>::SetAnyVariable(const std::string& rName, double value)
00382 {
00383     SetAnyVariable(GetAnyVariableIndex(rName), value);
00384 }
00385 
00386 
00387 std::string AbstractUntemplatedParameterisedSystem::GetAnyVariableUnits(unsigned index) const
00388 {
00389     return GetSystemInformation()->GetAnyVariableUnits(index);
00390 }
00391 
00392 std::string AbstractUntemplatedParameterisedSystem::GetAnyVariableUnits(const std::string& rName) const
00393 {
00394     return GetAnyVariableUnits(GetAnyVariableIndex(rName));
00395 }
00396 
00397 //
00398 // "Derived quantities" methods
00399 //
00400 
00401 unsigned AbstractUntemplatedParameterisedSystem::GetNumberOfDerivedQuantities() const
00402 {
00403     return GetSystemInformation()->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 
00420 const std::vector<std::string>& AbstractUntemplatedParameterisedSystem::rGetDerivedQuantityNames() const
00421 {
00422     return GetSystemInformation()->rGetDerivedQuantityNames();
00423 }
00424 
00425 const std::vector<std::string>& AbstractUntemplatedParameterisedSystem::rGetDerivedQuantityUnits() const
00426 {
00427     return GetSystemInformation()->rGetDerivedQuantityUnits();
00428 }
00429 
00430 unsigned AbstractUntemplatedParameterisedSystem::GetDerivedQuantityIndex(const std::string& rName) const
00431 {
00432     return GetSystemInformation()->GetDerivedQuantityIndex(rName);
00433 }
00434 
00435 bool AbstractUntemplatedParameterisedSystem::HasDerivedQuantity(const std::string& rName) const
00436 {
00437     return GetSystemInformation()->HasDerivedQuantity(rName);
00438 }
00439 
00440 std::string AbstractUntemplatedParameterisedSystem::GetDerivedQuantityUnits(unsigned index) const
00441 {
00442     return GetSystemInformation()->GetDerivedQuantityUnits(index);
00443 }
00444 
00445 
00446 unsigned AbstractUntemplatedParameterisedSystem::GetNumberOfAttributes() const
00447 {
00448     return GetSystemInformation()->GetNumberOfAttributes();
00449 }
00450 
00451 bool AbstractUntemplatedParameterisedSystem::HasAttribute(const std::string& rName) const
00452 {
00453     return GetSystemInformation()->HasAttribute(rName);
00454 }
00455 
00456 double AbstractUntemplatedParameterisedSystem::GetAttribute(const std::string& rName) const
00457 {
00458     return GetSystemInformation()->GetAttribute(rName);
00459 }
00460 
00461 
00462 
00464 // Explicit instantiation
00466 
00467 template class AbstractParameterisedSystem<std::vector<double> >;
00468 #ifdef CHASTE_CVODE
00469 template class AbstractParameterisedSystem<N_Vector>;
00470 #endif
Generated on Thu Dec 22 13:00:16 2011 for Chaste by  doxygen 1.6.3