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-2020 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 Class
28  Foam::complex
29 
30 Description
31  A complex number, similar to the C++ complex type.
32 
33 SourceFiles
34  complexI.H
35  complex.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef complex_H
40 #define complex_H
41 
42 #include "scalar.H"
43 #include "word.H"
44 #include "zero.H"
45 #include "contiguous.H"
46 #include <complex>
47 #include <type_traits>
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 namespace Foam
52 {
53 
54 // Forward Declarations
55 class complex;
56 
57 inline scalar magSqr(const complex&);
58 inline scalar mag(const complex&);
59 inline complex sqr(const complex&);
60 inline const complex& min(const complex&, const complex&);
61 inline const complex& max(const complex&, const complex&);
62 inline complex limit(const complex&, const complex&);
63 inline const complex& sum(const complex&);
64 inline complex operator-(const complex&);
65 inline complex operator+(const complex&, const complex&);
66 inline complex operator+(const complex&, const scalar);
67 inline complex operator+(const scalar, const complex&);
68 inline complex operator-(const complex&, const complex&);
69 inline complex operator-(const complex&, const scalar);
70 inline complex operator-(const scalar, const complex&);
71 inline complex operator*(const complex&, const complex&);
72 inline complex operator*(const complex&, const scalar);
73 inline complex operator*(const scalar, const complex&);
74 inline complex operator/(const complex&, const complex&);
75 inline complex operator/(const complex&, const scalar);
76 inline complex operator/(const scalar, const complex&);
77 
78 
79 /*---------------------------------------------------------------------------*\
80  Class complex Declaration
81 \*---------------------------------------------------------------------------*/
82 
83 class complex
84 {
85  // Private Data
86 
87  //- Real and imaginary parts
88  scalar re, im;
89 
90 
91 public:
92 
93  // Generated Methods
94 
95  //- Copy construct
96  complex(const complex&) = default;
97 
98  //- Copy assignment
99  complex& operator=(const complex&) = default;
100 
101  //- Move construct
102  complex(complex&&) = default;
103 
104  //- Move assignment
105  complex& operator=(complex&&) = default;
106 
107 
108  // Constructors
109 
110  //- Default construct, as zero-initialized
111  inline constexpr complex() noexcept;
112 
113  //- Construct zero-initialized from zero class
114  inline constexpr complex(const Foam::zero) noexcept;
115 
116  //- Construct from real component
117  inline explicit constexpr complex(const scalar r) noexcept;
118 
119  //- Construct from real and imaginary parts
120  inline constexpr complex(const scalar r, const scalar i) noexcept;
121 
122  //- Implicit construct from std::complex
123  inline complex(const std::complex<float>& c);
124 
125  //- Implicit construct from std::complex
126  inline complex(const std::complex<double>& c);
127 
128  //- Construct from Istream
129  explicit complex(Istream& is);
130 
131 
132  // Member Functions
133 
134  // STL getter/setter
135 
136  //- Real part of complex number - STL naming
137  constexpr scalar real() const
138  {
139  return re;
140  }
141 
142  //- Imaginary part of complex number - STL naming
143  constexpr scalar imag() const
144  {
145  return im;
146  }
147 
148  //- Set real part of complex number - STL naming
149  inline void real(scalar val);
150 
151  //- Set imaginary part of complex number - STL naming
152  inline void imag(scalar val);
153 
154 
155  // Access
156 
157  //- Real part of complex number
158  inline scalar Re() const;
159 
160  //- Imaginary part of complex number
161  inline scalar Im() const;
162 
163 
164  // Edit
165 
166  //- Real part of complex number
167  inline scalar& Re();
168 
169  //- Imaginary part of complex number
170  inline scalar& Im();
171 
172 
173  // Operations
174 
175  //- Complex conjugate
176  inline complex conjugate() const;
177 
178 
179  // Member Operators
180 
181  //- Implicit conversion to std::complex
182  operator std::complex<scalar>() const
183  {
184  return std::complex<scalar>(re, im);
185  }
186 
187 
188  //- Assign zero
189  inline void operator=(const Foam::zero);
190 
191  //- Assign scalar (imag = zero)
192  inline void operator=(const scalar s);
193 
194  inline void operator+=(const complex& c);
195  inline void operator+=(const scalar s);
196 
197  inline void operator-=(const complex& c);
198  inline void operator-=(const scalar s);
199 
200  inline void operator*=(const complex& c);
201  inline void operator*=(const scalar s);
202 
203  inline void operator/=(const complex& c);
204  inline void operator/=(const scalar s);
205 
206  inline bool operator==(const complex& c) const;
207  inline bool operator!=(const complex& c) const;
208 
209 
210  // Friend Functions
211 
212  friend scalar magSqr(const complex& c);
213  friend scalar mag(const complex& c);
214  friend complex sqr(const complex& c);
215 
216  //- sgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
217  friend complex sign(const complex& c);
218 
219  //- csgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
220  friend scalar csign(const complex& c);
221 
222  friend const complex& min(const complex& c1, const complex& c2);
223  friend const complex& max(const complex& c1, const complex& c2);
224  friend complex limit(const complex& c1, const complex& c2);
225  friend const complex& sum(const complex& c);
226 
227 
228  // Friend Operators
229 
230  friend complex operator-(const complex& c);
231 
232  friend complex operator+(const complex& c1, const complex& c2);
233  friend complex operator+(const complex& c, const scalar s);
234  friend complex operator+(const scalar s, const complex& c);
235 
236  friend complex operator-(const complex& c1, const complex& c2);
237  friend complex operator-(const complex& c, const scalar s);
238  friend complex operator-(const scalar s, const complex& c);
239 
240  friend complex operator*(const complex& c1, const complex& c2);
241  friend complex operator*(const complex& c, const scalar s);
242  friend complex operator*(const scalar s, const complex& c);
243 
244  friend complex operator/(const complex& c1, const complex& c2);
245  friend complex operator/(const complex& c, const scalar s);
246  friend complex operator/(const scalar s, const complex& c);
247 };
248 
249 
250 /*---------------------------------------------------------------------------*\
251  Class pTraits<complex> Declaration
252 \*---------------------------------------------------------------------------*/
253 
254 // Template specialisation for pTraits<complex>
255 template<>
256 class pTraits<complex>
257 {
258  complex p_;
259 
260 public:
261 
262  // Typedefs
263 
264  //- Component type
265  typedef complex cmptType;
266 
267  //- Magnitude type
268  typedef scalar magType;
269 
270  //- Equivalent type of labels used for valid component indexing
271  typedef label labelType;
272 
273 
274  // Member Constants
275 
276  //- Dimensionality of space
277  static constexpr direction dim = 3;
278 
279  //- Rank of complex is 0
280  static constexpr direction rank = 0;
281 
282  //- Number of components in complex is 2
283  static constexpr direction nComponents = 2;
284 
285 
286  // Static Data Members
287 
288  static const char* const typeName;
289  static const char* const componentNames[];
290 
291  static const complex zero;
292  static const complex one;
293  static const complex min;
294  static const complex max;
295  static const complex rootMin;
296  static const complex rootMax;
297 
298 
299  // Constructors
300 
301  //- Copy construct from primitive
302  explicit pTraits(const complex& val);
303 
304  //- Read construct from Istream
305  explicit pTraits(Istream& is);
306 
307 
308  // Member Functions
309 
310  //- Access to the value
311  operator complex() const
312  {
313  return p_;
314  }
315 
316  //- Access to the value
317  operator complex&()
318  {
319  return p_;
320  }
321 };
322 
323 
324 /*---------------------------------------------------------------------------*\
325  Namespace Detail
326 \*---------------------------------------------------------------------------*/
327 
328 namespace Detail
329 {
330  // Helper functions for complex, in Detail namespace to avoid possible
331  // name collisions (could change in the future)
332 
333  //- The 'conjugate' of non-complex returns itself (pass-through)
334  //- it does not return a complex!
335  template<class T>
336  typename std::enable_if
337  <
338  !std::is_same<complex, T>::value,
339  const T&
340  >::type conj(const T& val)
341  {
342  return val;
343  }
344 
345  //- The conjugate of a complex number
346  template<class T>
347  typename std::enable_if
348  <
349  std::is_same<complex, T>::value,
351  >::type conj(const T& val)
352  {
353  return val.conjugate();
354  }
355 
356 } // End namespace Detail
357 
358 
359 // * * * * * * * * * * * * * * * * * Traits * * * * * * * * * * * * * * * * //
360 
361 //- Contiguous data for complex
362 template<> struct is_contiguous<complex> : std::true_type {};
363 
364 //- Contiguous scalar data for complex
365 template<> struct is_contiguous_scalar<complex> : std::true_type {};
366 
367 
368 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
369 
371 Ostream& operator<<(Ostream& os, const complex& c);
372 
373 //- Complex conjugate
374 inline complex operator~(const complex& c);
375 
376 
377 // * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
378 
379 //- Return string representation of complex
380 word name(const complex& c);
381 
382 
383 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
384 
385 } // End namespace Foam
386 
387 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
388 
389 #include "complexI.H"
390 
391 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
392 
393 #endif
394 
395 // ************************************************************************* //
Foam::complex::limit
friend complex limit(const complex &c1, const complex &c2)
Definition: complexI.H:263
Foam::pTraits< complex >::zero
static const complex zero
complex (0,0)
Definition: complex.H:290
Foam::pTraits::pTraits
pTraits(const Base &obj)
Copy construct from base class.
Definition: pTraits.H:65
Foam::pTraits< complex >::min
static const complex min
complex (-VGREAT,-VGREAT)
Definition: complex.H:292
Foam::roots::complex
Definition: Roots.H:57
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::complex::operator+
friend complex operator+(const complex &c1, const complex &c2)
Definition: complexI.H:291
Foam::complex::operator-=
void operator-=(const complex &c)
Definition: complexI.H:146
Foam::complex::operator==
bool operator==(const complex &c) const
Definition: complexI.H:185
s
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))
Definition: gmvOutputSpray.H:25
Foam::complex::mag
friend scalar mag(const complex &c)
Definition: complexI.H:216
Foam::complex::operator!=
bool operator!=(const complex &c) const
Definition: complexI.H:191
Foam::complex::imag
constexpr scalar imag() const
Imaginary part of complex number - STL naming.
Definition: complex.H:142
Foam::pTraits< complex >::magType
scalar magType
Magnitude type.
Definition: complex.H:267
Foam::pTraits< complex >::labelType
label labelType
Equivalent type of labels used for valid component indexing.
Definition: complex.H:270
Foam::complex::sign
friend complex sign(const complex &c)
sgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
Definition: complexI.H:228
Foam::operator-
tmp< faMatrix< Type > > operator-(const faMatrix< Type > &)
Foam::one
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:61
Foam::complex::conjugate
complex conjugate() const
Complex conjugate.
Definition: complexI.H:111
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::complex::csign
friend scalar csign(const complex &c)
csgn() https://en.wikipedia.org/wiki/Sign_function#Complex_signum
Definition: complexI.H:235
Foam::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
Foam::magSqr
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
complexI.H
Foam::complex::operator*=
void operator*=(const complex &c)
Definition: complexI.H:159
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::constant::physicoChemical::c1
const dimensionedScalar c1
First radiation constant: default SI units: [W/m2].
Foam::complex::operator-
friend complex operator-(const complex &c)
Definition: complexI.H:285
Foam::complex::operator*
friend complex operator*(const complex &c1, const complex &c2)
Definition: complexI.H:335
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::complex::sum
const friend complex & sum(const complex &c)
Definition: complexI.H:269
Foam::pTraits< complex >::one
static const complex one
complex (1,0)
Definition: complex.H:291
Foam::complex::operator+=
void operator+=(const complex &c)
Definition: complexI.H:133
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
zero.H
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::complex::sqr
friend complex sqr(const complex &c)
Definition: complexI.H:222
scalar.H
os
OBJstream os(runTime.globalPath()/outputName)
Foam::complex::real
constexpr scalar real() const
Real part of complex number - STL naming.
Definition: complex.H:136
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::complex
A complex number, similar to the C++ complex type.
Definition: complex.H:82
Foam::constant::physicoChemical::c2
const dimensionedScalar c2
Second radiation constant: default SI units: [m.K].
Foam::pTraits< complex >::rootMax
static const complex rootMax
complex (ROOTVGREAT, ROOTVGREAT)
Definition: complex.H:295
Foam::is_contiguous_scalar
A template class to specify if a data type is composed solely of Foam::scalar elements.
Definition: contiguous.H:91
Foam::complex::max
const friend complex & max(const complex &c1, const complex &c2)
Definition: complexI.H:252
Foam::operator/
dimensionedScalar operator/(const scalar s1, const dimensionedScalar &ds2)
Definition: dimensionedScalar.C:68
Foam::pTraits< complex >::max
static const complex max
complex (VGREAT,VGREAT)
Definition: complex.H:293
Foam::operator~
bitSet operator~(const bitSet &bitset)
Bitset complement, returns a copy of the bitset with all its bits flipped.
Definition: bitSetI.H:746
Foam::pTraits< complex >::typeName
static const char *const typeName
Definition: complex.H:287
Foam::sqr
dimensionedSymmTensor sqr(const dimensionedVector &dv)
Definition: dimensionedSymmTensor.C:51
Foam::complex::operator=
complex & operator=(const complex &)=default
Copy assignment.
Foam::Detail::conj
std::enable_if< !std::is_same< complex, T >::value, const T & >::type conj(const T &val)
Definition: complex.H:339
contiguous.H
Foam::pTraits
A traits class, which is primarily used for primitives.
Definition: pTraits.H:56
Foam::operator+
tmp< faMatrix< Type > > operator+(const faMatrix< Type > &, const faMatrix< Type > &)
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::operator*
tmp< faMatrix< Type > > operator*(const areaScalarField &, const faMatrix< Type > &)
Foam::limit
complex limit(const complex &, const complex &)
Definition: complexI.H:263
Foam::sum
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
Definition: DimensionedFieldFunctions.C:327
Foam::direction
uint8_t direction
Definition: direction.H:52
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::complex::magSqr
friend scalar magSqr(const complex &c)
Definition: complexI.H:210
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
word.H
Foam::complex::min
const friend complex & min(const complex &c1, const complex &c2)
Definition: complexI.H:241
Foam::pTraits< complex >::cmptType
complex cmptType
Component type.
Definition: complex.H:264
Foam::complex::complex
constexpr complex() noexcept
Default construct, as zero-initialized.
Definition: complexI.H:31
Foam::complex::Re
scalar Re() const
Real part of complex number.
Definition: complexI.H:87
Foam::pTraits< complex >::rootMin
static const complex rootMin
complex (-ROOTVGREAT, -ROOTVGREAT)
Definition: complex.H:294
Foam::complex::operator/=
void operator/=(const complex &c)
Definition: complexI.H:172
Foam::complex::operator/
friend complex operator/(const complex &c1, const complex &c2)
Definition: complexI.H:357
Foam::complex::Im
scalar Im() const
Imaginary part of complex number.
Definition: complexI.H:93
Foam::is_contiguous
A template class to specify that a data type can be considered as being contiguous in memory.
Definition: contiguous.H:75
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62