suanPan
🧮 An Open Source, Parallel and Heterogeneous Finite Element Analysis Framework
Loading...
Searching...
No Matches
BalloonUtil.h
Go to the documentation of this file.
1/*******************************************************************************
2 * Copyright (C) 2017-2026 Theodore Chang
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
17
18#ifndef BALLOONUTIL_H
19#define BALLOONUTIL_H
20
21#include <Material/Material.h>
22#include <numeric> // std::accumulate
23
25 std::vector<double> buffer;
26 std::size_t head;
27
28 [[nodiscard]] auto max() const { return *std::ranges::max_element(buffer); }
29 [[nodiscard]] auto min() const { return *std::ranges::min_element(buffer); }
30 [[nodiscard]] auto mean() const { return std::accumulate(buffer.cbegin(), buffer.cend(), 0.) / static_cast<double>(buffer.size()); }
31
32public:
33 enum class Type : std::uint8_t {
34 MINIMUM,
35 MAXIMUM,
36 MEAN
37 };
38
39 explicit BalloonBuffer(const std::size_t size)
40 : buffer(std::max(std::size_t{1}, size), 0.)
41 , head(0) {}
42
43 auto& enqueue(const double value) {
44 buffer[head] = value;
45 head = (head + 1) % buffer.size();
46 return *this;
47 }
48
49 auto zeros() {
50 std::ranges::fill(buffer, 0.);
51 head = 0;
52 }
53
54 [[nodiscard]] auto operator()(const Type memory_type) const {
55 switch(memory_type) {
56 case Type::MINIMUM:
57 return min();
58 case Type::MAXIMUM:
59 return max();
60 case Type::MEAN:
61 default:
62 return mean();
63 }
64 }
65};
66
68 const double initial, linear, saturation, rate;
69
70public:
71 BalloonBound(const double I, const double K, const double S, const double M)
72 : initial(I)
73 , linear(K)
74 , saturation(S)
75 , rate(M) {}
76
77 [[nodiscard]] std::pair<double, double> operator()(const double q, const bool check_positive) const {
78 const auto exp_term = saturation * std::exp(-rate * q);
79 const auto y = initial + saturation + linear * q - exp_term;
80 return y < 0. && check_positive ? std::make_pair(0., 0.) : std::make_pair(y, linear + rate * exp_term);
81 }
82};
83
85 const double bound, rate;
86
87public:
88 BalloonSaturation(const double B, const double R)
89 : bound(B)
90 , rate(R) {}
91
92 [[nodiscard]] double a() const { return (rate > 0. ? b() : 1.) * bound; }
93 [[nodiscard]] double b() const { return rate; }
94};
95
97 static constexpr double z_bound = 1E-15;
98 inline static const double rate_bound = -std::log(z_bound);
99
100protected:
101 static pod2 yield_ratio(const double z) {
102 if(z < z_bound) return {rate_bound, 0.};
103
104 return {-log(z), -1. / z};
105 }
106};
107
108#endif
Definition BalloonUtil.h:96
static pod2 yield_ratio(const double z)
Definition BalloonUtil.h:101
Definition BalloonUtil.h:67
BalloonBound(const double I, const double K, const double S, const double M)
Definition BalloonUtil.h:71
std::pair< double, double > operator()(const double q, const bool check_positive) const
Definition BalloonUtil.h:77
Definition BalloonUtil.h:24
auto & enqueue(const double value)
Definition BalloonUtil.h:43
auto operator()(const Type memory_type) const
Definition BalloonUtil.h:54
Type
Definition BalloonUtil.h:33
auto zeros()
Definition BalloonUtil.h:49
BalloonBuffer(const std::size_t size)
Definition BalloonUtil.h:39
Definition BalloonUtil.h:84
BalloonSaturation(const double B, const double R)
Definition BalloonUtil.h:88
double a() const
Definition BalloonUtil.h:92
double b() const
Definition BalloonUtil.h:93
std::array< double, 2 > pod2
Definition Material.h:34