preCICE v3.2.0
Loading...
Searching...
No Matches
PolationTest.cpp
Go to the documentation of this file.
1#include <vector>
2#include "Eigen/Core"
4#include "mesh/Edge.hpp"
5#include "mesh/Triangle.hpp"
6#include "mesh/Vertex.hpp"
8#include "testing/Testing.hpp"
9
10using namespace precice;
11using namespace precice::mapping;
12
13BOOST_AUTO_TEST_SUITE(MappingTests)
14BOOST_AUTO_TEST_SUITE(Interpolation)
15
17BOOST_AUTO_TEST_CASE(VertexInterpolation)
18{
20 Eigen::Vector3d location(0.0, 0.0, 0.0);
21 mesh::Vertex vertex(Eigen::Vector3d(1.0, 2.0, 0.0), 0);
22
23 Polation polation(location, vertex);
24
25 std::vector<int> expectedIndices = {0};
26 std::vector<double> expectedWeights = {1.0};
27
28 BOOST_TEST(polation.getWeightedElements().size() == 1);
29 BOOST_TEST(polation.isInterpolation());
30 BOOST_TEST(polation.distance() == vertex.getCoords().norm());
31
32 for (size_t i = 0; i < polation.getWeightedElements().size(); ++i) {
33 BOOST_TEST(polation.getWeightedElements().at(i).weight == expectedWeights.at(i));
34 BOOST_TEST(polation.getWeightedElements().at(i).vertexID == expectedIndices.at(i));
35 }
36}
37
39BOOST_AUTO_TEST_CASE(EdgeInterpolation)
40{
42 mesh::Vertex v1(Eigen::Vector3d(0.0, 0.0, 0.0), 0);
43 mesh::Vertex v2(Eigen::Vector3d(0.0, 2.0, 0.0), 1);
44 mesh::Edge edge(v1, v2);
45
46 Eigen::Vector3d location(0.0, 0.4, 0.0);
47
48 Polation polation(location, edge);
49
50 std::vector<int> expectedIndices = {0, 1};
51 std::vector<double> expectedWeights = {0.8, 0.2};
52
53 BOOST_TEST(polation.getWeightedElements().size() == 2);
54 BOOST_TEST(polation.isInterpolation());
55 BOOST_TEST(polation.distance() == 0);
56
57 for (size_t i = 0; i < polation.getWeightedElements().size(); ++i) {
58 BOOST_TEST(polation.getWeightedElements().at(i).weight == expectedWeights.at(i));
59 BOOST_TEST(polation.getWeightedElements().at(i).vertexID == expectedIndices.at(i));
60 }
61}
62
64BOOST_AUTO_TEST_CASE(EdgeProjectedInterpolation)
65{
67 mesh::Vertex v1(Eigen::Vector3d(0.0, 0.0, 0.0), 0);
68 mesh::Vertex v2(Eigen::Vector3d(0.0, 2.0, 0.0), 1);
69 mesh::Edge edge(v1, v2);
70
71 Eigen::Vector3d location(0.0, 0.4, 0.12);
72
73 Polation polation(location, edge);
74
75 std::vector<int> expectedIndices = {0, 1};
76 std::vector<double> expectedWeights = {0.8, 0.2};
77
78 BOOST_TEST(polation.getWeightedElements().size() == 2);
79 BOOST_TEST(polation.isInterpolation());
80 BOOST_TEST(polation.distance() == 0.12);
81
82 for (size_t i = 0; i < polation.getWeightedElements().size(); ++i) {
83 BOOST_TEST(polation.getWeightedElements().at(i).weight == expectedWeights.at(i));
84 BOOST_TEST(polation.getWeightedElements().at(i).vertexID == expectedIndices.at(i));
85 }
86}
87
89BOOST_AUTO_TEST_CASE(TriangleInterpolation)
90{
92 mesh::Vertex v1(Eigen::Vector3d(0.0, 0.0, 0.0), 0);
93 mesh::Vertex v2(Eigen::Vector3d(2.0, 0.0, 0.0), 1);
94 mesh::Vertex v3(Eigen::Vector3d(1.0, 2.0, 0.0), 2);
95 mesh::Edge e1(v1, v2);
96 mesh::Edge e2(v2, v3);
97 mesh::Edge e3(v1, v3);
98 mesh::Triangle triangle(e1, e2, e3);
99 triangle.computeNormal();
100
101 Eigen::Vector3d location(1.0, 0.6, 0.0);
102
103 Polation polation(location, triangle);
104
105 std::vector<int> expectedIndices = {0, 1, 2};
106 std::vector<double> expectedWeights = {0.35, 0.35, 0.3};
107
108 BOOST_TEST(polation.getWeightedElements().size() == 3);
109 BOOST_TEST(polation.isInterpolation());
110 BOOST_TEST(polation.distance() == 0);
111
112 for (size_t i = 0; i < polation.getWeightedElements().size(); ++i) {
113
114 BOOST_TEST(polation.getWeightedElements().at(i).weight == expectedWeights.at(i));
115 BOOST_TEST(polation.getWeightedElements().at(i).vertexID == expectedIndices.at(i));
116 }
117}
118
119PRECICE_TEST_SETUP(1_rank)
120BOOST_AUTO_TEST_CASE(TriangleProjectedInterpolation)
121{
122 PRECICE_TEST();
123 mesh::Vertex v1(Eigen::Vector3d(0.0, 0.0, 0.0), 0);
124 mesh::Vertex v2(Eigen::Vector3d(2.0, 0.0, 0.0), 1);
125 mesh::Vertex v3(Eigen::Vector3d(1.0, 2.0, 0.0), 2);
126 mesh::Edge e1(v1, v2);
127 mesh::Edge e2(v2, v3);
128 mesh::Edge e3(v1, v3);
129 mesh::Triangle triangle(e1, e2, e3);
130 triangle.computeNormal();
131
132 Eigen::Vector3d location(1.0, 0.6, 0.14);
133
134 Polation polation(location, triangle);
135
136 std::vector<int> expectedIndices = {0, 1, 2};
137 std::vector<double> expectedWeights = {0.35, 0.35, 0.3};
138
139 BOOST_TEST(polation.getWeightedElements().size() == 3);
140 BOOST_TEST(polation.isInterpolation());
141 BOOST_TEST(polation.distance() == 0.14);
142
143 for (size_t i = 0; i < polation.getWeightedElements().size(); ++i) {
144
145 BOOST_TEST(polation.getWeightedElements().at(i).weight == expectedWeights.at(i));
146 BOOST_TEST(polation.getWeightedElements().at(i).vertexID == expectedIndices.at(i));
147 }
148}
149
150PRECICE_TEST_SETUP(1_rank)
151BOOST_AUTO_TEST_CASE(EdgeExtrapolation)
152{
153 PRECICE_TEST();
154 mesh::Vertex v1(Eigen::Vector3d(0.0, 0.0, 0.0), 0);
155 mesh::Vertex v2(Eigen::Vector3d(0.0, 2.0, 0.0), 1);
156 mesh::Edge edge(v1, v2);
157
158 Eigen::Vector3d location(0.0, 3.0, 0.0);
159
160 Polation polation(location, edge);
161
162 std::vector<int> expectedIndices = {0, 1};
163 std::vector<double> expectedWeights = {-0.5, 1.5};
164
165 BOOST_TEST(polation.getWeightedElements().size() == 2);
166 BOOST_TEST(not polation.isInterpolation());
167 // The distance to projection is still 0
168 BOOST_TEST(polation.distance() == 0);
169
170 for (size_t i = 0; i < polation.getWeightedElements().size(); ++i) {
171 BOOST_TEST(polation.getWeightedElements().at(i).weight == expectedWeights.at(i));
172 BOOST_TEST(polation.getWeightedElements().at(i).vertexID == expectedIndices.at(i));
173 }
174}
175
176PRECICE_TEST_SETUP(1_rank)
177BOOST_AUTO_TEST_CASE(TriangleExtrapolation)
178{
179 PRECICE_TEST();
180 mesh::Vertex v1(Eigen::Vector3d(0.0, 0.0, 0.0), 0);
181 mesh::Vertex v2(Eigen::Vector3d(2.0, 0.0, 0.0), 1);
182 mesh::Vertex v3(Eigen::Vector3d(1.0, 2.0, 0.0), 2);
183 mesh::Edge e1(v1, v2);
184 mesh::Edge e2(v2, v3);
185 mesh::Edge e3(v1, v3);
186 mesh::Triangle triangle(e1, e2, e3);
187 triangle.computeNormal();
188
189 Eigen::Vector3d location(4.0, 0.6, 0.0);
190
191 Polation polation(location, triangle);
192
193 std::vector<int> expectedIndices = {0, 1, 2};
194 std::vector<double> expectedWeights = {-1.15, 1.85, 0.3};
195
196 BOOST_TEST(polation.getWeightedElements().size() == 3);
197 BOOST_TEST(not polation.isInterpolation());
198 // The distance to projection is still 0
199 BOOST_TEST(polation.distance() == 0);
200
201 for (size_t i = 0; i < polation.getWeightedElements().size(); ++i) {
202
203 BOOST_TEST(polation.getWeightedElements().at(i).weight == expectedWeights.at(i));
204 BOOST_TEST(polation.getWeightedElements().at(i).vertexID == expectedIndices.at(i));
205 }
206}
207
208PRECICE_TEST_SETUP(1_rank)
209BOOST_AUTO_TEST_CASE(TetrahedronInterpolation)
210{
211 PRECICE_TEST();
212 mesh::Vertex v1(Eigen::Vector3d(1.0, 0.0, 0.0), 0);
213 mesh::Vertex v2(Eigen::Vector3d(0.0, 1.0, 0.0), 1);
214 mesh::Vertex v3(Eigen::Vector3d(0.0, 0.0, 1.0), 2);
215 mesh::Vertex v4(Eigen::Vector3d(0.0, 0.0, 0.0), 3);
216
217 mesh::Tetrahedron tetra(v1, v2, v3, v4);
218
219 Eigen::Vector3d location(0.15, 0.25, 0.40);
220
221 Polation polation(location, tetra);
222
223 std::vector<int> expectedIndices = {0, 1, 2, 3};
224 std::vector<double> expectedWeights = {0.15, 0.25, 0.40, 0.20};
225
226 BOOST_TEST(polation.getWeightedElements().size() == 4);
227 BOOST_TEST(polation.isInterpolation());
228 BOOST_TEST(polation.distance() == 0);
229
230 for (size_t i = 0; i < polation.getWeightedElements().size(); ++i) {
231
232 BOOST_TEST(polation.getWeightedElements().at(i).weight == expectedWeights.at(i));
233 BOOST_TEST(polation.getWeightedElements().at(i).vertexID == expectedIndices.at(i));
234 }
235}
236
237PRECICE_TEST_SETUP(1_rank)
238BOOST_AUTO_TEST_CASE(TetrahedronExtrapolation)
239{
240 PRECICE_TEST();
241 mesh::Vertex v1(Eigen::Vector3d(1.0, 0.0, 0.0), 0);
242 mesh::Vertex v2(Eigen::Vector3d(0.0, 1.0, 0.0), 1);
243 mesh::Vertex v3(Eigen::Vector3d(0.0, 0.0, 1.0), 2);
244 mesh::Vertex v4(Eigen::Vector3d(0.0, 0.0, 0.0), 3);
245
246 mesh::Tetrahedron tetra(v1, v2, v3, v4);
247
248 Eigen::Vector3d location(-0.15, 0.25, 0.40);
249
250 Polation polation(location, tetra);
251
252 std::vector<int> expectedIndices = {0, 1, 2, 3};
253 std::vector<double> expectedWeights = {-0.15, 0.25, 0.40, 0.50};
254
255 BOOST_TEST(polation.getWeightedElements().size() == 4);
256 BOOST_TEST(not polation.isInterpolation());
257 // There is no projection distance as such. This should always be 0
258 BOOST_TEST(polation.distance() == 0);
259
260 for (size_t i = 0; i < polation.getWeightedElements().size(); ++i) {
261
262 BOOST_TEST(polation.getWeightedElements().at(i).weight == expectedWeights.at(i));
263 BOOST_TEST(polation.getWeightedElements().at(i).vertexID == expectedIndices.at(i));
264 }
265}
266
267PRECICE_TEST_SETUP(1_rank)
268BOOST_AUTO_TEST_CASE(PolationToleranceEdge)
269{
270 PRECICE_TEST();
271 mesh::Vertex v1(Eigen::Vector3d(1.0, 0.0, 0.0), 0);
272 mesh::Vertex v2(Eigen::Vector3d(0.0, 1.0, 0.0), 1);
273
274 mesh::Edge edge(v1, v2);
275
276 Eigen::Vector3d location(1 + 1e-15, -1e-16, 0.0);
277
278 Polation polation(location, edge);
279
280 BOOST_TEST(polation.isInterpolation());
281}
282
283PRECICE_TEST_SETUP(1_rank)
284BOOST_AUTO_TEST_CASE(PolationToleranceTriangle)
285{
286 PRECICE_TEST();
287 mesh::Vertex v1(Eigen::Vector3d(1.0, 0.0, 0.0), 0);
288 mesh::Vertex v2(Eigen::Vector3d(0.0, 1.0, 0.0), 1);
289 mesh::Vertex v3(Eigen::Vector3d(0.0, 0.0, 0.0), 2);
290
291 mesh::Triangle triangle(v1, v2, v3);
292
293 Eigen::Vector3d location(0.5 + 1e-15, 0.5 + 1e-15, 1e-14);
294
295 Polation polation(location, triangle);
296
297 BOOST_TEST(polation.isInterpolation());
298}
299
300PRECICE_TEST_SETUP(1_rank)
301BOOST_AUTO_TEST_CASE(PolationToleranceTetra)
302{
303 PRECICE_TEST();
304 mesh::Vertex v1(Eigen::Vector3d(1.0, 0.0, 0.0), 0);
305 mesh::Vertex v2(Eigen::Vector3d(0.0, 1.0, 0.0), 1);
306 mesh::Vertex v3(Eigen::Vector3d(0.0, 0.0, 1.0), 2);
307 mesh::Vertex v4(Eigen::Vector3d(0.0, 0.0, 0.0), 3);
308
309 mesh::Tetrahedron tetra(v1, v2, v3, v4);
310
311 Eigen::Vector3d location(1 - 1e-15, -1e-15, -1e-15);
312
313 Polation polation(location, tetra);
314
315 BOOST_TEST(polation.isInterpolation());
316}
317
318BOOST_AUTO_TEST_SUITE_END() // Interpolation
319BOOST_AUTO_TEST_SUITE_END() // Mapping
BOOST_AUTO_TEST_CASE(testIQNIMVJPPWithSubsteps)
BOOST_AUTO_TEST_SUITE(PreProcess)
BOOST_AUTO_TEST_SUITE_END()
#define PRECICE_TEST()
Definition Testing.hpp:39
#define PRECICE_TEST_SETUP(...)
Creates and attaches a TestSetup to a Boost test case.
Definition Testing.hpp:29
T at(T... args)
Calculates the barycentric coordinates of a coordinate on the given vertex/edge/triangle and stores t...
Definition Polation.hpp:23
bool isInterpolation() const
Check whether all the weights are positive, which means it is interpolation.
Definition Polation.cpp:91
double distance() const
Returns the projection distance.
Definition Polation.cpp:96
const std::vector< WeightedElement > & getWeightedElements() const
Get the weights and indices of the calculated interpolation.
Definition Polation.cpp:81
Linear edge of a mesh, defined by two Vertex objects.
Definition Edge.hpp:15
Tetrahedron of a mesh, defined by 4 vertices.
Triangle of a mesh, defined by three vertices.
Definition Triangle.hpp:24
Eigen::VectorXd computeNormal() const
Computes the normal of the triangle.
Definition Triangle.cpp:75
Vertex of a mesh.
Definition Vertex.hpp:16
Eigen::VectorXd getCoords() const
Returns the coordinates of the vertex.
Definition Vertex.hpp:114
contains data mapping from points to meshes.
Main namespace of the precice library.