Loading...
Searching...
No Matches
complex.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
27Class
28 Foam::complex
29
30Description
31 A complex number, similar to the C++ complex type.
32
33SourceFiles
34 complexI.H
35 complex.C
36
37\*---------------------------------------------------------------------------*/
38
39#ifndef Foam_primitives_complex_H
40#define Foam_primitives_complex_H
41
42#include "scalar.H"
43#include "word.H"
44#include "zero.H"
45#include "contiguous.H"
46#include <complex>
47
48// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49
50namespace Foam
51{
52
53// Forward Declarations
54class complex;
55
56inline complex operator-(const complex&);
57inline complex operator+(const complex&, const complex&);
58inline complex operator+(const complex&, const scalar);
59inline complex operator+(const scalar, const complex&);
60inline complex operator-(const complex&, const complex&);
61inline complex operator-(const complex&, const scalar);
62inline complex operator-(const scalar, const complex&);
63inline complex operator*(const complex&, const complex&);
64inline complex operator*(const complex&, const scalar);
65inline complex operator*(const scalar, const complex&);
66inline complex operator/(const complex&, const complex&);
67inline complex operator/(const complex&, const scalar);
68inline complex operator/(const scalar, const complex&);
69
71/*---------------------------------------------------------------------------*\
72 Class complex Declaration
73\*---------------------------------------------------------------------------*/
74
75class complex
76{
77 // Private Data
78
79 //- Real and imaginary parts
80 scalar re, im;
81
82
83public:
84
85 // Generated Methods
86
87 //- Copy construct
88 complex(const complex&) noexcept = default;
89
90 //- Copy assignment
91 complex& operator=(const complex&) noexcept = default;
93 //- Move construct
94 complex(complex&&) noexcept = default;
95
96 //- Move assignment
97 complex& operator=(complex&&) noexcept = default;
98
99
100 // Constructors
101
102 //- Default construct, as zero-initialized
103 inline constexpr complex() noexcept;
104
105 //- Construct zero-initialized from zero class
106 inline constexpr complex(Foam::zero) noexcept;
107
108 //- Construct from real component
109 inline explicit constexpr complex(const scalar r) noexcept;
110
111 //- Construct from real and imaginary parts
112 inline constexpr complex(const scalar r, const scalar i) noexcept;
113
114 //- Implicit construct from std::complex
115 inline complex(const std::complex<float>& c);
116
117 //- Implicit construct from std::complex
118 inline complex(const std::complex<double>& c);
119
120 //- Construct from Istream
121 explicit complex(Istream& is);
122
123
124 // Member Functions
125
126 // STL getter/setter
127
128 //- Real part of complex number - STL naming
129 constexpr scalar real() const noexcept { return re; }
130
131 //- Imaginary part of complex number - STL naming
132 constexpr scalar imag() const noexcept { return im; }
133
134 //- Set real part of complex number - STL naming
135 void real(scalar val) noexcept { re = val; }
136
137 //- Set imaginary part of complex number - STL naming
138 void imag(scalar val) noexcept { im = val; }
139
140
141 // Methods
142
143 //- Complex conjugate
144 inline complex conjugate() const;
145
146 //- The magnitude (L2-norm) of complex.
147 //- Called magnitude() instead mag(), which looks too much like imag()
148 inline scalar magnitude() const;
149
150 //- The L2-norm squared of complex
151 inline scalar magSqr() const;
152
153 //- The sum of real/imag components
154 inline scalar cmptSum() const noexcept;
156
157 // Member Operators
158
159 //- Implicit conversion to std::complex
160 operator std::complex<scalar>() const
161 {
162 return std::complex<scalar>(re, im);
163 }
164
166 //- Assign zero
167 inline void operator=(Foam::zero);
168
169 //- Assign scalar (imag = zero)
170 inline void operator=(const scalar s);
171
172 inline void operator+=(const complex& c);
173 inline void operator+=(const scalar s);
174
175 inline void operator-=(const complex& c);
176 inline void operator-=(const scalar s);
177
178 inline void operator*=(const complex& c);
179 inline void operator*=(const scalar s);
180
181 inline void operator/=(const complex& c);
182 inline void operator/=(const scalar s);
183
184 inline bool operator==(const complex& c) const;
185 inline bool operator!=(const complex& c) const;
186
187
188 // Friend Operators
189
190 friend complex operator-(const complex& c);
191
192 friend complex operator+(const complex& c1, const complex& c2);
193 friend complex operator+(const complex& c, const scalar s);
194 friend complex operator+(const scalar s, const complex& c);
195
196 friend complex operator-(const complex& c1, const complex& c2);
197 friend complex operator-(const complex& c, const scalar s);
198 friend complex operator-(const scalar s, const complex& c);
199
200 friend complex operator*(const complex& c1, const complex& c2);
201 friend complex operator*(const complex& c, const scalar s);
202 friend complex operator*(const scalar s, const complex& c);
203
204 friend complex operator/(const complex& c1, const complex& c2);
205 friend complex operator/(const complex& c, const scalar s);
206 friend complex operator/(const scalar s, const complex& c);
207
208
209 // Housekeeping
210
211 //- Get real part of complex number. Same as real()
212 scalar Re() const noexcept { return re; }
213
214 //- Get imaginary part of complex number. Same as imag()
215 scalar Im() const noexcept { return im; }
216
217 //- Non-const access to real part. Prefer real() setter method
218 scalar& Re() noexcept { return re; }
219
220 //- Non-const access to imaginary part. Prefer imag() setter method
221 scalar& Im() noexcept { return im; }
222};
223
224
225/*---------------------------------------------------------------------------*\
226 Specialization pTraits<complex>
227\*---------------------------------------------------------------------------*/
228
229//- The underlying component data type for complex is scalar.
230// The regular pTraits<T>:cmptType as complex is currently (2023-11)
231// likely not quite correct (issue #3018)
232template<>
233struct pTraits_cmptType<complex> { typedef scalar type; };
234
235//- A complex has two scalar components
236template<>
238:
239 std::integral_constant<Foam::direction, 2>
240{};
241
242
243//- Template specialisation for pTraits<complex>
244template<>
245class pTraits<complex>
246{
247 complex p_;
248
249public:
250
251 // Typedefs
252
253 //- Component type
254 typedef complex cmptType;
256 //- Magnitude type
257 typedef scalar magType;
258
259 //- Equivalent type of labels used for valid component indexing
260 typedef label labelType;
261
262
263 // Member Constants
264
265 //- Dimensionality of space
266 static constexpr direction dim = 3;
267
268 //- Rank of complex is 0
269 static constexpr direction rank = 0;
271 //- Number of components in complex is 2
272 static constexpr direction nComponents = 2;
273
274
275 // Static Data Members
276
277 static const char* const typeName;
278 static const char* const componentNames[];
279
280 static const complex zero;
281 static const complex one;
282 static const complex min;
283 static const complex max;
284 static const complex rootMin;
285 static const complex rootMax;
286
287
288 // Constructors
289
290 //- Copy construct from primitive
291 explicit pTraits(const complex& val) noexcept : p_(val) {}
292
293
294 //- Read construct from Istream
295 explicit pTraits(Istream& is);
296
297
298 // Member Functions
299
300 //- Return the value
301 operator complex() const noexcept { return p_; }
302
303 //- Access the value
304 operator complex&() noexcept { return p_; }
305};
306
307
308/*---------------------------------------------------------------------------*\
309 Namespace Detail
310\*---------------------------------------------------------------------------*/
311
312namespace Detail
313{
314 // Helper functions for complex, in Detail namespace to avoid possible
315 // name collisions (could change in the future)
316
317 //- The 'conjugate' of non-complex returns itself (pass-through)
318 //- it does not return a complex!
319 template<class T>
320 std::enable_if_t<!std::is_same_v<complex, T>, const T&>
321 conj(const T& val)
323 return val;
324 }
325
326 //- The conjugate of a complex number
327 template<class T>
328 std::enable_if_t<std::is_same_v<complex, T>, complex>
329 conj(const T& val)
331 return val.conjugate();
332 }
333
334} // End namespace Detail
336
337// * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
338
339//- Contiguous data for complex
340template<> struct is_contiguous<complex> : std::true_type {};
341
342//- Contiguous scalar data for complex
343template<> struct is_contiguous_scalar<complex> : std::true_type {};
344
346// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
347
351//- Complex conjugate
352inline complex operator~(const complex& c);
354
355// * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
356
357//- Return string representation of complex
358word name(const complex& c);
359
360
361// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
362
363} // End namespace Foam
364
365// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
366
367#include "complexI.H"
368
369// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
370
371#endif
372
373// ************************************************************************* //
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A complex number, similar to the C++ complex type.
Definition complex.H:71
friend complex operator-(const complex &c)
Definition complexI.H:279
complex(complex &&) noexcept=default
Move construct.
scalar & Im() noexcept
Non-const access to imaginary part. Prefer imag() setter method.
Definition complex.H:270
complex conjugate() const
Complex conjugate.
Definition complexI.H:68
scalar Im() const noexcept
Get imaginary part of complex number. Same as imag().
Definition complex.H:260
void real(scalar val) noexcept
Set real part of complex number - STL naming.
Definition complex.H:160
scalar & Re() noexcept
Non-const access to real part. Prefer real() setter method.
Definition complex.H:265
complex & operator=(const complex &) noexcept=default
Copy assignment.
void operator+=(const complex &c)
Definition complexI.H:108
friend complex operator+(const complex &c1, const complex &c2)
Definition complexI.H:285
scalar Re() const noexcept
Get real part of complex number. Same as real().
Definition complex.H:255
constexpr scalar real() const noexcept
Real part of complex number - STL naming.
Definition complex.H:150
friend complex operator*(const complex &c1, const complex &c2)
Definition complexI.H:329
constexpr complex() noexcept
Default construct, as zero-initialized.
Definition complexI.H:24
void imag(scalar val) noexcept
Set imaginary part of complex number - STL naming.
Definition complex.H:165
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
friend complex operator/(const complex &c1, const complex &c2)
Definition complexI.H:351
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 1 (one) that can be used to avoid manipulating objects known to b...
Definition one.H:57
static const char *const typeName
Definition complex.H:345
static const complex zero
complex (0,0)
Definition complex.H:348
static const complex rootMax
complex (ROOTVGREAT, ROOTVGREAT)
Definition complex.H:353
static const complex one
complex (1,0)
Definition complex.H:349
static const complex min
complex (-VGREAT,-VGREAT)
Definition complex.H:350
complex cmptType
Component type.
Definition complex.H:312
static const complex max
complex (VGREAT,VGREAT)
Definition complex.H:351
static const char *const componentNames[]
Definition complex.H:346
pTraits(const complex &val) noexcept
Copy construct from primitive.
Definition complex.H:361
static constexpr direction nComponents
Number of components in complex is 2.
Definition complex.H:340
static constexpr direction rank
Rank of complex is 0.
Definition complex.H:335
static constexpr direction dim
Dimensionality of space.
Definition complex.H:330
scalar magType
Magnitude type.
Definition complex.H:317
pTraits(Istream &is)
Read construct from Istream.
label labelType
Equivalent type of labels used for valid component indexing.
Definition complex.H:322
static const complex rootMin
complex (-ROOTVGREAT, -ROOTVGREAT)
Definition complex.H:352
A traits class, which is primarily used for primitives and vector-space.
Definition pTraits.H:64
pTraits(const Base &obj)
Copy construct from base class.
Definition pTraits.H:72
A class for handling words, derived from Foam::string.
Definition word.H:66
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
const volScalarField & T
OBJstream os(runTime.globalPath()/outputName)
auto & name
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))
Implementation details for various OpenFOAM classes.
Definition zoneSubSet.C:30
std::enable_if_t<!std::is_same_v< complex, T >, const T & > conj(const T &val)
The 'conjugate' of non-complex returns itself (pass-through) it does not return a complex!
Definition complex.H:399
@ complex
Definition Roots.H:55
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
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:40
tmp< faMatrix< Type > > operator+(const faMatrix< Type > &, const faMatrix< Type > &)
tmp< faMatrix< Type > > operator*(const areaScalarField::Internal &, const faMatrix< Type > &)
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
dimensionedScalar operator/(const scalar s1, const dimensionedScalar &ds2)
const word GlobalIOList< Tuple2< scalar, vector > >::typeName("scalarVectorTable")
Istream & operator>>(Istream &, directionInfo &)
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:26
uint8_t direction
Definition direction.H:49
const direction noexcept
Definition scalarImpl.H:265
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
A template class to specify if a data type is composed solely of Foam::scalar elements.
Definition contiguous.H:87
A template class to specify that a data type can be considered as being contiguous in memory.
Definition contiguous.H:70
The underlying component data type: default is pass-through.
Definition pTraits.H:116
The vector-space number of components: default is 1.
Definition pTraits.H:140