OpenLB 1.7
Loading...
Searching...
No Matches
benchmarkUtil.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2019 Mathias J. Krause, Maximilian Gaedtke, Marc Haußmann,
4 * Davide Dapelo, Jonathan Jeppener-Haltenhoff, Julius Jeßberger
5 * E-mail contact: info@openlb.net
6 * The most recent release of OpenLB can be downloaded at
7 * <http://www.openlb.net/>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public
20 * License along with this program; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23*/
24
25#ifndef BENCHMARK_UTIL_H
26#define BENCHMARK_UTIL_H
27
28#include <deque>
29#include <functional>
30#include "calc.h"
34#include "io/ostreamManager.h"
35
36namespace olb {
37
38namespace util {
39
41
48template<typename T>
50public:
52
58 ValueTracer(T u, T L, T epsilon);
60
63 ValueTracer(int deltaT, T epsilon);
65
69 ValueTracer(int deltaT, T epsilon, std::string name );
71 void resetScale(T u, T L);
73 void resetValues();
75 int getDeltaT() const;
77 void takeValue(T val, bool doPrint=false);
79 bool hasConverged() const;
81 bool convergenceCheck() const;
83 bool hasConvergedMinMax() const;
84 T computeAverage() const;
85 T computeStdDev(T average) const;
86 T getEpsilon() const;
87 void setEpsilon(T epsilon);
88private:
89 int _deltaT;
90 T _epsilon;
91 int _t;
92 bool _converged;
93 std::deque<T> _values;
94 mutable OstreamManager clout;
95};
96
98
102template<typename T>
104public:
106
110 BisectStepper(T _iniVal, T _step=0.);
112 T getVal(bool stable, bool doPrint=false);
114 bool hasConverged(T epsilon) const;
115private:
116 T iniVal, currentVal, lowerVal, upperVal;
117 T step;
118 enum {first, up, down, bisect} state;
119 mutable OstreamManager clout;
120};
121
123template<typename T>
124class Newton1D {
125
126protected:
132
133public:
134 Newton1D(AnalyticalF1D<T,T>& f, T yValue = T(), T eps = 1.e-8, int maxIterations = 100);
135
136 T solve(T startValue, bool print=false);
137};
138
140template<typename T>
142
143protected:
145
146public:
148
149 T integrate(T min, T max, int nSteps);
150};
151
157template<typename T>
159public:
160 CircularBuffer(int size);
162 void insert(T entry);
164 T average();
166 T& get(int pos);
168 int getSize();
169
170private:
171 int _size;
172 std::vector<T> _data;
173};
174
176
179template<typename T, typename S>
181
182private:
183 const std::function<T(int)> _smoothingFactorFunction;
184 S _EMA;
185 int _noOfEntries;
186
188
192public:
193 ExponentialMovingAverage(const std::function<T(int)> smoothingFactorFunction = [](int i) -> T {
194 return 2. / (1 + i); });
195
197 void takeValue(const T val);
198
200 void resetNoOfEntries();
201
202 bool operator() (T output[], const S x[]) override;
203};
204
205
207
216template<typename T, int P=1>
218
219protected:
223
228
229 std::conditional_t< (P==0), T, KahanSummator<T>> result;
230
231public:
232 TimeIntegrator(T lowerBound, T upperBound, T dt) :
233 _lowerBound(lowerBound),
234 _upperBound(upperBound),
235 _dt(dt),
236 _firstStep(_lowerBound > 0 ? _lowerBound / dt + 1.0 : _lowerBound / dt),
238 _lastStep(_upperBound > 0 ? _upperBound / dt : _upperBound / dt - 1.0),
240 {
242 "Time integration needs smaller time steps.\n");
243 if constexpr (P==0) {
244 result = T(0);
245 }
246 }
247
248 void takeValue(int iT, T value)
249 {
250 if (iT >= _firstStep && iT <= _lastStep) {
251 if constexpr (P == 0) {
252 result = util::max(result, util::abs(value));
253 }
254 else {
255 const T time(iT * _dt);
256 T weight(_dt);
257
258 /*
259 // linear extrapolation at interval bounds
260 // was deactivated due to instability
261 if (iT == _firstStep) {
262 T offset (_lowerBound - time);
263 weight = 0.5 * _dt - 0.5 * (2.0 - offset / _dt) * offset;
264 //weight = 0.5 * _dt + time - _lowerBound;
265 }
266 else if (iT == _secondStep) {
267 T offset (_lowerBound - (time - _dt));
268 weight = _dt - 0.5 * offset * offset / _dt;
269 }
270 else if (iT == _prelastStep) {
271 T offset (_upperBound - (time + _dt));
272 weight = _dt - 0.5 * offset * offset / _dt;
273 }
274 else if (iT == _lastStep) {
275 T offset (_upperBound - time);
276 weight = 0.5 * _dt + 0.5 * (2.0 + offset / _dt) * offset;
277 //weight = 0.5 * _dt + offset;
278 }
279 */
280
281 // constant extrapolation at interval bounds
282 if (iT == _firstStep) {
283 weight = 0.5 * _dt + time - _lowerBound;
284 }
285 else if (iT == _lastStep) {
286 T offset (_upperBound - time);
287 weight = 0.5 * _dt + offset;
288 }
289
290 result.add(weight * util::pow(value, P));
291 }
292 }
293 }
294
295 void reset(T lowerBound, T upperBound, T dt)
296 {
297 _lowerBound = lowerBound;
298 _upperBound = upperBound;
299 _dt = dt;
300 _firstStep = _lowerBound / dt + 1.0;
302 _lastStep = _upperBound / dt;
304 if constexpr (P==0) {
305 result = T(0);
306 } else {
307 result.initialize();
308 }
309 if (_secondStep >= _prelastStep) {
310 throw std::runtime_error("Time integration needs smaller time steps.\n");
311 }
312 }
313
315 {
316 using BT = BaseType<T>;
317 if constexpr (P==0) {
318 return result;
319 } else {
320 return util::pow(result.getSum(), BT(1) / P);
321 }
322 __builtin_unreachable();
323 }
324};
325
327template<typename T, int numComponents, int P=1>
329
330protected:
331 std::array<std::unique_ptr<TimeIntegrator<T,P>>,numComponents> integrators;
332
333public:
334 TimeIntegratorsArray(T lowerBound, T upperBound, T dt) {
335 for (int i = 0; i < numComponents; ++i) {
336 integrators[i] = std::make_unique<TimeIntegrator<T,P>>(lowerBound,upperBound,dt);
337 }
338 }
339
341 void takeValues(int iT, std::array<T,numComponents> values) {
342 for (int i = 0; i < numComponents; ++i) {
343 integrators[i]->takeValue(iT,values[i]);
344 }
345 }
346
347 void reset(T lowerBound, T upperBound, T dt) {
348 for (auto& integrator : integrators) {
349 integrator->reset(lowerBound,upperBound,dt);
350 }
351 }
352
353 std::array<T,numComponents> getResult()
354 {
355 std::array<T,numComponents> results;
356 std::transform(integrators.begin(),integrators.end(),results.begin(),
357 [](auto& integrator){
358 return integrator->getResult();
359 });
360 return results;
361 }
362};
363
364} // namespace util
365
366} // namespace olb
367
368#endif
Some arithmetic helper functions.
Class for computing the derivative of a given 1D functor with a finite difference.
Definition derivativeF.h:43
class for marking output with some text
Propose successive test values of a scalar (e.g. Re) to check stability of a system.
BisectStepper(T _iniVal, T _step=0.)
The only constructor.
T getVal(bool stable, bool doPrint=false)
Get new value, and indicate if the previous value yielded a stable system or not.
bool hasConverged(T epsilon) const
Test for convergence.
Simple circular buffer to compute average and other quantities over pre-defined temporal windows.
void insert(T entry)
insert a new entry ed eventually erases the oldest one
T & get(int pos)
get reference to the last entry for pos=0, the second-to-last for pos=1, and so on
T average()
average over all the entries
int getSize()
return size of the buffer
Exponential moving average.
void resetNoOfEntries()
reset number of entries
void takeValue(const T val)
compute next EMA with given value
bool operator()(T output[], const S x[]) override
has to be implemented for 'every' derived class
ExponentialMovingAverage(const std::function< T(int)> smoothingFactorFunction=[](int i) -> T { return 2./(1+i);})
(constructor)
Accurate summation of floating point numbers with the Kahan algorithm Reduces round-off effects which...
Definition calc.h:58
1D Newton simple scheme
T solve(T startValue, bool print=false)
AnalyticalF1D< T, T > & _f
Newton1D(AnalyticalF1D< T, T > &f, T yValue=T(), T eps=1.e-8, int maxIterations=100)
AnalyticalDerivativeFD1D< T > _df
Integration with the trapezoid rule.
TimeIntegrator(T lowerBound, T upperBound, T dt)
std::conditional_t<(P==0), T, KahanSummator< T > > result
void takeValue(int iT, T value)
void reset(T lowerBound, T upperBound, T dt)
Helper class that manages an array of time integrators.
void takeValues(int iT, std::array< T, numComponents > values)
Values can be passed in {val1, val2, ...} notation.
std::array< T, numComponents > getResult()
void reset(T lowerBound, T upperBound, T dt)
std::array< std::unique_ptr< TimeIntegrator< T, P > >, numComponents > integrators
TimeIntegratorsArray(T lowerBound, T upperBound, T dt)
AnalyticalF1D< T, T > & _f
TrapezRuleInt1D(AnalyticalF1D< T, T > &f)
T integrate(T min, T max, int nSteps)
Check time-convergence of a scalar.
bool hasConverged() const
Test for convergence, with respect to stdDev.
int getDeltaT() const
Get characteristic time scale.
bool convergenceCheck() const
Test for convergence, with respect to difference between min and max value;.
void resetScale(T u, T L)
Change values of u and L to update characteristic scales of the system.
void setEpsilon(T epsilon)
bool hasConvergedMinMax() const
Test for convergence, with respect to difference between min and max value;.
ValueTracer(T u, T L, T epsilon)
Ctor.
void takeValue(T val, bool doPrint=false)
Feed the object with a new measured scalar.
T computeStdDev(T average) const
void resetValues()
reinitializes the values
ADf< T, DIM > abs(const ADf< T, DIM > &a)
Definition aDiff.h:1019
cpu::simd::Pack< T > min(cpu::simd::Pack< T > rhs, cpu::simd::Pack< T > lhs)
Definition pack.h:124
T average(const Vector< T, Size > &a)
computes the average of all elements
cpu::simd::Pack< T > max(cpu::simd::Pack< T > rhs, cpu::simd::Pack< T > lhs)
Definition pack.h:130
cpu::simd::Pack< T > pow(cpu::simd::Pack< T > base, cpu::simd::Pack< T > exp)
Definition pack.h:112
void print(U data, const std::string &name="", OstreamManager clout=OstreamManager(std::cout,"print"), const char delimiter=',')
Top level namespace for all of OpenLB.
typename util::BaseTypeHelper< T >::type BaseType
Definition baseType.h:59
#define OLB_ASSERT(COND, MESSAGE)
Definition olbDebug.h:45