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

ENH: add pTraits<complex> (#1247)

parent fe71bbd0
......@@ -30,6 +30,8 @@ Description
#include "argList.H"
#include "complexFields.H"
#include "ops.H"
#include "ListOps.H"
using namespace Foam;
......@@ -97,9 +99,6 @@ int main(int argc, char *argv[])
}
Info<< "sum = " << sum(fld1) << nl;
// Not yet Info<< "min = " << min(fld1) << nl;
fld1 *= 10;
Info<< "scalar multiply: " << flatOutput(fld1) << nl;
......@@ -120,6 +119,36 @@ int main(int argc, char *argv[])
// Info<< "pow(2) : " << pow(fld1, 2) << nl;
// Make some changes
{
label i = 1;
for (complex& c : fld1)
{
c.Re() += i;
c.Im() -= 10 - i;
++i;
}
}
Info<< nl
<< "field = " << fld1 << nl;
Info<< "magSqr = "
<< ListOps::create<scalar>
(
fld1,
[](const complex& c) { return magSqr(c); }
)
<< nl;
Info
<< "sum = " << sum(fld1) << nl
<< "min = " << min(fld1) << nl
<< "max = " << max(fld1) << nl;
// MinMax fails since there is no less comparison operator
// Info<< "min/max = " << MinMax<complex>(fld1) << nl;
Info<< "\nEnd\n" << endl;
return 0;
}
......
......@@ -32,6 +32,7 @@ Description
#include "HashOps.H"
#include "ListOps.H"
#include "scalarField.H"
#include "complexField.H"
#include "MinMax.H"
#include "dimensionedScalar.H"
......@@ -47,6 +48,19 @@ Ostream& printInfo(const MinMax<T>& range)
}
template<class T>
void testUniformField(const T& val)
{
constexpr label N = 10;
// Field<T> fld(N, val);
List<T> fld(N, val);
Info<< "field: " << fld << nl
<< "min/max: " << minMaxMag(fld) << nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
......@@ -225,6 +239,15 @@ int main(int argc, char *argv[])
Info<< "filtered: " << hashed << nl;
}
// Min/max of uniform fields
{
testUniformField<scalar>(100);
// testUniformField<complex>(complex(100, 0));
}
Info<< "\nEnd\n" << nl;
return 0;
}
......
......@@ -35,6 +35,41 @@ const Foam::complex Foam::complex::zero(0, 0);
const Foam::complex Foam::complex::one(1, 0);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
const char* const Foam::pTraits<Foam::complex>::typeName = "complex";
const char* const Foam::pTraits<Foam::complex>::componentNames[] = {"re", "im"};
const Foam::complex Foam::pTraits<Foam::complex>::zero(0, 0);
const Foam::complex Foam::pTraits<Foam::complex>::one(1, 0);
const Foam::complex Foam::pTraits<Foam::complex>::min(-VGREAT, -VGREAT);
const Foam::complex Foam::pTraits<Foam::complex>::max(VGREAT, VGREAT);
const Foam::complex Foam::pTraits<Foam::complex>::rootMin
(
-ROOTVGREAT, -ROOTVGREAT
);
const Foam::complex Foam::pTraits<Foam::complex>::rootMax
(
ROOTVGREAT, ROOTVGREAT
);
Foam::pTraits<Foam::complex>::pTraits(const complex& val)
:
p_(val)
{}
Foam::pTraits<Foam::complex>::pTraits(Istream& is)
{
is >> p_;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::complex::complex(Istream& is)
......
......@@ -232,6 +232,70 @@ public:
};
// Template specialisation for pTraits<complex>
template<>
class pTraits<complex>
{
complex p_;
public:
//- Component type
typedef complex cmptType;
//- Equivalent type of labels used for valid component indexing
typedef label labelType;
// Member constants
//- Dimensionality of space
static constexpr direction dim = 3;
//- Rank of complex is 0
static constexpr direction rank = 0;
//- Number of components in complex is 2
static constexpr direction nComponents = 2;
// Static Data Members
static const char* const typeName;
static const char* const componentNames[];
static const complex zero;
static const complex one;
static const complex max;
static const complex min;
static const complex rootMax;
static const complex rootMin;
// Constructors
//- Construct from primitive
explicit pTraits(const complex& val);
//- Construct from Istream
pTraits(Istream& is);
// Member Functions
//- Access to the value
operator complex() const
{
return p_;
}
//- Access to the value
operator complex&()
{
return p_;
}
};
// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
Istream& operator>>(Istream& is, complex& c);
......
......@@ -350,6 +350,7 @@ inline complex operator/(const scalar s, const complex& c)
return complex(s/c.re, s/c.im);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment