Loading...
Searching...
No Matches
scalarRangeI.H
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2018-2023 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26\*---------------------------------------------------------------------------*/
27
28// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29
30inline constexpr Foam::scalarRange::scalarRange
31(
32 const scalarRange::rangeTypes type,
33 const scalar minVal,
34 const scalar maxVal
37 min_(minVal),
38 max_(maxVal),
39 type_(type)
40{}
41
44:
45 scalarRange(rangeTypes::NONE, GREAT, -GREAT)
46{}
47
49inline constexpr Foam::scalarRange::scalarRange(const scalar val) noexcept
50:
51 scalarRange(rangeTypes::EQ, val, val)
52{}
53
54
56(
57 const scalar minVal,
58 const scalar maxVal
60:
61 scalarRange(rangeTypes::GE_LE, minVal, maxVal)
62{
63 if (maxVal < minVal)
64 {
65 reset(); // Inverted - explicitly mark as such
66 }
67 else if (equal(minVal, maxVal))
68 {
69 type_ = rangeTypes::EQ;
70 }
71}
72
73
74inline constexpr Foam::scalarRange
75Foam::scalarRange::ge(const scalar minVal) noexcept
76{
77 return scalarRange(rangeTypes::GE, minVal, GREAT);
78}
79
80
81inline constexpr Foam::scalarRange
82Foam::scalarRange::gt(const scalar minVal) noexcept
83{
84 return scalarRange(rangeTypes::GT, minVal, GREAT);
85}
86
87
88inline constexpr Foam::scalarRange
90{
91 return scalarRange(rangeTypes::GE, 0, GREAT);
92}
93
94
95inline constexpr Foam::scalarRange
97{
98 return scalarRange(rangeTypes::GT, 0, GREAT);
99}
100
101
102inline constexpr Foam::scalarRange
103Foam::scalarRange::le(const scalar maxVal) noexcept
104{
105 return scalarRange(rangeTypes::LE, -GREAT, maxVal);
106}
107
108inline constexpr Foam::scalarRange
109Foam::scalarRange::lt(const scalar maxVal) noexcept
110{
111 return scalarRange(rangeTypes::LT, -GREAT, maxVal);
112}
113
114
115inline constexpr Foam::scalarRange
118 return scalarRange(rangeTypes::GE_LE, 0, 1);
119}
120
121
122// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
123
126 min_ = GREAT;
127 max_ = -GREAT;
128 type_ = rangeTypes::NONE;
129}
130
132inline bool Foam::scalarRange::empty() const noexcept
133{
134 return (type_ == rangeTypes::NONE);
135}
136
138inline bool Foam::scalarRange::good() const noexcept
139{
140 return (type_ != rangeTypes::NONE);
141}
142
144inline bool Foam::scalarRange::single() const noexcept
145{
146 return (type_ == rangeTypes::EQ);
147}
148
149
150inline Foam::scalar Foam::scalarRange::value() const
151{
152 switch (type_)
153 {
154 case rangeTypes::EQ: // For equals, min and max are identical
155 case rangeTypes::GE:
156 case rangeTypes::GT:
157 return min_;
158
159 case rangeTypes::LE:
160 case rangeTypes::LT:
161 return max_;
162
163 case rangeTypes::GE_LE:
164 // Multiply before adding to avoid possible overflow
165 return (0.5 * min_) + (0.5 * max_);
167 default: // NONE or ALWAYS. Zero is reasonable dummy value.
168 return 0;
169 }
170}
171
172
173inline bool Foam::scalarRange::contains(const scalar val) const
174{
175 switch (type_)
176 {
177 case rangeTypes::EQ: return equal(val, min_);
178 case rangeTypes::GE: return (val >= min_);
179 case rangeTypes::GT: return (val > min_);
180 case rangeTypes::LE: return (val <= max_);
181 case rangeTypes::LT: return (val < max_);
182 case rangeTypes::GE_LE: return (val >= min_ && val <= max_);
183 case rangeTypes::ALWAYS: return true;
184 default: return false;
186}
187
188
189// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
190
191inline constexpr bool
192Foam::scalarRange::operator==(const scalarRange& rhs) const noexcept
193{
194 return (type_ == rhs.type_ && min_ == rhs.min_ && max_ == rhs.max_);
195}
196
197
198inline constexpr bool
199Foam::scalarRange::operator!=(const scalarRange& rhs) const noexcept
200{
201 return !(*this == rhs);
202}
203
204
205// ************************************************************************* //
Scalar bounds to be used as a unary predicate.
Definition scalarRange.H:65
bool single() const noexcept
True if the range bounds represent a single value.
void reset() noexcept
Reset to an empty (inverse, NONE) range.
static constexpr scalarRange ge0() noexcept
A greater-equals zero bound.
scalar value() const
A representative (average) value for the range.
static constexpr scalarRange le(const scalar maxVal) noexcept
A less-equals bound.
bool empty() const noexcept
True if range is empty (eg, inverted, NONE).
bool good() const noexcept
True if range is non-empty.
bool contains(const scalar val) const
True if the value is matched by the condition.
constexpr bool operator!=(const scalarRange &rhs) const noexcept
static constexpr scalarRange ge(const scalar minVal) noexcept
A greater-equals bound.
static constexpr scalarRange gt0() noexcept
A greater-than zero bound.
static constexpr scalarRange lt(const scalar maxVal) noexcept
A less-than bound.
constexpr scalarRange() noexcept
Construct an empty (inverse, NONE) range - never matches.
static constexpr scalarRange gt(const scalar minVal) noexcept
A greater-than bound.
constexpr bool operator==(const scalarRange &rhs) const noexcept
static constexpr scalarRange zero_one() noexcept
A greater-equals 0, less-equals 1 bound.
limits reset(1/(limits.max()+VSMALL), 1/(limits.min()+VSMALL))
bool equal(const T &a, const T &b)
Compare two values for equality.
Definition label.H:180
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
const direction noexcept
Definition scalarImpl.H:265