preCICE v3.1.1
Loading...
Searching...
No Matches
ExportVTUTest.cpp
Go to the documentation of this file.
1#ifndef PRECICE_NO_MPI
2
3#include <Eigen/Core>
4#include <algorithm>
5#include <map>
6#include <string>
8#include "io/Export.hpp"
9#include "io/ExportVTU.hpp"
10#include "mesh/Mesh.hpp"
12#include "testing/Testing.hpp"
13#include "utils/Parallel.hpp"
14
15namespace precice::mesh {
16class Edge;
17class Vertex;
18} // namespace precice::mesh
19
21
22using namespace precice;
23
24BOOST_AUTO_TEST_SUITE(VTUExport)
25
26BOOST_AUTO_TEST_CASE(ExportDataWithGradient2D)
27{
28 PRECICE_TEST(1_rank)
29
30 // Create mesh to map from
31 int dimensions = 2;
32 mesh::Mesh mesh("MyMesh", dimensions, testing::nextMeshID());
33 mesh::PtrData dataScalar = mesh.createData("dataScalar", 1, 0_dataID);
34 mesh::PtrData dataVector = mesh.createData("dataVector", dimensions, 1_dataID);
35 dataScalar->requireDataGradient();
36 dataVector->requireDataGradient();
37
38 mesh.createVertex(Eigen::Vector2d::Constant(0.0));
39 mesh.createVertex(Eigen::Vector2d::Constant(1.0));
40
41 // Create data
42 mesh.allocateDataValues();
43 Eigen::VectorXd &valuesScalar = dataScalar->values();
44 Eigen::VectorXd &valuesVector = dataVector->values();
45 valuesScalar.setLinSpaced(1., 5.);
46 valuesVector.setLinSpaced(1., 5.);
47
48 // Create corresponding gradient data (all gradient values = const = 1)
49 Eigen::MatrixXd &gradientsScalar = dataScalar->gradients();
50 Eigen::MatrixXd &gradientsVector = dataVector->gradients();
51 gradientsScalar.setOnes();
52 gradientsVector.setOnes();
53 io::ExportVTU exportVTU;
54 std::string filename = "io-VTUExport-ExportDatawithGradient" + std::to_string(dimensions);
55 std::string location = "";
56 exportVTU.doExport(filename, location, mesh);
57}
58
59BOOST_AUTO_TEST_CASE(ExportDataWithGradient3D)
60{
61 PRECICE_TEST(1_rank)
62 int dimensions = 3;
63 // Create mesh to map from
64 mesh::Mesh mesh("MyMesh", dimensions, testing::nextMeshID());
65 mesh::PtrData dataScalar = mesh.createData("dataScalar", 1, 0_dataID);
66 mesh::PtrData dataVector = mesh.createData("dataVector", dimensions, 1_dataID);
67 dataScalar->requireDataGradient();
68 dataVector->requireDataGradient();
69
70 mesh.createVertex(Eigen::Vector3d::Constant(0.0));
71 mesh.createVertex(Eigen::Vector3d::Constant(1.0));
72
73 // Create data
74 mesh.allocateDataValues();
75 Eigen::VectorXd &valuesScalar = dataScalar->values();
76 Eigen::VectorXd &valuesVector = dataVector->values();
77
78 valuesScalar.setLinSpaced(1., 5.);
79 valuesVector.setLinSpaced(1., 5.);
80
81 // Create corresponding gradient data (all gradient values = const = 1)
82 Eigen::MatrixXd &gradientsScalar = dataScalar->gradients();
83 Eigen::MatrixXd &gradientsVector = dataVector->gradients();
84 gradientsScalar.setOnes();
85 gradientsVector.setOnes();
86 io::ExportVTU exportVTU;
87 std::string filename = "io-VTUExport-ExportDatawithGradient" + std::to_string(dimensions);
88 std::string location = "";
89 exportVTU.doExport(filename, location, mesh);
90}
91
92BOOST_AUTO_TEST_CASE(ExportPolygonalMeshSerial)
93{
94 PRECICE_TEST(""_on(1_rank).setupIntraComm());
95 int dim = 2;
96 mesh::Mesh mesh("MyMesh", dim, testing::nextMeshID());
97 mesh::Vertex &v1 = mesh.createVertex(Eigen::Vector2d::Zero());
98 mesh::Vertex &v2 = mesh.createVertex(Eigen::Vector2d::Constant(1));
99 mesh::Vertex &v3 = mesh.createVertex(Eigen::Vector2d{1.0, 0.0});
100
101 mesh.createEdge(v1, v2);
102 mesh.createEdge(v2, v3);
103 mesh.createEdge(v3, v1);
104
105 io::ExportVTU exportVTU;
106 std::string filename = "io-VTUExport-ExportPolygonalMesh";
107 std::string location = "";
108 exportVTU.doExport(filename, location, mesh);
109}
110
111BOOST_AUTO_TEST_CASE(ExportPolygonalMesh)
112{
113 PRECICE_TEST(""_on(4_ranks).setupIntraComm());
114 int dim = 2;
115 mesh::Mesh mesh("MyMesh", dim, testing::nextMeshID());
116
117 if (context.isRank(0)) {
118 mesh::Vertex &v1 = mesh.createVertex(Eigen::Vector2d::Zero());
119 mesh::Vertex &v2 = mesh.createVertex(Eigen::Vector2d::Constant(1));
120 mesh::Vertex &v3 = mesh.createVertex(Eigen::Vector2d{1.0, 0});
121
122 mesh.createEdge(v1, v2);
123 mesh.createEdge(v2, v3);
124 mesh.createEdge(v3, v1);
125 mesh.setVertexOffsets({3, 3, 6, 7});
126
127 } else if (context.isRank(1)) {
128 // nothing
129 } else if (context.isRank(2)) {
130 mesh::Vertex &v1 = mesh.createVertex(Eigen::Vector2d::Constant(1));
131 mesh::Vertex &v2 = mesh.createVertex(Eigen::Vector2d::Constant(2));
132 mesh::Vertex &v3 = mesh.createVertex(Eigen::Vector2d{1.0, 0.0});
133
134 mesh.createEdge(v1, v2);
135 mesh.createEdge(v2, v3);
136 mesh.createEdge(v3, v1);
137 } else if (context.isRank(3)) {
138 mesh.createVertex(Eigen::Vector2d::Constant(3.0));
139 }
140
141 io::ExportVTU exportVTU;
142 std::string filename = "io-ExportVTUTest-testExportPolygonalMesh";
143 std::string location = "";
144 exportVTU.doExport(filename, location, mesh);
145}
146
147BOOST_AUTO_TEST_CASE(ExportTriangulatedMesh)
148{
149 PRECICE_TEST(""_on(4_ranks).setupIntraComm());
150 int dim = 3;
151 mesh::Mesh mesh("MyMesh", dim, testing::nextMeshID());
152
153 if (context.isRank(0)) {
154 mesh::Vertex &v1 = mesh.createVertex(Eigen::Vector3d::Zero());
155 mesh::Vertex &v2 = mesh.createVertex(Eigen::Vector3d::Constant(1));
156 mesh::Vertex &v3 = mesh.createVertex(Eigen::Vector3d{1.0, 0.0, 0.0});
157
158 mesh::Edge &e1 = mesh.createEdge(v1, v2);
159 mesh::Edge &e2 = mesh.createEdge(v2, v3);
160 mesh::Edge &e3 = mesh.createEdge(v3, v1);
161 mesh.createTriangle(e1, e2, e3);
162
163 mesh.setVertexOffsets({3, 3, 6, 7});
164
165 } else if (context.isRank(1)) {
166 // nothing
167 } else if (context.isRank(2)) {
168 mesh::Vertex &v1 = mesh.createVertex(Eigen::Vector3d::Constant(1));
169 mesh::Vertex &v2 = mesh.createVertex(Eigen::Vector3d::Constant(2));
170 mesh::Vertex &v3 = mesh.createVertex(Eigen::Vector3d{0.0, 1.0, 0.0});
171
172 mesh::Edge &e1 = mesh.createEdge(v1, v2);
173 mesh::Edge &e2 = mesh.createEdge(v2, v3);
174 mesh::Edge &e3 = mesh.createEdge(v3, v1);
175 mesh.createTriangle(e1, e2, e3);
176 } else if (context.isRank(3)) {
177 mesh.createVertex(Eigen::Vector3d::Constant(3.0));
178 }
179
180 io::ExportVTU exportVTU;
181 std::string filename = "io-ExportVTUTest-testExportTriangulatedMesh";
182 std::string location = "";
183 exportVTU.doExport(filename, location, mesh);
184}
185
186BOOST_AUTO_TEST_CASE(ExportSplitSquare)
187{
188 PRECICE_TEST(""_on(4_ranks).setupIntraComm());
189 int dim = 3;
190 mesh::Mesh mesh("MyMesh", dim, testing::nextMeshID());
191
192 mesh::Vertex &vm = mesh.createVertex(Eigen::Vector3d::Zero());
193 if (context.isRank(0)) {
194 mesh::Vertex &v1 = mesh.createVertex(Eigen::Vector3d{-1.0, 1.0, 0.0});
195 mesh::Vertex &v2 = mesh.createVertex(Eigen::Vector3d{1.0, 1.0, 0.0});
196 mesh::Vertex &vo = mesh.createVertex(Eigen::Vector3d{0.0, 2.0, 0.0});
197 mesh::Edge & em1 = mesh.createEdge(vm, v1);
198 mesh::Edge & e12 = mesh.createEdge(v1, v2);
199 mesh::Edge & e2m = mesh.createEdge(v2, vm);
200 mesh.createTriangle(em1, e12, e2m);
201 mesh::Edge &eo1 = mesh.createEdge(vo, v1);
202 mesh::Edge &e2o = mesh.createEdge(v2, vo);
203 mesh.createTriangle(eo1, e12, e2o);
204 mesh.setVertexOffsets({3, 6, 9, 12});
205
206 } else if (context.isRank(1)) {
207 mesh::Vertex &v1 = mesh.createVertex(Eigen::Vector3d{1.0, -1.0, 0.0});
208 mesh::Vertex &v2 = mesh.createVertex(Eigen::Vector3d{-1.0, -1.0, 0.0});
209 mesh::Vertex &vo = mesh.createVertex(Eigen::Vector3d{0.0, -2.0, 0.0});
210 mesh::Edge & em1 = mesh.createEdge(vm, v1);
211 mesh::Edge & e12 = mesh.createEdge(v1, v2);
212 mesh::Edge & e2m = mesh.createEdge(v2, vm);
213 mesh.createTriangle(em1, e12, e2m);
214 mesh::Edge &eo1 = mesh.createEdge(vo, v1);
215 mesh::Edge &e2o = mesh.createEdge(v2, vo);
216 mesh.createTriangle(eo1, e12, e2o);
217 } else if (context.isRank(2)) {
218 mesh::Vertex &v1 = mesh.createVertex(Eigen::Vector3d{-1.0, 1.0, 0.0});
219 mesh::Vertex &v2 = mesh.createVertex(Eigen::Vector3d{-1.0, -1.0, 0.0});
220 mesh::Vertex &vo = mesh.createVertex(Eigen::Vector3d{-2.0, 0.0, 0.0});
221 mesh::Edge & em1 = mesh.createEdge(vm, v1);
222 mesh::Edge & e12 = mesh.createEdge(v1, v2);
223 mesh::Edge & e2m = mesh.createEdge(v2, vm);
224 mesh.createTriangle(em1, e12, e2m);
225 mesh::Edge &eo1 = mesh.createEdge(vo, v1);
226 mesh::Edge &e2o = mesh.createEdge(v2, vo);
227 mesh.createTriangle(eo1, e12, e2o);
228 } else if (context.isRank(3)) {
229 mesh::Vertex &v1 = mesh.createVertex(Eigen::Vector3d{1.0, 1.0, 0.0});
230 mesh::Vertex &v2 = mesh.createVertex(Eigen::Vector3d{1.0, -1.0, 0.0});
231 mesh::Vertex &vo = mesh.createVertex(Eigen::Vector3d{2.0, 0.0, 0.0});
232 mesh::Edge & em1 = mesh.createEdge(vm, v1);
233 mesh::Edge & e12 = mesh.createEdge(v1, v2);
234 mesh::Edge & e2m = mesh.createEdge(v2, vm);
235 mesh.createTriangle(em1, e12, e2m);
236 mesh::Edge &eo1 = mesh.createEdge(vo, v1);
237 mesh::Edge &e2o = mesh.createEdge(v2, vo);
238 mesh.createTriangle(eo1, e12, e2o);
239 }
240
241 io::ExportVTU exportVTU;
242 std::string filename = "io-ExportVTUTest-Square";
243 std::string location = "";
244 exportVTU.doExport(filename, location, mesh);
245}
246
247BOOST_AUTO_TEST_CASE(ExportOneTetrahedron)
248{
249 PRECICE_TEST(""_on(1_rank).setupIntraComm());
250 int dim = 3;
251 mesh::Mesh mesh("MyMesh", dim, testing::nextMeshID());
252 mesh::Vertex &v0 = mesh.createVertex(Eigen::Vector3d::Zero());
253 mesh::Vertex &v1 = mesh.createVertex(Eigen::Vector3d{1.0, 0.0, 0.0});
254 mesh::Vertex &v2 = mesh.createVertex(Eigen::Vector3d{0.0, 1.0, 0.0});
255 mesh::Vertex &v3 = mesh.createVertex(Eigen::Vector3d{0.0, 0.0, 1.0});
256
257 mesh.createTetrahedron(v0, v1, v2, v3);
258
259 io::ExportVTU exportVTU;
260 std::string filename = "io-VTUExport-ExportOneTetrahedron";
261 std::string location = "";
262 exportVTU.doExport(filename, location, mesh);
263}
264
265BOOST_AUTO_TEST_CASE(ExportPartitionedCube)
266{
267 // Unit cube is made of 6 tetrahedra. We have 3 ranks with 2 tetra each
268 // as well as en empty rank. Empty rank is the 3rd
269 PRECICE_TEST(""_on(4_ranks).setupIntraComm());
270 int dim = 3;
271 mesh::Mesh mesh("MyMesh", dim, testing::nextMeshID());
272
273 if (context.isRank(0)) {
274 mesh::Vertex &v000 = mesh.createVertex(Eigen::Vector3d{0.0, 0.0, 0.0});
275 mesh::Vertex &v001 = mesh.createVertex(Eigen::Vector3d{0.0, 0.0, 1.0});
276 mesh::Vertex &v011 = mesh.createVertex(Eigen::Vector3d{0.0, 1.0, 1.0});
277 mesh::Vertex &v111 = mesh.createVertex(Eigen::Vector3d{1.0, 1.0, 1.0});
278 mesh::Vertex &v010 = mesh.createVertex(Eigen::Vector3d{0.0, 1.0, 0.0});
279
280 mesh.createTetrahedron(v000, v001, v011, v111);
281 mesh.createTetrahedron(v000, v010, v011, v111);
282 mesh.setVertexOffsets({4, 8, 8, 12});
283
284 } else if (context.isRank(1)) {
285 mesh::Vertex &v000 = mesh.createVertex(Eigen::Vector3d{0.0, 0.0, 0.0});
286 mesh::Vertex &v001 = mesh.createVertex(Eigen::Vector3d{0.0, 0.0, 1.0});
287 mesh::Vertex &v101 = mesh.createVertex(Eigen::Vector3d{1.0, 0.0, 1.0});
288 mesh::Vertex &v111 = mesh.createVertex(Eigen::Vector3d{1.0, 1.0, 1.0});
289 mesh::Vertex &v100 = mesh.createVertex(Eigen::Vector3d{1.0, 0.0, 0.0});
290
291 mesh.createTetrahedron(v000, v001, v101, v111);
292 mesh.createTetrahedron(v000, v100, v101, v111);
293 } else if (context.isRank(3)) {
294 mesh::Vertex &v000 = mesh.createVertex(Eigen::Vector3d{0.0, 0.0, 0.0});
295 mesh::Vertex &v010 = mesh.createVertex(Eigen::Vector3d{0.0, 1.0, 0.0});
296 mesh::Vertex &v100 = mesh.createVertex(Eigen::Vector3d{1.0, 0.0, 0.0});
297 mesh::Vertex &v111 = mesh.createVertex(Eigen::Vector3d{1.0, 1.0, 1.0});
298 mesh::Vertex &v110 = mesh.createVertex(Eigen::Vector3d{1.0, 1.0, 0.0});
299
300 mesh.createTetrahedron(v000, v010, v110, v111);
301 mesh.createTetrahedron(v000, v100, v110, v111);
302 }
303
304 io::ExportVTU exportVTU;
305 std::string filename = "io-ExportVTUTest-PartitionedCube";
306 std::string location = "";
307 exportVTU.doExport(filename, location, mesh);
308}
309
310BOOST_AUTO_TEST_SUITE_END() // IOTests
311BOOST_AUTO_TEST_SUITE_END() // VTUExport
312
313#endif // PRECICE_NO_MPI
BOOST_AUTO_TEST_CASE(ExportDataWithGradient2D)
BOOST_AUTO_TEST_SUITE(PreProcess)
BOOST_AUTO_TEST_SUITE_END()
#define PRECICE_TEST(...)
Definition Testing.hpp:27
void doExport(const std::string &name, const std::string &location, const mesh::Mesh &mesh) override
Does export. Has to be implemented in subclass.
Definition ExportXML.cpp:23
Linear edge of a mesh, defined by two Vertex objects.
Definition Edge.hpp:16
Container and creator for meshes.
Definition Mesh.hpp:39
Triangle & createTriangle(Edge &edgeOne, Edge &edgeTwo, Edge &edgeThree)
Creates and initializes a Triangle object.
Definition Mesh.cpp:119
PtrData & createData(const std::string &name, int dimension, DataID id, int waveformDegree=time::Time::DEFAULT_WAVEFORM_DEGREE)
Create only data for vertex.
Definition Mesh.cpp:151
Tetrahedron & createTetrahedron(Vertex &vertexOne, Vertex &vertexTwo, Vertex &vertexThree, Vertex &vertexFour)
Creates and initializes a Tetrahedron object.
Definition Mesh.cpp:141
void setVertexOffsets(VertexOffsets vertexOffsets)
Only used for tests.
Definition Mesh.hpp:254
Edge & createEdge(Vertex &vertexOne, Vertex &vertexTwo)
Creates and initializes an Edge object.
Definition Mesh.cpp:111
Vertex & createVertex(const Eigen::VectorXd &coords)
Creates and initializes a Vertex object.
Definition Mesh.cpp:103
void allocateDataValues()
Allocates memory for the vertex data values and corresponding gradient values.
Definition Mesh.cpp:233
Vertex of a mesh.
Definition Vertex.hpp:16
provides Mesh, Data and primitives.
Main namespace of the precice library.
T to_string(T... args)