mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: upgrade to use some C++17 constructs
- 'if constexpr (...)' * instead of std::enable_if * terminate template recursion * compile-time elimination of code - use C++14 '_t', '_v' versions, eg, std::is_integral_v<T> instead of std::is_integral<T>::value - std::begin, std::end, std::void_t instead of prev stdFoam versions - provide is_contiguous_v<..> as short form of is_contiguous<..>::value with the additional benefit of removing any cv qualifiers. ENH: include is_rotational_vectorspace trait - tests for vector-space and nComponents > 1 (ie, not sphericalTensor) ENH: improve robustness of pTraits_.. tests by removing cv qualifiers
This commit is contained in:
@ -5,7 +5,7 @@
|
||||
\\ / A nd | www.openfoam.com
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2020-2022 OpenCFD Ltd.
|
||||
Copyright (C) 2020-2025 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -40,11 +40,10 @@ Description
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "complex.H"
|
||||
#include "symmTensor.H"
|
||||
#include "transform.H"
|
||||
#include "Random.H"
|
||||
#include "scalar.H"
|
||||
#include "complex.H"
|
||||
|
||||
using namespace Foam;
|
||||
|
||||
@ -69,12 +68,12 @@ symmTensor makeRandomContainer(Random& rnd)
|
||||
|
||||
// Create a symmTensor based on a given value
|
||||
template<class Type>
|
||||
typename std::enable_if
|
||||
std::enable_if_t
|
||||
<
|
||||
std::is_same<floatScalar, Type>::value ||
|
||||
std::is_same<doubleScalar, Type>::value,
|
||||
std::is_floating_point_v<Type> || std::is_same_v<complex, Type>,
|
||||
symmTensor
|
||||
>::type makeContainer(const Type val)
|
||||
>
|
||||
makeContainer(const Type val)
|
||||
{
|
||||
symmTensor T(Zero);
|
||||
std::fill(T.begin(), T.end(), val);
|
||||
@ -82,12 +81,11 @@ typename std::enable_if
|
||||
}
|
||||
|
||||
|
||||
// Compare two floating point types, and print output.
|
||||
// Compare two containers elementwise, 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<pTraits<Type>::rank == 0, void>::type
|
||||
cmp
|
||||
void cmp
|
||||
(
|
||||
const word& msg,
|
||||
const Type& x,
|
||||
@ -96,51 +94,37 @@ cmp
|
||||
const scalar relTol = 1e-8 //<! are values the same within 8 decimals
|
||||
)
|
||||
{
|
||||
Info<< msg << x << "?=" << y << endl;
|
||||
const auto notEqual = [=](const auto& a, const auto& b) -> bool
|
||||
{
|
||||
return
|
||||
(
|
||||
Foam::max(absTol, relTol*Foam::max(Foam::mag(a), Foam::mag(b)))
|
||||
< Foam::mag(a - b)
|
||||
);
|
||||
};
|
||||
|
||||
unsigned nFail = 0;
|
||||
|
||||
if (max(absTol, relTol*max(mag(x), mag(y))) < mag(x - y))
|
||||
if constexpr (is_vectorspace_v<Type>)
|
||||
{
|
||||
++nFail;
|
||||
for (direction i = 0; i < pTraits<Type>::nComponents; ++i)
|
||||
{
|
||||
if (notEqual(x[i], y[i]))
|
||||
{
|
||||
++nFail;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (nFail)
|
||||
else
|
||||
{
|
||||
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<pTraits<Type>::rank != 0, 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 (direction i = 0; i < pTraits<Type>::nComponents; ++i)
|
||||
{
|
||||
if (max(absTol, relTol*max(mag(x[i]), mag(y[i]))) < mag(x[i] - y[i]))
|
||||
if (notEqual(x, y))
|
||||
{
|
||||
++nFail;
|
||||
}
|
||||
}
|
||||
|
||||
Info<< msg << x << "?=" << y << endl;
|
||||
|
||||
if (nFail)
|
||||
{
|
||||
Info<< nl
|
||||
@ -588,27 +572,26 @@ void test_eigen_funcs(const symmTensor& T)
|
||||
|
||||
// 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)
|
||||
void run_tests(const std::tuple<Tp...>& types, const List<word>& names)
|
||||
{
|
||||
Info<< nl << " ## Test constructors: "<< typeID[I] <<" ##" << nl;
|
||||
test_constructors(std::get<I>(types));
|
||||
if constexpr (I < sizeof...(Tp))
|
||||
{
|
||||
const auto& name = names[I];
|
||||
|
||||
Info<< nl << " ## Test member functions: "<< typeID[I] <<" ##" << nl;
|
||||
test_member_funcs(std::get<I>(types));
|
||||
Info<< nl << " ## Test constructors: " << name << " ##" << nl;
|
||||
test_constructors(std::get<I>(types));
|
||||
|
||||
Info<< nl << " ## Test global functions: "<< typeID[I] << " ##" << nl;
|
||||
test_global_funcs(std::get<I>(types));
|
||||
Info<< nl << " ## Test member functions: " << name << " ##" << nl;
|
||||
test_member_funcs(std::get<I>(types));
|
||||
|
||||
Info<< nl << " ## Test global operators: "<< typeID[I] <<" ##" << nl;
|
||||
test_global_opers(std::get<I>(types));
|
||||
Info<< nl << " ## Test global functions: " << name << " ##" << nl;
|
||||
test_global_funcs(std::get<I>(types));
|
||||
|
||||
run_tests<I + 1, Tp...>(types, typeID);
|
||||
Info<< nl << " ## Test global operators: " << name << " ##" << nl;
|
||||
test_global_opers(std::get<I>(types));
|
||||
|
||||
run_tests<I + 1, Tp...>(types, names);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -623,8 +606,8 @@ int main()
|
||||
|
||||
const List<word> typeID
|
||||
({
|
||||
"SymmTensor<floatScalar>",
|
||||
"SymmTensor<doubleScalar>",
|
||||
"SymmTensor<float>",
|
||||
"SymmTensor<double>",
|
||||
"SymmTensor<complex>"
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user