Skip to content

Commit

Permalink
Add plenty of rootfinding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dannys4 committed Sep 10, 2023
1 parent 52f9445 commit 85fead6
Showing 1 changed file with 47 additions and 12 deletions.
59 changes: 47 additions & 12 deletions tests/Test_RootFinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,74 @@ TEST_CASE( "RootFindingUtils", "[RootFindingUtils]") {
}
auto identity = [](double x){return x;};
auto sigmoid = [](double x){return 1./(std::exp(-x)+1);};
auto sigmoid_combo = [sigmoid](double x){return sigmoid(2*(x-1)) + 3*sigmoid(x*0.5) + 0.5*sigmoid(1.5*(x+1));};
SECTION("FindBound lower linear") {
double xd = -1.;
double xd = -1.1;
double yd = identity(xd);
double xub = 2., yub = 2.;
double xlb = 0., ylb = 0.;
FindBound<HostSpace>(false, yd, identity, xlb, ylb, xub, yub, 10'000);
FindBound<HostSpace>(false, yd, identity, xub, yub, xlb, ylb, 10'000);
CheckFoundBounds(identity, xlb, xd, xub, ylb, yd, yub);
}
SECTION("FindBound upper linear") {
double xd = 1.;
double xd = 1.1;
double yd = identity(xd);
double xub = 0., yub = 0.;
double xlb = -2., ylb = -2.;
FindBound<HostSpace>(true, yd, identity, xub, yub, xlb, ylb, 10'000);
FindBound<HostSpace>(true, yd, identity, xlb, ylb, xub, yub, 10'000);
CheckFoundBounds(identity, xlb, xd, xub, ylb, yd, yub);
}
SECTION("FindBound lower sigmoid") {
double xd = -0.5;
double yd = sigmoid(xd);
double xub = 2., yub = 2.;
double xlb = 0., ylb = 0.;
FindBound<HostSpace>(false, yd, sigmoid, xlb, ylb, xub, yub, 10'000);
double xub = 2., yub = sigmoid(xub);
double xlb = 0., ylb = sigmoid(xlb);
FindBound<HostSpace>(false, yd, sigmoid, xub, yub, xlb, ylb, 10'000);
CheckFoundBounds(sigmoid, xlb, xd, xub, ylb, yd, yub);
}
SECTION("FindBound upper sigmoid") {
double xd = 0.5;
double yd = sigmoid(xd);
double xub = 0., yub = 0.;
double xlb = -2., ylb = -2.;
FindBound<HostSpace>(true, yd, sigmoid, xub, yub, xlb, ylb, 10'000);
std::cout << "ylb = " << ylb << ", f(xlb) = " << sigmoid(xlb) << std::endl;
double xub = 0., yub = sigmoid(xub);
double xlb = -2., ylb = sigmoid(xlb);
FindBound<HostSpace>(true, yd, sigmoid, xlb, ylb, xub, yub, 10'000);
CheckFoundBounds(sigmoid, xlb, xd, xub, ylb, yd, yub);
}

SECTION("Test Inverse Linear, low x0") {
double xd = 0.5, yd = identity(xd);
double x0 = 0.0, xtol = 1e-5, ftol = 1e-5;
double xd_found = InverseSingleBracket<HostSpace>(yd, identity, x0, xtol, ftol);
CHECK( xd_found == Approx(xd).epsilon(2*xtol));
}
SECTION("Test Inverse Linear, high x0") {
double xd = 0.5, yd = identity(xd);
double x0 = 1.0, xtol = 1e-5, ftol = 1e-5;
double xd_found = InverseSingleBracket<HostSpace>(yd, identity, x0, xtol, ftol);
CHECK( xd_found == Approx(xd).epsilon(2*xtol));
}
SECTION("Test Inverse Sigmoid, low x0") {
double xd = 0.5, yd = sigmoid(xd);
double x0 = 0.0, xtol = 1e-5, ftol = 1e-5;
double xd_found = InverseSingleBracket<HostSpace>(yd, sigmoid, x0, xtol, ftol);
CHECK( xd_found == Approx(xd).epsilon(2*xtol));
}
SECTION("Test Inverse Sigmoid, high x0") {
double xd = 0.5, yd = sigmoid(xd);
double x0 = 1.0, xtol = 1e-5, ftol = 1e-5;
double xd_found = InverseSingleBracket<HostSpace>(yd, sigmoid, x0, xtol, ftol);
CHECK( xd_found == Approx(xd).epsilon(2*xtol));
}
SECTION("Test Inverse Sigmoid Combo, low x0") {
double xd = 0.5, yd = sigmoid_combo(xd);
double x0 = -5.0, xtol = 1e-5, ftol = 1e-5;
double xd_found = InverseSingleBracket<HostSpace>(yd, sigmoid_combo, x0, xtol, ftol);
CHECK( xd_found == Approx(xd).epsilon(2*xtol));
}
SECTION("Test Inverse Sigmoid Combo, high x0") {
double xd = 0.5, yd = sigmoid_combo(xd);
double x0 = 5.0, xtol = 1e-5, ftol = 1e-5;
double xd_found = InverseSingleBracket<HostSpace>(yd, sigmoid_combo, x0, xtol, ftol);
CHECK( xd_found == Approx(xd).epsilon(2*xtol));
}

}

0 comments on commit 85fead6

Please sign in to comment.