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