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 -------------------------------------------------------------------------------
10 License
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 InNamespace
27  Foam
28 
29 Description
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 
44 namespace Foam
45 {
46 
47 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
48 
49 //- The mag() function for min/max range
50 template<class T>
51 inline 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
58 template<class T>
59 inline 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
66 template<class T>
67 struct 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 
98 
111 
112 
113 //- Extract the min/max range from a list of values.
114 template<class T>
115 inline 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.
122 template<class T>
123 inline 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)
130 template<class T>
131 inline 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.
138 inline scalarMinMax minMaxMag(const scalar val)
139 {
140  return scalarMinMax(Foam::mag(val));
141 }
142 
144 //- The magnitude of a VectorSpace.
145 template<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
153 template<class T>
154 inline 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
167 template<class T>
168 inline 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.
179 template<class T1, class T2>
180 inline 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)
187 template<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
285 template<class T>
286 inline 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
293 template<class T>
294 inline 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
301 template<class T>
302 inline 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 
310 template<class T, class U>
311 inline std::enable_if_t<std::is_convertible_v<U, T>, bool>
312 operator<(const MinMax<T>& range, const U& val)
313 {
314  return (range.compare(val) < 0);
315 }
316 
317 template<class T, class U>
318 inline std::enable_if_t<std::is_convertible_v<U, T>, bool>
319 operator<=(const MinMax<T>& range, const U& val)
320 {
321  return (range.compare(val) <= 0);
322 }
323 
324 template<class T, class U>
325 inline std::enable_if_t<std::is_convertible_v<U, T>, bool>
326 operator>(const MinMax<T>& range, const U& val)
327 {
328  return (range.compare(val) > 0);
329 }
330 
331 template<class T, class U>
332 inline std::enable_if_t<std::is_convertible_v<U, T>, bool>
333 operator>=(const MinMax<T>& range, const U& val)
334 {
335  return (range.compare(val) >= 0);
336 }
337 
338 
339 template<class T, class U>
340 inline std::enable_if_t<std::is_convertible_v<U, T>, bool>
341 operator<(const U& val, const MinMax<T>& range)
342 {
343  return (range.compare(val) > 0);
344 }
345 
346 template<class T, class U>
347 inline std::enable_if_t<std::is_convertible_v<U, T>, bool>
348 operator<=(const U& val, const MinMax<T>& range)
349 {
350  return (range.compare(val) >= 0);
351 }
352 
353 template<class T, class U>
354 inline std::enable_if_t<std::is_convertible_v<U, T>, bool>
355 operator>(const U& val, const MinMax<T>& range)
356 {
357  return (range.compare(val) < 0);
358 }
359 
360 template<class T, class U>
361 inline std::enable_if_t<std::is_convertible_v<U, T>, bool>
362 operator>=(const U& val, const MinMax<T>& range)
363 {
364  return (range.compare(val) <= 0);
365 }
366 
367 
368 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
369 
370 } // End namespace Foam
372 #endif
373 
374 // ************************************************************************* //
MinMax< scalar > scalarMinMax
A scalar min/max range.
Definition: MinMax.H:97
clampOp(const T &min, const T &max)
Construct from min/max limits. No validity checks.
Definition: MinMaxOps.H:75
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
A min/max value pair with additional methods. In addition to conveniently storing values...
Definition: HashSet.H:72
bool operator>(const IOstreamOption::versionNumber &a, const IOstreamOption::versionNumber &b) noexcept
Version A newer than B.
Templated vector space.
Definition: VectorSpace.H:52
T clip(const T &val, const MinMax< T > &range)
Return the value after clamping by the min/max limiter.
Definition: MinMaxOps.H:57
dimensionedScalar operator/(const scalar s1, const dimensionedScalar &ds2)
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.
scalar range
MinMax< label > minMax(const labelHashSet &set)
Find the min/max values of labelHashSet.
Definition: hashSets.C:54
dimensioned< scalarMinMax > minMaxMag(const DimensionedField< Type, GeoMesh > &f1, const label comm)
scalar y
Unary function for applying component-wise clamping.
Definition: MinMaxOps.H:67
tmp< faMatrix< Type > > operator*(const areaScalarField::Internal &, const faMatrix< Type > &)
const T lower
Definition: MinMaxOps.H:69
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:26
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
const T upper
Definition: MinMaxOps.H:70
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
MinMax< T > & add(const MinMax &other)
Extend the range to include the other min/max range.
Definition: MinMaxI.H:240
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Represents 0/1 range or concept. Used for tagged dispatch or clamping.
Definition: pTraits.H:51
U
Definition: pEqn.H:72
T operator()(const T &val) const
Definition: MinMaxOps.H:98
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.