Commit 55e5a777 authored by shunbo's avatar shunbo
Browse files

initial commit

parents
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2014 OpenFOAM Foundation
Copyright (C) 2019-2020 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
Test-Tensor2D
Description
Tests for \c Tensor2D constructors, member functions and operators
using \c floatScalar, \c doubleScalar, and \c complex base types.
Eigen decomposition tests for \c tensor2D, i.e. Tensor2D<scalar>.
Cross-checks were obtained from 'NumPy 1.15.1' and 'SciPy 1.1.0' if no
theoretical cross-check exists (like eigendecomposition relations), and
were hard-coded for elementwise comparisons.
For \c complex base type, the cross-checks do only involve zero imag part.
\*---------------------------------------------------------------------------*/
#include "vector2DField.H"
#include "tensor2D.H"
#include "symmTensor2D.H"
#include "transform.H"
#include "Random.H"
#include "scalar.H"
#include "complex.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Total number of unit tests
unsigned nTest_ = 0;
// Total number of failed unit tests
unsigned nFail_ = 0;
// Create a random tensor2D
tensor2D makeRandomContainer(Random& rnd)
{
tensor2D A(Zero);
std::generate(A.begin(), A.end(), [&]{ return rnd.GaussNormal<scalar>(); });
return A;
}
// Compare two floating point types, and print output.
// Do ++nFail_ if values of two objects are not equal within a given tolerance.
// The function is converted from PEP-485.
template<class Type>
typename std::enable_if
<
std::is_same<floatScalar, Type>::value ||
std::is_same<doubleScalar, Type>::value ||
std::is_same<complex, Type>::value,
void
>::type cmp
(
const word& msg,
const Type& x,
const Type& y,
const scalar absTol = 0, //<! useful for cmps near zero
const scalar relTol = 1e-8 //<! are values the same within 8 decimals
)
{
Info<< msg << x << "?=" << y << endl;
unsigned nFail = 0;
if (max(absTol, relTol*max(mag(x), mag(y))) < mag(x - y))
{
++nFail;
}
if (nFail)
{
Info<< nl
<< " #### Fail in " << nFail << " comps ####" << nl << endl;
++nFail_;
}
++nTest_;
}
// Compare two containers elementwise, and print output.
// Do ++nFail_ if two components are not equal within a given tolerance.
// The function is converted from PEP-485
template<class Type>
typename std::enable_if
<
!std::is_same<floatScalar, Type>::value &&
!std::is_same<doubleScalar, Type>::value &&
!std::is_same<complex, Type>::value,
void
>::type cmp
(
const word& msg,
const Type& x,
const Type& y,
const scalar absTol = 0,
const scalar relTol = 1e-8
)
{
Info<< msg << x << "?=" << y << endl;
unsigned nFail = 0;
for (label i = 0; i < pTraits<Type>::nComponents; ++i)
{
if (max(absTol, relTol*max(mag(x[i]), mag(y[i]))) < mag(x[i] - y[i]))
{
++nFail;
}
}
if (nFail)
{
Info<< nl
<< " #### Fail in " << nFail << " comps ####" << nl << endl;
++nFail_;
}
++nTest_;
}
// Create each constructor of Tensor2D<Type>, and print output
template<class Type>
void test_constructors(Type)
{
{
Info<< "# Construct initialized to zero:" << nl;
const Tensor2D<Type> T(Zero);
Info<< T << endl;
}
{
Info<< "# Construct given VectorSpace:" << nl;
const VectorSpace<Tensor2D<Type>, Type, 4> V(Zero);
const Tensor2D<Type> T(V);
Info<< T << endl;
}
{
Info<< "# Construct given SymmTensor2D:" << nl;
const SymmTensor2D<Type> S
(
Type(1), Type(2),
Type(3)
);
const Tensor2D<Type> T(S);
Info<< T << endl;
}
{
Info<< "# Construct given SphericalTensor2D:" << nl;
const SphericalTensor2D<Type> Sp(Type(5));
const Tensor2D<Type> T(Sp);
Info<< T << endl;
}
{
Info<< "# Construct given the two row vectors:" << nl;
const Vector2D<Type> x(Type(1), Type(2));
const Vector2D<Type> y(Type(3), Type(4));
const Tensor2D<Type> T(x, y);
Info<< T << endl;
}
{
Info<< "# Construct given the four components:" << nl;
const Tensor2D<Type> T
(
Type(1), Type(2),
Type(3), Type(4)
);
Info<< T << endl;
}
{
Info<< "# Copy construct:" << nl;
const Tensor2D<Type> T(Zero);
const Tensor2D<Type> Tcopy(T);
Info<< T << endl;
}
}
// Execute each member function of Tensor2D<Type>, and print output
template<class Type>
void test_member_funcs(Type)
{
Tensor2D<Type> T
(
Type(1), Type(2),
Type(4), Type(5)
);
Tensor2D<Type> Tbak = T;
const Tensor2D<Type> cT
(
Type(-9), Type(8),
Type(-6), Type(5)
);
Info<< "# Operand: " << nl
<< " Tensor2D = " << T << endl;
{
Info<< "# Component access:" << nl;
Tensor2D<Type> cpT
(
T.xx(), T.xy(),
T.yx(), T.yy()
);
cmp(" 'Tensor2D' access:", T, cpT);
const Tensor2D<Type> cpcT
(
cT.xx(), cT.xy(),
cT.yx(), cT.yy()
);
cmp(" 'const Tensor2D' access:", cT, cpcT);
}
{
Info<< "# Column-vector access:" << nl;
cmp(" cx():", T.cx(), Vector2D<Type>(Type(1), Type(4)));
cmp(" cy():", T.cy(), Vector2D<Type>(Type(2), Type(5)));
cmp(" col(0):", T.col(0), Vector2D<Type>(Type(1), Type(4)));
cmp(" col(1):", T.col(1), Vector2D<Type>(Type(2), Type(5)));
cmp
(
" col<0>:",
T.template col<0>(),
Vector2D<Type>(Type(1), Type(4))
);
cmp
(
" col<1>:",
T.template col<1>(),
Vector2D<Type>(Type(2), Type(5))
);
// Compilation error: Info << " col<2> = " << T.col<2>() << nl;
Info<< "# Column-vector manipulation:" << nl;
T.col(1, Vector2D<Type>(Type(0), Type(1)));
cmp
(
" col(1, Vector):",
T.col(1),
Vector2D<Type>(Type(0), Type(1))
);
T.cols
(
Vector2D<Type>(Type(1), Type(1)),
Vector2D<Type>(Type(-1), Type(1))
);
cmp
(
" cols(Vectors):",
T,
Tensor2D<Type>
(
Type(1), Type(-1),
Type(1), Type(1)
)
);
}
{
Info<< "# Row-vector access:" << nl;
T = Tbak;
cmp(" x():", T.x(), Vector2D<Type>(Type(1), Type(2)));
cmp(" y():", T.y(), Vector2D<Type>(Type(4), Type(5)));
cmp(" row(0):", T.row(0), Vector2D<Type>(Type(1), Type(2)));
cmp(" row(1):", T.row(1), Vector2D<Type>(Type(4), Type(5)));
cmp
(
" row<0>:",
T.template row<0>(),
Vector2D<Type>(Type(1), Type(2))
);
cmp
(
" row<1>:",
T.template row<1>(),
Vector2D<Type>(Type(4), Type(5))
);
// Compilation error: Info << " row<2> = " << T.row<2>() << nl;
Info<< "# Row-vector manipulation:" << nl;
T.row(1, Vector2D<Type>(Type(0), Type(1)));
cmp
(
" row(1, Vector):",
T.row(1),
Vector2D<Type>(Type(0), Type(1))
);
T.rows
(
Vector2D<Type>(Type(1), Type(1)),
Vector2D<Type>(Type(-1), Type(1))
);
cmp
(
" rows(Vectors):",
T,
Tensor2D<Type>
(
Type(1), Type(1),
Type(-1), Type(1)
)
);
}
{
Info<< "# Diagonal access:" << nl;
T = Tbak;
cmp
(
" 'Tensor2D'.diag():",
T.diag(),
Vector2D<Type>(Type(1), Type(5))
);
cmp
(
" 'const Tensor2D'.diag():",
cT.diag(),
Vector2D<Type>(Type(-9), Type(5))
);
Info<< "# Diagonal manipulation:" << nl;
T.diag(Vector2D<Type>(Type(-10), Type(-15)));
cmp
(
" 'Tensor2D'.diag('Vector'):",
T.diag(),
Vector2D<Type>(Type(-10), Type(-15))
);
}
{
Info<< "# Tensor operations:" << nl;
T = Tbak;
cmp(" Transpose:", T, (T.T()).T());
cmp
(
" Inner-product:",
T.inner(T),
Tensor2D<Type>
(
Type(9), Type(12),
Type(24), Type(33)
)
);
cmp
(
" Schur-product:",
T.schur(T),
Tensor2D<Type>
(
Type(1), Type(4),
Type(16), Type(25)
)
);
}
{
Info<< "# Member operators:" << nl;
T = SphericalTensor2D<Type>(Type(5));
cmp
(
" Assign to a SphericalTensor2D:",
T,
Tensor2D<Type>
(
Type(5), Zero,
Zero, Type(5)
)
);
T = SymmTensor2D<Type>
(
Type(1), Type(2),
Type(5)
);
cmp
(
" Assign to a SymmTensor2D:",
T,
Tensor2D<Type>
(
Type(1), Type(2),
Type(2), Type(5)
)
);
}
}
// Execute each global function of Tensor2D<Type>, and print output
template<class Type>
void test_global_funcs(Type)
{
const Tensor2D<Type> T
(
Type(-1), Type(2),
Type(4), Type(5)
);
Info<< "# Operand: " << nl
<< " Tensor2D = " << T << endl;
cmp(" Trace = ", tr(T), Type(4));
cmp(" Spherical part = ", sph(T), SphericalTensor2D<Type>(tr(T)/Type(2)));
cmp
(
" Symmetric part = ",
symm(T),
SymmTensor2D<Type>
(
Type(-1), Type(3),
Type(5)
)
);
cmp
(
" Twice the symmetric part = ",
twoSymm(T),
SymmTensor2D<Type>
(
Type(-2), Type(6),
Type(10)
)
);
cmp
(
" Skew-symmetric part = ",
skew(T),
Tensor2D<Type>
(
Type(0), Type(-1),
Type(1), Type(0)
)
);
cmp
(
" Deviatoric part = ",
dev(T),
Tensor2D<Type>
(
Type(-3), Type(2),
Type(4), Type(3)
)
);
cmp(" Two-third deviatoric part = ", dev2(T), T - 2*sph(T));
cmp(" Determinant = ", det(T), Type(-13));
cmp
(
" Cofactor tensor2D = ",
cof(T),
Tensor2D<Type>
(
Type(5), Type(-4),
Type(-2), Type(-1)
)
);
cmp
(
" Inverse = ",
inv(T, det(T)),
Tensor2D<Type>
(
Type(-0.38461538), Type(0.15384615),
Type(0.30769231), Type(0.07692308)
),
1e-6,
1e-6
);
cmp
(
" Inverse (another) = ",
inv(T),
Tensor2D<Type>
(
Type(-0.38461538), Type(0.15384615),
Type(0.30769231), Type(0.07692308)
),
1e-6,
1e-6
);
cmp(" First invariant = ", invariantI(T), Type(4));
cmp(" Second invariant = ", invariantII(T), Type(-13));
}
// Execute each global operator of Tensor2D<Type>, and print output
template<class Type>
void test_global_opers(Type)
{
const Tensor2D<Type> T
(
Type(-1), Type(2),
Type(4), Type(5)
);
const SymmTensor2D<Type> sT
(
Type(1), Type(2),
Type(5)
);
const SphericalTensor2D<Type> spT(Type(1));
const Vector2D<Type> v(Type(3), Type(2));
const Type x(4);
Info<< "# Operands:" << nl
<< " Tensor2D = " << T << nl
<< " SymmTensor2D = " << sT << nl
<< " SphericalTensor2D = " << spT << nl
<< " Vector2D = " << v << nl
<< " Type = " << x << endl;
cmp
(
" Sum of SpTensor2D-Tensor2D = ",
(spT + T),
Tensor2D<Type>
(
Type(0), Type(2),
Type(4), Type(6)
)
);
cmp
(
" Sum of Tensor2D-SpTensor2D = ",
(T + spT),
Tensor2D<Type>
(
Type(0), Type(2),
Type(4), Type(6)
)
);
cmp
(
" Sum of SymmTensor2D-Tensor2D = ",
(sT + T),
Tensor2D<Type>
(
Type(0), Type(4),
Type(6), Type(10)
)
);
cmp
(
" Sum of Tensor2D-SymmTensor2D = ",
(T + sT),
Tensor2D<Type>
(
Type(0), Type(4),
Type(6), Type(10)
)
);
cmp
(
" Subtract Tensor2D from SpTensor2D = ",
(spT - T),
Tensor2D<Type>
(
Type(2), Type(-2),
Type(-4), Type(-4)
)
);
cmp
(
" Subtract SpTensor2D from Tensor2D = ",
(T - spT),
Tensor2D<Type>
(
Type(-2), Type(2),
Type(4), Type(4)
)
);
cmp
(
" Subtract Tensor2D from SymmTensor2D = ",
(sT - T),
Tensor2D<Type>
(
Type(2), Type(0),
Type(-2), Type(0)
)
);
cmp
(
" Subtract SymmTensor2D from Tensor2D = ",
(T - sT),
Tensor2D<Type>
(
Type(-2), Type(0),
Type(2), Type(0)
)
);
cmp
(
" Division of Tensor2D by Type = ",
(T/x),
Tensor2D<Type>
(
Type(-0.25), Type(0.5),
Type(1), Type(1.25)
)
);
cmp
(
" Inner-product of Tensor2D-Tensor2D = ",
(T & T),
Tensor2D<Type>
(
Type(9), Type(8),
Type(16), Type(33)
)
);
cmp
(
" Inner-product of SpTensor2D-Tensor2D = ",
(spT & T),
Tensor2D<Type>
(
Type(-1), Type(2),
Type(4), Type(5)
)
);
cmp
(
" Inner-product of Tensor2D-SpTensor2D = ",
(T & spT),
Tensor2D<Type>
(
Type(-1), Type(2),
Type(4), Type(5)
)
);
cmp
(
" Inner-product of SymmTensor2D-Tensor2D = ",
(sT & T),
Tensor2D<Type>
(
Type(7), Type(12),
Type(18), Type(29)
)
);
cmp
(
" Inner-product of Tensor2D-SymmTensor2D = ",
(T & sT),
Tensor2D<Type>
(
Type(3), Type(8),
Type(14), Type(33)
)
);
cmp
(
" Inner-product of Tensor2D-Vector2D = ",
(T & v),
Vector2D<Type>(Type(1), Type(22)) // Column-vector
);
cmp
(
" Inner-product of Vector2D-Tensor2D = ",
(v & T),
Vector2D<Type>(Type(5), Type(16)) // Row-vector
);
cmp(" D-inner-product of SpTensor2D-Tensor2D = ", (spT && T), Type(4));
cmp(" D-inner-product of Tensor2D-SpTensor2D = ", (T && spT), Type(4));
cmp(" D-inner-product of SymmTensor2D-Tensor2D = ", (sT && T), Type(36));
cmp(" D-inner-product of Tensor2D-SymmTensor2D = ", (T && sT), Type(36));
cmp
(
" Outer-product of Vector2D-Vector2D = ",
(v*v),
Tensor2D<Type>
(
Type(9), Type(6),
Type(6), Type(4)
)
);
}
// Return false if given eigenvalues fail to satisy eigenvalue relations
// Relations: (Beauregard & Fraleigh (1973), ISBN 0-395-14017-X, p. 307)
void test_eigenvalues(const tensor2D& T, const Vector2D<complex>& EVals)
{
{
const scalar determinant = det(T);
// In case of complex EVals, the production is effectively scalar
// due to the (complex*complex conjugate) results in zero imag part
const scalar EValsProd = ((EVals.x()*EVals.y()).real());
cmp("# Product of eigenvalues = det(T):", EValsProd, determinant, 1e-7);
}
{
const scalar trace = tr(T);
scalar EValsSum = 0.0;
// In case of complex EVals, the summation is effectively scalar
// due to the (complex+complex conjugate) results in zero imag part
for (const auto& val : EVals)
{
EValsSum += val.real();
}
cmp("# Sum of eigenvalues = trace(T):", EValsSum, trace, 1e-8);
}
}
// Return false if a given eigenvalue-eigenvector pair
// fails to satisfy the characteristic equation
void test_characteristic_equation
(
const tensor2D& T,
const Vector2D<complex>& EVals,
const Tensor2D<complex>& EVecs
)
{
Info<< "# Characteristic equation:" << nl;
Tensor2D<complex> Tc(Zero);
forAll(T, i)
{
Tc[i] = complex(T[i], 0);
}
for (direction dir = 0; dir < pTraits<vector2D>::nComponents; ++dir)
{
const Vector2D<complex> leftSide(Tc & EVecs.row(dir));
const Vector2D<complex> rightSide(EVals[dir]*EVecs.row(dir));
const Vector2D<complex> X(leftSide - rightSide);
for (const auto x : X)
{
cmp(" (Tc & EVec - EVal*EVec) = 0:", mag(x), 0.0, 1e-5);
}
}
}
// Return false if the eigen functions fail to satisfy relations
void test_eigen_funcs(const tensor2D& T)
{
Info<< "# Operand:" << nl
<< " tensor2D = " << T << nl;
Info<< "# Return eigenvalues of a given tensor2D:" << nl;
const Vector2D<complex> EVals(eigenValues(T));
Info<< EVals << endl;
test_eigenvalues(T, EVals);
Info<< "# Return an eigenvector of a given tensor2D in a given direction"
<< " corresponding to a given eigenvalue:" << nl;
const Vector2D<complex> standardBasis(pTraits<complex>::one, Zero);
const Vector2D<complex> EVec(eigenVector(T, EVals.x(), standardBasis));
Info<< EVec << endl;
Info<< "# Return eigenvectors of a given tensor2D corresponding to"
<< " given eigenvalues:" << nl;
const Tensor2D<complex> EVecs0(eigenVectors(T, EVals));
Info<< EVecs0 << endl;
test_characteristic_equation(T, EVals, EVecs0);
Info<< "# Return eigenvectors of a given tensor2D by computing"
<< " the eigenvalues of the tensor2D in the background:" << nl;
const Tensor2D<complex> EVecs1(eigenVectors(T));
Info<< EVecs1 << endl;
}
// Do compile-time recursion over the given types
template<std::size_t I = 0, typename... Tp>
inline typename std::enable_if<I == sizeof...(Tp), void>::type
run_tests(const std::tuple<Tp...>& types, const List<word>& typeID){}
template<std::size_t I = 0, typename... Tp>
inline typename std::enable_if<I < sizeof...(Tp), void>::type
run_tests(const std::tuple<Tp...>& types, const List<word>& typeID)
{
Info<< nl << " ## Test constructors: "<< typeID[I] <<" ##" << nl;
test_constructors(std::get<I>(types));
Info<< nl << " ## Test member functions: "<< typeID[I] <<" ##" << nl;
test_member_funcs(std::get<I>(types));
Info<< nl << " ## Test global functions: "<< typeID[I] << " ##" << nl;
test_global_funcs(std::get<I>(types));
Info<< nl << " ## Test global operators: "<< typeID[I] <<" ##" << nl;
test_global_opers(std::get<I>(types));
run_tests<I + 1, Tp...>(types, typeID);
}
// * * * * * * * * * * * * * * * Main Program * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
const std::tuple<floatScalar, doubleScalar, complex> types
(
std::make_tuple(Zero, Zero, Zero)
);
const List<word> typeID
({
"Tensor2D<floatScalar>",
"Tensor2D<doubleScalar>",
"Tensor2D<complex>"
});
run_tests(types, typeID);
Info<< nl << " ## Test tensor2D eigen functions: ##" << nl;
const label numberOfTests = 10000;
Random rndGen(1234);
for (label i = 0; i < numberOfTests; ++i)
{
const tensor2D T(makeRandomContainer(rndGen));
test_eigen_funcs(T);
}
{
Info<< nl << " ## A zero tensor2D: ##"<< nl;
const tensor2D zeroT(Zero);
test_eigen_funcs(zeroT);
}
{
Info<< nl
<< " ## A tensor2D with repeated eigenvalues: ##"
<< nl;
const tensor2D T
(
-1.0, 2.0,
0.0, -1.0
);
test_eigen_funcs(T);
}
{
Info<< nl
<< " ## A skew-symmetric tensor2D with no-real eigenvalues: ##"
<< nl;
const tensor2D T
(
0.0, 1.0,
-1.0, 0.0
);
test_eigen_funcs(T);
}
{
Info<< nl
<< " ## A stiff tensor2D: ##"
<< nl;
const tensor2D stiff
(
pow(10.0, 10), pow(10.0, 8),
pow(10.0, -8), pow(10.0, 9)
);
test_eigen_funcs(stiff);
}
{
Info<< nl
<< " ## Random tensor2D with tiny off-diag elements: ##"
<< nl;
const List<scalar> epsilons
({
0, SMALL, Foam::sqrt(SMALL), sqr(SMALL), Foam::cbrt(SMALL),
-SMALL, -Foam::sqrt(SMALL), -sqr(SMALL), -Foam::cbrt(SMALL)
});
for (label i = 0; i < numberOfTests; ++i)
{
for (const auto& eps : epsilons)
{
{
tensor2D T(makeRandomContainer(rndGen));
T.xy() = eps*rndGen.GaussNormal<scalar>();
test_eigen_funcs(T);
}
{
tensor2D T(makeRandomContainer(rndGen));
T.yx() = eps*rndGen.GaussNormal<scalar>();
test_eigen_funcs(T);
}
{
tensor2D T(makeRandomContainer(rndGen));
T.xy() = eps*rndGen.GaussNormal<scalar>();
T.yx() = eps*rndGen.GaussNormal<scalar>();
test_eigen_funcs(T);
}
{
tensor2D T(makeRandomContainer(rndGen));
T.xy() = 0;
T.yx() = eps*rndGen.GaussNormal<scalar>();
test_eigen_funcs(T);
}
{
tensor2D T(makeRandomContainer(rndGen));
T.xy() = eps*rndGen.GaussNormal<scalar>();
T.yx() = 0;
test_eigen_funcs(T);
}
{
tensor2D T(makeRandomContainer(rndGen));
T.xy() = eps;
test_eigen_funcs(T);
}
{
tensor2D T(makeRandomContainer(rndGen));
T.yx() = eps;
test_eigen_funcs(T);
}
{
tensor2D T(makeRandomContainer(rndGen));
T.xy() = eps;
T.yx() = eps;
test_eigen_funcs(T);
}
}
}
}
{
Info<< "# Pre-v2006 tests:" << nl;
vector2D v1(1, 2), v2(3, 4);
tensor2D t3 = v1*v2;
Info<< v1 << "*" << v2 << " = " << t3 << endl;
{
Info<< "rows:" << nl;
for (direction i = 0; i < 2; ++i)
{
Info<< " (" << i << ") = " << t3.row(i) << nl;
}
}
{
Info<< "cols:" << nl;
for (direction i = 0; i < 2; ++i)
{
Info<< " (" << i << ") = " << t3.col(i) << nl;
}
Info<< "col<0> = " << t3.col<0>() << nl;
Info<< "col<1> = " << t3.col<1>() << nl;
// Compilation error: Info << "col<3> = " << t3.col<3>() << nl;
t3.col<0>({0, 2});
Info<< "replaced col<0> = " << t3.col<0>() << nl;
Info<< "tensor " << t3 << nl;
t3.row<1>(Zero);
Info<< "replaced row<1> = " << t3.row<1>() << nl;
Info<< "tensor " << t3 << 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;
}
}
if (nFail_)
{
Info<< nl << " #### "
<< "Failed in " << nFail_ << " tests "
<< "out of total " << nTest_ << " tests "
<< "####\n" << endl;
return 1;
}
Info<< nl << " #### Passed all " << nTest_ <<" tests ####\n" << endl;
return 0;
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2020-2021 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/>.
Description
Various tools for test applications.
\*---------------------------------------------------------------------------*/
using namespace Foam;
#include "floatScalar.H"
#include "doubleScalar.H"
#include "complex.H"
#include "Matrix.H"
#include "Random.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Total number of unit tests
unsigned nTest_ = 0;
// Total number of failed unit tests
unsigned nFail_ = 0;
// Create a non-complex random Matrix.
template<class MatrixType>
typename std::enable_if
<
!std::is_same<complex, typename MatrixType::cmptType>:: value,
MatrixType
>::type makeRandomMatrix
(
const labelPair& dims,
Random& rndGen
)
{
MatrixType mat(dims);
std::generate
(
mat.begin(),
mat.end(),
[&]{return rndGen.GaussNormal<scalar>();}
);
return mat;
}
// Create a complex random Matrix.
template<class MatrixType>
typename std::enable_if
<
std::is_same<complex, typename MatrixType::cmptType>:: value,
MatrixType
>::type makeRandomMatrix
(
const labelPair& dims,
Random& rndGen
)
{
MatrixType mat(dims);
for (auto& x : mat)
{
x = complex(rndGen.GaussNormal<scalar>(), rndGen.GaussNormal<scalar>());
}
return mat;
}
// Copy an initializer list into a DiagonalMatrix
template<class Type>
void assignMatrix
(
UList<Type>& A,
std::initializer_list<Type> list
)
{
std::copy(list.begin(), list.end(), A.begin());
}
// Copy an initializer list into a SymmetricSquareMatrix.
template<class Form, class Type>
void assignMatrix
(
Matrix<Form, Type>& A,
std::initializer_list<typename Matrix<Form, Type>::cmptType> list
)
{
const label nargs = list.size();
if (nargs != A.size())
{
FatalErrorInFunction
<< "Mismatch in matrix dimension ("
<< A.m() << ", "
<< A.n() << ") and number of args (" << nargs << ')' << nl
<< exit(FatalError);
}
std::copy(list.begin(), list.end(), A.begin());
}
// Return a copy of the Matrix collapsed into one dimension.
template<class Form, class Type>
List<Type> flt
(
const Matrix<Form, Type>& A,
const bool rowMajorOrder = true
)
{
List<Type> flatMatrix(A.size());
if (rowMajorOrder)
{
std::copy(A.cbegin(), A.cend(), flatMatrix.begin());
}
else
{
for (label j = 0; j < A.n(); ++j)
{
for (label i = 0; i < A.m(); ++i)
{
flatMatrix[i + j*A.m()] = A(i, j);
}
}
}
return flatMatrix;
}
// Compare two floating point types, and print output.
// Do ++nFail_ if values of two objects are not equal within a given tolerance.
// The function is converted from PEP-485.
template<class Type>
typename std::enable_if
<
std::is_same<floatScalar, Type>::value ||
std::is_same<doubleScalar, Type>::value ||
std::is_same<complex, Type>::value,
void
>::type cmp
(
const word& msg,
const Type& x,
const Type& y,
const scalar absTol = 0, //<! useful for cmps near zero
const scalar relTol = 1e-8 //<! are values the same within 8 decimals
)
{
Info<< msg << x << "?=" << y << endl;
unsigned nFail = 0;
if (max(absTol, relTol*max(mag(x), mag(y))) < mag(x - y))
{
++nFail;
}
if (nFail)
{
Info<< nl
<< " #### Fail in " << nFail << " comps ####" << nl << endl;
++nFail_;
}
++nTest_;
}
// Compare two containers elementwise, and print output.
// Do ++nFail_ if two components are not equal within a given tolerance.
// The function is converted from PEP-485
template<class Type>
typename std::enable_if
<
!std::is_same<floatScalar, Type>::value &&
!std::is_same<doubleScalar, Type>::value &&
!std::is_same<complex, Type>::value,
void
>::type cmp
(
const word& msg,
const Type& x,
const Type& y,
const scalar absTol = 0,
const scalar relTol = 1e-8
)
{
Info<< msg << x << "?=" << y << endl;
unsigned nFail = 0;
for (label i = 0; i < x.size(); ++i)
{
if (max(absTol, relTol*max(mag(x[i]), mag(y[i]))) < mag(x[i] - y[i]))
{
++nFail;
}
}
if (nFail)
{
Info<< nl
<< " #### Fail in " << nFail << " comps ####" << nl << endl;
++nFail_;
}
++nTest_;
}
// Compare two containers elementwise, and print output.
// Do ++nFail_ if two components are not equal within a given tolerance.
// The function is converted from PEP-485
template<class Type1, class Type2>
typename std::enable_if
<
!std::is_same<floatScalar, Type1>::value &&
!std::is_same<doubleScalar, Type1>::value &&
!std::is_same<complex, Type1>::value,
void
>::type cmp
(
const word& msg,
const Type1& x,
const Type2& y,
const scalar absTol = 0,
const scalar relTol = 1e-8
)
{
Info<< msg << x << "?=" << y << endl;
unsigned nFail = 0;
for (label i = 0; i < x.size(); ++i)
{
if (max(absTol, relTol*max(mag(x[i]), mag(y[i]))) < mag(x[i] - y[i]))
{
++nFail;
}
}
if (nFail)
{
Info<< nl
<< " #### Fail in " << nFail << " comps ####" << nl << endl;
++nFail_;
}
++nTest_;
}
// Compare two Booleans, and print output.
// Do ++nFail_ if two Booleans are not equal.
void cmp
(
const word& msg,
const bool x,
const bool y
)
{
Info<< msg << x << "?=" << y << endl;
unsigned nFail = 0;
if (x != y)
{
++nFail;
}
if (nFail)
{
Info<< nl
<< " #### Fail in " << nFail << " comps ####" << nl << endl;
++nFail_;
}
++nTest_;
}
// ************************************************************************* //
Test-Tuple2.C
EXE = $(FOAM_USER_APPBIN)/Test-Tuple2
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation
Copyright (C) 2019-2020 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
Test-Tuple2
Description
Test construction, comparison etc for Tuple2 and Pair.
\*---------------------------------------------------------------------------*/
#include "labelPair.H"
#include "Tuple2.H"
#include "label.H"
#include "scalar.H"
#include "List.H"
#include "ListOps.H"
#include "ops.H"
#include "PstreamCombineReduceOps.H"
#include <functional>
using namespace Foam;
// Test for special comparison operation using compareOp
// Normal sort on label, reverse sort on scalar
struct special1
{
typedef Tuple2<label, scalar> type;
bool operator()(const type& a, const type& b) const
{
const label val = compareOp<label>()(a.first(), b.first());
return (val == 0) ? (b.second() < a.second()) : (val < 0);
}
};
// Test for special comparison operation using compareOp
// Normal sort on scalar, reverse sort on label
struct special2
{
typedef Tuple2<label, scalar> type;
bool operator()(const type& a, const type& b) const
{
const scalar val = compareOp<scalar>()(a.second(), b.second());
return (val == 0) ? (b.first() < a.first()) : (val < 0);
}
};
// Print content and info
void printTuple2(const word& f, const word& s)
{
Info<< '(' << f << ' ' << s << ") @ "
<< name(f.data()) << ','
<< name(s.data()) << nl;
}
// Print content and info
void printTuple2(const Tuple2<word, word>& t)
{
Info<< "tuple: " << t << " @ "
<< name(t.first().data()) << ','
<< name(t.second().data()) << nl;
}
// Print content and info
void printTuple2(const Pair<word>& t)
{
Info<< "tuple: " << t << " @ "
<< name(t.first().data()) << ','
<< name(t.second().data()) << nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main()
{
typedef Tuple2<label, scalar> indexedScalar;
Info<< "Default constructed Tuple: " << indexedScalar() << nl;
Info<< "Default constructed Pair: " << Pair<scalar>() << nl;
indexedScalar t2(1, 3.2);
Info<< "Foam::Tuple2: " << t2 << nl;
// As list. Generated so that we have duplicate indices
List<indexedScalar> list1(3*4);
for (label i = 0; i < 4; ++i)
{
const label j = (i+1);
const label idx = ((i % 2) ? -1 : 1) * (j);
list1[i] = indexedScalar(idx, (j*j));
list1[i+4] = indexedScalar(idx, 2*j); // duplicate index
list1[i+8] = indexedScalar(idx+12, 2*j); // duplicate value
}
Info<< "Unsorted tuples:" << nl << list1 << nl;
// Test minFirst, maxFirst functors
{
indexedScalar minIndexed(labelMax, Zero);
indexedScalar maxIndexed(labelMin, Zero);
for (const auto& item : list1)
{
minFirstEqOp<label>()(minIndexed, item);
maxFirstEqOp<label>()(maxIndexed, item);
}
Foam::combineReduce(minIndexed, minFirstEqOp<label>());
Foam::combineReduce(maxIndexed, maxFirstEqOp<label>());
Info<< "Min indexed: " << minIndexed << nl
<< "Max indexed: " << maxIndexed << nl;
}
// Test minFirst, maxFirst functors
{
indexedScalar minIndexed(labelMax, Zero);
indexedScalar maxIndexed(labelMin, Zero);
for (const auto& item : list1)
{
minIndexed = minFirstOp<label>()(minIndexed, item);
maxIndexed = maxFirstOp<label>()(maxIndexed, item);
}
Foam::combineReduce(minIndexed, minFirstEqOp<label>());
Foam::combineReduce(maxIndexed, maxFirstEqOp<label>());
Info<< "Min indexed: " << minIndexed << nl
<< "Max indexed: " << maxIndexed << nl;
}
Foam::sort(list1, std::less<indexedScalar>());
Info<< "sorted tuples:" << nl << list1 << nl;
Foam::sort(list1, std::greater<indexedScalar>());
Info<< "reverse sorted tuples:" << nl << list1 << nl;
Foam::sort(list1, special1());
Info<< "special sorted tuples - sort on index, reverse on value:"
<< nl << list1 << nl;
Foam::sort(list1, special2());
Info<< "special sorted tuples - sort on value, reverse on index:"
<< nl << list1 << nl;
{
Info<< nl << nl << "Foam::Pair" << nl;
typedef Pair<label> indexedLabel;
indexedLabel pr(1, 3);
Info<< "pair: "
<< pr << " => "
<< pr.first() << ' ' << pr.second() << nl;
List<indexedLabel> list2 = ListOps::create<indexedLabel>
(
list1,
[](const indexedScalar& t2)
{
return indexedLabel(t2.first(), t2.second());
}
);
Info<< "Unsorted pairs:" << nl << list2 << nl;
}
{
Info<< nl << nl << "std::pair" << nl;
typedef std::pair<label, label> indexedLabel;
indexedLabel pr(1, 3);
Info<< "pair: "
<< pr << " => "
<< pr.first << ' ' << pr.second << nl;
List<indexedLabel> list2 = ListOps::create<indexedLabel>
(
list1,
[](const indexedScalar& t2)
{
return indexedLabel(t2.first(), t2.second());
}
);
Info<< "Unsorted pairs:" << nl << list2 << nl;
}
{
word word1("hello");
word word2("word");
Info<< "create with ";
printTuple2(word1, word2);
Tuple2<word> tup(std::move(word2), std::move(word1));
printTuple2(tup);
Info<< "input is now ";
printTuple2(word1, word2);
}
{
word word1("hello");
word word2("word");
Info<< "create with ";
printTuple2(word1, word2);
Pair<word> tup(std::move(word2), std::move(word1));
printTuple2(tup);
Info<< "input is now ";
printTuple2(word1, word2);
}
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //
Test-UDictionary.C
EXE = $(FOAM_USER_APPBIN)/Test-UDictionary
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation
-------------------------------------------------------------------------------
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
\*---------------------------------------------------------------------------*/
#include "OSspecific.H"
#include "IOstreams.H"
#include "UDictionary.H"
using namespace Foam;
class ent
:
public UDictionary<ent>::link
{
word keyword_;
int i_;
public:
ent(const word& keyword, int i)
:
keyword_(keyword),
i_(i)
{}
const word& keyword() const
{
return keyword_;
}
friend Ostream& operator<<(Ostream& os, const ent& e)
{
os << e.keyword_ << ' ' << e.i_ << endl;
return os;
}
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
UDictionary<ent>* dictPtr = new UDictionary<ent>;
UDictionary<ent>& dict = *dictPtr;
for (int i = 0; i<10; i++)
{
ent* ePtr = new ent(word("ent") + name(i), i);
dict.append(ePtr->keyword(), ePtr);
dict.swapUp(ePtr);
}
Info<< dict << endl;
dict.swapDown(dict.first());
forAllConstIter(UDictionary<ent>, dict, iter)
{
Info<< "element : " << *iter;
}
Info<< dict.toc() << endl;
delete dictPtr;
dictPtr = new UDictionary<ent>;
UDictionary<ent>& dict2 = *dictPtr;
for (int i = 0; i<10; i++)
{
ent* ePtr = new ent(word("ent") + name(i), i);
dict2.append(ePtr->keyword(), ePtr);
dict2.swapUp(ePtr);
}
Info<< dict2 << endl;
dict2.remove("ent9");
dict2.UILList<DLListBase, ent>::remove(dict2.first());
Info<< dict2 << endl;
Info<< nl << "Testing transfer: " << nl << endl;
Info<< "original: " << dict2 << endl;
UDictionary<ent> newDict;
newDict.transfer(dict2);
Info<< nl << "source: " << dict2 << nl
<< "keys: " << dict2.toc() << nl
<< "target: " << newDict << nl
<< "keys: " << newDict.toc() << endl;
Info<< nl << "Done." << endl;
return 0;
}
// ************************************************************************* //
Test-UIListStream.C
EXE = $(FOAM_USER_APPBIN)/Test-UIListStream
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2018 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/>.
Description
\*---------------------------------------------------------------------------*/
#include "UListStream.H"
#include "wordList.H"
#include "IOstreams.H"
#include "argList.H"
#include <sstream>
#include <vector>
using namespace Foam;
Ostream& toString(Ostream& os, const UList<char>& list)
{
os << '"';
for (const char c : list)
{
os << c;
}
os << '"';
return os;
}
Ostream& toString(Ostream& os, const std::vector<char>& list)
{
os << '"';
for (const char c : list)
{
os << c;
}
os << '"';
return os;
}
template<class BufType>
void printInfo(const BufType& buf)
{
Info<< nl << "=========================" << endl;
buf.print(Info);
toString(Info, buf.list());
Info<< nl << "=========================" << endl;
}
template<>
void printInfo(const UList<char>& buf)
{
Info<< nl << "=========================" << endl;
toString(Info, buf);
Info<< nl << "=========================" << endl;
}
void printTokens(Istream& is)
{
label count = 0;
token t;
while (is.good())
{
is >> t;
if (t.good())
{
++count;
Info<<"token: " << t << endl;
}
}
Info<< count << " tokens" << endl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
// Buffer storage
DynamicList<char> storage(1000);
UOListStream obuf(storage);
obuf << 1002 << "\n" << "abcd" << "\n" << "def" << "\n" << 3.14159 << ";\n";
obuf.print(Info);
// Match size
storage.resize(obuf.size());
Info<<"as string: " << string(storage.cdata(), storage.size()) << endl;
// Attach input buffer - could also do without previous resize
{
UIListStream ibuf(storage);
printTokens(ibuf);
Info<< nl << "Repeat..." << endl;
ibuf.rewind();
printTokens(ibuf);
}
// Attach input buffer - could also do without previous resize
{
Info<< "parse as std::istream\n";
uiliststream is(storage.cdata(), storage.size());
string tok;
while (std::getline(is, tok))
{
std::cerr << "tok: " << tok << nl;
}
Info<< nl << "Repeat..." << endl;
is.rewind();
while (std::getline(is, tok))
{
std::cerr << "tok: " << tok << nl;
}
}
// Simple test using stl items only
{
std::vector<char> chars;
chars.reserve(1024);
for (int i=0; i < 10; ++i)
{
chars.push_back('A' + i);
chars.push_back('0' + i);
chars.push_back('\n');
}
Info<< "parse std::vector of char: ";
toString(Info, chars);
Info<< "----" << nl;
uiliststream is(chars.data(), chars.size());
string tok;
std::cerr<< nl << "Parsed..." << nl;
while (std::getline(is, tok))
{
std::cerr << "tok: " << tok << nl;
}
Info<< nl << "Repeat..." << endl;
is.rewind();
while (std::getline(is, tok))
{
std::cerr << "tok: " << tok << nl;
}
}
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //
Test-UIndirectList.C
EXE = $(FOAM_USER_APPBIN)/Test-UIndirectList
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation
-------------------------------------------------------------------------------
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/>.
Description
\*---------------------------------------------------------------------------*/
#include "UIndirectList.H"
#include "DynamicList.H"
#include "IOstreams.H"
#include "ListOps.H"
#include "labelIndList.H"
using namespace Foam;
template<class ListType>
void printInfo(const ListType& lst)
{
Info<< "addr: " << flatOutput(lst.addressing()) << nl
<< "list: " << flatOutput(lst) << nl
<< endl;
}
template<class T, class ListType>
void testFind(const T& val, const ListType& lst)
{
Info<< nl
<< "Search for "<< val << " in " << flatOutput(lst) << nl
<<" found() = " << lst.found(val)
<<" find() = " << lst.find(val)
<<" rfind() = " << lst.rfind(val)
<<" find(2) = " << lst.find(val, 2)
<<" rfind(2) = " << lst.rfind(val, 2) << nl
<< nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
labelList completeList(20);
labelList scratch(20);
scratch = -1;
forAll(completeList, i)
{
completeList[i] = 10*i;
}
Info<< "raw : " << flatOutput(completeList) << nl << endl;
List<label> addresses({ 1, 0, 3, 7, 4, 8, 5, 1, 0, 3, 7, 4, 8, 5 });
labelUIndList idl1(completeList, addresses);
printInfo(idl1);
labelList::subList slice(scratch, idl1.size());
slice = idl1;
Info<< "sliced: " << flatOutput(slice) << nl;
Info<< "scratch: " << flatOutput(scratch) << nl;
// Again, but offset and using intermediate only
scratch = -1;
labelList::subList(scratch, idl1.size(), 5) = idl1;
Info<< "offset: " << flatOutput(scratch) << nl;
for (const label val : { 10, 30, 40, 50, 90, 80, 120 } )
{
testFind(val, idl1);
}
Info<< flatOutput(idl1) << nl;
idl1[1] = -666;
Info<< "idl1[1] changed: " << flatOutput(idl1) << endl;
idl1 = -999;
Info<< "idl1 changed: " << flatOutput(idl1) << endl;
labelUIndList idl2(idl1);
Info<< "idl2: " << flatOutput(idl2) << endl;
{
List<label> ident(idl1.size());
forAll(ident, i)
{
ident[i] = ident.size() - i;
}
idl1 = ident;
}
Info<< "idl1 assigned from UList: " << flatOutput(idl1) << endl;
// test List operations
List<label> flatList(labelUIndList(completeList, addresses));
Info<< "List construct from UIndirectList: " << flatOutput(flatList) << nl;
flatList = labelUIndList(completeList, addresses);
Info<< "List assign from UIndirectList: " << flatOutput(flatList) << nl;
flatList.append(labelUIndList(completeList, addresses));
Info<< "List::append(UIndirectList): " << flatOutput(flatList) << nl;
DynamicList<label> dynList(labelUIndList(completeList, addresses));
Info<< "DynamicList construct from UIndirectList: " << flatOutput(dynList)
<< nl;
dynList.append(labelUIndList(completeList, addresses));
Info<< "DynamicList::append(UIndirectList): " << flatOutput(dynList) << nl;
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //
Test-UList.C
EXE = $(FOAM_USER_APPBIN)/Test-UList
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-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
Test-UList
Description
Simple tests for UList constructors
See also
Foam::List
\*---------------------------------------------------------------------------*/
#include "OSspecific.H"
#include "IOstreams.H"
#include "StringStream.H"
#include "labelList.H"
#include "ListOps.H"
#include "SubList.H"
#include "FlatOutput.H"
using namespace Foam;
template<class ListType>
void print(const ListType& list)
{
Info << flatOutput(list) << " data addr: " << name(list.cdata()) << nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
List<label> source = identity(7);
List<label> other = identity(7);
// Text for reading as a SLList"
string inputSLList("(10 20 30 40 50 60 70)");
// Text for reading as a SLList"
string inputCompound("List<label> (-1 -2 -3 -4 -5 -6 -7)");
reverse(other);
UList<label> ulist(source.data(), source.size());
Info<<"source: "; print(source);
Info<<"other: "; print(other);
Info<<"UList: "; print(ulist);
{
Info<<"shallow copy" << nl;
ulist.shallowCopy(other);
Info<<"source: "; print(source);
Info<<"other: "; print(other);
Info<<"UList: "; print(ulist);
}
{
Info<<"deep copy" << nl;
ulist.deepCopy(source);
Info<<"source: "; print(source);
Info<<"other: "; print(other);
Info<<"UList: "; print(ulist);
}
{
Info<<"Read from " << inputSLList << nl;
IStringStream is(inputSLList);
is >> ulist;
// Info<<"source: "; print(source);
// Info<<"other: "; print(other);
Info<<"UList: "; print(ulist);
}
{
Info<<"Read from " << inputCompound << nl;
IStringStream is(inputCompound);
is >> ulist;
// Info<<"source: "; print(source);
// Info<<"other: "; print(other);
Info<<"UList: "; print(ulist);
}
return 0;
}
// ************************************************************************* //
Test-UniformField.C
EXE = $(FOAM_USER_APPBIN)/Test-UniformField
/* EXE_INC = -I$(LIB_SRC)/finiteVolume/lnInclude */
/* EXE_LIBS = -lfiniteVolume */
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
Application
Test-UniformField
Description
Test uniform list/field
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "vector.H"
#include "IOstreams.H"
#include "UniformField.H"
using namespace Foam;
template<class T>
void printInfo(const UniformList<T>& list, const label i=0)
{
Info<< nl
<< "value: " << list.value() << nl
<< "cast: " << static_cast<const T&>(list) << nl
<< "[" << i << "] = " << list[i] << nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
{
UniformField<scalar> fld(3.14159);
printInfo(fld, -100);
// Change value
fld.value() *= 0.5;
Info<< nl << "/= 2 " << nl;
printInfo(fld, -100);
}
{
UniformField<vector> fld(vector(1, 2, -1));
printInfo(fld);
// Change value
fld.value() *= 0.5;
Info<< nl << "/= 2 " << nl;
printInfo(fld);
}
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //
Markdown is supported
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