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-2020 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::testType type,
33 const scalar minVal,
34 const scalar maxVal
36:
37 min_(minVal),
38 max_(maxVal),
39 type_(type)
40{}
41
42
43inline constexpr Foam::scalarRange::scalarRange() noexcept
44:
45 scalarRange(scalarRange::NONE, GREAT, -GREAT)
46{}
47
48
49inline constexpr Foam::scalarRange::scalarRange(const scalar val) noexcept
50:
51 scalarRange(scalarRange::EQ, val, val)
52{}
53
54
56(
57 const scalar minVal,
58 const scalar maxVal
60:
61 scalarRange(scalarRange::GE_LE, minVal, maxVal)
62{
63 if (minVal > maxVal)
64 {
65 clear(); // Inverted - explicitly mark as such
66 }
67 else if (equal(minVal, maxVal))
68 {
69 type_ = scalarRange::EQ;
70 }
71}
72
73
74inline constexpr Foam::scalarRange
75Foam::scalarRange::ge(const scalar minVal) noexcept
76{
77 return scalarRange(scalarRange::GE, minVal, GREAT);
78}
79
80
81inline constexpr Foam::scalarRange
82Foam::scalarRange::gt(const scalar minVal) noexcept
83{
84 return scalarRange(scalarRange::GT, minVal, GREAT);
85}
86
87
88inline constexpr Foam::scalarRange
90{
91 return scalarRange(scalarRange::GE, 0, GREAT);
92}
93
94
95inline constexpr Foam::scalarRange
97{
98 return scalarRange(scalarRange::GT, 0, GREAT);
99}
100
101
102inline constexpr Foam::scalarRange
103Foam::scalarRange::le(const scalar maxVal) noexcept
104{
105 return scalarRange(scalarRange::LE, -GREAT, maxVal);
106}
107
108inline constexpr Foam::scalarRange
109Foam::scalarRange::lt(const scalar maxVal) noexcept
110{
111 return scalarRange(scalarRange::LT, -GREAT, maxVal);
112}
113
114
115inline constexpr Foam::scalarRange
117{
118 return scalarRange(scalarRange::GE_LE, 0, 1);
119}
120
121
122// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
123
125{
126 min_ = GREAT;
127 max_ = -GREAT;
128 type_ = scalarRange::NONE;
129}
130
131
133{
134 return type_ == scalarRange::NONE;
135}
136
137
139{
140 return bool(type_);
141}
142
143
145{
146 return type_ == scalarRange::EQ;
147}
148
149
150inline Foam::scalar Foam::scalarRange::min() const noexcept
151{
152 return min_;
153}
154
155
156inline Foam::scalar Foam::scalarRange::max() const noexcept
157{
158 return max_;
159}
160
161
162inline Foam::scalar Foam::scalarRange::value() const
163{
164 switch (type_)
165 {
166 case scalarRange::EQ: // For equals, min and max are identical
167 case scalarRange::GE:
168 case scalarRange::GT:
169 return min_;
170
171 case scalarRange::LE:
172 case scalarRange::LT:
173 return max_;
174
175 case scalarRange::GE_LE:
176 // Multiply before adding to avoid possible overflow
177 return (0.5 * min_) + (0.5 * max_);
178
179 default: // NONE or ALWAYS. Zero is reasonable dummy value.
180 return 0;
181 }
182}
183
184
185inline bool Foam::scalarRange::match(const scalar& val) const
186{
187 switch (type_)
188 {
189 case scalarRange::EQ: return equal(val, min_);
190 case scalarRange::GE: return (val >= min_);
191 case scalarRange::GT: return (val > min_);
192 case scalarRange::LE: return (val <= max_);
193 case scalarRange::LT: return (val < max_);
194 case scalarRange::GE_LE: return (val >= min_ && val <= max_);
195 case scalarRange::ALWAYS: return true;
196 default: return false;
197 }
198}
199
200
201// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
202
203inline bool Foam::scalarRange::operator()(const scalar& val) const
204{
205 return match(val);
206}
207
208
209inline constexpr bool
211(
212 const scalarRange& rhs
213) const noexcept
214{
215 return (type_ == rhs.type_ && min_ == rhs.min_ && max_ == rhs.max_);
216}
217
218
219inline constexpr bool
221(
222 const scalarRange& rhs
223) const noexcept
224{
225 return !(*this == rhs);
226}
227
228
229// ************************************************************************* //
Ostream & operator()() const
Output stream (master only).
Definition: ensightCaseI.H:74
friend Ostream & operator(Ostream &, const faMatrix< Type > &)
Scalar bounds to be used as a unary predicate.
Definition: scalarRange.H:69
void clear() noexcept
Reset to an empty (inverse, NONE) range.
Definition: scalarRangeI.H:124
bool single() const noexcept
True if the range bounds represent a single value.
Definition: scalarRangeI.H:144
static constexpr scalarRange ge0() noexcept
A greater-equals zero bound.
Definition: scalarRangeI.H:89
scalar value() const
A representative (average) value for the range.
Definition: scalarRangeI.H:162
static constexpr scalarRange le(const scalar maxVal) noexcept
A less-equals bound.
Definition: scalarRangeI.H:103
bool match(const scalar &val) const
True if the value matches the condition.
Definition: scalarRangeI.H:185
bool empty() const noexcept
True if range is empty (eg, inverted, NONE)
Definition: scalarRangeI.H:132
scalar min() const noexcept
The min value of the range.
Definition: scalarRangeI.H:150
static constexpr scalarRange ge(const scalar minVal) noexcept
A greater-equals bound.
Definition: scalarRangeI.H:75
bool valid() const noexcept
True if range is non-empty.
Definition: scalarRangeI.H:138
static constexpr scalarRange gt0() noexcept
A greater-than zero bound.
Definition: scalarRangeI.H:96
static constexpr scalarRange lt(const scalar maxVal) noexcept
A less-than bound.
Definition: scalarRangeI.H:109
scalar max() const noexcept
The max value of the range.
Definition: scalarRangeI.H:156
constexpr scalarRange() noexcept
Construct an empty (inverse, NONE) range - never matches.
Definition: scalarRangeI.H:43
static constexpr scalarRange gt(const scalar minVal) noexcept
A greater-than bound.
Definition: scalarRangeI.H:82
static constexpr scalarRange zero_one() noexcept
A greater-equals 0, less-equals 1 bound.
Definition: scalarRangeI.H:116
bool
Definition: EEqn.H:20
patchWriters clear()
bool equal(const T &s1, const T &s2)
Compare two values for equality.
Definition: doubleFloat.H:46
const direction noexcept
Definition: Scalar.H:223