Chaste  Release::2017.1
AbstractOdeSystemInformation.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 
37 #include <cassert>
38 #include <algorithm>
39 
40 #include "AbstractOdeSystemInformation.hpp"
41 #include "Exception.hpp"
42 
44  : mInitialised(false)
45 {
46 }
47 
49 {
50 }
51 
53 {
54  return mSystemName;
55 }
56 
58 {
59  return mFreeVariableName;
60 }
61 
63 {
64  return mFreeVariableUnits;
65 }
66 
67 void AbstractOdeSystemInformation::SetDefaultInitialConditions(const std::vector<double>& rInitialConditions)
68 {
69  assert(mInitialised);
70  mInitialConditions = rInitialConditions;
71 }
72 
73 void AbstractOdeSystemInformation::SetDefaultInitialCondition(unsigned index, double initialCondition)
74 {
75  assert(mInitialised);
76  mInitialConditions.at(index) = initialCondition;
77 }
78 
80 {
81  assert(mInitialised);
82  return mInitialConditions;
83 }
84 
85 const std::vector<std::string>& AbstractOdeSystemInformation::rGetStateVariableNames() const
86 {
87  assert(mInitialised);
88  return mVariableNames;
89 }
90 
91 const std::vector<std::string>& AbstractOdeSystemInformation::rGetStateVariableUnits() const
92 {
93  assert(mInitialised);
94  return mVariableUnits;
95 }
96 
97 unsigned AbstractOdeSystemInformation::GetStateVariableIndex(const std::string& rName) const
98 {
99  assert(mInitialised);
100  std::vector<std::string>::const_iterator it = find(mVariableNames.begin(), mVariableNames.end(), rName);
101  if (it == mVariableNames.end())
102  {
103  EXCEPTION("No state variable named '" + rName + "'.");
104  }
105  return (unsigned)(it - mVariableNames.begin());
106 }
107 
108 bool AbstractOdeSystemInformation::HasStateVariable(const std::string& rName) const
109 {
110  assert(mInitialised);
111  std::vector<std::string>::const_iterator it = find(mVariableNames.begin(), mVariableNames.end(), rName);
112  return (it != mVariableNames.end());
113 }
114 
116 {
117  assert(mInitialised);
118  if (index >= mVariableUnits.size())
119  {
120  EXCEPTION("The index passed in must be less than the number of state variables.");
121  }
122  return mVariableUnits[index];
123 }
124 
125 const std::vector<std::string>& AbstractOdeSystemInformation::rGetParameterNames() const
126 {
127  assert(mInitialised);
128  return mParameterNames;
129 }
130 
131 const std::vector<std::string>& AbstractOdeSystemInformation::rGetParameterUnits() const
132 {
133  assert(mInitialised);
134  return mParameterUnits;
135 }
136 
137 unsigned AbstractOdeSystemInformation::GetParameterIndex(const std::string& rName) const
138 {
139  assert(mInitialised);
140  std::vector<std::string>::const_iterator it = find(mParameterNames.begin(), mParameterNames.end(), rName);
141  if (it == mParameterNames.end())
142  {
143  EXCEPTION("No parameter named '" + rName + "'.");
144  }
145  return (unsigned)(it - mParameterNames.begin());
146 }
147 
148 bool AbstractOdeSystemInformation::HasParameter(const std::string& rName) const
149 {
150  assert(mInitialised);
151  std::vector<std::string>::const_iterator it = find(mParameterNames.begin(), mParameterNames.end(), rName);
152  return (it != mParameterNames.end());
153 }
154 
155 std::string AbstractOdeSystemInformation::GetParameterUnits(unsigned index) const
156 {
157  assert(mInitialised);
158  if (index >= mParameterUnits.size())
159  {
160  EXCEPTION("The index passed in must be less than the number of parameters.");
161  }
162  return mParameterUnits[index];
163 }
164 
166 {
167  assert(mInitialised);
168  return mParameterUnits.size();
169 }
170 
171 unsigned AbstractOdeSystemInformation::GetAnyVariableIndex(const std::string& rName) const
172 {
173  assert(mInitialised);
174  if (HasStateVariable(rName))
175  {
176  return GetStateVariableIndex(rName);
177  }
178  else if (HasParameter(rName))
179  {
180  return mVariableNames.size() + GetParameterIndex(rName);
181  }
182  else if (HasDerivedQuantity(rName))
183  {
184  return mVariableNames.size() + mParameterNames.size() + GetDerivedQuantityIndex(rName);
185  }
186  else
187  {
188  EXCEPTION("No state variable, parameter, or derived quantity named '" + rName + "'.");
189  }
190 }
191 
192 
193 bool AbstractOdeSystemInformation::HasAnyVariable(const std::string& rName) const
194 {
195  assert(mInitialised);
196  return (HasStateVariable(rName) || HasParameter(rName) || HasDerivedQuantity(rName));
197 }
198 
199 std::string AbstractOdeSystemInformation::GetAnyVariableUnits(unsigned index) const
200 {
201  assert(mInitialised);
202  if (index < mVariableUnits.size())
203  {
204  return mVariableUnits[index];
205  }
206  else
207  {
208  unsigned offset = mVariableUnits.size();
209  if (index - offset < mParameterUnits.size())
210  {
211  return mParameterUnits[index - offset];
212  }
213  else
214  {
215  offset += mParameterUnits.size();
216  if (index - offset < mDerivedQuantityUnits.size())
217  {
218  return mDerivedQuantityUnits[index - offset];
219  }
220  else
221  {
222  EXCEPTION("Invalid index passed to GetAnyVariableUnits.");
223  }
224  }
225  }
226 }
227 
228 
229 const std::vector<std::string>& AbstractOdeSystemInformation::rGetDerivedQuantityNames() const
230 {
231  assert(mInitialised);
232  return mDerivedQuantityNames;
233 }
234 
235 const std::vector<std::string>& AbstractOdeSystemInformation::rGetDerivedQuantityUnits() const
236 {
237  assert(mInitialised);
238  return mDerivedQuantityUnits;
239 }
240 
241 unsigned AbstractOdeSystemInformation::GetDerivedQuantityIndex(const std::string& rName) const
242 {
243  assert(mInitialised);
244  std::vector<std::string>::const_iterator it = find(mDerivedQuantityNames.begin(), mDerivedQuantityNames.end(), rName);
245  if (it == mDerivedQuantityNames.end())
246  {
247  EXCEPTION("No derived quantity named '" + rName + "'.");
248  }
249  return (unsigned)(it - mDerivedQuantityNames.begin());
250 }
251 
252 bool AbstractOdeSystemInformation::HasDerivedQuantity(const std::string& rName) const
253 {
254  assert(mInitialised);
255  std::vector<std::string>::const_iterator it = find(mDerivedQuantityNames.begin(), mDerivedQuantityNames.end(), rName);
256  return (it != mDerivedQuantityNames.end());
257 }
258 
260 {
261  assert(mInitialised);
262  if (index >= mDerivedQuantityUnits.size())
263  {
264  EXCEPTION("The index passed in must be less than the number of derived quantities.");
265  }
266  return mDerivedQuantityUnits[index];
267 }
268 
270 {
271  assert(mInitialised);
272  return mDerivedQuantityUnits.size();
273 }
274 
276 {
277  assert(mInitialised);
278  return mAttributes.size();
279 }
280 
281 bool AbstractOdeSystemInformation::HasAttribute(const std::string& rName) const
282 {
283  assert(mInitialised);
284  return (mAttributes.find(rName) != mAttributes.end());
285 }
286 
287 double AbstractOdeSystemInformation::GetAttribute(const std::string& rName) const
288 {
289  assert(mInitialised);
290  std::map<std::string, double>::const_iterator it = mAttributes.find(rName);
291  if (it == mAttributes.end())
292  {
293  EXCEPTION("No attribute '" + rName + "' found.");
294  }
295  return it->second;
296 }
std::string GetDerivedQuantityUnits(unsigned index) const
std::vector< std::string > mDerivedQuantityNames
const std::vector< std::string > & rGetStateVariableUnits() const
void SetDefaultInitialConditions(const std::vector< double > &rInitialConditions)
std::vector< std::string > mParameterNames
std::string GetParameterUnits(unsigned index) const
void SetDefaultInitialCondition(unsigned index, double initialCondition)
#define EXCEPTION(message)
Definition: Exception.hpp:143
std::string GetStateVariableUnits(unsigned index) const
std::vector< double > GetInitialConditions() const
const std::vector< std::string > & rGetParameterNames() const
std::string GetAnyVariableUnits(unsigned index) const
unsigned GetStateVariableIndex(const std::string &rName) const
const std::vector< std::string > & rGetParameterUnits() const
std::vector< std::string > mVariableNames
std::vector< std::string > mParameterUnits
unsigned GetAnyVariableIndex(const std::string &rName) const
bool HasAnyVariable(const std::string &rName) const
std::vector< std::string > mVariableUnits
double GetAttribute(const std::string &rName) const
bool HasAttribute(const std::string &rName) const
std::vector< std::string > mDerivedQuantityUnits
bool HasStateVariable(const std::string &rName) const
unsigned GetDerivedQuantityIndex(const std::string &rName) const
bool HasDerivedQuantity(const std::string &rName) const
unsigned GetParameterIndex(const std::string &rName) const
bool HasParameter(const std::string &rName) const
const std::vector< std::string > & rGetDerivedQuantityUnits() const
const std::vector< std::string > & rGetStateVariableNames() const
std::map< std::string, double > mAttributes
const std::vector< std::string > & rGetDerivedQuantityNames() const