diff --git a/applications/test/complex/Make/files b/applications/test/complex/Make/files new file mode 100644 index 0000000000000000000000000000000000000000..9b48d7cbaf227bc15369c08f23ac3b923dda1250 --- /dev/null +++ b/applications/test/complex/Make/files @@ -0,0 +1,3 @@ +Test-complex.C + +EXE = $(FOAM_USER_APPBIN)/Test-complex diff --git a/applications/test/complex/Make/options b/applications/test/complex/Make/options new file mode 100644 index 0000000000000000000000000000000000000000..18e6fe47afacb902cddccf82632772447704fd88 --- /dev/null +++ b/applications/test/complex/Make/options @@ -0,0 +1,2 @@ +/* EXE_INC = */ +/* EXE_LIBS = */ diff --git a/applications/test/complex/Test-complex.C b/applications/test/complex/Test-complex.C new file mode 100644 index 0000000000000000000000000000000000000000..0cf31f9c857b6627ff4b026f5638f0006059b5c9 --- /dev/null +++ b/applications/test/complex/Test-complex.C @@ -0,0 +1,93 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. + +Application + +Description + Some tests for complex numbers +\*---------------------------------------------------------------------------*/ + +#include "argList.H" +#include "complex.H" +#include "complexVector.H" +#include "Field.H" + +using namespace Foam; + +void print1(const complex& z) +{ + Info<<"r: " << z.real() << " i: " << z.imag() << nl; +} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + Info<< "complex() : " << complex() << nl + << "complex(zero) : " << complex(Zero) << nl + << "complex::zero : " << complex::zero << nl + << "complex::one : " << complex::one << nl + << "complex(scalar) : " << complex(3.14519) << nl + << nl; + + Info<< "complexVector::zero : " << complexVector::zero << nl + << "complexVector::one : " << complexVector::one << nl + << nl; + + // Comparison + + for (complex c : { complex{1, 0}, complex{1, 2}} ) + { + print1(c); + // TDB: allow implicit construct from scalar? + // + // if (c == 1.0) + // { + // Info<< c << " == " << 1 << nl; + // } + } + + Field<complex> fld1(3, complex(2.0, 1.0)); + + Info<< "Field " << flatOutput(fld1) << nl; + + Info<< "sum = " << sum(fld1) << nl; + // Not yet Info<< "min = " << min(fld1) << nl; + + fld1 *= 10; + Info<< "Multiply: " << flatOutput(fld1) << nl; + + for (complex& c : fld1) + { + c = ~c; + } + + Info<< "Conjugate: " << flatOutput(fld1) << nl; + + Info<< "\nEnd\n" << endl; + return 0; +} + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/Vector/complexVector/complexVector.C b/src/OpenFOAM/primitives/Vector/complexVector/complexVector.C index 1b43be5cc5ef5741504d8964704010dfa25add6d..bb99cf534b88ee7a579de77d5e4a834a5106f494 100644 --- a/src/OpenFOAM/primitives/Vector/complexVector/complexVector.C +++ b/src/OpenFOAM/primitives/Vector/complexVector/complexVector.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -44,13 +44,13 @@ const char* const Foam::complexVector::vsType::componentNames[] = template<> const Foam::complexVector Foam::complexVector::vsType::zero ( - complexVector::uniform(complex(0, 0)) + complexVector::uniform(complex::zero) ); template<> const Foam::complexVector Foam::complexVector::vsType::one ( - complexVector::uniform(complex(1, 1)) + complexVector::uniform(complex::one) ); template<> diff --git a/src/OpenFOAM/primitives/complex/complex.C b/src/OpenFOAM/primitives/complex/complex.C index 89aa30c14f9df864b7ada98a227d673b6895f026..a85f596fdccd0b336d80ea8f35aaea85eba2b077 100644 --- a/src/OpenFOAM/primitives/complex/complex.C +++ b/src/OpenFOAM/primitives/complex/complex.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011 OpenFOAM Foundation @@ -28,14 +28,13 @@ License #include "complex.H" #include "IOstreams.H" -#include <sstream> - // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // const char* const Foam::complex::typeName = "complex"; const Foam::complex Foam::complex::zero(0, 0); const Foam::complex Foam::complex::one(1, 1); + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::complex::complex(Istream& is) @@ -48,9 +47,7 @@ Foam::complex::complex(Istream& is) Foam::word Foam::name(const complex& c) { - std::ostringstream buf; - buf << '(' << c.Re() << ',' << c.Im() << ')'; - return buf.str(); + return '(' + std::to_string(c.Re()) + ',' + std::to_string(c.Im()) + ')'; } @@ -58,12 +55,10 @@ Foam::word Foam::name(const complex& c) Foam::Istream& Foam::operator>>(Istream& is, complex& c) { - // Read beginning of complex is.readBegin("complex"); is >> c.re >> c.im; - // Read end of complex is.readEnd("complex"); is.check(FUNCTION_NAME); diff --git a/src/OpenFOAM/primitives/complex/complex.H b/src/OpenFOAM/primitives/complex/complex.H index 0ffe362ed6cfd169b505045b078a57f6cf6798a2..c4b258a06ed42350ddaae2bb9d0372860c97cd82 100644 --- a/src/OpenFOAM/primitives/complex/complex.H +++ b/src/OpenFOAM/primitives/complex/complex.H @@ -2,10 +2,10 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- - | Copyright (C) 2011-2016 OpenFOAM Foundation + | Copyright (C) 2011-2014 OpenFOAM Foundation ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -27,7 +27,7 @@ Class Foam::complex Description - Extension to the c++ complex library type. + A complex number, similar to the C++ complex type. SourceFiles complexI.H @@ -39,8 +39,8 @@ SourceFiles #define complex_H #include "scalar.H" -#include "bool.H" #include "word.H" +#include "zero.H" #include "contiguous.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -48,15 +48,14 @@ SourceFiles namespace Foam { -// Forward declaration of friend functions and operators - +// Forward Declarations class complex; inline scalar magSqr(const complex&); -inline complex sqr(const complex&); inline scalar mag(const complex&); -inline const complex& max(const complex&, const complex&); +inline complex sqr(const complex&); inline const complex& min(const complex&, const complex&); +inline const complex& max(const complex&, const complex&); inline complex limit(const complex&, const complex&); inline const complex& sum(const complex&); inline complex operator+(const complex&, const complex&); @@ -68,8 +67,9 @@ inline complex operator*(const scalar, const complex&); inline complex operator*(const complex&, const scalar); inline complex operator/(const complex&, const scalar); inline complex operator/(const scalar, const complex&); -Istream& operator>>(Istream&, complex&); -Ostream& operator<<(Ostream&, const complex&); + +Istream& operator>>(Istream& is, complex& c); +Ostream& operator<<(Ostream& os, const complex& c); /*---------------------------------------------------------------------------*\ @@ -78,114 +78,166 @@ Ostream& operator<<(Ostream&, const complex&); class complex { - // private data + // Private Data - //- Real and imaginary parts of the complex number + //- Real and imaginary parts scalar re, im; + public: //- Component type typedef complex cmptType; - // Static data members + // Static Data Members + //- The type name is "complex" static const char* const typeName; + //- A complex zero (0,0) static const complex zero; + + //- A complex one (1,1) static const complex one; // Constructors - //- Construct null - inline complex(); + //- Construct null as zero-initialized + inline constexpr complex() noexcept; - //- Construct given real and imaginary parts - inline complex(const scalar Re, const scalar Im); + //- Default copy constructor + complex(const complex&) = default; + + //- Construct zero-initialized from zero class + inline constexpr complex(const Foam::zero) noexcept; + + //- Construct from real component + inline explicit constexpr complex(const scalar r) noexcept; + + //- Construct from real and imaginary parts + inline constexpr complex(const scalar r, const scalar i) noexcept; //- Construct from Istream - complex(Istream&); + explicit complex(Istream& is); + + + // Member Functions + + // STL getter/setter + + //- Real part of complex number - STL naming + constexpr scalar real() const + { + return re; + } + //- Imaginary part of complex number - STL naming + constexpr scalar imag() const + { + return im; + } - // Member functions + //- Set real part of complex number - STL naming + inline void real(scalar val); - // Access + //- Set imaginary part of complex number - STL naming + inline void imag(scalar val); - inline scalar Re() const; - inline scalar Im() const; - // Edit + // Access - inline scalar& Re(); - inline scalar& Im(); + //- Real part of complex number + inline scalar Re() const; - // Operators + //- Imaginary part of complex number + inline scalar Im() const; - inline complex conjugate() const; + // Edit - // Member operators + //- Real part of complex number + inline scalar& Re(); - inline void operator=(const complex&); - inline void operator+=(const complex&); - inline void operator-=(const complex&); - inline void operator*=(const complex&); - inline void operator/=(const complex&); + //- Imaginary part of complex number + inline scalar& Im(); - inline void operator=(const scalar); - inline void operator+=(const scalar); - inline void operator-=(const scalar); - inline void operator*=(const scalar); - inline void operator/=(const scalar); + // Operations + + //- Complex conjugate + inline complex conjugate() const; + + + // Member Operators + + //- Copy assignment + inline void operator=(const complex& c); + + //- Assign zero + inline void operator=(const Foam::zero); + + inline void operator+=(const complex& c); + inline void operator-=(const complex& c); + inline void operator*=(const complex& c); + inline void operator/=(const complex& c); + + inline void operator=(const scalar s); + inline void operator+=(const scalar s); + inline void operator-=(const scalar s); + inline void operator*=(const scalar s); + inline void operator/=(const scalar s); + + //- Conjugate + inline complex operator~() const; + + //- Conjugate inline complex operator!() const; - inline bool operator==(const complex&) const; - inline bool operator!=(const complex&) const; + inline bool operator==(const complex& c) const; + inline bool operator!=(const complex& c) const; - // Friend functions + // Friend Functions friend scalar magSqr(const complex& c); - friend complex sqr(const complex& c); friend scalar mag(const complex& c); - friend const complex& max(const complex&, const complex&); + friend complex sqr(const complex& c); friend const complex& min(const complex&, const complex&); + friend const complex& max(const complex&, const complex&); friend complex limit(const complex&, const complex&); friend const complex& sum(const complex&); - // Friend operators - - friend complex operator+(const complex&, const complex&); - friend complex operator-(const complex&); - friend complex operator-(const complex&, const complex&); - friend complex operator*(const complex&, const complex&); - friend complex operator/(const complex&, const complex&); + // Friend Operators - friend complex operator*(const scalar, const complex&); - friend complex operator*(const complex&, const scalar); - friend complex operator/(const complex&, const scalar); - friend complex operator/(const scalar, const complex&); + friend complex operator+(const complex& c1, const complex& c2); + friend complex operator-(const complex& c); + friend complex operator-(const complex& c1, const complex& c2); + friend complex operator*(const complex& c1, const complex& c2); + friend complex operator/(const complex& c1, const complex& c2); + friend complex operator*(const scalar s, const complex& c); + friend complex operator*(const complex& c, const scalar s); + friend complex operator/(const complex& c, const scalar s); + friend complex operator/(const scalar s, const complex& c); - // IOstream operators - friend Istream& operator>>(Istream&, complex&); - friend Ostream& operator<<(Ostream&, const complex&); + // IOstream Operators + friend Istream& operator>>(Istream& is, complex& c); + friend Ostream& operator<<(Ostream& os, const complex& c); }; -// * * * * * * * * * * * * * * Global functions * * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * // -//- Return a string representation of a complex -word name(const complex&); +//- Return string representation of complex +word name(const complex& c); //- Data associated with complex type are contiguous template<> diff --git a/src/OpenFOAM/primitives/complex/complexI.H b/src/OpenFOAM/primitives/complex/complexI.H index 346f7a192ab75b7dd0871ff29cd8c7d69c7c7571..7104ac84b7eb09fadae81d9cc1f4c0e9b8b938a0 100644 --- a/src/OpenFOAM/primitives/complex/complexI.H +++ b/src/OpenFOAM/primitives/complex/complexI.H @@ -2,10 +2,10 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- - | Copyright (C) 2011-2016 OpenFOAM Foundation + | Copyright (C) 2011-2014 OpenFOAM Foundation ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -25,51 +25,75 @@ License \*---------------------------------------------------------------------------*/ -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -namespace Foam -{ +inline constexpr Foam::complex::complex() noexcept +: + re(0), + im(0) +{} -// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -inline complex::complex() +inline constexpr Foam::complex::complex(const Foam::zero) noexcept +: + re(0), + im(0) +{} + + +inline constexpr Foam::complex::complex(const scalar r) noexcept +: + re(r), + im(0) {} -inline complex::complex(const scalar Re, const scalar Im) +inline constexpr Foam::complex::complex(const scalar r, const scalar i) noexcept : - re(Re), - im(Im) + re(r), + im(i) {} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -inline scalar complex::Re() const +inline void Foam::complex::real(scalar val) +{ + re = val; +} + + +inline void Foam::complex::imag(scalar val) +{ + im = val; +} + + +inline Foam::scalar Foam::complex::Re() const { return re; } -inline scalar complex::Im() const +inline Foam::scalar Foam::complex::Im() const { return im; } -inline scalar& complex::Re() +inline Foam::scalar& Foam::complex::Re() { return re; } -inline scalar& complex::Im() +inline Foam::scalar& Foam::complex::Im() { return im; } -inline complex complex::conjugate() const +inline Foam::complex Foam::complex::conjugate() const { return complex(re, -im); } @@ -77,85 +101,98 @@ inline complex complex::conjugate() const // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // -inline void complex::operator=(const complex& c) +inline void Foam::complex::operator=(const complex& c) { re = c.re; im = c.im; } -inline void complex::operator+=(const complex& c) +inline void Foam::complex::operator=(const Foam::zero) +{ + re = 0; + im = 0; +} + + +inline void Foam::complex::operator+=(const complex& c) { re += c.re; im += c.im; } -inline void complex::operator-=(const complex& c) +inline void Foam::complex::operator-=(const complex& c) { re -= c.re; im -= c.im; } -inline void complex::operator*=(const complex& c) +inline void Foam::complex::operator*=(const complex& c) { *this = (*this)*c; } -inline void complex::operator/=(const complex& c) +inline void Foam::complex::operator/=(const complex& c) { *this = *this/c; } -inline void complex::operator=(const scalar s) +inline void Foam::complex::operator=(const scalar s) { re = s; - im = 0.0; + im = 0; } -inline void complex::operator+=(const scalar s) +inline void Foam::complex::operator+=(const scalar s) { re += s; } -inline void complex::operator-=(const scalar s) +inline void Foam::complex::operator-=(const scalar s) { re -= s; } -inline void complex::operator*=(const scalar s) +inline void Foam::complex::operator*=(const scalar s) { re *= s; im *= s; } -inline void complex::operator/=(const scalar s) +inline void Foam::complex::operator/=(const scalar s) { re /= s; im /= s; } -inline complex complex::operator!() const +inline Foam::complex Foam::complex::operator~() const +{ + return conjugate(); +} + + +inline Foam::complex Foam::complex::operator!() const { return conjugate(); } -inline bool complex::operator==(const complex& c) const +inline bool Foam::complex::operator==(const complex& c) const { return (equal(re, c.re) && equal(im, c.im)); } -inline bool complex::operator!=(const complex& c) const +inline bool Foam::complex::operator!=(const complex& c) const { return !operator==(c); } @@ -164,47 +201,46 @@ inline bool complex::operator!=(const complex& c) const // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * // +namespace Foam +{ + inline scalar magSqr(const complex& c) { return (c.re*c.re + c.im*c.im); } -inline complex sqr(const complex& c) +inline scalar mag(const complex& c) { - return c * c; + return sqrt(magSqr(c)); // OR std::hypot(c.re, c.im); } -inline scalar mag(const complex& c) +inline complex sqr(const complex& c) { - return sqrt(magSqr(c)); + return c * c; } -inline const complex& max(const complex& c1, const complex& c2) +inline const complex& min(const complex& c1, const complex& c2) { - if (mag(c1) > mag(c2)) + if (magSqr(c1) < magSqr(c2)) { return c1; } - else - { - return c2; - } + + return c2; } -inline const complex& min(const complex& c1, const complex& c2) +inline const complex& max(const complex& c1, const complex& c2) { - if (mag(c1) < mag(c2)) - { - return c1; - } - else + if (magSqr(c1) < magSqr(c2)) { return c2; } + + return c1; } @@ -220,8 +256,7 @@ inline const complex& sum(const complex& c) } -template<class Cmpt> -class Tensor; +template<class Cmpt> class Tensor; inline complex transform(const Tensor<scalar>&, const complex c) { @@ -273,7 +308,7 @@ inline complex operator*(const complex& c1, const complex& c2) inline complex operator/(const complex& c1, const complex& c2) { - scalar sqrC2 = magSqr(c2); + const scalar sqrC2 = magSqr(c2); return complex ( diff --git a/src/randomProcesses/processes/UOprocess/UOprocess.C b/src/randomProcesses/processes/UOprocess/UOprocess.C index 67f3d07d132335100c5148bec61433c614a9ccd6..0df0a1491cf608fc349230bd89d370c8a16fe3da 100644 --- a/src/randomProcesses/processes/UOprocess/UOprocess.C +++ b/src/randomProcesses/processes/UOprocess/UOprocess.C @@ -51,7 +51,6 @@ complexVector UOprocess::WeinerProcess() // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -// from components UOprocess::UOprocess ( const Kmesh& kmesh, @@ -85,12 +84,7 @@ UOprocess::UOprocess } else { - UOfield[i] = complexVector - ( - complex(0, 0), - complex(0, 0), - complex(0, 0) - ); + UOfield[i] = complexVector::zero; } } }