preCICE v3.1.2
Loading...
Searching...
No Matches
CommunicateMeshTest.cpp
Go to the documentation of this file.
1#ifndef PRECICE_NO_MPI
2
3#include <Eigen/Core>
4#include <algorithm>
5#include <memory>
6#include "com/Extra.hpp"
8#include "m2n/M2N.hpp"
9#include "mesh/Mesh.hpp"
11#include "testing/Testing.hpp"
12#include "utils/IntraComm.hpp"
13
14namespace precice::mesh {
15class Edge;
16class Triangle;
17class Vertex;
18} // namespace precice::mesh
19
20using namespace precice;
21using namespace precice::com;
22
23BOOST_AUTO_TEST_SUITE(CommunicationTests)
24
25BOOST_AUTO_TEST_SUITE(MeshTests)
26
27BOOST_AUTO_TEST_CASE(VertexEdgeMesh)
28{
29 PRECICE_TEST("A"_on(1_rank), "B"_on(1_rank), Require::Events);
30 auto m2n = context.connectPrimaryRanks("A", "B");
31
32 for (int dim = 2; dim <= 3; dim++) {
33 mesh::Mesh sendMesh("Sent Mesh", dim, testing::nextMeshID());
34 mesh::Vertex &v0 = sendMesh.createVertex(Eigen::VectorXd::Constant(dim, 0));
35 mesh::Vertex &v1 = sendMesh.createVertex(Eigen::VectorXd::Constant(dim, 1));
36 mesh::Vertex &v2 = sendMesh.createVertex(Eigen::VectorXd::Constant(dim, 2));
37 mesh::Edge & e0 = sendMesh.createEdge(v0, v1);
38 mesh::Edge & e1 = sendMesh.createEdge(v1, v2);
39 mesh::Edge & e2 = sendMesh.createEdge(v2, v0);
40
41 auto &comm = *m2n->getPrimaryRankCommunication();
42
43 if (context.isNamed("A")) {
44 com::sendMesh(comm, 0, sendMesh);
45 } else {
46 // receiveMesh can also deal with delta meshes
47 mesh::Mesh recvMesh("Received Mesh", dim, testing::nextMeshID());
48 recvMesh.createVertex(Eigen::VectorXd::Constant(dim, 9));
49 com::receiveMesh(comm, 0, recvMesh);
50 BOOST_TEST(recvMesh.nVertices() == 4);
51 BOOST_TEST(testing::equals(recvMesh.vertex(0).getCoords(), Eigen::VectorXd::Constant(dim, 9)));
52 BOOST_TEST(recvMesh.vertex(1) == v0);
53 BOOST_TEST(recvMesh.vertex(2) == v1);
54 BOOST_TEST(recvMesh.vertex(3) == v2);
55 BOOST_TEST(recvMesh.edges().at(0) == e0);
56 BOOST_TEST(recvMesh.edges().at(1) == e1);
57 BOOST_TEST(recvMesh.edges().at(2) == e2);
58 }
59 }
60}
61
62BOOST_AUTO_TEST_CASE(VertexEdgeTriangleMesh)
63{
64 PRECICE_TEST("A"_on(1_rank), "B"_on(1_rank), Require::Events);
65 auto m2n = context.connectPrimaryRanks("A", "B");
66
67 int dim = 3;
68 mesh::Mesh sendMesh("Sent Mesh", dim, testing::nextMeshID());
69 mesh::Vertex & v0 = sendMesh.createVertex(Eigen::VectorXd::Constant(dim, 0));
70 mesh::Vertex & v1 = sendMesh.createVertex(Eigen::VectorXd::Constant(dim, 1));
71 mesh::Vertex & v2 = sendMesh.createVertex(Eigen::VectorXd::Constant(dim, 2));
72 mesh::Edge & e0 = sendMesh.createEdge(v0, v1);
73 mesh::Edge & e1 = sendMesh.createEdge(v1, v2);
74 mesh::Edge & e2 = sendMesh.createEdge(v2, v0);
75 mesh::Triangle &t0 = sendMesh.createTriangle(e0, e1, e2);
76
77 // Create mesh communicator
78 auto &comm = *m2n->getPrimaryRankCommunication();
79
80 if (context.isNamed("A")) {
81 com::sendMesh(comm, 0, sendMesh);
82 } else {
83 mesh::Mesh recvMesh("Received Mesh", dim, testing::nextMeshID());
84 // receiveMesh can also deal with delta meshes
85 recvMesh.createVertex(Eigen::VectorXd::Constant(dim, 9));
86 com::receiveMesh(comm, 0, recvMesh);
87 BOOST_TEST(recvMesh.nVertices() == 4);
88 BOOST_TEST(testing::equals(recvMesh.vertex(0).getCoords(), Eigen::VectorXd::Constant(dim, 9)));
89 BOOST_TEST(recvMesh.vertex(1) == v0);
90 BOOST_TEST(recvMesh.vertex(2) == v1);
91 BOOST_TEST(recvMesh.vertex(3) == v2);
92 BOOST_TEST(recvMesh.edges().at(0) == e0);
93 BOOST_TEST(recvMesh.edges().at(1) == e1);
94 BOOST_TEST(recvMesh.edges().at(2) == e2);
95
96 BOOST_TEST(recvMesh.triangles().at(0) == t0);
97 }
98}
99
100BOOST_AUTO_TEST_CASE(BroadcastVertexEdgeTriangleMesh)
101{
102 PRECICE_TEST(""_on(2_ranks).setupIntraComm(), Require::Events);
103
104 int dim = 3;
105 mesh::Mesh sendMesh("Sent Mesh", dim, testing::nextMeshID());
106 mesh::Vertex & v0 = sendMesh.createVertex(Eigen::VectorXd::Constant(dim, 0));
107 mesh::Vertex & v1 = sendMesh.createVertex(Eigen::VectorXd::Constant(dim, 1));
108 mesh::Vertex & v2 = sendMesh.createVertex(Eigen::VectorXd::Constant(dim, 2));
109 mesh::Edge & e0 = sendMesh.createEdge(v0, v1);
110 mesh::Edge & e1 = sendMesh.createEdge(v1, v2);
111 mesh::Edge & e2 = sendMesh.createEdge(v2, v0);
112 mesh::Triangle &t0 = sendMesh.createTriangle(e0, e1, e2);
113
114 // Create mesh communicator
116
117 if (context.isPrimary()) {
119 } else {
120 mesh::Mesh recvMesh("Received Mesh", dim, testing::nextMeshID());
121 // receiveMesh can also deal with delta meshes
122 recvMesh.createVertex(Eigen::VectorXd::Constant(dim, 9));
123 com::broadcastReceiveMesh(comm, recvMesh);
124 BOOST_TEST(recvMesh.nVertices() == 4);
125 BOOST_TEST(testing::equals(recvMesh.vertex(0).getCoords(), Eigen::VectorXd::Constant(dim, 9)));
126 BOOST_TEST(recvMesh.vertex(1) == v0);
127 BOOST_TEST(recvMesh.vertex(2) == v1);
128 BOOST_TEST(recvMesh.vertex(3) == v2);
129 BOOST_TEST(recvMesh.edges().at(0) == e0);
130 BOOST_TEST(recvMesh.edges().at(1) == e1);
131 BOOST_TEST(recvMesh.edges().at(2) == e2);
132 BOOST_TEST(recvMesh.triangles().at(0) == t0);
133 }
134}
135
136BOOST_AUTO_TEST_CASE(OneTetraCommunication)
137{
138 PRECICE_TEST("A"_on(1_rank), "B"_on(1_rank), Require::Events);
139 auto m2n = context.connectPrimaryRanks("A", "B");
140
141 int dim = 3;
142 mesh::Mesh sendMesh("Sent Mesh", dim, testing::nextMeshID());
143 mesh::Vertex &v0 = sendMesh.createVertex(Eigen::Vector3d{0.0, 0.0, 0.0});
144 mesh::Vertex &v1 = sendMesh.createVertex(Eigen::Vector3d{1.0, 0.0, 0.0});
145 mesh::Vertex &v2 = sendMesh.createVertex(Eigen::Vector3d{0.0, 1.0, 0.0});
146 mesh::Vertex &v3 = sendMesh.createVertex(Eigen::Vector3d{0.0, 0.0, 1.0});
147
148 mesh::Tetrahedron &t0 = sendMesh.createTetrahedron(v0, v1, v2, v3);
149
150 // Create mesh communicator
151 auto &comm = *m2n->getPrimaryRankCommunication();
152
153 if (context.isNamed("A")) {
154 com::sendMesh(comm, 0, sendMesh);
155 } else {
156 mesh::Mesh recvMesh("Received Mesh", dim, testing::nextMeshID());
157 // receiveMesh can also deal with delta meshes
158 recvMesh.createVertex(Eigen::VectorXd::Constant(dim, 9));
159 com::receiveMesh(comm, 0, recvMesh);
160 BOOST_TEST(recvMesh.nVertices() == 5); // 4 + 1
161 BOOST_TEST(testing::equals(recvMesh.vertex(0).getCoords(), Eigen::VectorXd::Constant(dim, 9)));
162 BOOST_TEST(recvMesh.tetrahedra().size() == 1);
163 BOOST_TEST(testing::equals(recvMesh.tetrahedra()[0].vertex(0).getCoords(), Eigen::Vector3d{0.0, 0.0, 0.0}));
164 BOOST_TEST(recvMesh.tetrahedra()[0] == t0);
165 }
166}
167
168BOOST_AUTO_TEST_CASE(BroadcastTetra)
169{
170 PRECICE_TEST(""_on(2_ranks).setupIntraComm(), Require::Events);
171
172 int dim = 3;
173 mesh::Mesh sendMesh("Sent Mesh", dim, testing::nextMeshID());
174 mesh::Vertex &v0 = sendMesh.createVertex(Eigen::Vector3d{0.0, 0.0, 0.0});
175 mesh::Vertex &v1 = sendMesh.createVertex(Eigen::Vector3d{1.0, 0.0, 0.0});
176 mesh::Vertex &v2 = sendMesh.createVertex(Eigen::Vector3d{0.0, 1.0, 0.0});
177 mesh::Vertex &v3 = sendMesh.createVertex(Eigen::Vector3d{0.0, 0.0, 1.0});
178
179 mesh::Tetrahedron &t0 = sendMesh.createTetrahedron(v0, v1, v2, v3);
180
181 // Create mesh communicator
183
184 if (context.isPrimary()) {
186 } else {
187 mesh::Mesh recvMesh("Received Mesh", dim, testing::nextMeshID());
188 // receiveMesh can also deal with delta meshes
189 recvMesh.createVertex(Eigen::VectorXd::Constant(dim, 9));
190 com::broadcastReceiveMesh(comm, recvMesh);
191 BOOST_TEST(recvMesh.nVertices() == 5); // 4 + 1
192 BOOST_TEST(testing::equals(recvMesh.vertex(0).getCoords(), Eigen::VectorXd::Constant(dim, 9)));
193 BOOST_TEST(recvMesh.tetrahedra().size() == 1);
194 BOOST_TEST(testing::equals(recvMesh.tetrahedra()[0].vertex(0).getCoords(), Eigen::Vector3d{0.0, 0.0, 0.0}));
195 BOOST_TEST(recvMesh.tetrahedra()[0] == t0);
196 }
197}
198
200BOOST_AUTO_TEST_SUITE_END() // Communication
201
202#endif // not PRECICE_NO_MPI
BOOST_AUTO_TEST_CASE(VertexEdgeMesh)
BOOST_AUTO_TEST_SUITE(PreProcess)
BOOST_AUTO_TEST_SUITE_END()
#define PRECICE_TEST(...)
Definition Testing.hpp:27
T at(T... args)
Linear edge of a mesh, defined by two Vertex objects.
Definition Edge.hpp:16
Container and creator for meshes.
Definition Mesh.hpp:39
std::size_t nVertices() const
Returns the number of vertices.
Definition Mesh.cpp:63
TetraContainer & tetrahedra()
Returns modifiable container holding all tetrahedra.
Definition Mesh.cpp:93
Vertex & vertex(VertexID id)
Mutable access to a vertex by VertexID.
Definition Mesh.cpp:41
TriangleContainer & triangles()
Returns modifiable container holding all triangles.
Definition Mesh.cpp:78
EdgeContainer & edges()
Returns modifiable container holding all edges.
Definition Mesh.cpp:68
Vertex & createVertex(const Eigen::VectorXd &coords)
Creates and initializes a Vertex object.
Definition Mesh.cpp:103
Tetrahedron of a mesh, defined by 4 vertices.
Triangle of a mesh, defined by three vertices.
Definition Triangle.hpp:27
Vertex of a mesh.
Definition Vertex.hpp:16
Eigen::VectorXd getCoords() const
Returns the coordinates of the vertex.
Definition Vertex.hpp:116
static com::PtrCommunication & getCommunication()
Intra-participant communication.
Definition IntraComm.hpp:31
contains the data communication abstraction layer.
void sendMesh(Communication &communication, int rankReceiver, const mesh::Mesh &mesh)
Definition Extra.cpp:8
void broadcastSendMesh(Communication &communication, const mesh::Mesh &mesh)
Definition Extra.cpp:18
void broadcastReceiveMesh(Communication &communication, mesh::Mesh &mesh)
Definition Extra.cpp:23
void receiveMesh(Communication &communication, int rankSender, mesh::Mesh &mesh)
Definition Extra.cpp:13
provides Mesh, Data and primitives.
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:65
Main namespace of the precice library.
T size(T... args)