diff --git a/applications/test/complex/Test-complex.C b/applications/test/complex/Test-complex.C index 06a419a33303a2c25d5183dadf3246fcded00f24..4ca9cfe311153e094391fdf120dbbee9d98c69ed 100644 --- a/applications/test/complex/Test-complex.C +++ b/applications/test/complex/Test-complex.C @@ -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) diff --git a/applications/test/tensor2D/Test-tensor2D.C b/applications/test/tensor2D/Test-tensor2D.C index a3f0ab0411815dc4f8636d89fcf5a4c3ee9b0866..a1847bdbc5eab382b11c76dd254cbd92841a8b5b 100644 --- a/applications/test/tensor2D/Test-tensor2D.C +++ b/applications/test/tensor2D/Test-tensor2D.C @@ -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; } + +// ************************************************************************* // diff --git a/src/OpenFOAM/fields/Fields/complex/complexField.C b/src/OpenFOAM/fields/Fields/complex/complexField.C index 79945b6d00694ca5c29de23ee847afb259ed0da6..f24fafbf65805a41475790d4c049c44a392357cf 100644 --- a/src/OpenFOAM/fields/Fields/complex/complexField.C +++ b/src/OpenFOAM/fields/Fields/complex/complexField.C @@ -43,21 +43,75 @@ namespace Foam // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // -Foam::complexField Foam::ComplexField +void Foam::zip ( + complexField& result, const UList<scalar>& re, const UList<scalar>& im ) { - complexField cf(re.size()); + const label len = result.size(); - forAll(cf, i) + #ifdef FULLDEBUG + if (len != re.size() || len != im.size()) { - cf[i].Re() = re[i]; - cf[i].Im() = im[i]; + FatalErrorInFunction + << "Components sizes do not match: " << len << " (" + << re.size() << ' ' << im.size() << ')' + << nl + << abort(FatalError); } + #endif - return cf; + 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 result(re.size()); + + Foam::zip(result, re, im); + + 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]; } diff --git a/src/OpenFOAM/fields/Fields/complex/complexField.H b/src/OpenFOAM/fields/Fields/complex/complexField.H index 2dcc7af4066c9af9da027313d354dac5f9d900a4..70a2d44e905a4190abde585ee30bb3eb8a8452a1 100644 --- a/src/OpenFOAM/fields/Fields/complex/complexField.H +++ b/src/OpenFOAM/fields/Fields/complex/complexField.H @@ -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 diff --git a/src/OpenFOAM/fields/Fields/vector2DField/vector2DField.H b/src/OpenFOAM/fields/Fields/vector2DField/vector2DField.H index 4a0850dd03b844ed7457d549ed1dd790ef58bd72..75c0802aa72fe98ad83915b488d7cd433be6c95f 100644 --- a/src/OpenFOAM/fields/Fields/vector2DField/vector2DField.H +++ b/src/OpenFOAM/fields/Fields/vector2DField/vector2DField.H @@ -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 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/fields/Fields/vector2DField/vector2DFieldTemplates.C b/src/OpenFOAM/fields/Fields/vector2DField/vector2DFieldTemplates.C new file mode 100644 index 0000000000000000000000000000000000000000..082888899f73703ac8430fe585c89eb04ddcad42 --- /dev/null +++ b/src/OpenFOAM/fields/Fields/vector2DField/vector2DFieldTemplates.C @@ -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(); + } +} + + +// ************************************************************************* //