OpenLB 1.7
Loading...
Searching...
No Matches
calc.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2021 Julius Jessberger
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
28#ifndef CALC_H
29#define CALC_H
30
31#include "omath.h"
32
33namespace olb {
34
35namespace util {
36
38template<typename T, typename S>
39inline auto fmod_pos(T a, S b) {
40 const auto res = util::fmod(a, b);
41 if (res < 0) {
42 return (res + b);
43 } else {
44 return res;
45 }
46}
47
48
56template<typename T>
58{
59private:
60 T sum {0};
61 T c {0}; // tracks the cancellation errors
62
63public:
64 void add(T summand) {
65 const T y {summand - c};
66 const T t {sum + y};
67 c = (t - sum) - y;
68 sum = t;
69 }
70
71 T getSum() const {
72 return sum;
73 }
74
75 void initialize(T value = 0.) {
76 sum = value;
77 c = T(0);
78 }
79};
80
81} // namespace util
82
83} // namespace olb
84
85#endif
Accurate summation of floating point numbers with the Kahan algorithm Reduces round-off effects which...
Definition calc.h:58
void initialize(T value=0.)
Definition calc.h:75
void add(T summand)
Definition calc.h:64
ADf< T, DIM > fmod(const ADf< T, DIM > &a, const ADf< T, DIM > &b)
Definition aDiff.h:703
auto fmod_pos(T a, S b)
Variant of fmod (floating point modulo) that always returns positive values.
Definition calc.h:39
Top level namespace for all of OpenLB.