Loading...
Searching...
No Matches
MinMax.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) 2019-2025 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
26Class
27 Foam::MinMax
28
29Description
30 A min/max value pair with additional methods.
31 In addition to conveniently storing values, it can be used for logic
32 operations or to modify data. A few global functions and functors are
33 also provided.
34
35 Examples of use.
36
37 Determine min/max limits from a List of values:
38 \verbatim
39 List<scalar> values = ...;
40
41 // on construction
42 MinMax<scalar> range(values);
43
44 range.reset();
45
46 range += val;
47
48 // global minMax() function
49 Info<< minMax(values) << nl;
50 \endverbatim
51
52 General comparison operations
53 \verbatim
54 scalar val;
55 if (val < range) ... value is below range min
56 if (range.contains(val)) ... value within range
57 if (range.compare(val) > 0) ... value is above range max
58 if (range(val)) ... value within range - as predicate
59 \endverbatim
60
61 Since the range has a predicate form, it can be used as a filter method.
62 For example,
63 \verbatim
64 Info<< "values in range: " << subsetList(values, range) << nl;
65
66 boolList mask = ListOps::create<bool>(values, range);
67 Info<< "values values in range " << mask << nl;
68 \endverbatim
69
70 One advantage offered by MinMax is to clamp or limit values
71 to a particular range. For example,
72 \verbatim
73 scalarMinMax range(lower, upper);
74
75 scalar val;
76 val = range.clamp(val) .. return clamped values
77
78 // vs.
79 val = min(max(value, lower), upper)
80 \endverbatim
81
82\*---------------------------------------------------------------------------*/
83
84#ifndef Foam_MinMax_H
85#define Foam_MinMax_H
86
87#include "scalar.H"
88#include "zero.H"
89#include "Pair.H"
90#include "Tuple2.H"
91#include "VectorSpace.H"
92#include <type_traits>
93
94// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
95
96namespace Foam
98
99// Forward Declarations
100template<class T> class MinMax;
101
102// Common min/max types
106
107/*---------------------------------------------------------------------------*\
108 Class MinMax Declaration
109\*---------------------------------------------------------------------------*/
110
111template<class T>
112class MinMax
113{
114 // Private Data
115
116 //- Min value
117 T min_;
118
119 //- Max value
120 T max_;
121
122public:
123
124 // Typedefs
125
126 //- The value type the MinMax represents
127 typedef T value_type;
128
129 //- Component type
130 typedef typename pTraits<T>::cmptType cmptType;
132
133 // Constructors
134
135 //- Default construct: an inverted range
136 inline MinMax();
137
138 //- Construct from min/max limits
139 inline MinMax(const T& minVal, const T& maxVal);
140
141 //- Implicit construct from min/max limits
142 inline MinMax(const std::pair<T,T>& range);
143
144 //- Implicit construct from min/max limits
145 inline MinMax(const Pair<T>& range);
146
147 //- Implicit construct from min/max limits
148 inline MinMax(const Tuple2<T,T>& range);
150 //- Construct with a single zero value
151 inline explicit MinMax(Foam::zero);
152
153 //- Implicit construct from zero_one as 0-1 range (pTraits zero, one)
155
156 //- Construct with a single initial value
157 inline explicit MinMax(const T& val);
158
159 //- Construct from list of values
160 inline explicit MinMax(const UList<T>& vals);
161
162
163 // Static Member Functions
165 //- A semi-infinite range from minVal to the type max
166 inline static MinMax<T> ge(const T& minVal);
167
168 //- A semi-infinite range from type min to maxVal
169 inline static MinMax<T> le(const T& maxVal);
170
171 //- A 0-1 range corresponding to the pTraits zero, one
172 inline static MinMax<T> zero_one();
173
175 // Member Functions
176
177 // Access
178
179 //- The min value
180 const T& min() const noexcept { return min_; }
181
182 //- The min value
183 T& min() noexcept { return min_; }
184
185 //- The max value
186 const T& max() const noexcept { return max_; }
188 //- The max value
189 T& max() noexcept { return max_; }
190
191 //- The min/max average value
192 inline T centre() const;
193
194 //- The min to max span. Zero for invalid range.
195 inline T span() const;
196
197 //- The magnitude of the min to max span. Zero for invalid range.
198 inline scalar mag() const;
199
200 //- Range is empty if it is inverted
201 inline bool empty() const;
202
203 //- Range is non-inverted
204 inline bool good() const;
205
206 //- Reset to an inverted (invalid) range
207 inline void reset();
208
209 //- Reset min/max to be identical to the specified value
210 inline void reset(const T& val);
211
212 //- Reset min/max to specified values
213 inline void reset(const T& minVal, const T& maxVal);
214
215 //- Same as reset() - reset to an inverted (invalid) range
216 void clear() { reset(); }
218
219 // Testing / Query
220
221 //- Test if the ranges intersect (exclusive check)
222 inline bool intersects(const MinMax<T>& b) const;
223
224 //- Test if ranges overlap/touch (inclusive check)
225 inline bool overlaps(const MinMax<T>& b) const;
226
227 //- Compares the min/max range with the specified value.
228 // \return
229 // - 0: value is within the range, or range is invalid
230 // - -1: range (max) is less than the value
231 // - +1: range (min) is greater than value
232 inline int compare(const T& val) const;
233
234 //- True if the value is within the range (inclusive check)
235 inline bool contains(const T& val) const;
236
237 //- Return value clamped component-wise.
238 // If the range is invalid, just returns the value.
239 inline T clamp(const T& val) const;
240
241
242 // Manipulate
243
244 //- Extend the range to include the other min/max range
245 inline MinMax<T>& add(const MinMax& other);
246
247 //- Include the value into the range
248 inline MinMax<T>& add(const T& val);
249
250 //- Include the values into the range
251 inline MinMax<T>& add(const UList<T>& vals);
253 //- Include two or more values into the range.
254 // All values must be similar types
255 template<class... Args>
256 inline MinMax<T>& add(const T& val0, const T& val1, Args&&... values);
258
259 // Member Operators
260
261 //- Identical to contains(), for use as a predicate.
262 inline bool operator()(const T& val) const;
263
264 //- Restrict min/max range to union with other range
265 inline MinMax<T>& operator&=(const MinMax<T>& b);
266
267 //- Extend min/max range to include other range
268 // Can be used in a reduction operation.
269 inline MinMax<T>& operator+=(const MinMax<T>& b);
270
271 //- Extend min/max range to include value
272 inline MinMax<T>& operator+=(const T& val);
273
274 //- Extend min/max range to include all values
275 inline MinMax<T>& operator+=(const UList<T>& vals);
276
277 //- Multiply range by scalar factor
278 inline MinMax<T>& operator*=(scalar s);
279
280 //- Divide range by scalar factor
281 inline MinMax<T>& operator/=(scalar s);
282
283
284 // Housekeeping
285
286 //- Range is non-inverted. Same as good (2022-10)
287 bool valid() const { return good(); }
288
289 //- Old method name. Same as clamp (2023-01)
290 T clip(const T& val) const { return this->clamp(val); }
291};
292
293
294// * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
296//- Declare MinMax as non-contiguous (similar to Tuple2).
297// Output remains separate (even in binary) and, since the defined
298// \c operator+ is somewhat non-standard, also avoid false matching with
299// any MPI intrinsic operation.
300template<class T>
301struct is_contiguous<MinMax<T>> : std::false_type {};
303
304// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
305
306//- Min/max range as a string
307template<class T>
308word name(const MinMax<T>& range)
309{
310 return '(' + Foam::name(range.min()) + ',' + Foam::name(range.max()) + ')';
311}
312
313// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
314
315//- Read min/max range as (min max) tuple from Istream
316template<class T>
318{
319 is.readBegin("min.max");
320 is >> range.min() >> range.max();
321 is.readEnd("min.max");
322
324 return is;
325}
326
327//- Write min/max range as (min max) tuple to Ostream
328template<class T>
329inline Ostream& operator<<(Ostream& os, const MinMax<T>& range)
330{
332 << range.min() << token::SPACE << range.max()
334 return os;
335}
337
338// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
339
340} // End namespace Foam
342// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
343
344#include "MinMaxI.H"
345
346// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
347
348#endif
349
350// Global Functions and Operators
351#include "MinMaxOps.H"
352
354// ************************************************************************* //
scalar range
Global functions and operators related to the MinMax class. Included by MinMax.H.
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition IOstream.C:45
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
bool readEnd(const char *funcName)
End read of data chunk, ends with ')'.
Definition Istream.C:152
bool readBegin(const char *funcName)
Begin read of data chunk, starts with '('.
Definition Istream.C:134
A min/max value pair with additional methods. In addition to conveniently storing values,...
Definition MinMax.H:106
MinMax< T > & add(const UList< T > &vals)
Include the values into the range.
Definition MinMaxI.H:260
MinMax(const UList< T > &vals)
Construct from list of values.
Definition MinMaxI.H:112
T & min() noexcept
The min value.
Definition MinMax.H:212
bool operator()(const T &val) const
Identical to contains(), for use as a predicate.
Definition MinMaxI.H:292
MinMax< T > & operator&=(const MinMax< T > &b)
Restrict min/max range to union with other range.
Definition MinMaxI.H:330
bool valid() const
Range is non-inverted. Same as good (2022-10).
Definition MinMax.H:376
MinMax(const Pair< T > &range)
Implicit construct from min/max limits.
Definition MinMaxI.H:72
void reset(const T &minVal, const T &maxVal)
Reset min/max to specified values.
Definition MinMaxI.H:177
T clip(const T &val) const
Old method name. Same as clamp (2023-01).
Definition MinMax.H:381
MinMax(Foam::zero_one)
Implicit construct from zero_one as 0-1 range (pTraits zero, one).
Definition MinMaxI.H:96
bool contains(const T &val) const
True if the value is within the range (inclusive check).
Definition MinMaxI.H:220
MinMax< T > & add(const T &val0, const T &val1, Args &&... values)
Include two or more values into the range.
bool empty() const
Range is empty if it is inverted.
Definition MinMaxI.H:145
MinMax(const Tuple2< T, T > &range)
Implicit construct from min/max limits.
Definition MinMaxI.H:80
T clamp(const T &val) const
Return value clamped component-wise.
Definition MinMaxI.H:227
MinMax< T > & add(const T &val)
Include the value into the range.
Definition MinMaxI.H:250
MinMax< T > & operator/=(scalar s)
Divide range by scalar factor.
Definition MinMaxI.H:371
const T & max() const noexcept
The max value.
Definition MinMax.H:217
MinMax< T > & operator*=(scalar s)
Multiply range by scalar factor.
Definition MinMaxI.H:362
T & max() noexcept
The max value.
Definition MinMax.H:222
MinMax(const std::pair< T, T > &range)
Implicit construct from min/max limits.
Definition MinMaxI.H:64
scalar mag() const
The magnitude of the min to max span. Zero for invalid range.
Definition MinMaxI.H:138
static MinMax< T > le(const T &maxVal)
A semi-infinite range from type min to maxVal.
Definition MinMaxI.H:31
const T & min() const noexcept
The min value.
Definition MinMax.H:207
bool overlaps(const MinMax< T > &b) const
Test if ranges overlap/touch (inclusive check).
Definition MinMaxI.H:193
static MinMax< T > ge(const T &minVal)
A semi-infinite range from minVal to the type max.
Definition MinMaxI.H:24
T centre() const
The min/max average value.
Definition MinMaxI.H:123
bool good() const
Range is non-inverted.
Definition MinMaxI.H:153
void clear()
Same as reset() - reset to an inverted (invalid) range.
Definition MinMax.H:267
MinMax< T > & operator+=(const UList< T > &vals)
Extend min/max range to include all values.
Definition MinMaxI.H:355
void reset()
Reset to an inverted (invalid) range.
Definition MinMaxI.H:160
MinMax(const T &minVal, const T &maxVal)
Construct from min/max limits.
Definition MinMaxI.H:56
pTraits< label >::cmptType cmptType
Definition MinMax.H:131
MinMax< T > & operator+=(const MinMax< T > &b)
Extend min/max range to include other range.
Definition MinMaxI.H:341
T span() const
The min to max span. Zero for invalid range.
Definition MinMaxI.H:131
int compare(const T &val) const
Compares the min/max range with the specified value.
Definition MinMaxI.H:201
MinMax()
Default construct: an inverted range.
Definition MinMaxI.H:47
bool intersects(const MinMax< T > &b) const
Test if the ranges intersect (exclusive check).
Definition MinMaxI.H:185
MinMax(Foam::zero)
Construct with a single zero value.
Definition MinMaxI.H:88
MinMax< T > & operator+=(const T &val)
Extend min/max range to include value.
Definition MinMaxI.H:348
MinMax< T > & add(const MinMax &other)
Extend the range to include the other min/max range.
Definition MinMaxI.H:240
MinMax(const T &val)
Construct with a single initial value.
Definition MinMaxI.H:104
static MinMax< T > zero_one()
A 0-1 range corresponding to the pTraits zero, one.
Definition MinMaxI.H:38
void reset(const T &val)
Reset min/max to be identical to the specified value.
Definition MinMaxI.H:169
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
An ordered pair of two objects of type <T> with first() and second() elements.
Definition Pair.H:66
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition Tuple2.H:51
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
A traits class, which is primarily used for primitives and vector-space.
Definition pTraits.H:64
@ BEGIN_LIST
Begin list [isseparator].
Definition token.H:174
@ END_LIST
End list [isseparator].
Definition token.H:175
@ SPACE
Space [isspace].
Definition token.H:144
A class for handling words, derived from Foam::string.
Definition word.H:66
Represents 0/1 range or concept. Used for tagged dispatch or clamping.
Definition pTraits.H:51
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
const volScalarField & T
OBJstream os(runTime.globalPath()/outputName)
auto & name
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
#define FUNCTION_NAME
Namespace for OpenFOAM.
MinMax< label > labelMinMax
A label min/max range.
Definition MinMax.H:96
MinMax< scalar > scalarMinMax
A scalar min/max range.
Definition MinMax.H:97
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
Istream & operator>>(Istream &, directionInfo &)
const direction noexcept
Definition scalarImpl.H:265
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
volScalarField & b
A template class to specify that a data type can be considered as being contiguous in memory.
Definition contiguous.H:70