DNDSR 0.1.0.dev1+gcd065ad
Distributed Numeric Data Structure for CFV
Loading...
Searching...
No Matches
test_Scalar.cpp
Go to the documentation of this file.
1/**
2 * @file test_Scalar.cpp
3 * @brief Unit tests for BisectSolveLower in Solver/Scalar.hpp.
4 */
5
6#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
7#include "doctest.h"
8
9#include "Solver/Scalar.hpp"
10
11#include <cmath>
12
13using namespace DNDS;
14
15TEST_CASE("BisectSolveLower: x^2 = 4 => x = 2")
16{
17 auto F = [](double x) { return x * x; };
18 double v = Scalar::BisectSolveLower(F, 0.0, 3.0, 4.0, 100);
19 CHECK(v == doctest::Approx(2.0).epsilon(1e-10));
20}
21
22TEST_CASE("BisectSolveLower: sin(x) = 0.5 => x ~ 0.5236")
23{
24 auto F = [](double x) { return std::sin(x); };
25 double v = Scalar::BisectSolveLower(F, 0.0, 1.5, 0.5, 100);
26 double expected = std::asin(0.5);
27 CHECK(v == doctest::Approx(expected).epsilon(1e-10));
28}
29
30TEST_CASE("BisectSolveLower: linear f(x)=x, target=0.75")
31{
32 auto F = [](double x) { return x; };
33 double v = Scalar::BisectSolveLower(F, 0.0, 1.0, 0.75, 100);
34 CHECK(v == doctest::Approx(0.75).epsilon(1e-10));
35}
36
37TEST_CASE("BisectSolveLower: exp(x) = e => x = 1")
38{
39 auto F = [](double x) { return std::exp(x); };
40 double v = Scalar::BisectSolveLower(F, 0.0, 2.0, std::exp(1.0), 100);
41 CHECK(v == doctest::Approx(1.0).epsilon(1e-10));
42}
43
44TEST_CASE("BisectSolveLower: few iterations gives lower accuracy")
45{
46 auto F = [](double x) { return x * x; };
47 double v = Scalar::BisectSolveLower(F, 0.0, 3.0, 4.0, 5);
48 // 5 iterations: interval halves 5 times: 3/2^5 = 0.09375
49 CHECK(std::abs(v - 2.0) < 0.1);
50 CHECK(std::abs(v - 2.0) > 1e-6); // should not be exact with only 5 iters
51}
real BisectSolveLower(TF &&F, real v0, real v1, real fTarget, int maxIter)
Definition Scalar.hpp:10
the host side operators are provided as implemented
tVec x(NCells)
TEST_CASE("3D: VFV P2 HQM error < P1 on sinCos3D")
double v
double expected
CHECK(v==doctest::Approx(expected).epsilon(1e-10))