ValueCache is a generic helper template to implement an unstructured data cache, where arbitrary result types can be placed in an n-dimensional space of real numbers. More...
#include <ValueCache.h>
Public Member Functions | |
ValueCache (std::size_t in_dim, std::size_t max_leaf_size=10) | |
Construct a ValueCache with indices in in_dim dimensions. More... | |
ValueCache (const std::string &file_name, std::size_t in_dim, std::size_t max_leaf_size=10) | |
Same as above, but provide a filename for storing/restoring the cache content between runs. More... | |
~ValueCache () | |
Object destructor. If the cache was constructed with a file name, it gets written here. More... | |
void | insert (const std::vector< Real > &in_val, const T &out_val) |
insert a new value out_value at the position in_val More... | |
std::tuple< const std::vector< Real > &, const T &, Real > | getNeighbor (const std::vector< Real > &in_val) |
get a single neighbor of in_val along with stored values and distances More... | |
std::vector< std::tuple< const std::vector< Real > &, const T &, Real > > | getNeighbors (const std::vector< Real > &in_val, const std::size_t k) |
get a list of up to k neighbors of in_val along with stored values and distances More... | |
std::size_t | size () |
return the number of cache entries More... | |
void | clear () |
remove all data from the cache More... | |
std::size_t | kdtree_get_point_count () const |
Nanoflann interface functions. More... | |
Real | kdtree_get_pt (const std::size_t idx, const std::size_t dim) const |
template<class BBOX > | |
bool | kdtree_get_bbox (BBOX &bb) const |
Protected Types | |
using | KdTreeT = nanoflann::KDTreeSingleIndexDynamicAdaptor< nanoflann::L2_Simple_Adaptor< Real, ValueCache< T > >, ValueCache< T >, -1, std::size_t > |
Protected Member Functions | |
void | rebuildTree () |
rebuild the kd-tree from scratch and update the bounding box More... | |
Protected Attributes | |
std::vector< std::pair< std::vector< Real >, T > > | _location_data |
std::unique_ptr< KdTreeT > | _kd_tree |
const std::size_t | _in_dim |
const std::size_t | _max_leaf_size |
const std::size_t | _max_subtrees |
std::optional< std::string > | _persistent_storage_file |
file name for persistent store/restore of the cache More... | |
std::vector< std::pair< Real, Real > > | _bbox |
bounding box (updated upon insertion) More... | |
Friends | |
void | dataStore (std::ostream &stream, ValueCache< T > &c, void *context) |
void | dataLoad (std::istream &stream, ValueCache< T > &c, void *context) |
ValueCache is a generic helper template to implement an unstructured data cache, where arbitrary result types can be placed in an n-dimensional space of real numbers.
Upon lookup the closest result to a given point in n-space is returned. Applications include caching reasonable initial guesses for local Newton solves as found in certain phase field models as well as thermodynamic Gibbs energy minimization.
Definition at line 27 of file ValueCache.h.
|
protected |
Definition at line 86 of file ValueCache.h.
ValueCache< T >::ValueCache | ( | std::size_t | in_dim, |
std::size_t | max_leaf_size = 10 |
||
) |
Construct a ValueCache with indices in in_dim dimensions.
Definition at line 106 of file ValueCache.h.
ValueCache< T >::ValueCache | ( | const std::string & | file_name, |
std::size_t | in_dim, | ||
std::size_t | max_leaf_size = 10 |
||
) |
Same as above, but provide a filename for storing/restoring the cache content between runs.
Definition at line 112 of file ValueCache.h.
ValueCache< T >::~ValueCache | ( | ) |
Object destructor. If the cache was constructed with a file name, it gets written here.
Definition at line 132 of file ValueCache.h.
void ValueCache< T >::clear | ( | ) |
remove all data from the cache
Definition at line 243 of file ValueCache.h.
std::tuple< const std::vector< Real > &, const T &, Real > ValueCache< T >::getNeighbor | ( | const std::vector< Real > & | in_val | ) |
get a single neighbor of in_val along with stored values and distances
Retrieve a single closest neighbor to in_val.
Throws an exception if no values are stored.
Definition at line 184 of file ValueCache.h.
std::vector< std::tuple< const std::vector< Real > &, const T &, Real > > ValueCache< T >::getNeighbors | ( | const std::vector< Real > & | in_val, |
const std::size_t | k | ||
) |
get a list of up to k neighbors of in_val along with stored values and distances
This function performs a search on the value cache and returns either the k-nearest neighbors or the neighbors available if the cache size is less than k.
Definition at line 209 of file ValueCache.h.
void ValueCache< T >::insert | ( | const std::vector< Real > & | in_val, |
const T & | out_val | ||
) |
insert a new value out_value at the position in_val
Definition at line 146 of file ValueCache.h.
bool ValueCache< T >::kdtree_get_bbox | ( | BBOX & | bb | ) | const |
Definition at line 291 of file ValueCache.h.
std::size_t ValueCache< T >::kdtree_get_point_count | ( | ) | const |
Nanoflann interface functions.
Definition at line 276 of file ValueCache.h.
Real ValueCache< T >::kdtree_get_pt | ( | const std::size_t | idx, |
const std::size_t | dim | ||
) | const |
Definition at line 283 of file ValueCache.h.
|
protected |
rebuild the kd-tree from scratch and update the bounding box
Definition at line 251 of file ValueCache.h.
Referenced by dataLoad().
std::size_t ValueCache< T >::size | ( | ) |
return the number of cache entries
Definition at line 236 of file ValueCache.h.
|
friend |
Definition at line 311 of file ValueCache.h.
Referenced by ValueCache< T >::ValueCache().
|
friend |
Definition at line 304 of file ValueCache.h.
|
protected |
bounding box (updated upon insertion)
Definition at line 99 of file ValueCache.h.
|
protected |
Definition at line 91 of file ValueCache.h.
|
protected |
Definition at line 89 of file ValueCache.h.
|
protected |
Definition at line 88 of file ValueCache.h.
Referenced by dataLoad(), and dataStore().
|
protected |
Definition at line 92 of file ValueCache.h.
|
protected |
Definition at line 93 of file ValueCache.h.
|
protected |
file name for persistent store/restore of the cache
Definition at line 96 of file ValueCache.h.
Referenced by ValueCache< T >::ValueCache().