preCICE v3.2.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DataContextTest.cpp
Go to the documentation of this file.
1#include <Eigen/Core>
2#include <string>
3#include "mesh/Data.hpp"
4#include "mesh/Mesh.hpp"
5#include "mesh/Vertex.hpp"
10#include "testing/Testing.hpp"
11
12using namespace precice;
13using namespace precice::impl;
14
15BOOST_AUTO_TEST_SUITE(PreciceTests)
16
17BOOST_AUTO_TEST_SUITE(DataContextTests)
18
20BOOST_AUTO_TEST_CASE(testDataContextWriteMapping)
21{
23
25
26 // Create mesh object for from mesh
27 int dimensions = 3;
28 mesh::PtrMesh ptrFromMesh = std::make_shared<mesh::Mesh>("ParticipantMesh", dimensions, testing::nextMeshID());
29 mesh::PtrData ptrFromData = ptrFromMesh->createData("MappedData", dimensions, 0_dataID);
30
31 ptrFromMesh->createVertex(Eigen::Vector3d(0.0, 0.0, 0.0));
32 ptrFromMesh->createVertex(Eigen::Vector3d(1.0, 0.0, 0.0));
33 ptrFromMesh->createVertex(Eigen::Vector3d(0.0, 0.0, 1.0));
34
35 // Create mesh object for from mesh
36 mesh::PtrMesh ptrToMesh = std::make_shared<mesh::Mesh>("OtherMesh", dimensions, testing::nextMeshID());
37 mesh::PtrData ptrToData = ptrToMesh->createData("MappedData", dimensions, 1_dataID);
38
39 ptrToMesh->createVertex(Eigen::Vector3d(0.0, 0.1, 0.0));
40 ptrToMesh->createVertex(Eigen::Vector3d(1.0, 0.1, 0.0));
41 ptrToMesh->createVertex(Eigen::Vector3d(0.0, 0.1, 1.0));
42
43 MeshContext toMeshContext;
44 toMeshContext.mesh = ptrToMesh;
45
46 WriteDataContext dataContext(ptrFromData, ptrFromMesh);
47
48 MappingContext mappingContext;
49 mappingContext.fromMeshID = ptrFromMesh->getID();
50 mappingContext.toMeshID = ptrToMesh->getID();
51
52 BOOST_TEST(ptrToData->getID() != ptrFromData->getID());
53 BOOST_TEST(ptrToMesh->getID() != ptrFromMesh->getID());
54
55 BOOST_TEST(!fixture.hasMapping(dataContext));
56 BOOST_TEST(fixture.getProvidedDataID(dataContext) == ptrFromData->getID());
57 BOOST_TEST(dataContext.getMeshID() == ptrFromMesh->getID());
58
59 dataContext.appendMappingConfiguration(mappingContext, toMeshContext);
60
61 // mapping is configured. Write mapping, therefore _providedData == _fromData
62 BOOST_TEST(fixture.hasMapping(dataContext));
63 BOOST_TEST(fixture.getFromDataID(dataContext, 0) == ptrFromData->getID());
64 BOOST_TEST(fixture.getToDataID(dataContext, 0) == ptrToData->getID());
65 BOOST_TEST(fixture.getProvidedDataID(dataContext) != ptrToData->getID());
66 BOOST_TEST(fixture.getProvidedDataID(dataContext) == ptrFromData->getID());
67 BOOST_TEST(dataContext.getMeshID() != ptrToMesh->getID());
68 BOOST_TEST(dataContext.getMeshID() == ptrFromMesh->getID());
69 BOOST_TEST(fixture.hasWriteMapping(dataContext));
70 BOOST_TEST(!fixture.hasReadMapping(dataContext));
71 BOOST_TEST(fixture.mappingContexts(dataContext)[0].fromMeshID == mappingContext.fromMeshID);
72 BOOST_TEST(fixture.mappingContexts(dataContext)[0].toMeshID == mappingContext.toMeshID);
73 BOOST_TEST(fixture.mappingContexts(dataContext)[0].mapping == mappingContext.mapping);
74}
75
77BOOST_AUTO_TEST_CASE(testDataContextMultipleWriteMapping)
78{
80
82
83 // Create mesh object for from mesh
84 int dimensions = 3;
85 mesh::PtrMesh ptrFromMesh = std::make_shared<mesh::Mesh>("ParticipantMesh", dimensions, testing::nextMeshID());
86 mesh::PtrData ptrFromData = ptrFromMesh->createData("MappedData", dimensions, 0_dataID);
87
88 ptrFromMesh->createVertex(Eigen::Vector3d(0.0, 0.0, 0.0));
89 ptrFromMesh->createVertex(Eigen::Vector3d(1.0, 0.0, 0.0));
90 ptrFromMesh->createVertex(Eigen::Vector3d(0.0, 0.0, 1.0));
91
92 // Create mesh object for to mesh
93 mesh::PtrMesh ptrToMesh = std::make_shared<mesh::Mesh>("OtherMesh", dimensions, testing::nextMeshID());
94 mesh::PtrData ptrToData = ptrToMesh->createData("MappedData", dimensions, 1_dataID);
95
96 ptrToMesh->createVertex(Eigen::Vector3d(0.0, 0.1, 0.0));
97 ptrToMesh->createVertex(Eigen::Vector3d(1.0, 0.1, 0.0));
98 ptrToMesh->createVertex(Eigen::Vector3d(0.0, 0.1, 1.0));
99
100 MeshContext toMeshContext1;
101 toMeshContext1.mesh = ptrToMesh;
102
103 WriteDataContext dataContext(ptrFromData, ptrFromMesh);
104
105 MappingContext mappingContext;
106 mappingContext.fromMeshID = ptrFromMesh->getID();
107 mappingContext.toMeshID = ptrToMesh->getID();
108
109 BOOST_TEST(ptrToData->getID() != ptrFromData->getID());
110 BOOST_TEST(ptrToMesh->getID() != ptrFromMesh->getID());
111
112 BOOST_TEST(!fixture.hasMapping(dataContext));
113 BOOST_TEST(fixture.getProvidedDataID(dataContext) == ptrFromData->getID());
114 BOOST_TEST(dataContext.getMeshID() == ptrFromMesh->getID());
115
116 // Add the first mapping we configured for this context
117 dataContext.appendMappingConfiguration(mappingContext, toMeshContext1);
118
119 // Add a second mapping targeting a different to mesh
120 // Create the object for to mesh and the data
121 mesh::PtrMesh ptrToMesh2 = std::make_shared<mesh::Mesh>("SecondOtherMesh", dimensions, testing::nextMeshID());
122 mesh::PtrData ptrToData2 = ptrToMesh2->createData("MappedData", dimensions, 2_dataID);
123
124 ptrToMesh2->createVertex(Eigen::Vector3d(0.0, 1.1, 0.0));
125 ptrToMesh2->createVertex(Eigen::Vector3d(2.0, 1.1, 0.0));
126 ptrToMesh2->createVertex(Eigen::Vector3d(0.0, 2.1, 4.0));
127
128 MeshContext toMeshContext2;
129 toMeshContext2.mesh = ptrToMesh2;
130
131 MappingContext mappingContext2;
132 mappingContext2.fromMeshID = ptrFromMesh->getID();
133 mappingContext2.toMeshID = ptrToMesh2->getID();
134
135 // the mapping configuration
136 dataContext.appendMappingConfiguration(mappingContext2, toMeshContext2);
137
138 // First, we repeat the checks from above in order to check that nothing changed
139 BOOST_TEST(fixture.hasMapping(dataContext));
140 BOOST_TEST(fixture.getProvidedDataID(dataContext) != ptrToData->getID());
141 BOOST_TEST(fixture.getProvidedDataID(dataContext) == ptrFromData->getID());
142 BOOST_TEST(dataContext.getMeshID() != ptrToMesh->getID());
143 BOOST_TEST(dataContext.getMeshID() == ptrFromMesh->getID());
144 BOOST_TEST(fixture.hasWriteMapping(dataContext));
145 BOOST_TEST(!fixture.hasReadMapping(dataContext));
146 // Test dedicated content of the first mapping configuration
147 BOOST_TEST(fixture.getFromDataID(dataContext, 0) == ptrFromData->getID());
148 BOOST_TEST(fixture.getToDataID(dataContext, 0) == ptrToData->getID());
149 BOOST_TEST(fixture.mappingContexts(dataContext)[0].fromMeshID == mappingContext.fromMeshID);
150 BOOST_TEST(fixture.mappingContexts(dataContext)[0].toMeshID == mappingContext.toMeshID);
151 BOOST_TEST(fixture.mappingContexts(dataContext)[0].mapping == mappingContext.mapping);
152
153 // Now, test the newly added mapping
154 BOOST_TEST(fixture.getProvidedDataID(dataContext) != ptrToData2->getID());
155 BOOST_TEST(dataContext.getMeshID() != ptrToMesh2->getID());
156 BOOST_TEST(fixture.hasWriteMapping(dataContext));
157 BOOST_TEST(!fixture.hasReadMapping(dataContext));
158 // Test dedicated content of the first mapping configuration
159 BOOST_TEST(fixture.getFromDataID(dataContext, 1) == ptrFromData->getID());
160 BOOST_TEST(fixture.getToDataID(dataContext, 1) == ptrToData2->getID());
161 BOOST_TEST(fixture.mappingContexts(dataContext)[1].fromMeshID == mappingContext2.fromMeshID);
162 BOOST_TEST(fixture.mappingContexts(dataContext)[1].toMeshID == mappingContext2.toMeshID);
163 BOOST_TEST(fixture.mappingContexts(dataContext)[1].mapping == mappingContext2.mapping);
164}
165
166PRECICE_TEST_SETUP(1_rank)
167BOOST_AUTO_TEST_CASE(testDataContextWriteBuffer)
168{
169 PRECICE_TEST();
170
172
173 // Create mesh object for from mesh
174 int dimensions = 3;
175 int dataDimensions = 1;
176 int nValues = 3;
177 mesh::PtrMesh ptrMesh = std::make_shared<mesh::Mesh>("Mesh", dimensions, testing::nextMeshID());
178 mesh::PtrData ptrData = ptrMesh->createData("Data", dataDimensions, 0_dataID);
179
180 std::vector<VertexID> ids(nValues);
181
182 ids[0] = ptrMesh->createVertex(Eigen::Vector3d(0.0, 0.0, 0.0)).getID();
183 ids[1] = ptrMesh->createVertex(Eigen::Vector3d(1.0, 0.0, 0.0)).getID();
184 ids[2] = ptrMesh->createVertex(Eigen::Vector3d(0.0, 0.0, 1.0)).getID();
185
186 BOOST_TEST(ids[0] == 0);
187 BOOST_TEST(ids[1] == 1);
188 BOOST_TEST(ids[2] == 2);
189
190 std::vector<double> values(nValues);
191 for (int i = 0; i < nValues; i++) {
192 values[i] = i;
193 }
194
195 WriteDataContext dataContext(ptrData, ptrMesh);
196
197 BOOST_TEST(!fixture.hasMapping(dataContext));
198 BOOST_TEST(fixture.getProvidedDataID(dataContext) == ptrData->getID());
199 BOOST_TEST(dataContext.getMeshID() == ptrMesh->getID());
200
201 dataContext.resizeBufferTo(nValues);
202 dataContext.writeValuesIntoDataBuffer(ids, values);
203
204 // add another vertex
205 ids.emplace_back(ptrMesh->createVertex(Eigen::Vector3d(0.0, 1.0, 0.0)).getID());
206 values.emplace_back(nValues);
207
208 BOOST_TEST(ids[3] == 3);
209
210 dataContext.resizeBufferTo(nValues + 1);
211 dataContext.writeValuesIntoDataBuffer(ids, values);
212}
213
214PRECICE_TEST_SETUP(1_rank)
215BOOST_AUTO_TEST_CASE(testDataContextReadMapping)
216{
217 PRECICE_TEST();
218
220
221 // Create mesh object
222 int dimensions = 3;
223 mesh::PtrMesh ptrToMesh = std::make_shared<mesh::Mesh>("ParticipantMesh", dimensions, testing::nextMeshID());
224 mesh::PtrData ptrToData = ptrToMesh->createData("MappedData", dimensions, 0_dataID);
225
226 ptrToMesh->createVertex(Eigen::Vector3d(0.0, 0.0, 0.0));
227 ptrToMesh->createVertex(Eigen::Vector3d(1.0, 0.0, 0.0));
228 ptrToMesh->createVertex(Eigen::Vector3d(0.0, 0.0, 1.0));
229
230 // Create mesh object for from mesh
231 mesh::PtrMesh ptrFromMesh = std::make_shared<mesh::Mesh>("OtherMesh", dimensions, testing::nextMeshID());
232 mesh::PtrData ptrFromData = ptrFromMesh->createData("MappedData", dimensions, 1_dataID);
233
234 ptrFromMesh->createVertex(Eigen::Vector3d(0.0, 0.1, 0.0));
235 ptrFromMesh->createVertex(Eigen::Vector3d(1.0, 0.1, 0.0));
236 ptrFromMesh->createVertex(Eigen::Vector3d(0.0, 0.1, 1.0));
237
238 MeshContext fromMeshContext;
239 fromMeshContext.mesh = ptrFromMesh;
240
241 ReadDataContext dataContext(ptrToData, ptrToMesh);
242
243 MappingContext mappingContext;
244 mappingContext.fromMeshID = ptrFromMesh->getID();
245 mappingContext.toMeshID = ptrToMesh->getID();
246
247 BOOST_TEST(ptrToData->getID() != ptrFromData->getID());
248 BOOST_TEST(ptrToMesh->getID() != ptrFromMesh->getID());
249
250 BOOST_TEST(!fixture.hasMapping(dataContext));
251 BOOST_TEST(fixture.getProvidedDataID(dataContext) == ptrToData->getID());
252 BOOST_TEST(dataContext.getMeshID() == ptrToMesh->getID());
253
254 // Add the mapping we configured for this context
255 // For read data contexts, there is only one context allowed
256 dataContext.appendMappingConfiguration(mappingContext, fromMeshContext);
257
258 // mapping is configured. Write mapping, therefore _providedData == _toData
259 BOOST_TEST(fixture.hasMapping(dataContext));
260 BOOST_TEST(fixture.getFromDataID(dataContext, 0) == ptrFromData->getID());
261 BOOST_TEST(fixture.getToDataID(dataContext, 0) == ptrToData->getID());
262 BOOST_TEST(fixture.getProvidedDataID(dataContext) == ptrToData->getID());
263 BOOST_TEST(fixture.getProvidedDataID(dataContext) != ptrFromData->getID());
264 BOOST_TEST(dataContext.getMeshID() == ptrToMesh->getID());
265 BOOST_TEST(dataContext.getMeshID() != ptrFromMesh->getID());
266 BOOST_TEST(!fixture.hasWriteMapping(dataContext));
267 BOOST_TEST(fixture.hasReadMapping(dataContext));
268 BOOST_TEST(fixture.mappingContexts(dataContext)[0].fromMeshID == mappingContext.fromMeshID);
269 BOOST_TEST(fixture.mappingContexts(dataContext)[0].toMeshID == mappingContext.toMeshID);
270 BOOST_TEST(fixture.mappingContexts(dataContext)[0].mapping == mappingContext.mapping);
271}
272
BOOST_AUTO_TEST_CASE(testDataContextWriteMapping)
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
MeshID getMeshID() const
Get the ID of _mesh.
Stores one Data object with related mesh. Context stores data to be read from and potentially provide...
void appendMappingConfiguration(MappingContext &mappingContext, const MeshContext &meshContext) override
Adds a MappingContext and the MeshContext required by the read mapping to the corresponding ReadDataC...
Stores one Data object with related mesh. Context stores data to be written to and potentially provid...
void writeValuesIntoDataBuffer(::precice::span< const VertexID > vertices, ::precice::span< const double > values)
Store values in _writeDataBuffer.
void appendMappingConfiguration(MappingContext &mappingContext, const MeshContext &meshContext) override
Adds a MappingContext and the MeshContext required by the write mapping to the corresponding WriteDat...
int getToDataID(precice::impl::DataContext &dataContext, int dataVectorIndex)
int getProvidedDataID(precice::impl::DataContext &dataContext)
bool hasReadMapping(precice::impl::DataContext &dataContext)
int getFromDataID(precice::impl::DataContext &dataContext, int dataVectorIndex)
bool hasWriteMapping(precice::impl::DataContext &dataContext)
bool hasMapping(precice::impl::DataContext &dataContext)
std::vector< impl::MappingContext > mappingContexts(precice::impl::DataContext &dataContext)
T emplace_back(T... args)
T make_unique(T... args)
Main namespace of the precice library.
Holds a data mapping and related information.
mapping::PtrMapping mapping
Data mapping.
MeshID fromMeshID
id of mesh from which is mapped
MeshID toMeshID
id of mesh to which is mapped
Stores a mesh and related objects and data.
mesh::PtrMesh mesh
Mesh holding the geometry data structure.