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