Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ValueCache.h
Go to the documentation of this file.
1 /**
2  * @file ValueCache.h
3  */
4 
5 #ifndef CT_VALUECACHE_H
6 #define CT_VALUECACHE_H
7 
8 #include "ct_defs.h"
9 #include <limits>
10 
11 namespace Cantera
12 {
13 
14 /*! A cached property value and the state at which it was evaluated
15  *
16  * This struct stores the value of some property evaluated at a particular
17  * thermodynamic state. The #value can be either a real scalar or an array,
18  * depending on the template parameter `T`. The exact meaning of #state1,
19  * #state2, and #stateNum is determined by the function using the cached value,
20  * which can check any combination of these variables before deciding whether
21  * to recompute the cached values.
22  *
23  * References to CachedValue objects are returned by the "get" methods of
24  * ValueCache, e.g. ValueCache::getScalar. Functions accessing cached values
25  * should use the typedefs CachedScalar and CachedArray. See ValueCache for
26  * details on how these classes should be used together.
27  */
28 template <class T>
29 struct CachedValue {
30  CachedValue() :
31  state1(std::numeric_limits<double>::quiet_NaN()),
32  state2(std::numeric_limits<double>::quiet_NaN()),
33  stateNum(std::numeric_limits<int>::min()),
34  value(T())
35  {
36  }
37 
38  //! Check whether the currently cached value is valid based on
39  //! a single state variable. If it is not valid it updates the stored
40  //! state to the new state in addition to returning false.
41  bool validate(double state1New) {
42  if(state1 == state1New) {
43  return true;
44  } else {
45  state1 = state1New;
46  }
47  return false;
48  }
49 
50  //! Check whether the currently cached value is valid based on
51  //! state1 and state2. If it is not valid it updates the stored
52  //! state to the new state in addition to returning false.
53  bool validate(double state1New, double state2New) {
54  if(state1 == state1New && state2 == state2New) {
55  return true;
56  } else {
57  state1 = state1New;
58  state2 = state2New;
59  }
60  return false;
61  }
62 
63  //! Check whether the currently cached value is valid based on
64  //! state1 and stateNum. If it is not valid it updates the stored
65  //! state to the new state in addition to returning false.
66  bool validate(double state1New, int stateNumNew) {
67  if(state1 == state1New && stateNum == stateNumNew) {
68  return true;
69  } else {
70  state1 = state1New;
71  stateNum = stateNumNew;
72  }
73  return false;
74  }
75 
76  //! Check whether the currently cached value is valid based on
77  //! stateNum. If it is not valid it updates the stored
78  //! state to the new state in addition to returning false.
79  bool validate(int stateNumNew) {
80  if(stateNum == stateNumNew) {
81  return true;
82  } else {
83  stateNum = stateNumNew;
84  }
85  return false;
86  }
87 
88  //! Check whether the currently cached value is valid based on
89  //! state1, state2, and stateNum. If it is not valid it updates the stored
90  //! state to the new state in addition to returning false.
91  bool validate(double state1New, double state2New, int stateNumNew) {
92  if(state1 == state1New && state2 == state2New && stateNum == stateNumNew) {
93  return true;
94  } else {
95  state1 = state1New;
96  state2 = state2New;
97  stateNum = stateNumNew;
98  }
99  return false;
100  }
101 
102  //! Value of the first state variable for the state at which #value was
103  //! evaluated, e.g. temperature.
104  double state1;
105 
106  //! Value of the second state variable for the state at which #value was
107  //! evaluated, e.g. density or pressure.
108  double state2;
109 
110  //! A surrogate for the composition. For cached properties of Phase,
111  //! this should be set to Phase::stateMFNumber()
112  int stateNum;
113 
114  //! The value of the cached property
115  T value;
116 };
117 
120 
121 /*! Storage for cached values
122  *
123  * Stores cached values of properties evaluated at a particular thermodynamic
124  * state. A class that needs cached values can have a ValueCache as a
125  * member variable.
126  *
127  * Each method in the class that implements caching behavior needs a unique id
128  * for its cached value. This id should be obtained by using the getId()
129  * function to initialize a static variable within the function.
130  *
131  * For cases where the property is a scalar or vector, the cached value can be
132  * stored in the CachedValue object. If the data type of the cached value is
133  * more complex, then it can be stored in the calling class, and the value
134  * attribute of the CachedScalar object can be ignored.
135  *
136  * An example use of class ValueCache:
137  * @code
138  * class Example {
139  * ValueCache m_cache;
140  * doublereal get_property(doublereal T, doublereal P) {
141  * const static int cacheId = m_cache.getId();
142  * CachedScalar cached = m_cache.getScalar(cacheId);
143  * if (T != cached.state1 || P != cached.state2) {
144  * cached.value = some_expensive_function(T,P);
145  * cached.state1 = T;
146  * cached.state2 = P;
147  * }
148  * return cached.value;
149  * }
150  * };
151  * @endcode
152  */
154 {
155 public:
156  //! Get a unique id for a cached value. Must be called exactly once for each
157  //! method that implements caching behavior.
158  int getId();
159 
160  //! Get a reference to a CachedValue object representing a scalar
161  //! (doublereal) with the given id.
163  return m_scalarCache[id];
164  }
165 
166  //! Get a reference to a CachedValue object representing an array (vector_fp)
167  //! with the given id.
169  return m_arrayCache[id];
170  }
171 
172  //! Clear all cached values. This method should be called if the cached
173  //! values may be invalidated in a way that is not represented by the state
174  //! variables alone, such as a change to the constants defining a species
175  //! thermodynamics as a function of temperature.
176  void clear();
177 
178 protected:
179  //! Cached scalar values
180  std::map<int, CachedValue<double> > m_scalarCache;
181 
182  //! Cached array values
183  std::map<int, CachedValue<vector_fp> > m_arrayCache;
184 
185  //! The last assigned id. Automatically incremented by the getId() method.
186  static int m_last_id;
187 };
188 
189 }
190 
191 #endif
int getId()
Get a unique id for a cached value.
Definition: ValueCache.cpp:18
This file contains definitions of terms that are used in internal routines and are unlikely to need m...
int stateNum
A surrogate for the composition.
Definition: ValueCache.h:112
static int m_last_id
The last assigned id. Automatically incremented by the getId() method.
Definition: ValueCache.h:186
bool validate(int stateNumNew)
Check whether the currently cached value is valid based on stateNum.
Definition: ValueCache.h:79
bool validate(double state1New, int stateNumNew)
Check whether the currently cached value is valid based on state1 and stateNum.
Definition: ValueCache.h:66
std::map< int, CachedValue< vector_fp > > m_arrayCache
Cached array values.
Definition: ValueCache.h:183
CachedScalar getScalar(int id)
Get a reference to a CachedValue object representing a scalar (doublereal) with the given id...
Definition: ValueCache.h:162
std::map< int, CachedValue< double > > m_scalarCache
Cached scalar values.
Definition: ValueCache.h:180
void clear()
Clear all cached values.
Definition: ValueCache.cpp:24
bool validate(double state1New)
Check whether the currently cached value is valid based on a single state variable.
Definition: ValueCache.h:41
double state1
Value of the first state variable for the state at which value was evaluated, e.g.
Definition: ValueCache.h:104
double state2
Value of the second state variable for the state at which value was evaluated, e.g.
Definition: ValueCache.h:108
CachedArray getArray(int id)
Get a reference to a CachedValue object representing an array (vector_fp) with the given id...
Definition: ValueCache.h:168
T value
The value of the cached property.
Definition: ValueCache.h:115
bool validate(double state1New, double state2New)
Check whether the currently cached value is valid based on state1 and state2.
Definition: ValueCache.h:53
bool validate(double state1New, double state2New, int stateNumNew)
Check whether the currently cached value is valid based on state1, state2, and stateNum.
Definition: ValueCache.h:91