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 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
87  clampOp(const Foam::zero_one)
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 //- Combine values and/or MinMax ranges
138 template<class T>
139 struct minMaxOp
140 {
141  MinMax<T> operator()(const T& x, const T& y) const
142  {
143  return MinMax<T>(x).add(y);
144  }
145 
146  MinMax<T> operator()(const MinMax<T>& x, const T& y) const
147  {
148  return MinMax<T>(x).add(y);
149  }
150 
151  MinMax<T> operator()(const T& x, const MinMax<T>& y) const
152  {
153  return MinMax<T>(y).add(x);
154  }
156  MinMax<T> operator()(const MinMax<T>& x, const MinMax<T>& y) const
157  {
158  return MinMax<T>(x).add(y); // Same as (x + y)
159  }
160 };
161 
162 
163 //- Combine assignment for MinMax range
164 template<class T>
166 {
167  MinMax<T>& operator()(MinMax<T>& x, const T& y) const
168  {
169  return x.add(y);
170  }
171 
172  MinMax<T>& operator()(MinMax<T>& x, const UList<T>& y) const
173  {
174  return x.add(y);
175  }
176 
177  MinMax<T>& operator()(MinMax<T>& x, const MinMax<T>& y) const
178  {
179  return x.add(y);
180  }
181 };
182 
184 //- The magnitude of a single value.
185 inline scalarMinMax minMaxMag(const scalar val)
186 {
187  return scalarMinMax(Foam::mag(val));
188 }
189 
190 
191 //- The magnitude of a VectorSpace.
192 template<class Form, class Cmpt, direction nCmpt>
194 {
195  return scalarMinMax(Foam::mag(vs));
196 }
197 
198 
199 //- The min/max magnitudes from a list of values
200 template<class T>
201 inline scalarMinMax minMaxMag(const UList<T>& vals)
202 {
203  scalarMinMax result;
204  for (const T& val : vals)
205  {
206  result += Foam::mag(val);
207  }
208 
209  return result;
210 }
211 
212 
213 //- The min/max magnitudes from a min/max range
214 template<class T>
215 inline scalarMinMax minMaxMag(const MinMax<T>& range)
216 {
217  return
218  (
220  );
221 }
222 
224 //- Combine the magitude of two values to create a min/max range.
225 //- Order is unimportant.
226 template<class T>
227 inline scalarMinMax minMaxMag(const T& x, const T& y)
228 {
229  return minMaxMag(x).add(Foam::mag(y));
230 }
231 
232 
233 //- Scalar combine two MinMax ranges of same type
234 template<class T>
235 inline scalarMinMax minMaxMag(const MinMax<T>& x, const MinMax<T>& y)
236 {
237  return
238  (
240  .add(Foam::mag(y.min()))
241  .add(Foam::mag(y.max()))
242  );
243 }
244 
245 
246 //- Scalar combine two MinMax ranges of dissimilar types
247 template<class T1, class T2>
248 inline scalarMinMax minMaxMag(const MinMax<T1>& x, const MinMax<T2>& y)
249 {
250  return
251  (
252  minMaxMag(x)
253  .add(Foam::mag(y.min()))
254  .add(Foam::mag(y.max()))
255  );
256 }
257 
258 
259 //- Scalar combine the magitude of a value.
260 template<class T>
261 struct minMaxMagOp
262 {
263  scalarMinMax operator()(const scalarMinMax& x, const T& y) const
264  {
265  return minMaxMag(x).add(Foam::mag(y));
266  }
267 
268  template<class T1, class T2>
269  scalarMinMax operator()(const MinMax<T1>& x, const MinMax<T2>& y) const
270  {
271  return minMaxMag(x, y);
272  }
273 };
274 
275 
276 //- Combine assignment for MinMax range
277 template<class T>
279 {
280  scalarMinMax& operator()(scalarMinMax& x, const T& y) const
281  {
282  x = minMaxMag(x);
283  return x.add(Foam::mag(y));
284  }
285 
287  {
288  x = minMaxMag(x);
289 
290  return
291  (
292  x
293  .add(Foam::mag(y.min()))
294  .add(Foam::mag(y.max()))
295  );
296  }
297 
299  {
300  x = minMaxMag(x);
302  for (const T& val : y)
303  {
304  x.add(Foam::mag(val));
305  }
306 
307  return x;
308  }
309 };
310 
311 
312 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
313 
314 //- Combine two ranges
315 template<class T>
316 inline MinMax<T> operator+(const MinMax<T>& x, const MinMax<T>& y)
317 {
318  return MinMax<T>(x).add(y);
319 }
321 
322 //- Multiply range by scalar factor
323 template<class T>
324 inline MinMax<T> operator*(const MinMax<T>& x, const scalar& s)
325 {
326  return MinMax<T>(x.min()*s, x.max()*s);
327 }
328 
329 
330 //- Divide range by scalar factor
331 template<class T>
332 inline MinMax<T> operator/(const MinMax<T>& x, const scalar& s)
333 {
334  return MinMax<T>(x.min()/s, x.max()/s);
335 }
336 
337 
338 // Comparison
339 
340 template<class T, class U>
341 inline typename std::enable_if<std::is_convertible<U, T>::value, bool>::type
342 operator<(const MinMax<T>& range, const U& val)
343 {
344  return (range.compare(val) < 0);
345 }
346 
347 template<class T, class U>
348 inline typename std::enable_if<std::is_convertible<U, T>::value, bool>::type
349 operator<=(const MinMax<T>& range, const U& val)
350 {
351  return (range.compare(val) <= 0);
352 }
353 
354 template<class T, class U>
355 inline typename std::enable_if<std::is_convertible<U, T>::value, bool>::type
356 operator>(const MinMax<T>& range, const U& val)
357 {
358  return (range.compare(val) > 0);
359 }
360 
361 template<class T, class U>
362 inline typename std::enable_if<std::is_convertible<U, T>::value, bool>::type
363 operator>=(const MinMax<T>& range, const U& val)
364 {
365  return (range.compare(val) >= 0);
366 }
367 
368 
369 template<class T, class U>
370 inline typename std::enable_if<std::is_convertible<U, T>::value, bool>::type
371 operator<(const U& val, const MinMax<T>& range)
372 {
373  return (range.compare(val) > 0);
374 }
375 
376 template<class T, class U>
377 inline typename std::enable_if<std::is_convertible<U, T>::value, bool>::type
378 operator<=(const U& val, const MinMax<T>& range)
379 {
380  return (range.compare(val) >= 0);
381 }
383 template<class T, class U>
384 inline typename std::enable_if<std::is_convertible<U, T>::value, bool>::type
385 operator>(const U& val, const MinMax<T>& range)
386 {
387  return (range.compare(val) < 0);
388 }
390 template<class T, class U>
391 inline typename std::enable_if<std::is_convertible<U, T>::value, bool>::type
392 operator>=(const U& val, const MinMax<T>& range)
393 {
394  return (range.compare(val) <= 0);
395 }
397 
398 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
399 
400 } // End namespace Foam
401 
402 #endif
404 // ************************************************************************* //
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
void clip(Field< Type > &result, const UList< Type > &f1, const MinMax< Type > &s2)
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
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
scalar y
Unary function for applying component-wise clamping.
Definition: MinMaxOps.H:67
tmp< faMatrix< Type > > operator*(const areaScalarField::Internal &, const faMatrix< Type > &)
scalarMinMax operator()(const scalarMinMax &x, const T &y) const
Definition: MinMaxOps.H:295
MinMax< T > & operator()(MinMax< T > &x, const T &y) const
Definition: MinMaxOps.H:183
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:250
MinMax< T > operator()(const T &x, const T &y) const
Definition: MinMaxOps.H:155
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Combine assignment for MinMax range.
Definition: MinMaxOps.H:181
scalarMinMax & operator()(scalarMinMax &x, const T &y) const
Definition: MinMaxOps.H:314
Represents 0/1 range or concept. Used for tagged dispatch or clamping.
Definition: pTraits.H:65
dimensioned< scalarMinMax > minMaxMag(const DimensionedField< Type, GeoMesh > &f1)
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))
Combine assignment for MinMax range.
Definition: MinMaxOps.H:312
Namespace for OpenFOAM.