Loading...
Searching...
No Matches
MinMaxOps.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
26InNamespace
27 Foam
28
29Description
30 Global functions and operators related to the MinMax class.
31 Included by MinMax.H
32
33\*---------------------------------------------------------------------------*/
34
35#ifndef Foam_MinMaxOps_H
36#define Foam_MinMaxOps_H
37
38#include "MinMax.H"
39#include "VectorSpace.H"
40#include <type_traits>
41
42// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43
44namespace Foam
45{
46
47// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
48
49//- The mag() function for min/max range
50template<class T>
51inline scalar mag(const MinMax<T>& range)
52{
53 return range.mag();
54}
55
56
57//- Return the value after clamping by the min/max limiter
58template<class T>
59inline T clip(const T& val, const MinMax<T>& range)
60{
61 return range.clamp(val);
62}
63
64
65//- Unary function for applying component-wise clamping
66template<class T>
67struct clampOp
68{
69 const T lower;
70 const T upper;
71
72 //- Construct from min/max limits. No validity checks
73 clampOp(const T& min, const T& max)
74 :
76 upper(max)
77 {}
78
79 //- Construct from min/max range. No validity checks
80 clampOp(const MinMax<T>& range)
81 :
82 lower(range.min()),
83 upper(range.max())
84 {}
85
86 //- Construct as 0-1 min/max range
88 :
90 {}
91
92 T operator()(const T& val) const
93 {
94 return min(max(val, lower), upper);
95 }
96};
97
102
103
111
112
113//- Extract the min/max range from a list of values.
114template<class T>
115inline MinMax<T> minMax(const UList<T>& vals)
116{
117 return MinMax<T>(vals);
118}
119
120
121//- Combine two values to create a min/max range. Order is unimportant.
122template<class T>
123inline MinMax<T> minMax(const T& x, const T& y)
124{
125 return MinMax<T>(x).add(y);
126}
127
128
129//- Combine two MinMax ranges (same as x + y)
130template<class T>
131inline MinMax<T> minMax(const MinMax<T>& x, const MinMax<T>& y)
132{
133 return MinMax<T>(x).add(y);
134}
135
136
137//- The magnitude of a single value.
138inline scalarMinMax minMaxMag(const scalar val)
139{
140 return scalarMinMax(Foam::mag(val));
141}
142
144//- The magnitude of a VectorSpace.
145template<class Form, class Cmpt, direction nCmpt>
147{
148 return scalarMinMax(Foam::mag(vs));
149}
150
151
152//- The min/max magnitudes from a list of values
153template<class T>
154inline scalarMinMax minMaxMag(const UList<T>& vals)
155{
156 scalarMinMax result;
157 for (const T& val : vals)
158 {
159 result.add(Foam::mag(val));
160 }
161
162 return result;
163}
164
165
166//- The min/max magnitudes from a min/max range
167template<class T>
168inline scalarMinMax minMaxMag(const MinMax<T>& range)
169{
170 return
171 (
173 );
174}
175
176
177//- Combine the magitude of two values (similar or dissimilar types)
178//- to create a min/max range. Order is unimportant.
179template<class T1, class T2>
180inline scalarMinMax minMaxMag(const T1& x, const T2& y)
181{
182 return minMaxMag(x).add(Foam::mag(y));
183}
184
185
186//- Scalar combine two MinMax ranges (same or dissimilar types)
187template<class T1, class T2>
189{
190 return minMaxMag(x).add(Foam::mag(y.min()), Foam::mag(y.max()));
191}
192
193
194// Mark as unused (2025-02)
195// ~~~~~~~~~~~~~~~~~~~~~~~~
196//
197// //- Combine values and/or MinMax ranges
198// template<class T>
199// struct minMaxOp
200// {
201// MinMax<T> operator()(const T& x, const T& y) const
202// {
203// return MinMax<T>(x).add(y);
204// }
205//
206// MinMax<T> operator()(const MinMax<T>& x, const T& y) const
207// {
208// return MinMax<T>(x).add(y);
209// }
210//
211// MinMax<T> operator()(const T& x, const MinMax<T>& y) const
212// {
213// return MinMax<T>(y).add(x);
214// }
215// };
216//
217//
218// //- Combine assignment for MinMax range
219// template<class T>
220// struct minMaxEqOp
221// {
222// MinMax<T>& operator()(MinMax<T>& x, const T& y) const
223// {
224// return x.add(y);
225// }
226//
227// MinMax<T>& operator()(MinMax<T>& x, const UList<T>& y) const
228// {
229// return x.add(y);
230// }
231// };
232//
233//
234// //- Scalar combine the magitude of a value.
235// template<class T>
236// struct minMaxMagOp
237// {
238// scalarMinMax operator()(const scalarMinMax& x, const T& y) const
239// {
240// return minMaxMag(x).add(Foam::mag(y));
241// }
242//
243// template<class T1, class T2>
244// scalarMinMax operator()(const MinMax<T1>& x, const MinMax<T2>& y) const
245// {
246// return minMaxMag(x).add(Foam::mag(y.min()), Foam::mag(y.max()));
247// }
248// };
249//
250//
251// //- Combine assignment for MinMax range
252// template<class T>
253// struct minMaxMagEqOp
254// {
255// scalarMinMax& operator()(scalarMinMax& x, const T& y) const
256// {
257// x = minMaxMag(x);
258// return x.add(Foam::mag(y));
259// }
260//
261// scalarMinMax& operator()(scalarMinMax& x, const MinMax<T>& y) const
262// {
263// x = minMaxMag(x);
264//
265// return x.add(Foam::mag(y.min()), Foam::mag(y.max()));
266// }
267//
268// scalarMinMax& operator()(scalarMinMax& x, const UList<T>& y) const
269// {
270// x = minMaxMag(x);
271//
272// for (const T& val : y)
273// {
274// x.add(Foam::mag(val));
275// }
276//
277// return x;
278// }
279// };
280
281
282// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
283
284//- Combine two ranges
285template<class T>
286inline MinMax<T> operator+(const MinMax<T>& x, const MinMax<T>& y)
287{
288 return MinMax<T>(x).add(y);
289}
290
291
292//- Multiply range by scalar factor
293template<class T>
294inline MinMax<T> operator*(const MinMax<T>& x, scalar s)
295{
296 return MinMax<T>(x.min()*s, x.max()*s);
297}
298
299
300//- Divide range by scalar factor
301template<class T>
302inline MinMax<T> operator/(const MinMax<T>& x, scalar s)
303{
304 return MinMax<T>(x.min()/s, x.max()/s);
305}
306
307
308// Comparison
309
310template<class T, class U>
311inline std::enable_if_t<std::is_convertible_v<U, T>, bool>
312operator<(const MinMax<T>& range, const U& val)
313{
314 return (range.compare(val) < 0);
315}
316
317template<class T, class U>
318inline std::enable_if_t<std::is_convertible_v<U, T>, bool>
319operator<=(const MinMax<T>& range, const U& val)
320{
321 return (range.compare(val) <= 0);
323
324template<class T, class U>
325inline std::enable_if_t<std::is_convertible_v<U, T>, bool>
326operator>(const MinMax<T>& range, const U& val)
327{
328 return (range.compare(val) > 0);
329}
330
331template<class T, class U>
332inline std::enable_if_t<std::is_convertible_v<U, T>, bool>
333operator>=(const MinMax<T>& range, const U& val)
334{
335 return (range.compare(val) >= 0);
336}
337
338
339template<class T, class U>
340inline std::enable_if_t<std::is_convertible_v<U, T>, bool>
341operator<(const U& val, const MinMax<T>& range)
342{
343 return (range.compare(val) > 0);
344}
345
346template<class T, class U>
347inline std::enable_if_t<std::is_convertible_v<U, T>, bool>
348operator<=(const U& val, const MinMax<T>& range)
349{
350 return (range.compare(val) >= 0);
351}
352
353template<class T, class U>
354inline std::enable_if_t<std::is_convertible_v<U, T>, bool>
355operator>(const U& val, const MinMax<T>& range)
357 return (range.compare(val) < 0);
358}
359
360template<class T, class U>
361inline std::enable_if_t<std::is_convertible_v<U, T>, bool>
362operator>=(const U& val, const MinMax<T>& range)
364 return (range.compare(val) <= 0);
365}
366
367
368// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
369
370} // End namespace Foam
371
372#endif
373
374// ************************************************************************* //
scalar range
scalar y
A min/max value pair with additional methods. In addition to conveniently storing values,...
Definition MinMax.H:106
MinMax< T > & add(const MinMax &other)
Extend the range to include the other min/max range.
Definition MinMaxI.H:240
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
Templated vector space.
Definition VectorSpace.H:75
Represents 0/1 range or concept. Used for tagged dispatch or clamping.
Definition pTraits.H:51
U
Definition pEqn.H:72
const volScalarField & T
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))
Namespace for OpenFOAM.
dimensioned< scalarMinMax > minMaxMag(const DimensionedField< Type, GeoMesh > &f1, const label comm)
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:40
T clip(const T &val, const MinMax< T > &range)
Return the value after clamping by the min/max limiter.
Definition MinMaxOps.H:57
bool operator<=(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A same or older than B.
tmp< faMatrix< Type > > operator+(const faMatrix< Type > &, const faMatrix< Type > &)
bool operator>=(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A same or newer than B.
MinMax< scalar > scalarMinMax
A scalar min/max range.
Definition MinMax.H:97
tmp< faMatrix< Type > > operator*(const areaScalarField::Internal &, const faMatrix< Type > &)
dimensionedScalar operator/(const scalar s1, const dimensionedScalar &ds2)
MinMax< label > minMax(const labelHashSet &set)
Find the min/max values of labelHashSet.
Definition hashSets.C:54
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:26
bool operator<(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A older than B.
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
bool operator>(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A newer than B.
Unary function for applying component-wise clamping.
Definition MinMaxOps.H:68
clampOp(Foam::zero_one)
Construct as 0-1 min/max range.
Definition MinMaxOps.H:93
T operator()(const T &val) const
Definition MinMaxOps.H:98
clampOp(const MinMax< T > &range)
Construct from min/max range. No validity checks.
Definition MinMaxOps.H:84
clampOp(const T &min, const T &max)
Construct from min/max limits. No validity checks.
Definition MinMaxOps.H:75
const T lower
Definition MinMaxOps.H:69
const T upper
Definition MinMaxOps.H:70