preCICE
Toggle main menu visibility
Loading...
Searching...
No Matches
src
math
tests
BSplineTest.cpp
Go to the documentation of this file.
1
#include "
math/Bspline.hpp
"
2
#include "
testing/TestContext.hpp
"
3
#include "
testing/Testing.hpp
"
4
5
#include <Eigen/Core>
6
7
using namespace
precice
;
8
9
BOOST_AUTO_TEST_SUITE
(MathTests)
10
BOOST_AUTO_TEST_SUITE
(BSpline)
11
12
PRECICE_TEST_SETUP
(1_rank)
13
BOOST_AUTO_TEST_CASE
(TwoPointsLinear)
14
{
15
PRECICE_TEST
();
16
Eigen::Vector2d
ts
;
17
ts
<< 1, 2;
18
Eigen::MatrixXd xs(3, 2);
19
xs << 1, 2, 10, 20, 100, 200;
20
21
precice::math::Bspline
bspline(
ts
, xs, 1);
22
// Limits
23
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(1.0), Eigen::Vector3d(1, 10, 100)));
24
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(2.0), Eigen::Vector3d(2, 20, 200)));
25
26
// Midpoint
27
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(1.5), Eigen::Vector3d(1.5, 15, 150)));
28
29
// Quarters
30
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(1.25), Eigen::Vector3d(1.25, 12.5, 125)));
31
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(1.75), Eigen::Vector3d(1.75, 17.5, 175)));
32
}
33
34
PRECICE_TEST_SETUP
(1_rank)
35
BOOST_AUTO_TEST_CASE
(TwoPointsLinearRoundoff)
36
{
37
PRECICE_TEST
();
38
Eigen::Vector2d
ts
;
39
ts
<< 1e-6, 2e-6;
40
41
// Evaluate Bspline slightly outside of borders of time window.
42
Eigen::Vector2d teval;
43
teval << 1e-6 - std::numeric_limits<double>::epsilon(), 2e-6 + std::numeric_limits<double>::epsilon();
44
// However, cannot distinguish between ts and teval
45
BOOST_ASSERT(
testing::equals
(
ts
[0], teval[0]));
46
BOOST_ASSERT(
testing::equals
(
ts
[1], teval[1]));
47
48
Eigen::MatrixXd xs(3, 2);
49
xs << 1, 2, 10, 20, 100, 200;
50
51
precice::math::Bspline
bspline(
ts
, xs, 1);
52
// Make sure that evaluating at borders of window (again: with some floating point error within eps) does not introduce observable errors
53
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(teval[0]), Eigen::Vector3d(1, 10, 100)));
54
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(teval[1]), Eigen::Vector3d(2, 20, 200)));
55
}
56
57
PRECICE_TEST_SETUP
(1_rank)
58
BOOST_AUTO_TEST_CASE
(ThreePointsLinear)
59
{
60
PRECICE_TEST
();
61
Eigen::Vector3d
ts
;
62
ts
<< 0, 1, 2;
63
Eigen::MatrixXd xs(3, 3);
64
xs << 1, 2, 3, 10, 20, 30, 100, 200, 300;
65
precice::math::Bspline
bspline(
ts
, xs, 1);
66
// Points
67
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(0.0), Eigen::Vector3d(1, 10, 100)));
68
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(1.0), Eigen::Vector3d(2, 20, 200)));
69
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(2.0), Eigen::Vector3d(3, 30, 300)));
70
71
// Midpoints
72
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(0.5), Eigen::Vector3d(1.5, 15, 150)));
73
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(1.5), Eigen::Vector3d(2.5, 25, 250)));
74
}
75
76
PRECICE_TEST_SETUP
(1_rank)
77
BOOST_AUTO_TEST_CASE
(ThreePointsLinearNonEquidistant)
78
{
79
PRECICE_TEST
();
80
Eigen::Vector3d
ts
;
81
ts
<< 0, 1, 3;
82
Eigen::MatrixXd xs(3, 3);
83
xs << 1, 2, 3, 10, 20, 30, 100, 200, 300;
84
precice::math::Bspline
bspline(
ts
, xs, 1);
85
// Points
86
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(0.0), Eigen::Vector3d(1, 10, 100)));
87
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(1.0), Eigen::Vector3d(2, 20, 200)));
88
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(3.0), Eigen::Vector3d(3, 30, 300)));
89
90
// Midpoints
91
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(0.5), Eigen::Vector3d(1.5, 15, 150)));
92
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(2.0), Eigen::Vector3d(2.5, 25, 250), 1e-13));
93
}
94
95
PRECICE_TEST_SETUP
(1_rank)
96
BOOST_AUTO_TEST_CASE
(ThreePointsQuadratic)
97
{
98
PRECICE_TEST
();
99
Eigen::Vector3d
ts
;
100
ts
<< 0, 1, 2;
101
Eigen::MatrixXd xs(3, 3);
102
xs << 1, 2, 3, 10, 20, 30, 100, 200, 300;
103
precice::math::Bspline
bspline(
ts
, xs, 2);
104
// Points
105
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(0.0), Eigen::Vector3d(1, 10, 100), 1e-13));
106
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(1.0), Eigen::Vector3d(2, 20, 200)));
107
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(2.0), Eigen::Vector3d(3, 30, 300)));
108
109
// Midpoints
110
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(0.5), Eigen::Vector3d(1.5, 15, 150)));
111
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(1.5), Eigen::Vector3d(2.5, 25, 250)));
112
}
113
114
PRECICE_TEST_SETUP
(1_rank)
115
BOOST_AUTO_TEST_CASE
(ThreePointsQuadraticNonEquidistant)
116
{
117
PRECICE_TEST
();
118
Eigen::Vector3d
ts
;
119
ts
<< 0, 1, 3;
120
Eigen::MatrixXd xs(3, 3);
121
xs << 1, 2, 3, 10, 20, 30, 100, 200, 300;
122
precice::math::Bspline
bspline(
ts
, xs, 2);
123
// Points
124
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(0.0), Eigen::Vector3d(1, 10, 100), 1e-13));
125
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(1.0), Eigen::Vector3d(2, 20, 200)));
126
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(3.0), Eigen::Vector3d(3, 30, 300)));
127
128
// Midpoints
129
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(0.5), Eigen::Vector3d(37.0 / 24, 370.0 / 24, 3700.0 / 24), 1e-13));
130
BOOST_TEST(
testing::equals
(bspline.
interpolateAt
(2.0), Eigen::Vector3d(8.0 / 3, 80.0 / 3, 800.0 / 3), 1e-13));
131
}
132
133
PRECICE_TEST_SETUP
(1_rank)
134
BOOST_AUTO_TEST_CASE
(FloatingPointAccuracy)
// see https://github.com/precice/precice/issues/1981
135
{
136
PRECICE_TEST
();
137
Eigen::Vector2d
ts
;
138
ts
<< 256.1, 256.2;
139
Eigen::MatrixXd xs(3, 2);
140
xs << 1, 2, 10, 20, 100, 200;
141
precice::math::Bspline
bspline(
ts
, xs, 1);
142
// Points
143
BOOST_TEST(
testing::equals
(bspline.interpolateAt(256.1), Eigen::Vector3d(1, 10, 100)));
144
BOOST_TEST(
testing::equals
(bspline.interpolateAt(256.2), Eigen::Vector3d(2, 20, 200)));
145
// 256.1 + 0.1 > 256.2 in floating point numbers!
146
BOOST_TEST(
testing::equals
(bspline.interpolateAt(256.1 + 0.1), Eigen::Vector3d(2, 20, 200)));
147
}
148
149
BOOST_AUTO_TEST_SUITE_END
()
// BSpline
150
BOOST_AUTO_TEST_SUITE_END
()
// Math
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(testIQNIMVJPPWithSubsteps)
Definition
AccelerationSerialTest.cpp:124
ts
Eigen::Vector2d ts
Definition
BSplineTest.cpp:137
Bspline.hpp
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(PreProcess)
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
TestContext.hpp
Testing.hpp
PRECICE_TEST
#define PRECICE_TEST()
Definition
Testing.hpp:39
PRECICE_TEST_SETUP
#define PRECICE_TEST_SETUP(...)
Creates and attaches a TestSetup to a Boost test case.
Definition
Testing.hpp:29
precice::math::Bspline
Definition
Bspline.hpp:6
precice::math::Bspline::interpolateAt
Eigen::VectorXd interpolateAt(double t) const
Samples the B-Spline interpolation.
Definition
Bspline.cpp:69
precice::testing::equals
boost::test_tools::predicate_result equals(const std::vector< float > &VectorA, const std::vector< float > &VectorB, float tolerance)
equals to be used in tests. Compares two std::vectors using a given tolerance. Prints both operands o...
Definition
Testing.cpp:94
precice
Main namespace of the precice library.
Definition
Acceleration.cpp:5