Chaste  Release::2017.1
AbstractParameterisedSystem.cpp
1 /*
2 
3 Copyright (c) 2005-2017, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #include <sstream>
37 #include <cassert>
38 
39 #include "AbstractParameterisedSystem.hpp"
40 
41 #include "Exception.hpp"
43 
44 
45 template<typename VECTOR>
47  : AbstractUntemplatedParameterisedSystem(numberOfStateVariables)
48 {
51 }
52 
53 template<typename VECTOR>
54 std::string AbstractParameterisedSystem<VECTOR>::DumpState(const std::string& rMessage)
55 {
56  return GetStateMessage(rMessage, mStateVariables);
57 }
58 
59 template<typename VECTOR>
60 std::string AbstractParameterisedSystem<VECTOR>::DumpState(const std::string& rMessage,
61  VECTOR Y)
62 {
63  return GetStateMessage(rMessage, Y);
64 }
65 
66 template<typename VECTOR>
67 std::string AbstractParameterisedSystem<VECTOR>::DumpState(const std::string& rMessage,
68  VECTOR Y,
69  double time)
70 {
71  std::stringstream extra_message;
72  extra_message << std::endl << "At independent variable (usually time) = " << time;
73  std::string new_message = rMessage + extra_message.str();
74  return GetStateMessage(new_message, Y);
75 }
76 
77 template<typename VECTOR>
78 std::string AbstractParameterisedSystem<VECTOR>::GetStateMessage(const std::string& rMessage, VECTOR Y)
79 {
80  std::stringstream res;
81  res << rMessage << std::endl << "State:" << std::endl;
82  assert(rGetStateVariableNames().size()==GetVectorSize(Y));
83  const std::vector<std::string>& r_units = rGetStateVariableUnits();
84  for (unsigned i=0; i<GetVectorSize(Y); i++)
85  {
86  res << "\t" << rGetStateVariableNames()[i] << ":" << GetVectorComponent(Y, i);
87  if (!r_units.empty())
88  {
89  res << " " << r_units[i];
90  }
91  res << std::endl;
92  }
93  return res.str();
94 }
95 
96 template<typename VECTOR>
97 void AbstractParameterisedSystem<VECTOR>::CheckParametersOnLoad(const std::vector<double>& rParameters, const std::vector<std::string>& rParameterNames)
98 {
100  {
101  // Subclass constructor didn't give default values, so we need the archive to provide them all
102  if (rParameterNames.size() != rGetParameterNames().size())
103  {
104  EXCEPTION("Number of ODE parameters in archive does not match number in class.");
105  }
107  }
108 
109  // Check whether the archive specifies parameters that don't appear in this class,
110  // and create a map from archive index to local index
111  std::vector<unsigned> index_map(rParameterNames.size());
112  for (unsigned i=0; i<rParameterNames.size(); ++i)
113  {
114  index_map[i] = find(rGetParameterNames().begin(), rGetParameterNames().end(), rParameterNames[i])
115  - rGetParameterNames().begin();
116  if (index_map[i] == rGetParameterNames().size())
117  {
118  EXCEPTION("Archive specifies a parameter '" + rParameterNames[i] + "' which does not appear in this class.");
119  }
120  }
121 
122  for (unsigned i=0; i<rParameterNames.size(); ++i)
123  {
124  SetVectorComponent(mParameters,index_map[i],rParameters[i]);
125  }
126 
127  // Paranoia check
128  assert(GetVectorSize(mParameters) == rGetParameterNames().size());
129 }
130 
131 //
132 // State variable methods
133 //
134 
135 template<typename VECTOR>
137 {
138  return mStateVariables;
139 }
140 
141 template<typename VECTOR>
143 {
144  return CopyVector(mStateVariables);
145 }
146 
147 template<typename VECTOR>
149 {
150  if (mNumberOfStateVariables != GetVectorSize(rStateVariables))
151  {
152  EXCEPTION("The size of the passed in vector must be that of the number of state variables.");
153  }
154 
156  for (unsigned i=0; i<mNumberOfStateVariables; i++)
157  {
158  SetVectorComponent(mStateVariables, i, GetVectorComponent(rStateVariables, i));
159  }
160 }
161 
162 template<typename VECTOR>
164 {
165  if (index >= mNumberOfStateVariables)
166  {
167  EXCEPTION("The index passed in must be less than the number of state variables.");
168  }
169  return GetVectorComponent(mStateVariables, index);
170 }
171 
172 template<typename VECTOR>
173 double AbstractParameterisedSystem<VECTOR>::GetStateVariable(const std::string& rName) const
174 {
176 }
177 
178 template<typename VECTOR>
179 void AbstractParameterisedSystem<VECTOR>::SetStateVariable(unsigned index, double newValue)
180 {
181  if (mNumberOfStateVariables <= index)
182  {
183  EXCEPTION("The index passed in must be less than the number of state variables.");
184  }
185  SetVectorComponent(mStateVariables, index, newValue);
186 }
187 
188 template<typename VECTOR>
189 void AbstractParameterisedSystem<VECTOR>::SetStateVariable(const std::string& rName, double newValue)
190 {
191  SetStateVariable(GetStateVariableIndex(rName), newValue);
192 }
193 
194 //
195 // Initial condition methods
196 //
197 
198 template<typename VECTOR>
200 {
201  if (GetVectorSize(rInitialConditions) != mNumberOfStateVariables)
202  {
203  EXCEPTION("The number of initial conditions must be that of the number of state variables.");
204  }
205  assert(mpSystemInfo);
206  std::vector<double> inits;
207  CopyToStdVector(rInitialConditions, inits);
208  mpSystemInfo->SetDefaultInitialConditions(inits);
209 }
210 
211 template<typename VECTOR>
212 void AbstractParameterisedSystem<VECTOR>::SetDefaultInitialCondition(unsigned index, double initialCondition)
213 {
214  if (index >= mNumberOfStateVariables)
215  {
216  EXCEPTION("Index is greater than the number of state variables.");
217  }
218  assert(mpSystemInfo);
219  mpSystemInfo->SetDefaultInitialCondition(index, initialCondition);
220 }
221 
222 template<typename VECTOR>
224 {
225  assert(mpSystemInfo);
226  VECTOR v;
229  CopyFromStdVector(mpSystemInfo->GetInitialConditions(), v);
230  return v;
231 }
232 
233 template<typename VECTOR>
235 {
236  VECTOR inits = GetInitialConditions();
237  SetStateVariables(inits);
238  DeleteVector(inits);
239 }
240 
241 //
242 // Parameter methods
243 //
244 
245 template<typename VECTOR>
247 {
248  if (index >= GetVectorSize(mParameters))
249  {
250  EXCEPTION("The index passed in must be less than the number of parameters.");
251  }
252  return GetVectorComponent(mParameters, index);
253 }
254 
255 template<typename VECTOR>
256 void AbstractParameterisedSystem<VECTOR>::SetParameter(unsigned index, double value)
257 {
258  if (index >= GetVectorSize(mParameters))
259  {
260  EXCEPTION("The index passed in must be less than the number of parameters.");
261  }
262  SetVectorComponent(mParameters, index, value);
263 }
264 
265 template<typename VECTOR>
266 void AbstractParameterisedSystem<VECTOR>::SetParameter(const std::string& rName, double value)
267 {
269 }
270 
271 template<typename VECTOR>
272 double AbstractParameterisedSystem<VECTOR>::GetParameter(const std::string& rName) const
273 {
274  return GetParameter(GetParameterIndex(rName));
275 }
276 
277 //
278 // "Any variable" methods
279 //
280 
281 template<typename VECTOR>
282 double AbstractParameterisedSystem<VECTOR>::GetAnyVariable(unsigned index, double time,
283  VECTOR* pDerivedQuantities)
284 {
285  if (index < mNumberOfStateVariables)
286  {
287  return GetVectorComponent(mStateVariables, index);
288  }
290  {
292  }
293  else
294  {
296  if (index - offset < GetNumberOfDerivedQuantities())
297  {
298  VECTOR dqs;
299  if (pDerivedQuantities == nullptr)
300  {
302  pDerivedQuantities = &dqs;
303  }
304  double value = GetVectorComponent(*pDerivedQuantities, index - offset);
305  if (pDerivedQuantities == &dqs)
306  {
307  DeleteVector(dqs);
308  }
309  return value;
310  }
311  else
312  {
313  EXCEPTION("Invalid index passed to GetAnyVariable.");
314  }
315  }
316 }
317 
318 template<typename VECTOR>
320  double time,
321  VECTOR* pDerivedQuantities)
322 {
323  return GetAnyVariable(GetAnyVariableIndex(rName), time, pDerivedQuantities);
324 }
325 
326 template<typename VECTOR>
327 void AbstractParameterisedSystem<VECTOR>::SetAnyVariable(unsigned index, double value)
328 {
329  if (index < mNumberOfStateVariables)
330  {
331  SetVectorComponent(mStateVariables, index, value);
332  }
334  {
336  }
337  else
338  {
339  EXCEPTION("Cannot set the value of a derived quantity, or invalid index.");
340  }
341 }
342 
343 template<typename VECTOR>
344 void AbstractParameterisedSystem<VECTOR>::SetAnyVariable(const std::string& rName, double value)
345 {
346  SetAnyVariable(GetAnyVariableIndex(rName), value);
347 }
348 
349 //
350 // "Derived quantities" methods
351 //
352 
353 template<typename VECTOR>
355  const VECTOR& rState)
356 {
357  EXCEPTION("This ODE system does not define derived quantities.");
358 }
359 
360 template<typename VECTOR>
362 {
363  return this->ComputeDerivedQuantities(time, mStateVariables);
364 }
365 
366 
368 
370 #ifdef CHASTE_CVODE
372 #endif
void SetDefaultInitialCondition(unsigned index, double initialCondition)
void SetDefaultInitialConditions(const VECTOR &rInitialConditions)
const std::vector< std::string > & rGetStateVariableUnits() const
#define EXCEPTION(message)
Definition: Exception.hpp:143
VECTOR CopyVector(VECTOR &rVec)
std::string DumpState(const std::string &rMessage)
void InitialiseEmptyVector(VECTOR &rVec)
void SetStateVariables(const VECTOR &rStateVariables)
double GetStateVariable(unsigned index) const
const std::vector< std::string > & rGetStateVariableNames() const
unsigned GetParameterIndex(const std::string &rName) const
double GetParameter(unsigned index) const
unsigned GetAnyVariableIndex(const std::string &rName) const
void CreateVectorIfEmpty(VECTOR &rVec, unsigned size)
unsigned GetStateVariableIndex(const std::string &rName) const
void SetVectorComponent(VECTOR &rVec, unsigned index, double value)
void SetAnyVariable(unsigned index, double value)
void SetParameter(const std::string &rName, double value)
boost::shared_ptr< AbstractOdeSystemInformation > mpSystemInfo
virtual VECTOR ComputeDerivedQuantities(double time, const VECTOR &rState)
void SetStateVariable(unsigned index, double newValue)
void CopyFromStdVector(const std::vector< double > &rSrc, VECTOR &rDest)
void CheckParametersOnLoad(const std::vector< double > &rParameters, const std::vector< std::string > &rParameterNames)
void CopyToStdVector(const VECTOR &rSrc, std::vector< double > &rDest)
double GetVectorComponent(const VECTOR &rVec, unsigned index)
VECTOR ComputeDerivedQuantitiesFromCurrentState(double time)
std::string GetStateMessage(const std::string &rMessage, VECTOR Y)
const std::vector< std::string > & rGetParameterNames() const
void DeleteVector(VECTOR &rVec)
double GetAnyVariable(unsigned index, double time=0.0, VECTOR *pDerivedQuantities=NULL)
unsigned GetVectorSize(const VECTOR &rVec)
AbstractParameterisedSystem(unsigned numberOfStateVariables)