diff --git a/applications/test/complex/Test-complex.C b/applications/test/complex/Test-complex.C
index 06a419a333..4ca9cfe311 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 a3f0ab0411..a1847bdbc5 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 .
+
+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 79945b6d00..f24fafbf65 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 * * * * * * * * * * * * * //
+void Foam::zip
+(
+ complexField& result,
+ const UList& re,
+ const UList& 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& 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& re,
const UList& 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& 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& 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 2dcc7af406..70a2d44e90 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 complexField;
+// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
+
+//- Zip together complex field from components
+void zip
+(
+ complexField& result,
+ const UList& re,
+ const UList& im
+);
+
+//- Unzip complex field into components
+void unzip
+(
+ const UList& 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& cf);
//- Sum real and imag components
scalarField ReImSum(const UList& cf);
+
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Sum product
diff --git a/src/OpenFOAM/fields/Fields/vector2DField/vector2DField.H b/src/OpenFOAM/fields/Fields/vector2DField/vector2DField.H
index 4a0850dd03..75c0802aa7 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
+void zip
+(
+ Field>& result,
+ const UList& x,
+ const UList& y
+);
+
+//- Unzip 2D vector field into components
+template
+void unzip
+(
+ const UList>& input,
+ Field& x,
+ Field& 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 0000000000..082888899f
--- /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 .
+
+\*---------------------------------------------------------------------------*/
+
+#include "vector2DField.H"
+
+// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
+
+template
+void Foam::zip
+(
+ Field>& result,
+ const UList& x,
+ const UList& y
+)
+{
+ typedef Vector2D 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
+void Foam::unzip
+(
+ const UList>& input,
+ Field& x,
+ Field& 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();
+ }
+}
+
+
+// ************************************************************************* //