Commit a1999bc9 authored by Mark OLESEN's avatar Mark OLESEN Committed by Andrew Heather
Browse files

ENH: additional construct/assignment methods for complex (#1247)

- construct/assign from Zero
parent a33f1787
Test-complex.C
EXE = $(FOAM_USER_APPBIN)/Test-complex
/* EXE_INC = */
/* EXE_LIBS = */
/*---------------------------------------------------------------------------*\
========= |
\\ / 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;
}
// ************************************************************************* //
......@@ -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<>
......
......@@ -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);
......
......@@ -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<>
......
......@@ -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