Loading...
Searching...
No Matches
complexI.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-2014 OpenFOAM Foundation
9 Copyright (C) 2019-2025 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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\*---------------------------------------------------------------------------*/
28
29// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30
32:
33 re(0),
34 im(0)
35{}
36
37
39:
40 re(0),
41 im(0)
42{}
43
44
45inline constexpr Foam::complex::complex(const scalar r) noexcept
46:
47 re(r),
48 im(0)
49{}
50
51
52inline constexpr Foam::complex::complex(const scalar r, const scalar i) noexcept
53:
54 re(r),
55 im(i)
56{}
57
58
59inline Foam::complex::complex(const std::complex<float>& c)
60:
61 re(c.real()),
62 im(c.imag())
63{}
64
65
66inline Foam::complex::complex(const std::complex<double>& c)
67:
68 re(c.real()),
69 im(c.imag())
70{}
71
72
73// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
76{
77 return complex(re, -im);
78}
79
81inline Foam::scalar Foam::complex::magnitude() const
82{
83 return std::hypot(re, im);
84}
85
87inline Foam::scalar Foam::complex::magSqr() const
88{
89 return (re*re + im*im);
90}
91
92
93inline Foam::scalar Foam::complex::cmptSum() const noexcept
95 return (re + im);
96}
97
98
99// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
100
102{
103 re = 0;
104 im = 0;
105}
106
107
108inline void Foam::complex::operator=(const scalar s)
109{
110 re = s;
111 im = 0;
112}
113
114
116{
117 re += c.re;
118 im += c.im;
119}
120
122inline void Foam::complex::operator+=(const scalar s)
123{
124 re += s;
125}
126
127
129{
130 re -= c.re;
131 im -= c.im;
132}
133
135inline void Foam::complex::operator-=(const scalar s)
136{
137 re -= s;
138}
139
141inline void Foam::complex::operator*=(const complex& c)
142{
143 *this = (*this)*c;
144}
145
146
147inline void Foam::complex::operator*=(const scalar s)
148{
149 re *= s;
150 im *= s;
151}
152
154inline void Foam::complex::operator/=(const complex& c)
155{
156 *this = *this/c;
157}
158
159
160inline void Foam::complex::operator/=(const scalar s)
161{
162 re /= s;
163 im /= s;
164}
165
167inline bool Foam::complex::operator==(const complex& c) const
168{
169 return (equal(re, c.re) && equal(im, c.im));
170}
171
172
173inline bool Foam::complex::operator!=(const complex& c) const
175 return !operator==(c);
176}
177
178
179// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
180
181inline Foam::complex Foam::operator~(const complex& c)
182{
183 return c.conjugate();
184}
186
187// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
188
189namespace Foam
190{
192inline scalar mag(const complex& c)
193{
194 return c.magnitude();
195}
196
198inline scalar magSqr(const complex& c)
199{
200 return c.magSqr();
201}
202
203
204inline complex sqr(const complex& c)
205{
206 return c*c;
207}
208
209
210//- sgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
211inline complex sign(const complex& c)
212{
213 // TBD: Use volatile to avoid aggressive branch optimization
214 const scalar s(mag(c));
215 return s < ROOTVSMALL ? Zero : c/s;
216}
218
219//- csgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
220inline scalar csign(const complex& c)
221{
222 return equal(c.real(), 0) ? sign(c.imag()) : sign(c.real());
224
225
226inline const complex& min(const complex& c1, const complex& c2)
227{
228 return (c1.magSqr() < c2.magSqr()) ? c1 : c2;
230
231
232inline const complex& max(const complex& c1, const complex& c2)
233{
234 return (c1.magSqr() < c2.magSqr()) ? c2 : c1;
236
237
238inline complex limit(const complex& c1, const complex& c2)
239{
240 return complex
241 (
242 limit(c1.real(), c2.real()),
243 limit(c1.imag(), c2.imag())
244 );
245}
246
247
248//- Linear interpolation of complex a and b by factor t
249inline complex lerp(const complex& a, const complex& b, const scalar t)
250{
251 const scalar onet = (1-t);
252
253 return complex
254 (
255 onet*a.real() + t*b.real(),
256 onet*a.imag() + t*b.imag()
257 );
258}
259
261inline const complex& sum(const complex& c)
262{
263 return c;
264}
265
266
267template<class Cmpt> class Tensor;
268
269//- No-op rotational transform for complex
270inline complex transform(const Tensor<scalar>&, const complex c)
272 return c;
273}
274
275
276// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
277
278inline complex operator-(const complex& c)
280 return complex(-c.re, -c.im);
281}
282
283
284inline complex operator+(const complex& c1, const complex& c2)
286 return complex
287 (
288 c1.re + c2.re,
289 c1.im + c2.im
290 );
291}
292
293
294inline complex operator+(const complex& c, const scalar s)
296 return complex(c.re + s, c.im);
297}
298
299
300inline complex operator+(const scalar s, const complex& c)
302 return complex(c.re + s, c.im);
303}
304
305
306inline complex operator-(const complex& c1, const complex& c2)
308 return complex
309 (
310 c1.re - c2.re,
311 c1.im - c2.im
312 );
313}
314
315
316inline complex operator-(const complex& c, const scalar s)
318 return complex(c.re - s, c.im);
319}
320
321
322inline complex operator-(const scalar s, const complex& c)
324 return complex(s - c.re, -c.im);
325}
326
327
328inline complex operator*(const complex& c1, const complex& c2)
330 return complex
331 (
332 c1.re*c2.re - c1.im*c2.im,
333 c1.im*c2.re + c1.re*c2.im
334 );
335}
336
337
338inline complex operator*(const complex& c, const scalar s)
340 return complex(s*c.re, s*c.im);
341}
342
343
344inline complex operator*(const scalar s, const complex& c)
346 return complex(s*c.re, s*c.im);
347}
348
349
350inline complex operator/(const complex& c1, const complex& c2)
352 const scalar sqrC2 = c2.magSqr();
353
354 return complex
355 (
356 (c1.re*c2.re + c1.im*c2.im)/sqrC2,
357 (c1.im*c2.re - c1.re*c2.im)/sqrC2
358 );
359}
360
361
362inline complex operator/(const complex& c, const scalar s)
364 return complex(c.re/s, c.im/s);
365}
366
367
368inline complex operator/(const scalar s, const complex& c)
370 return complex(s)/c;
371}
372
373
374// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
375
376} // End namespace Foam
377
378
379// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
380
381// Complex transcendental functions
382namespace Foam
383{
384 #define transFunc(func) \
385 inline complex func(const complex& c) \
386 { \
387 return std:: func (static_cast<std::complex<scalar>>(c)); \
388 }
389
407// Special treatment for pow()
408inline complex pow(const complex& x, const complex& y)
410 return std::pow
411 (
412 static_cast<std::complex<scalar>>(x),
413 static_cast<std::complex<scalar>>(y)
414 );
415}
416
417
418// Combinations of complex and integral/float
419#define powFuncs(type2) \
420 inline complex pow(const complex& x, const type2 y) \
421 { \
422 return std::pow(static_cast<std::complex<scalar>>(x), y); \
423 } \
424 \
425 inline complex pow(const type2 x, const complex& y) \
426 { \
427 return std::pow \
428 ( \
429 static_cast<std::complex<scalar>>(x), \
430 static_cast<std::complex<scalar>>(y) \
431 ); \
432 }
433
434powFuncs(int)
436#if (WM_LABEL_SIZE == 64) && (defined(__APPLE__) || defined(_WIN32))
437powFuncs(int64_t)
438#endif
439powFuncs(float)
440powFuncs(double)
442
443inline complex pow3(const complex& c)
445 return c*sqr(c);
446}
447
448
449inline complex pow4(const complex& c)
451 return sqr(sqr(c));
452}
453
454
455inline complex pow5(const complex& c)
457 return c*pow4(c);
458}
459
460
461inline complex pow6(const complex& c)
463 return pow3(sqr(c));
464}
465
466
467inline complex pow025(const complex& c)
469 return sqrt(sqrt(c));
470}
471
472} // End namespace Foam
473
474#undef transFunc
475#undef powFuncs
476
477// ************************************************************************* //
scalar y
A templated (3 x 3) tensor of objects of <T> derived from MatrixSpace.
Definition Tensor.H:60
A complex number, similar to the C++ complex type.
Definition complex.H:71
complex conjugate() const
Complex conjugate.
Definition complexI.H:68
complex & operator=(const complex &) noexcept=default
Copy assignment.
void operator+=(const complex &c)
Definition complexI.H:108
constexpr scalar real() const noexcept
Real part of complex number - STL naming.
Definition complex.H:150
constexpr complex() noexcept
Default construct, as zero-initialized.
Definition complexI.H:24
constexpr scalar imag() const noexcept
Imaginary part of complex number - STL naming.
Definition complex.H:155
void operator-=(const complex &c)
Definition complexI.H:121
complex(const complex &) noexcept=default
Copy construct.
void operator*=(const complex &c)
Definition complexI.H:134
bool operator!=(const complex &c) const
Definition complexI.H:166
void operator/=(const complex &c)
Definition complexI.H:147
scalar cmptSum() const noexcept
The sum of real/imag components.
Definition complexI.H:86
scalar magSqr() const
The L2-norm squared of complex.
Definition complexI.H:80
bool operator==(const complex &c) const
Definition complexI.H:160
scalar magnitude() const
The magnitude (L2-norm) of complex. Called magnitude() instead mag(), which looks too much like imag(...
Definition complexI.H:74
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
#define powFuncs(type2)
Definition complexI.H:420
#define transFunc(func)
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))
pressureControl limit(p)
Namespace for OpenFOAM.
tmp< faMatrix< Type > > operator-(const faMatrix< Type > &)
Unary negation.
bitSet operator~(const bitSet &bitset)
Bitset complement, returns a copy of the bitset with all its bits flipped.
Definition bitSetI.H:674
dimensionedScalar pow6(const dimensionedScalar &ds)
dimensionedScalar pow5(const dimensionedScalar &ds)
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:40
dimensionedScalar asin(const dimensionedScalar &ds)
dimensionedScalar exp(const dimensionedScalar &ds)
dimensionedScalar tan(const dimensionedScalar &ds)
dimensionedScalar sign(const dimensionedScalar &ds)
dimensionedSymmTensor sqr(const dimensionedVector &dv)
complex limit(const complex &c1, const complex &c2)
Definition complexI.H:235
tmp< faMatrix< Type > > operator+(const faMatrix< Type > &, const faMatrix< Type > &)
dimensionedScalar pow3(const dimensionedScalar &ds)
dimensionedScalar cosh(const dimensionedScalar &ds)
dimensionedScalar sin(const dimensionedScalar &ds)
bool equal(const T &a, const T &b)
Compare two values for equality.
Definition label.H:180
dimensionedScalar tanh(const dimensionedScalar &ds)
refinementData transform(const tensor &, const refinementData val)
No-op rotational transform for base types.
dimensionedScalar sinh(const dimensionedScalar &ds)
dimensionedScalar log10(const dimensionedScalar &ds)
tmp< faMatrix< Type > > operator*(const areaScalarField::Internal &, const faMatrix< Type > &)
dimensionedScalar log(const dimensionedScalar &ds)
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
dimensionedScalar operator/(const scalar s1, const dimensionedScalar &ds2)
dimensionedScalar acosh(const dimensionedScalar &ds)
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
dimensionedScalar sqrt(const dimensionedScalar &ds)
scalar csign(const complex &c)
csgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
Definition complexI.H:217
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
dimensionedScalar pow4(const dimensionedScalar &ds)
dimensionedScalar atanh(const dimensionedScalar &ds)
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &f1, const label comm)
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127
const direction noexcept
Definition scalarImpl.H:265
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 atan(const dimensionedScalar &ds)
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
dimensionedScalar cos(const dimensionedScalar &ds)
dimensioned< Type > lerp(const dimensioned< Type > &a, const dimensioned< Type > &b, const scalar t)
dimensionedScalar acos(const dimensionedScalar &ds)
dimensionedScalar pow025(const dimensionedScalar &ds)
dimensionedScalar asinh(const dimensionedScalar &ds)
volScalarField & b