Skip to content
Snippets Groups Projects
Commit da888535 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add a few missing tests for math functions in DiscreteFunctionP0

parent 4440bdca
No related branches found
No related tags found
1 merge request!116Add tests for EmbeddedIDiscreteFunctionUtils
...@@ -2309,6 +2309,263 @@ TEST_CASE("DiscreteFunctionP0", "[scheme]") ...@@ -2309,6 +2309,263 @@ TEST_CASE("DiscreteFunctionP0", "[scheme]")
} }
} }
SECTION("math functions")
{
#define CHECK_STD_MATH_FUNCTION(data_expression, FCT) \
{ \
DiscreteFunctionP0 data = data_expression; \
DiscreteFunctionP0 result = FCT(data); \
bool is_same = true; \
parallel_for(data.cellValues().numberOfItems(), [&](const CellId cell_id) { \
if (result[cell_id] != std::FCT(data[cell_id])) { \
is_same = false; \
} \
}); \
REQUIRE(is_same); \
}
#define CHECK_STD_BINARY_MATH_FUNCTION(lhs_expression, rhs_expression, FCT) \
{ \
DiscreteFunctionP0 lhs = lhs_expression; \
DiscreteFunctionP0 rhs = rhs_expression; \
DiscreteFunctionP0 result = FCT(lhs, rhs); \
bool is_same = true; \
parallel_for(lhs.cellValues().numberOfItems(), [&](const CellId cell_id) { \
if (result[cell_id] != std::FCT(lhs[cell_id], rhs[cell_id])) { \
is_same = false; \
} \
}); \
REQUIRE(is_same); \
}
#define CHECK_STD_BINARY_MATH_FUNCTION_WITH_LHS_VALUE(lhs, rhs_expression, FCT) \
{ \
DiscreteFunctionP0 rhs = rhs_expression; \
DiscreteFunctionP0 result = FCT(lhs, rhs); \
bool is_same = true; \
parallel_for(rhs.cellValues().numberOfItems(), [&](const CellId cell_id) { \
if (result[cell_id] != std::FCT(lhs, rhs[cell_id])) { \
is_same = false; \
} \
}); \
REQUIRE(is_same); \
}
#define CHECK_STD_BINARY_MATH_FUNCTION_WITH_RHS_VALUE(lhs_expression, rhs, FCT) \
{ \
DiscreteFunctionP0 lhs = lhs_expression; \
DiscreteFunctionP0 result = FCT(lhs, rhs); \
bool is_same = true; \
parallel_for(lhs.cellValues().numberOfItems(), [&](const CellId cell_id) { \
if (result[cell_id] != std::FCT(lhs[cell_id], rhs)) { \
is_same = false; \
} \
}); \
REQUIRE(is_same); \
}
SECTION("1D")
{
std::shared_ptr mesh = MeshDataBaseForTests::get().cartesianMesh1D();
constexpr size_t Dimension = 1;
auto xj = MeshDataManager::instance().getMeshData(*mesh).xj();
DiscreteFunctionP0<Dimension, double> positive_function{mesh};
parallel_for(
mesh->numberOfCells(),
PUGS_LAMBDA(const CellId cell_id) { positive_function[cell_id] = 1 + std::abs(xj[cell_id][0]); });
const double min_value = min(positive_function);
SECTION("min")
{
double local_min = std::numeric_limits<double>::max();
for (CellId cell_id = 0; cell_id < mesh->numberOfCells(); ++cell_id) {
local_min = std::min(local_min, positive_function[cell_id]);
}
REQUIRE(min_value == parallel::allReduceMin(local_min));
}
const double max_value = max(positive_function);
SECTION("max")
{
double local_max = -std::numeric_limits<double>::max();
for (CellId cell_id = 0; cell_id < mesh->numberOfCells(); ++cell_id) {
local_max = std::max(local_max, positive_function[cell_id]);
}
REQUIRE(max_value == parallel::allReduceMax(local_max));
}
REQUIRE(min_value < max_value);
DiscreteFunctionP0 unsigned_function = positive_function - 0.5 * (min_value + max_value);
SECTION("sqrt")
{
CHECK_STD_MATH_FUNCTION(positive_function, sqrt);
}
SECTION("abs")
{
CHECK_STD_MATH_FUNCTION(positive_function, abs);
}
SECTION("cos")
{
CHECK_STD_MATH_FUNCTION(positive_function, cos);
}
SECTION("sin")
{
CHECK_STD_MATH_FUNCTION(positive_function, sin);
}
SECTION("tan")
{
CHECK_STD_MATH_FUNCTION(positive_function, tan);
}
DiscreteFunctionP0<Dimension, double> unit_function{mesh};
parallel_for(
mesh->numberOfCells(), PUGS_LAMBDA(const CellId cell_id) {
unit_function[cell_id] = (2 * (positive_function[cell_id] - min_value) / (max_value - min_value) - 1) * 0.95;
});
SECTION("acos")
{
CHECK_STD_MATH_FUNCTION(unit_function, acos);
}
SECTION("asin")
{
CHECK_STD_MATH_FUNCTION(unit_function, asin);
}
SECTION("atan")
{
CHECK_STD_MATH_FUNCTION(unit_function, atan);
}
SECTION("cosh")
{
CHECK_STD_MATH_FUNCTION(positive_function, cosh);
}
SECTION("sinh")
{
CHECK_STD_MATH_FUNCTION(positive_function, sinh);
}
SECTION("tanh")
{
CHECK_STD_MATH_FUNCTION(positive_function, tanh);
}
SECTION("acosh")
{
CHECK_STD_MATH_FUNCTION(positive_function, acosh);
}
SECTION("asinh")
{
CHECK_STD_MATH_FUNCTION(positive_function, asinh);
}
SECTION("atanh")
{
CHECK_STD_MATH_FUNCTION(unit_function, atanh);
}
SECTION("exp")
{
CHECK_STD_MATH_FUNCTION(positive_function, exp);
}
SECTION("log")
{
CHECK_STD_MATH_FUNCTION(positive_function, log);
}
SECTION("max(uh,hv)")
{
CHECK_STD_BINARY_MATH_FUNCTION(cos(positive_function), sin(positive_function), max);
}
SECTION("max(0.2,vh)")
{
CHECK_STD_BINARY_MATH_FUNCTION_WITH_LHS_VALUE(0.2, sin(positive_function), max);
}
SECTION("max(uh,0.2)")
{
CHECK_STD_BINARY_MATH_FUNCTION_WITH_RHS_VALUE(cos(positive_function), 0.2, max);
}
SECTION("atan2(uh,hv)")
{
CHECK_STD_BINARY_MATH_FUNCTION(positive_function, 2 + positive_function, atan2);
}
SECTION("atan2(0.5,uh)")
{
CHECK_STD_BINARY_MATH_FUNCTION_WITH_LHS_VALUE(0.5, 2 + positive_function, atan2);
}
SECTION("atan2(uh,0.2)")
{
CHECK_STD_BINARY_MATH_FUNCTION_WITH_RHS_VALUE(2 + cos(positive_function), 0.2, atan2);
}
SECTION("pow(uh,hv)")
{
CHECK_STD_BINARY_MATH_FUNCTION(positive_function, 0.5 * positive_function, pow);
}
SECTION("pow(uh,0.5)")
{
CHECK_STD_BINARY_MATH_FUNCTION_WITH_LHS_VALUE(0.5, positive_function, pow);
}
SECTION("pow(uh,0.2)")
{
CHECK_STD_BINARY_MATH_FUNCTION_WITH_RHS_VALUE(positive_function, 1.3, pow);
}
SECTION("min(uh,hv)")
{
CHECK_STD_BINARY_MATH_FUNCTION(sin(positive_function), cos(positive_function), min);
}
SECTION("min(uh,0.5)")
{
CHECK_STD_BINARY_MATH_FUNCTION_WITH_LHS_VALUE(0.5, cos(positive_function), min);
}
SECTION("min(uh,0.2)")
{
CHECK_STD_BINARY_MATH_FUNCTION_WITH_RHS_VALUE(sin(positive_function), 0.5, min);
}
SECTION("max(uh,hv)")
{
CHECK_STD_BINARY_MATH_FUNCTION(sin(positive_function), cos(positive_function), max);
}
SECTION("min(uh,0.5)")
{
CHECK_STD_BINARY_MATH_FUNCTION_WITH_LHS_VALUE(0.1, cos(positive_function), max);
}
SECTION("min(uh,0.2)")
{
CHECK_STD_BINARY_MATH_FUNCTION_WITH_RHS_VALUE(sin(positive_function), 0.1, max);
}
}
}
#ifndef NDEBUG #ifndef NDEBUG
SECTION("error") SECTION("error")
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment