diff --git a/test/common/geometry/CMakeLists.txt b/test/common/geometry/CMakeLists.txt index ae96d62fc480d20f3ec458f0744270e00643dd51..d6fb00cb610ac724531ef6311484431b8b0c0f29 100644 --- a/test/common/geometry/CMakeLists.txt +++ b/test/common/geometry/CMakeLists.txt @@ -1,3 +1,4 @@ +dune_add_test(SOURCES test_0d1d_intersection.cc LABELS unit) dune_add_test(SOURCES test_1d3d_intersection.cc LABELS unit) dune_add_test(SOURCES test_1d2d_intersection.cc LABELS unit) dune_add_test(SOURCES test_2d3d_intersection.cc LABELS unit) diff --git a/test/common/geometry/test_0d1d_intersection.cc b/test/common/geometry/test_0d1d_intersection.cc new file mode 100644 index 0000000000000000000000000000000000000000..73e1a15f3f2bf747d199a2e7b7326b0f13acfd4d --- /dev/null +++ b/test/common/geometry/test_0d1d_intersection.cc @@ -0,0 +1,91 @@ +#include <config.h> + +#include <iostream> +#include <algorithm> +#include <initializer_list> + +#include <dune/common/exceptions.hh> +#include <dune/common/parallel/mpihelper.hh> +#include <dune/common/fvector.hh> + +#include <dumux/common/geometry/intersectspointgeometry.hh> + +#ifndef DOXYGEN +template<int dimworld = 3> +bool testIntersection(const Dune::FieldVector<double, dimworld>& a, + const Dune::FieldVector<double, dimworld>& b, + const Dune::FieldVector<double, dimworld>& p, + bool foundExpected = false) +{ + bool found = Dumux::intersectsPointSimplex(p, a, b); + if (!found && foundExpected) + std::cerr << "Failed detecting intersection with " << p << std::endl; + else if (found && foundExpected) + std::cout << "Found intersection with " << p << std::endl; + else if (found && !foundExpected) + std::cerr << "Found false positive: intersection with " << p << std::endl; + else if (!found && !foundExpected) + std::cout << "No intersection with " << p << std::endl; + return (found == foundExpected); +} + +template<int dimWorld> +void testIntersections(std::vector<bool>& returns) +{ + // test if points lie on 3d segments + using GlobalPosition = Dune::FieldVector<double, dimWorld>; + + for (auto scaling : {1.0, 1e3, 1e12, 1e-12}) + { + const GlobalPosition a(0.0); + auto b = a; + b[dimWorld-1] = 1.0*scaling; + + GlobalPosition p1 = a; + GlobalPosition p2 = b; + GlobalPosition p3 = a + b; + p3 /= 2.0; + GlobalPosition p4 = b + (b - a); + GlobalPosition p5 = a - (b - a); + GlobalPosition p6(0.5*scaling); + + returns.push_back(testIntersection(a, b, p1, true)); + returns.push_back(testIntersection(a, b, p2, true)); + returns.push_back(testIntersection(a, b, p3, true)); + returns.push_back(testIntersection(a, b, p4)); + returns.push_back(testIntersection(a, b, p5)); + returns.push_back(testIntersection(a, b, p6)); + } +} + +#endif + +int main (int argc, char *argv[]) try +{ + // maybe initialize mpi + Dune::MPIHelper::instance(argc, argv); + + // collect returns to determine exit code + std::vector<bool> returns; + + // test for dimWorld = 2 + testIntersections<2>(returns); + + // test for dimWorld = 3 + testIntersections<3>(returns); + + // determine the exit code + if (std::any_of(returns.begin(), returns.end(), [](bool i){ return !i; })) + return 1; + + std::cout << "All tests passed!" << std::endl; + + return 0; +} +// ////////////////////////////////// +// Error handler +// ///////////////////////////////// +catch (const Dune::Exception& e) { + std::cout << e << std::endl; + return 1; +}