Cantera  3.1.0
Loading...
Searching...
No Matches
ValueCache Class Reference

Storage for cached values. More...

#include <ValueCache.h>

Detailed Description

Storage for cached values.

Stores cached values of properties evaluated at a particular thermodynamic state. A class that needs cached values can have a ValueCache as a member variable.

Each method in the class that implements caching behavior needs a unique id for its cached value. This id should be obtained by using the getId() function to initialize a static variable within the function.

For cases where the property is a scalar or vector, the cached value can be stored in the CachedValue object. If the data type of the cached value is more complex, then it can be stored in the calling class, and the value attribute of the CachedScalar object can be ignored.

An example use of class ValueCache:

class Example {
ValueCache m_cache;
double get_property(double T, double P) {
const static int cacheId = m_cache.getId();
CachedScalar cached = m_cache.getScalar(cacheId);
if (T != cached.state1 || P != cached.state2) {
cached.value = some_expensive_function(T,P);
cached.state1 = T;
cached.state2 = P;
}
return cached.value;
}
};
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
int getId()
Get a unique id for a cached value.
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
T value
The value of the cached property.
Definition ValueCache.h:113
double state1
Value of the first state variable for the state at which value was evaluated, for example temperature...
Definition ValueCache.h:102

Definition at line 152 of file ValueCache.h.

Public Member Functions

int getId ()
 Get a unique id for a cached value.
 
CachedScalar getScalar (int id)
 Get a reference to a CachedValue object representing a scalar (double) with the given id.
 
CachedArray getArray (int id)
 Get a reference to a CachedValue object representing an array (vector<double>) with the given id.
 
void clear ()
 Clear all cached values.
 

Protected Attributes

map< int, CachedValue< double > > m_scalarCache
 Cached scalar values.
 
map< int, CachedValue< vector< double > > > m_arrayCache
 Cached array values.
 

Static Protected Attributes

static int m_last_id = 0
 The last assigned id. Automatically incremented by the getId() method.
 

Member Function Documentation

◆ getId()

int getId ( )

Get a unique id for a cached value.

Must be called exactly once for each method that implements caching behavior.

Definition at line 21 of file ValueCache.cpp.

◆ getScalar()

CachedScalar getScalar ( int  id)
inline

Get a reference to a CachedValue object representing a scalar (double) with the given id.

Definition at line 161 of file ValueCache.h.

◆ getArray()

CachedArray getArray ( int  id)
inline

Get a reference to a CachedValue object representing an array (vector<double>) with the given id.

Definition at line 167 of file ValueCache.h.

◆ clear()

void clear ( )

Clear all cached values.

This method should be called if the cached values may be invalidated in a way that is not represented by the state variables alone, such as a change to the constants defining a species thermodynamics as a function of temperature.

Definition at line 27 of file ValueCache.cpp.

Member Data Documentation

◆ m_scalarCache

map<int, CachedValue<double> > m_scalarCache
protected

Cached scalar values.

Definition at line 179 of file ValueCache.h.

◆ m_arrayCache

map<int, CachedValue<vector<double> > > m_arrayCache
protected

Cached array values.

Definition at line 182 of file ValueCache.h.

◆ m_last_id

int m_last_id = 0
staticprotected

The last assigned id. Automatically incremented by the getId() method.

Definition at line 185 of file ValueCache.h.


The documentation for this class was generated from the following files: