Scalar.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  Scalar.C
55 
56 \*---------------------------------------------------------------------------*/
57 
58 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
59 
60 namespace Foam
61 {
62 
63 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
64 
65 // Template specialisation for pTraits<Scalar>
66 template<>
67 class pTraits<Scalar>
68 {
69  Scalar p_;
70 
71 public:
72 
73  // Typedefs
74 
75  //- Component type
76  typedef Scalar cmptType;
77 
78  //- Magnitude type
79  typedef Scalar magType;
80 
81  //- Equivalent type of labels used for valid component indexing
82  typedef label labelType;
83 
84 
85  // Member Constants
86 
87  //- Dimensionality of space
88  static constexpr direction dim = 3;
89 
90  //- Rank of Scalar is 0
91  static constexpr direction rank = 0;
92 
93  //- Number of components in Scalar is 1
94  static constexpr direction nComponents = 1;
95 
96 
97  // Static Data Members
98 
99  static const char* const typeName;
100  static const char* const componentNames[];
101  static const Scalar zero;
102  static const Scalar one;
103  static const Scalar max;
104  static const Scalar min;
105  static const Scalar rootMax;
106  static const Scalar rootMin;
107  static const Scalar vsmall;
110  // Constructors
112  //- Copy construct from primitive
113  explicit pTraits(Scalar val) noexcept
114  :
115  p_(val)
116  {}
117 
118  //- Read construct from Istream
119  explicit pTraits(Istream& is);
120 
121 
122  // Member Functions
124  //- Return the value
125  operator Scalar() const noexcept
126  {
127  return p_;
128  }
129 
130  //- Access the value
131  operator Scalar&() noexcept
132  {
133  return p_;
134  }
135 };
136 
137 
138 // * * * * * * * * * * * * * * * IO/Conversion * * * * * * * * * * * * * * * //
140 //- A word representation of a floating-point value.
141 // Uses stringstream instead of std::to_string for more consistent formatting.
142 word name(const Scalar val);
143 
144 
145 //- A word representation of a floating-point value.
146 template<>
147 struct nameOp<Scalar>
148 {
149  word operator()(const Scalar val) const
150  {
151  return Foam::name(val);
152  }
153 };
154 
155 
156 //- Parse entire buffer as a float/double, skipping leading/trailing whitespace.
157 // \return Parsed value or FatalIOError on any problem
158 Scalar ScalarRead(const char* buf);
159 
160 //- Parse entire buffer as a float/double, skipping leading/trailing whitespace.
161 // \return True if successful.
162 bool ScalarRead(const char* buf, Scalar& val);
163 
164 //- Parse entire string as a float/double, skipping leading/trailing whitespace.
165 // \return Parsed value or FatalIOError on any problem
166 inline Scalar ScalarRead(const std::string& str)
167 {
168  return ScalarRead(str.c_str());
169 }
171 //- Parse entire string as a float/double, skipping leading/trailing whitespace.
172 // \return True if successful.
173 inline bool ScalarRead(const std::string& str, Scalar& val)
174 {
175  return ScalarRead(str.c_str(), val);
176 }
177 
178 
180 Istream& operator>>(Istream& is, Scalar& val);
181 Ostream& operator<<(Ostream& os, const Scalar val);
182 
183 
184 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
185 
186 // Standard C++ transcendental functions
188 
190 transFunc(exp)
191 transFunc(log)
193 transFunc(sin)
194 transFunc(cos)
195 transFunc(tan)
205 
206 // Standard ANSI-C (but not in <cmath>) transcendental functions
207 
208 transFunc(erf)
211 transFunc(tgamma)
212 
213 besselFunc(j0)
214 besselFunc(j1)
215 besselFunc(y0)
216 besselFunc(y1)
219 
220 
221 //- Non-const access to scalar-type (has no components)
222 inline Scalar& setComponent(Scalar& val, const direction) noexcept
223 {
224  return val;
225 }
226 
227 
228 //- Return scalar value (has no components)
229 inline constexpr Scalar component(const Scalar val, const direction) noexcept
230 {
231  return val;
232 }
233 
234 
235 //- Return 1 if s is greater_equal zero, or otherwise -1
236 inline Scalar sign(const Scalar s) noexcept
237 {
238  return (s >= 0)? 1: -1;
239 }
240 
241 
242 //- Return 1 if s is greater than zero, otherwise 0
243 inline Scalar pos(const Scalar s) noexcept
244 {
245  return (s > 0)? 1: 0;
246 }
247 
248 
249 //- Return 1 if s is greater_equal zero, or otherwise 0
250 inline Scalar pos0(const Scalar s) noexcept
251 {
252  return (s >= 0)? 1: 0;
253 }
254 
255 
256 //- Return 1 if s is less than zero, or otherwise 0
257 inline Scalar neg(const Scalar s) noexcept
258 {
259  return (s < 0)? 1: 0;
260 }
261 
262 
263 //- Return 1 if s is less_equal zero, or otherwise 0
264 inline Scalar neg0(const Scalar s) noexcept
265 {
266  return (s <= 0)? 1: 0;
267 }
268 
269 
270 //- Return the positive part of s, otherwise zero. Same as max(0, s).
271 inline Scalar posPart(const Scalar s) noexcept
272 {
273  return (s > 0)? s: 0;
274 }
276 
277 //- Return the negative part of s, otherwise zero. Same as min(0, s).
278 // Does not change the sign
279 inline Scalar negPart(const Scalar s) noexcept
280 {
281  return (s < 0)? s: 0;
282 }
283 
285 inline bool equal(const Scalar& a, const Scalar& b)
286 {
287  return mag(a - b) <= ScalarVSMALL;
288 }
289 
290 
291 inline bool notEqual(const Scalar a, const Scalar b)
292 {
293  return mag(a - b) > ScalarVSMALL;
294 }
295 
296 
297 //- Clamp scalar value to a 0-1 range
298 inline Scalar clamp(const Scalar val, const Foam::zero_one)
299 {
300  return (val < 0) ? 0 : (1 < val) ? 1 : val;
301 }
303 
304 // Fast implementation, and with scalar promotion of upper/lower limits
305 inline Scalar clamp(const Scalar val, const Scalar lower, const Scalar upper)
306 {
307  return (val < lower) ? lower : (upper < val) ? upper : val;
308 }
309 
310 
311 inline Scalar limit(const Scalar s1, const Scalar s2)
312 {
313  return (mag(s1) < mag(s2)) ? s1: 0.0;
314 }
315 
316 
317 inline Scalar minMod(const Scalar s1, const Scalar s2)
318 {
319  return (mag(s1) < mag(s2)) ? s1: s2;
320 }
321 
322 
323 //- Linear interpolation of scalars a and b by factor t
324 // Explicit form (not fused multiply add etc) for exact values at end points.
325 // Unlike std::lerp (C++20) there are no isfinite() checks etc.
326 inline constexpr Scalar lerp(const Scalar a, const Scalar b, const Scalar t)
327 {
328  return (Scalar{1}-t)*a + t*b;
329 }
330 
332 inline Scalar magSqr(const Scalar s)
333 {
334  return s*s;
335 }
336 
338 inline Scalar sqr(const Scalar s)
339 {
340  return s*s;
341 }
342 
344 inline Scalar pow3(const Scalar s)
345 {
346  return s*sqr(s);
347 }
348 
349 
350 inline Scalar pow4(const Scalar s)
351 {
352  return sqr(sqr(s));
353 }
354 
355 
356 inline Scalar pow5(const Scalar s)
357 {
358  return s*pow4(s);
359 }
360 
361 
362 inline Scalar pow6(const Scalar s)
363 {
364  return pow3(sqr(s));
365 }
366 
367 
368 inline Scalar pow025(const Scalar s)
369 {
370  return sqrt(sqrt(s));
371 }
372 
373 
374 inline Scalar inv(const Scalar s)
375 {
376  return 1.0/s;
377 }
378 
379 
380 inline Scalar dot(const Scalar s1, const Scalar s2)
381 {
382  return s1*s2;
383 }
384 
385 
386 inline Scalar cmptMultiply(const Scalar s1, const Scalar s2)
387 {
388  return s1*s2;
389 }
390 
391 
392 inline Scalar cmptPow(const Scalar s1, const Scalar s2)
393 {
394  return pow(s1, s2);
395 }
396 
397 
398 inline Scalar cmptDivide(const Scalar s1, const Scalar s2)
399 {
400  return s1/s2;
401 }
402 
403 
404 inline Scalar cmptMax(const Scalar s)
405 {
406  return s;
407 }
408 
409 
410 inline Scalar cmptMin(const Scalar s)
411 {
412  return s;
413 }
414 
415 
416 inline Scalar cmptAv(const Scalar s)
417 {
418  return s;
419 }
420 
421 
422 inline Scalar cmptSqr(const Scalar s)
423 {
424  return sqr(s);
425 }
426 
427 
428 inline Scalar cmptMag(const Scalar s)
429 {
430  return mag(s);
431 }
432 
433 
434 inline Scalar cmptMagSqr(const Scalar s)
435 {
436  return magSqr(s);
437 }
438 
439 
440 inline Scalar sqrtSumSqr(const Scalar a, const Scalar b)
441 {
442  const Scalar maga = mag(a);
443  const Scalar magb = mag(b);
444 
445  if (maga > magb)
446  {
447  return maga*sqrt(1.0 + sqr(magb/maga));
448  }
449  else
450  {
451  return magb < ScalarVSMALL ? 0.0 : magb*sqrt(1.0 + sqr(maga/magb));
452  }
453 }
454 
456 //- Stabilisation around zero for division
457 inline Scalar stabilise(const Scalar s, const Scalar tol)
458 {
459  if (s >= 0)
460  {
461  return s + tol;
462  }
463  else
464  {
465  return s - tol;
466  }
467 }
468 
469 
470 // Specializations
471 
472 // Default definition in ops.H
473 template<class T> struct compareOp;
474 
475 //- Compare scalar values
476 template<>
477 struct compareOp<Scalar>
478 {
479  const Scalar tolerance;
480 
481  //- Construct with specified tolerance (non-negative value)
483  :
484  tolerance(tol)
485  {}
486 
487  Scalar operator()(const Scalar& a, const Scalar& b) const
488  {
489  return (mag(a - b) <= tolerance) ? 0 : (a - b);
490  }
491 };
492 
493 
494 // Default definition in ops.H
495 template<class T> struct equalOp;
496 
497 //- Compare scalar values for equality
498 template<>
499 struct equalOp<Scalar>
500 {
501  const Scalar tolerance;
502 
503  //- Construct with specified tolerance (non-negative value)
505  :
506  tolerance(tol)
507  {}
508 
509  bool operator()(const Scalar& a, const Scalar& b) const
510  {
511  return mag(a - b) <= tolerance;
512  }
513 };
514 
515 
516 // Default definition in ops.H
517 template<class T> struct notEqualOp;
518 
519 //- Compare scalar values for inequality
520 template<>
521 struct notEqualOp<Scalar>
522 {
523  const Scalar tolerance;
524 
525  //- Construct with specified tolerance (non-negative value)
527  :
528  tolerance(tol)
529  {}
530 
531  bool operator()(const Scalar& a, const Scalar& b) const
532  {
533  return mag(a - b) > tolerance;
534  }
535 };
536 
537 
539 // Default definition in ops.H (future?)
540 template<class T> struct floorOp;
541 
542 //- Round scalar downwards - functor version of std::floor
543 template<>
544 struct floorOp<Scalar>
545 {
546  Scalar operator()(const Scalar& x) const
547  {
548  return std::floor(x);
549  }
550 };
551 
552 
553 // Default definition in ops.H (future?)
554 template<class T> struct ceilOp;
555 
556 //- Round scalar upwards - functor version of std::ceil
557 template<>
558 struct ceilOp<Scalar>
559 {
560  Scalar operator()(const Scalar& x) const
561  {
562  return std::ceil(x);
563  }
564 };
565 
567 // Default definition in ops.H (future?)
568 template<class T> struct roundOp;
569 
570 //- Round scalar - functor version of std::round
571 template<>
572 struct roundOp<Scalar>
573 {
574  Scalar operator()(const Scalar& x) const
575  {
576  return std::round(x);
577  }
578 };
579 
580 
581 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
582 
583 } // End namespace Foam
584 
585 // ************************************************************************* //
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:260
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:75
dimensionedScalar stabilise(const dimensionedScalar &x, const dimensionedScalar &y)
string upper(const std::string &s)
Return string copy transformed with std::toupper on each character.
Definition: stringOps.C:1187
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: Scalar.H:79
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:86
bool notEqual(const Scalar a, const Scalar b)
Definition: Scalar.H:343
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: Scalar.H:497
dimensionedScalar neg0(const dimensionedScalar &ds)
FOAM_NODISCARD int operator()(const T &a, const T &b) const
Definition: ops.H:262
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: Scalar.H:449
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: Scalar.H:258
dimensionedScalar pos0(const dimensionedScalar &ds)
dimensionedScalar sin(const dimensionedScalar &ds)
Scalar cmptSqr(const Scalar s)
Definition: Scalar.H:479
dimensionedScalar erf(const dimensionedScalar &ds)
OBJstream os(runTime.globalPath()/outputName)
Scalar cmptType
Component type.
Definition: Scalar.H:74
#define besselFunc2(func)
Definition: doubleScalar.H:129
#define Scalar
Definition: doubleScalar.C:34
dimensioned< Type > lerp(const dimensioned< Type > &a, const dimensioned< Type > &b, const scalar t)
FOAM_NODISCARD bool operator()(const T &x, const T &y) const
Definition: ops.H:226
Represents 0/1 range or concept. Used for tagged dispatch or clamping.
Definition: pTraits.H:65
Scalar ScalarRead(const char *buf)
Parse entire buffer as a float/double, skipping leading/trailing whitespace.
Definition: Scalar.C:61
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: Scalar.H:371
FOAM_NODISCARD bool operator()(const T &x, const T &y) const
Definition: ops.H:225
dimensionedScalar erfc(const dimensionedScalar &ds)
string lower(const std::string &s)
Return string copy transformed with std::tolower on each character.
Definition: stringOps.C:1171
dimensionedScalar sinh(const dimensionedScalar &ds)
label labelType
Equivalent type of labels used for valid component indexing.
Definition: Scalar.H:84
dimensionedScalar atan(const dimensionedScalar &ds)
dimensionedScalar pow4(const dimensionedScalar &ds)
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)
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