scalarImpl.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) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2016-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Typedef
28  Foam::Scalar
29 
30 Description
31  Floating-point number (float or double)
32 
33 Note
34  The floor/ceil/round operations could easily be extended to handle
35  VectorSpace when the need arises. For example,
36 
37  \code
38  template<class T>
39  struct floorOp
40  {
41  T operator()(const T& x) const
42  {
43  T ret;
44  for (direction cmpt=0; cmpt < pTraits<T>::nComponents; ++cmpt)
45  {
46  setComponent(ret, cmpt) = std::floor(component(x, cmpt));
47  }
48  return ret;
49  }
50  };
51  \endcode
52 
53 SourceFiles
54  scalarImpl.C
55 
56 \*---------------------------------------------------------------------------*/
57 
58 // Could also check if 'Scalar' is defined...
59 #ifndef Foam_use_scalarImpl_header
60 #error "scalarImpl.H" is only to be included internally
61 #endif
62 
63 
64 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
65 
66 namespace Foam
67 {
68 
69 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
70 
71 // Template specialisation for pTraits<Scalar>
72 template<>
73 class pTraits<Scalar>
74 {
75  Scalar p_;
76 
77 public:
78 
79  // Typedefs
80 
81  //- Component type
82  typedef Scalar cmptType;
83 
84  //- Magnitude type
85  typedef Scalar magType;
86 
87  //- Equivalent type of labels used for valid component indexing
88  typedef label labelType;
89 
90 
91  // Member Constants
92 
93  //- Dimensionality of space
94  static constexpr direction dim = 3;
95 
96  //- Rank of Scalar is 0
97  static constexpr direction rank = 0;
98 
99  //- Number of components in Scalar is 1
100  static constexpr direction nComponents = 1;
101 
102 
103  // Static Data Members
104 
105  static const char* const typeName;
106  static const char* const componentNames[];
107  static const Scalar zero;
108  static const Scalar one;
109  static const Scalar max;
110  static const Scalar min;
111  static const Scalar rootMax;
112  static const Scalar rootMin;
113  static const Scalar vsmall;
116  // Constructors
118  //- Copy construct from primitive
119  explicit pTraits(Scalar val) noexcept : p_(val) {}
121  //- Read construct from Istream
122  explicit pTraits(Istream& is);
123 
124 
125  // Member Functions
126 
127  //- Return the value
128  operator Scalar() const noexcept { return p_; }
130  //- Access the value
131  operator Scalar&() noexcept { return p_; }
132 };
133 
134 
135 // * * * * * * * * * * * * * * * IO/Conversion * * * * * * * * * * * * * * * //
136 
137 //- A word representation of a floating-point value.
138 // Uses stringstream instead of std::to_string for more consistent formatting.
139 word name(const Scalar val);
140 
141 
142 //- A word representation of a floating-point value.
143 template<>
144 struct nameOp<Scalar>
145 {
146  word operator()(const Scalar val) const
147  {
148  return Foam::name(val);
149  }
150 };
151 
152 
153 //- Parse entire buffer as a float/double, skipping leading/trailing whitespace.
154 // \return Parsed value or FatalIOError on any problem
155 Scalar ScalarRead(const char* buf);
156 
157 //- Parse entire buffer as a float/double, skipping leading/trailing whitespace.
158 // \return True if successful.
159 bool ScalarRead(const char* buf, Scalar& val);
160 
161 //- Parse entire string as a float/double, skipping leading/trailing whitespace.
162 // \return Parsed value or FatalIOError on any problem
163 inline Scalar ScalarRead(const std::string& str)
164 {
165  return ScalarRead(str.c_str());
166 }
168 //- Parse entire string as a float/double, skipping leading/trailing whitespace.
169 // \return True if successful.
170 inline bool ScalarRead(const std::string& str, Scalar& val)
171 {
172  return ScalarRead(str.c_str(), val);
173 }
174 
175 
177 Istream& operator>>(Istream& is, Scalar& val);
178 Ostream& operator<<(Ostream& os, const Scalar val);
179 
180 
181 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
182 
183 // Standard C++ transcendental functions
185 
187 transFunc(exp)
188 transFunc(log)
190 transFunc(sin)
191 transFunc(cos)
192 transFunc(tan)
202 
203 // Standard ANSI-C (but not in <cmath>) transcendental functions
204 
205 transFunc(erf)
208 transFunc(tgamma)
209 
210 besselFunc(j0)
211 besselFunc(j1)
212 besselFunc(y0)
213 besselFunc(y1)
216 
217 
218 //- Non-const access to scalar-type (has no components)
219 inline Scalar& setComponent(Scalar& val, const direction) noexcept
220 {
221  return val;
222 }
223 
224 
225 //- Return scalar value (has no components)
226 inline constexpr Scalar component(const Scalar val, const direction) noexcept
227 {
228  return val;
229 }
230 
231 
232 //- Return 1 if s is greater_equal zero, or otherwise -1
233 inline Scalar sign(const Scalar s) noexcept
234 {
235  return (s >= 0)? 1: -1;
236 }
237 
238 
239 //- Return 1 if s is greater than zero, otherwise 0
240 inline Scalar pos(const Scalar s) noexcept
241 {
242  return (s > 0)? 1: 0;
243 }
244 
245 
246 //- Return 1 if s is greater_equal zero, or otherwise 0
247 inline Scalar pos0(const Scalar s) noexcept
248 {
249  return (s >= 0)? 1: 0;
250 }
251 
252 
253 //- Return 1 if s is less than zero, or otherwise 0
254 inline Scalar neg(const Scalar s) noexcept
255 {
256  return (s < 0)? 1: 0;
257 }
258 
259 
260 //- Return 1 if s is less_equal zero, or otherwise 0
261 inline Scalar neg0(const Scalar s) noexcept
262 {
263  return (s <= 0)? 1: 0;
264 }
265 
266 
267 //- Return the positive part of s, otherwise zero. Same as max(0, s).
268 inline Scalar posPart(const Scalar s) noexcept
269 {
270  return (s > 0)? s: 0;
271 }
273 
274 //- Return the negative part of s, otherwise zero. Same as min(0, s).
275 // Does not change the sign
276 inline Scalar negPart(const Scalar s) noexcept
277 {
278  return (s < 0)? s: 0;
279 }
280 
282 inline bool equal(const Scalar& a, const Scalar& b)
283 {
284  return mag(a - b) <= ScalarVSMALL;
285 }
286 
287 
288 inline bool notEqual(const Scalar a, const Scalar b)
289 {
290  return mag(a - b) > ScalarVSMALL;
291 }
292 
293 
294 //- Clamp scalar value to a 0-1 range
295 inline Scalar clamp(const Scalar val, Foam::zero_one)
296 {
297  return (val < 0) ? 0 : (1 < val) ? 1 : val;
298 }
300 
301 // Fast implementation, and with scalar promotion of upper/lower limits
302 inline Scalar clamp(const Scalar val, const Scalar lower, const Scalar upper)
303 {
304  return (val < lower) ? lower : (upper < val) ? upper : val;
305 }
306 
307 
308 inline Scalar limit(const Scalar s1, const Scalar s2)
309 {
310  return (mag(s1) < mag(s2)) ? s1: 0.0;
311 }
312 
313 
314 inline Scalar minMod(const Scalar s1, const Scalar s2)
315 {
316  return (mag(s1) < mag(s2)) ? s1: s2;
317 }
318 
319 
320 //- Linear interpolation of scalars a and b by factor t
321 // Explicit form (not fused multiply add etc) for exact values at end points.
322 // Unlike std::lerp (C++20) there are no isfinite() checks etc.
323 inline constexpr Scalar lerp(const Scalar a, const Scalar b, const Scalar t)
324 {
325  return (Scalar{1}-t)*a + t*b;
326 }
327 
329 inline Scalar magSqr(const Scalar s)
330 {
331  return s*s;
332 }
333 
335 inline Scalar sqr(const Scalar s)
336 {
337  return s*s;
338 }
339 
341 inline Scalar pow3(const Scalar s)
342 {
343  return s*sqr(s);
344 }
345 
346 
347 inline Scalar pow4(const Scalar s)
348 {
349  return sqr(sqr(s));
350 }
351 
352 
353 inline Scalar pow5(const Scalar s)
354 {
355  return s*pow4(s);
356 }
357 
358 
359 inline Scalar pow6(const Scalar s)
360 {
361  return pow3(sqr(s));
362 }
363 
364 
365 inline Scalar pow025(const Scalar s)
366 {
367  return sqrt(sqrt(s));
368 }
369 
370 
371 inline Scalar inv(const Scalar s)
372 {
373  return 1.0/s;
374 }
375 
376 
377 inline Scalar dot(const Scalar s1, const Scalar s2)
378 {
379  return s1*s2;
380 }
381 
382 
383 inline Scalar cmptMultiply(const Scalar s1, const Scalar s2)
384 {
385  return s1*s2;
386 }
387 
388 
389 inline Scalar cmptPow(const Scalar s1, const Scalar s2)
390 {
391  return pow(s1, s2);
392 }
393 
394 
395 inline Scalar cmptDivide(const Scalar s1, const Scalar s2)
396 {
397  return s1/s2;
398 }
399 
400 
401 inline Scalar cmptMax(const Scalar s)
402 {
403  return s;
404 }
405 
406 
407 inline Scalar cmptMin(const Scalar s)
408 {
409  return s;
410 }
411 
412 
413 inline Scalar cmptAv(const Scalar s)
414 {
415  return s;
416 }
417 
418 
419 inline Scalar cmptSqr(const Scalar s)
420 {
421  return sqr(s);
422 }
423 
424 
425 inline Scalar cmptMag(const Scalar s)
426 {
427  return mag(s);
428 }
429 
430 
431 inline Scalar cmptMagSqr(const Scalar s)
432 {
433  return magSqr(s);
434 }
435 
436 
437 inline Scalar sqrtSumSqr(const Scalar a, const Scalar b)
438 {
439  const Scalar maga = mag(a);
440  const Scalar magb = mag(b);
441 
442  if (maga > magb)
443  {
444  return maga*sqrt(1.0 + sqr(magb/maga));
445  }
446  else
447  {
448  return magb < ScalarVSMALL ? 0.0 : magb*sqrt(1.0 + sqr(maga/magb));
449  }
450 }
451 
453 //- Stabilisation around zero for division
454 inline Scalar stabilise(const Scalar s, const Scalar tol)
455 {
456  if (s >= 0)
457  {
458  return s + tol;
459  }
460  else
461  {
462  return s - tol;
463  }
464 }
465 
466 
467 // Specializations
468 
469 // Default definition in ops.H
470 template<class T> struct compareOp;
471 
472 //- Compare scalar values
473 template<>
474 struct compareOp<Scalar>
475 {
476  const Scalar tolerance;
477 
478  //- Construct with specified tolerance (non-negative value)
480  :
481  tolerance(tol)
482  {}
483 
484  Scalar operator()(const Scalar& a, const Scalar& b) const
485  {
486  return (mag(a - b) <= tolerance) ? 0 : (a - b);
487  }
488 };
489 
490 
491 // Default definition in ops.H
492 template<class T> struct equalOp;
493 
494 //- Compare scalar values for equality
495 template<>
496 struct equalOp<Scalar>
497 {
498  const Scalar tolerance;
499 
500  //- Construct with specified tolerance (non-negative value)
502  :
503  tolerance(tol)
504  {}
505 
506  bool operator()(const Scalar& a, const Scalar& b) const
507  {
508  return mag(a - b) <= tolerance;
509  }
510 };
511 
512 
513 // Default definition in ops.H
514 template<class T> struct notEqualOp;
515 
516 //- Compare scalar values for inequality
517 template<>
518 struct notEqualOp<Scalar>
519 {
520  const Scalar tolerance;
521 
522  //- Construct with specified tolerance (non-negative value)
524  :
525  tolerance(tol)
526  {}
527 
528  bool operator()(const Scalar& a, const Scalar& b) const
529  {
530  return mag(a - b) > tolerance;
531  }
532 };
533 
534 
536 // Default definition in ops.H (future?)
537 template<class T> struct floorOp;
538 
539 //- Round scalar downwards - functor version of std::floor
540 template<>
541 struct floorOp<Scalar>
542 {
543  Scalar operator()(const Scalar& x) const
544  {
545  return std::floor(x);
546  }
547 };
548 
549 
550 // Default definition in ops.H (future?)
551 template<class T> struct ceilOp;
552 
553 //- Round scalar upwards - functor version of std::ceil
554 template<>
555 struct ceilOp<Scalar>
556 {
557  Scalar operator()(const Scalar& x) const
558  {
559  return std::ceil(x);
560  }
561 };
562 
564 // Default definition in ops.H (future?)
565 template<class T> struct roundOp;
566 
567 //- Round scalar - functor version of std::round
568 template<>
569 struct roundOp<Scalar>
570 {
571  Scalar operator()(const Scalar& x) const
572  {
573  return std::round(x);
574  }
575 };
576 
577 
578 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
579 
580 } // End namespace Foam
581 
582 // ************************************************************************* //
dimensionedScalar sign(const dimensionedScalar &ds)
dimensionedScalar tanh(const dimensionedScalar &ds)
dimensionedScalar acos(const dimensionedScalar &ds)
void cmptMax(FieldField< Field, typename FieldField< Field, Type >::cmptType > &cf, const FieldField< Field, Type > &f)
uint8_t direction
Definition: direction.H:46
tmp< DimensionedField< typename DimensionedField< Type, GeoMesh >::cmptType, GeoMesh >> cmptAv(const DimensionedField< Type, GeoMesh > &f1)
dimensionedScalar log(const dimensionedScalar &ds)
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
bool equal(const T &a, const T &b)
Compare two values for equality.
Definition: label.H:164
#define besselFunc(func)
Definition: doubleScalar.H:124
#define ScalarVSMALL
Definition: doubleScalar.C:36
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
dimensionedSymmTensor sqr(const dimensionedVector &dv)
dimensionedSphericalTensor inv(const dimensionedSphericalTensor &dt)
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
dimensionedScalar yn(const int n, const dimensionedScalar &ds)
Three-way comparison operation of two parameters,.
Definition: ops.H:258
dimensionedScalar sqrt(const dimensionedScalar &ds)
dimensionedScalar pow025(const dimensionedScalar &ds)
A traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:61
bool operator()(const T &x, const T &y) const
Definition: ops.H:224
dimensionedScalar stabilise(const dimensionedScalar &x, const dimensionedScalar &y)
dimensionedScalar y0(const dimensionedScalar &ds)
::Foam::direction nComponents(const expressions::valueTypeCode) noexcept
The number of components associated with given valueTypeCode.
Definition: exprTraits.C:40
Scalar magType
Magnitude type.
Definition: scalarImpl.H:85
dimensionedScalar posPart(const dimensionedScalar &ds)
dimensionedScalar neg(const dimensionedScalar &ds)
dimensionedScalar asin(const dimensionedScalar &ds)
dimensionedScalar pow5(const dimensionedScalar &ds)
void dot(FieldField< Field1, typename innerProduct< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
Extract name (as a word) from an object, typically using its name() method.
Definition: word.H:340
dimensionedScalar j1(const dimensionedScalar &ds)
pTraits(const Base &obj)
Copy construct from base class.
Definition: pTraits.H:72
bool notEqual(const Scalar a, const Scalar b)
Definition: scalarImpl.H:340
dimensionedScalar pos(const dimensionedScalar &ds)
dimensionedScalar acosh(const dimensionedScalar &ds)
dimensionedScalar cos(const dimensionedScalar &ds)
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
dimensionedScalar exp(const dimensionedScalar &ds)
void cmptMagSqr(Field< Type > &result, const UList< Type > &f1)
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
A class for handling words, derived from Foam::string.
Definition: word.H:63
dimensionedScalar asinh(const dimensionedScalar &ds)
dimensionedScalar jn(const int n, const dimensionedScalar &ds)
Istream & operator>>(Istream &, directionInfo &)
dimensionedScalar cbrt(const dimensionedScalar &ds)
complex limit(const complex &c1, const complex &c2)
Definition: complexI.H:235
Scalar sqrtSumSqr(const Scalar a, const Scalar b)
Definition: scalarImpl.H:494
dimensionedScalar neg0(const dimensionedScalar &ds)
dimensionedScalar atanh(const dimensionedScalar &ds)
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:26
dimensionedScalar y1(const dimensionedScalar &ds)
Scalar cmptPow(const Scalar s1, const Scalar s2)
Definition: scalarImpl.H:446
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
word operator()(const T &obj) const
Definition: word.H:342
void cmptMag(FieldField< Field, Type > &cf, const FieldField< Field, Type > &f)
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: scalarImpl.H:255
dimensionedScalar pos0(const dimensionedScalar &ds)
dimensionedScalar sin(const dimensionedScalar &ds)
Scalar cmptSqr(const Scalar s)
Definition: scalarImpl.H:476
dimensionedScalar erf(const dimensionedScalar &ds)
OBJstream os(runTime.globalPath()/outputName)
Scalar cmptType
Component type.
Definition: scalarImpl.H:80
#define besselFunc2(func)
Definition: doubleScalar.H:129
#define Scalar
Definition: doubleScalar.C:34
string upper(const std::string &s)
Return string copy transformed with std::toupper on each character.
dimensioned< Type > lerp(const dimensioned< Type > &a, const dimensioned< Type > &b, const scalar t)
Represents 0/1 range or concept. Used for tagged dispatch or clamping.
Definition: pTraits.H:51
Scalar ScalarRead(const char *buf)
Parse entire buffer as a float/double, skipping leading/trailing whitespace.
Definition: scalarImpl.C:66
dimensionedScalar lgamma(const dimensionedScalar &ds)
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
dimensionedScalar pow3(const dimensionedScalar &ds)
Scalar minMod(const Scalar s1, const Scalar s2)
Definition: scalarImpl.H:368
dimensionedScalar erfc(const dimensionedScalar &ds)
dimensionedScalar sinh(const dimensionedScalar &ds)
label labelType
Equivalent type of labels used for valid component indexing.
Definition: scalarImpl.H:90
dimensionedScalar atan(const dimensionedScalar &ds)
dimensionedScalar pow4(const dimensionedScalar &ds)
string lower(const std::string &s)
Return string copy transformed with std::tolower on each character.
dimensionedScalar pow6(const dimensionedScalar &ds)
::Foam::direction rank(const expressions::valueTypeCode) noexcept
The vector-space rank associated with given valueTypeCode.
Definition: exprTraits.C:70
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
transFunc(sqrt) transFunc(cbrt) transFunc(exp) transFunc(log) transFunc(log10) transFunc(sin) transFunc(cos) transFunc(tan) transFunc(asin) transFunc(acos) transFunc(atan) transFunc(sinh) transFunc(cosh) transFunc(tanh) transFunc(asinh) transFunc(acosh) transFunc(atanh) transFunc(erf) transFunc(erfc) transFunc(lgamma) transFunc(tgamma) besselFunc(j0) besselFunc(j1) besselFunc(y0) besselFunc(y1) besselFunc2(jn) besselFunc2(yn) inline Scalar &setComponent(Scalar &val
Non-const access to scalar-type (has no components)
dimensionedScalar cosh(const dimensionedScalar &ds)
void cmptMin(FieldField< Field, typename FieldField< Field, Type >::cmptType > &cf, const FieldField< Field, Type > &f)
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))
dimensionedScalar tan(const dimensionedScalar &ds)
int operator()(const T &a, const T &b) const
Definition: ops.H:260
dimensionedScalar j0(const dimensionedScalar &ds)
label & setComponent(label &val, const direction) noexcept
Non-const access to integer-type (has no components)
Definition: label.H:144
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
dimensionedScalar log10(const dimensionedScalar &ds)
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
Namespace for OpenFOAM.
dimensionedScalar negPart(const dimensionedScalar &ds)
dimensionSet clamp(const dimensionSet &a, const dimensionSet &range)
Definition: dimensionSet.C:271
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:56
bool operator()(const T &x, const T &y) const
Definition: ops.H:223