OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
TimeDependenceCache.cpp
Go to the documentation of this file.
1//
2// Class TimeDependenceCache
4// Copyright (c) 2025, Jon Thompson, STFC Rutherford Appleton Laboratory, Didcot, UK
5//
6// This file is part of OPAL.
7//
8// OPAL is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation, either version 3 of the License, or
11// (at your option) any later version.
12//
13// You should have received a copy of the GNU General Public License
14// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
15//
16
17#include "TimeDependenceCache.h"
18#include <algorithm>
19
21 timeDependence_m = timeDependence;
22 reset();
23}
24
26 head_m = 0;
27 if (timeDependence_m) {
28 // Start with the cache full to make expiry handling easier
29 for (auto& [time_m, value_m, integral_m] : cache_m) {
30 time_m = 0.0;
31 value_m = timeDependence_m->getValue(0.0);
32 integral_m = timeDependence_m->getIntegral(0.0);
33 }
34 }
35}
36
39 return std::find_if(cache_m.begin(), cache_m.end(), [time](const Item& item) {
40 return item.time_m == time;
41 });
42}
43
44double TimeDependenceCache::getValue(const double time) {
45 double result{};
46 if (timeDependence_m) {
47 if (const auto pos = find(time); pos == cache_m.end()) {
48 result = placeInCache(time).value_m;
49 } else {
50 result = pos->value_m;
51 }
52 }
53 return result;
54}
55
56double TimeDependenceCache::getIntegral(const double time) {
57 double result{};
58 if (timeDependence_m) {
59 if (const auto pos = find(time); pos == cache_m.end()) {
60 result = placeInCache(time).integral_m;
61 } else {
62 result = pos->integral_m;
63 }
64 }
65 return result;
66}
67
69 auto& result = cache_m.at(head_m);
70 result.time_m = time;
71 result.value_m = timeDependence_m->getValue(time);
72 result.integral_m = timeDependence_m->getIntegral(time);
73 ++head_m;
74 if (head_m >= CacheSize) {
75 head_m = 0;
76 }
77 return result;
78}
std::string::iterator iterator
Definition: MSLang.h:15
Time dependence abstraction for field parameters that vary slowly with time; for example,...
virtual double getValue(double time)=0
getValue(time) returns the value as a function of time.
virtual double getIntegral(double time)=0
getIntegral(time) returns the integral from 0 to time of the function.
std::array< Item, CacheSize > cache_m
const Item & placeInCache(double time)
double getIntegral(double time)
std::array< Item, CacheSize >::iterator find(double time)
static constexpr size_t CacheSize
AbstractTimeDependence * timeDependence_m
void setTimeDependence(AbstractTimeDependence *timeDependence)
double getValue(double time)