mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: add zip/unzip functions for complexField and vector2DField
This commit is contained in:
committed by
Andrew Heather
parent
a23e8bf540
commit
39a1191bd5
@ -33,14 +33,15 @@ Description
|
||||
#include "argList.H"
|
||||
#include "complex.H"
|
||||
#include "complexFields.H"
|
||||
#include "ops.H"
|
||||
#include "scalarField.H"
|
||||
#include "ListOps.H"
|
||||
#include "ops.H"
|
||||
|
||||
using namespace Foam;
|
||||
|
||||
void print1(const complex& z)
|
||||
{
|
||||
Info<<"r: " << z.real() << " i: " << z.imag() << nl;
|
||||
Info<< "r: " << z.real() << " i: " << z.imag() << nl;
|
||||
}
|
||||
|
||||
|
||||
@ -82,6 +83,40 @@ int main(int argc, char *argv[])
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
// Test zip/unzip
|
||||
{
|
||||
scalarField reals(4);
|
||||
scalarField imags(4);
|
||||
|
||||
forAll(reals, i)
|
||||
{
|
||||
reals[i] = i;
|
||||
}
|
||||
forAll(imags, i)
|
||||
{
|
||||
imags[i] = (i % 2) ? -i : i;
|
||||
}
|
||||
|
||||
complexField cmplx(4);
|
||||
|
||||
zip(cmplx, reals, imags);
|
||||
Info<< nl
|
||||
<< "zip " << reals << nl
|
||||
<< " " << imags << nl
|
||||
<< " => " << cmplx << nl;
|
||||
|
||||
reverse(cmplx);
|
||||
|
||||
Info<< "reverse order: " << cmplx << nl;
|
||||
|
||||
unzip(cmplx, reals, imags);
|
||||
|
||||
Info<< "unzip " << cmplx << nl
|
||||
<< " => " << reals << nl
|
||||
<< " => " << imags << nl;
|
||||
}
|
||||
|
||||
complexField fld1(3, complex(2.0, 1.0));
|
||||
complexField fld2(fld1);
|
||||
|
||||
@ -90,8 +125,9 @@ int main(int argc, char *argv[])
|
||||
c = ~c;
|
||||
}
|
||||
|
||||
Info<< "Field " << flatOutput(fld1) << nl;
|
||||
Info<< "Conjugate: " << flatOutput(fld2) << nl;
|
||||
Info<< nl
|
||||
<< "Field " << flatOutput(fld1) << nl
|
||||
<< "Conjugate: " << flatOutput(fld2) << nl;
|
||||
|
||||
// Some arbitrary change
|
||||
for (complex& c : fld2)
|
||||
|
||||
@ -1,8 +1,43 @@
|
||||
/*---------------------------------------------------------------------------*\
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | www.openfoam.com
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2019 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
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
|
||||
Tests for tensor2D and vector2D
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "tensor2D.H"
|
||||
#include "vector2DField.H"
|
||||
#include "IOstreams.H"
|
||||
|
||||
using namespace Foam;
|
||||
|
||||
// * * * * * * * * * * * * * * * Main Program * * * * * * * * * * * * * * * //
|
||||
|
||||
int main()
|
||||
{
|
||||
vector2D v1(1, 2), v2(3, 4);
|
||||
@ -36,7 +71,37 @@ int main()
|
||||
Info<< "replaced row<1> = " << t3.row<1>() << nl;
|
||||
Info<< "tensor " << t3 << nl;
|
||||
}
|
||||
Info<< nl;
|
||||
|
||||
|
||||
{
|
||||
vector2DField vfld1(8, Zero);
|
||||
|
||||
forAll(vfld1, i)
|
||||
{
|
||||
vfld1[i] = (i+1) * ((i % 2) ? v1 : v2);
|
||||
}
|
||||
|
||||
Info<< "vector: " << flatOutput(vfld1) << nl;
|
||||
|
||||
scalarField xvals(8);
|
||||
scalarField yvals(8);
|
||||
unzip(vfld1, xvals, yvals);
|
||||
|
||||
Info<< "unzip" << nl
|
||||
<< " x => " << flatOutput(xvals) << nl
|
||||
<< " y => " << flatOutput(yvals) << nl;
|
||||
|
||||
reverse(xvals);
|
||||
zip(vfld1, xvals, yvals);
|
||||
|
||||
Info<< "rezip (with reversed x)" << nl
|
||||
<< " => " << flatOutput(vfld1) << nl;
|
||||
}
|
||||
|
||||
|
||||
Info<< nl << "End\n" << nl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ************************************************************************* //
|
||||
|
||||
@ -43,21 +43,75 @@ namespace Foam
|
||||
|
||||
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
|
||||
|
||||
void Foam::zip
|
||||
(
|
||||
complexField& result,
|
||||
const UList<scalar>& re,
|
||||
const UList<scalar>& im
|
||||
)
|
||||
{
|
||||
const label len = result.size();
|
||||
|
||||
#ifdef FULLDEBUG
|
||||
if (len != re.size() || len != im.size())
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "Components sizes do not match: " << len << " ("
|
||||
<< re.size() << ' ' << im.size() << ')'
|
||||
<< nl
|
||||
<< abort(FatalError);
|
||||
}
|
||||
#endif
|
||||
|
||||
for (label i=0; i < len; ++i)
|
||||
{
|
||||
result[i].Re() = re[i];
|
||||
result[i].Im() = im[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Foam::unzip
|
||||
(
|
||||
const UList<complex>& input,
|
||||
scalarField& re,
|
||||
scalarField& im
|
||||
)
|
||||
{
|
||||
const label len = input.size();
|
||||
|
||||
#ifdef FULLDEBUG
|
||||
if (len != re.size() || len != im.size())
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "Components sizes do not match: " << len << " ("
|
||||
<< re.size() << ' ' << im.size() << ')'
|
||||
<< nl
|
||||
<< abort(FatalError);
|
||||
}
|
||||
#endif
|
||||
|
||||
for (label i=0; i < len; ++i)
|
||||
{
|
||||
re[i] = input[i].Re();
|
||||
im[i] = input[i].Im();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
Foam::complexField Foam::ComplexField
|
||||
(
|
||||
const UList<scalar>& re,
|
||||
const UList<scalar>& im
|
||||
)
|
||||
{
|
||||
complexField cf(re.size());
|
||||
complexField result(re.size());
|
||||
|
||||
forAll(cf, i)
|
||||
{
|
||||
cf[i].Re() = re[i];
|
||||
cf[i].Im() = im[i];
|
||||
}
|
||||
Foam::zip(result, re, im);
|
||||
|
||||
return cf;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@ -68,7 +122,7 @@ Foam::complexField Foam::ReComplexField(const UList<scalar>& re)
|
||||
forAll(cf, i)
|
||||
{
|
||||
cf[i].Re() = re[i];
|
||||
cf[i].Im() = 0.0;
|
||||
cf[i].Im() = Zero;
|
||||
}
|
||||
|
||||
return cf;
|
||||
@ -81,7 +135,7 @@ Foam::complexField Foam::ImComplexField(const UList<scalar>& im)
|
||||
|
||||
forAll(cf, i)
|
||||
{
|
||||
cf[i].Re() = 0.0;
|
||||
cf[i].Re() = Zero;
|
||||
cf[i].Im() = im[i];
|
||||
}
|
||||
|
||||
|
||||
@ -53,6 +53,25 @@ namespace Foam
|
||||
|
||||
typedef Field<complex> complexField;
|
||||
|
||||
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
|
||||
|
||||
//- Zip together complex field from components
|
||||
void zip
|
||||
(
|
||||
complexField& result,
|
||||
const UList<scalar>& re,
|
||||
const UList<scalar>& im
|
||||
);
|
||||
|
||||
//- Unzip complex field into components
|
||||
void unzip
|
||||
(
|
||||
const UList<complex>& input,
|
||||
scalarField& re,
|
||||
scalarField& im
|
||||
);
|
||||
|
||||
|
||||
//- Zip up two lists of values into a list of complex
|
||||
complexField ComplexField
|
||||
(
|
||||
@ -75,6 +94,7 @@ scalarField Im(const UList<complex>& cf);
|
||||
//- Sum real and imag components
|
||||
scalarField ReImSum(const UList<complex>& cf);
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
//- Sum product
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2011-2017 OpenFOAM Foundation
|
||||
Copyright (C) 2019 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -26,8 +27,13 @@ License
|
||||
Description
|
||||
Foam::vector2DField
|
||||
|
||||
Note
|
||||
There is no 'zip(const scalarField& x, const scalarField& y)'
|
||||
function since it would not be easily distinguishable
|
||||
between vector2DField and complexField.
|
||||
|
||||
SourceFiles
|
||||
vector2DField.H
|
||||
vector2DFieldTemplates.C
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
@ -39,6 +45,39 @@ SourceFiles
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
namespace Foam
|
||||
{
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
//- Zip together 2D vector field from components
|
||||
template<class Cmpt>
|
||||
void zip
|
||||
(
|
||||
Field<Vector2D<Cmpt>>& result,
|
||||
const UList<Cmpt>& x,
|
||||
const UList<Cmpt>& y
|
||||
);
|
||||
|
||||
//- Unzip 2D vector field into components
|
||||
template<class Cmpt>
|
||||
void unzip
|
||||
(
|
||||
const UList<Vector2D<Cmpt>>& input,
|
||||
Field<Cmpt>& x,
|
||||
Field<Cmpt>& y
|
||||
);
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
} // End namespace Foam
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
#ifdef NoRepository
|
||||
#include "vector2DFieldTemplates.C"
|
||||
#endif
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
|
||||
@ -0,0 +1,93 @@
|
||||
/*---------------------------------------------------------------------------*\
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | www.openfoam.com
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2019 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
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/>.
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "vector2DField.H"
|
||||
|
||||
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
|
||||
|
||||
template<class Cmpt>
|
||||
void Foam::zip
|
||||
(
|
||||
Field<Vector2D<Cmpt>>& result,
|
||||
const UList<Cmpt>& x,
|
||||
const UList<Cmpt>& y
|
||||
)
|
||||
{
|
||||
typedef Vector2D<Cmpt> value_type;
|
||||
|
||||
const label len = result.size();
|
||||
|
||||
#ifdef FULLDEBUG
|
||||
if (len != x.size() || len != y.size())
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "Components sizes do not match: " << len << " ("
|
||||
<< x.size() << ' '
|
||||
<< y.size() << ')'
|
||||
<< nl
|
||||
<< abort(FatalError);
|
||||
}
|
||||
#endif
|
||||
|
||||
for (label i=0; i < len; ++i)
|
||||
{
|
||||
result[i] = value_type(x[i], y[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<class Cmpt>
|
||||
void Foam::unzip
|
||||
(
|
||||
const UList<Vector2D<Cmpt>>& input,
|
||||
Field<Cmpt>& x,
|
||||
Field<Cmpt>& y
|
||||
)
|
||||
{
|
||||
const label len = input.size();
|
||||
|
||||
#ifdef FULLDEBUG
|
||||
if (len != x.size() || len != y.size())
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "Components sizes do not match: " << len << " ("
|
||||
<< x.size() << ' '
|
||||
<< y.size() << ')'
|
||||
<< nl
|
||||
<< abort(FatalError);
|
||||
}
|
||||
#endif
|
||||
|
||||
for (label i=0; i < len; ++i)
|
||||
{
|
||||
x[i] = input[i].x();
|
||||
y[i] = input[i].y();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
Reference in New Issue
Block a user