preCICE v3.1.2
Loading...
Searching...
No Matches
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
19BOOST_AUTO_TEST_CASE(testDataContextWriteMapping)
20{
21 PRECICE_TEST(1_rank);
22
24
25 // Create mesh object for from mesh
26 int dimensions = 3;
27 mesh::PtrMesh ptrFromMesh = std::make_shared<mesh::Mesh>("ParticipantMesh", dimensions, testing::nextMeshID());
28 mesh::PtrData ptrFromData = ptrFromMesh->createData("MappedData", dimensions, 0_dataID);
29
30 ptrFromMesh->createVertex(Eigen::Vector3d(0.0, 0.0, 0.0));
31 ptrFromMesh->createVertex(Eigen::Vector3d(1.0, 0.0, 0.0));
32 ptrFromMesh->createVertex(Eigen::Vector3d(0.0, 0.0, 1.0));
33
34 // Create mesh object for from mesh
35 mesh::PtrMesh ptrToMesh = std::make_shared<mesh::Mesh>("OtherMesh", dimensions, testing::nextMeshID());
36 mesh::PtrData ptrToData = ptrToMesh->createData("MappedData", dimensions, 1_dataID);
37
38 ptrToMesh->createVertex(Eigen::Vector3d(0.0, 0.1, 0.0));
39 ptrToMesh->createVertex(Eigen::Vector3d(1.0, 0.1, 0.0));
40 ptrToMesh->createVertex(Eigen::Vector3d(0.0, 0.1, 1.0));
41
42 MeshContext toMeshContext;
43 toMeshContext.mesh = ptrToMesh;
44
45 WriteDataContext dataContext(ptrFromData, ptrFromMesh);
46
47 MappingContext mappingContext;
48 mappingContext.fromMeshID = ptrFromMesh->getID();
49 mappingContext.toMeshID = ptrToMesh->getID();
50
51 BOOST_TEST(ptrToData->getID() != ptrFromData->getID());
52 BOOST_TEST(ptrToMesh->getID() != ptrFromMesh->getID());
53
54 BOOST_TEST(!fixture.hasMapping(dataContext));
55 BOOST_TEST(fixture.getProvidedDataID(dataContext) == ptrFromData->getID());
56 BOOST_TEST(dataContext.getMeshID() == ptrFromMesh->getID());
57
58 dataContext.appendMappingConfiguration(mappingContext, toMeshContext);
59
60 // mapping is configured. Write mapping, therefore _providedData == _fromData
61 BOOST_TEST(fixture.hasMapping(dataContext));
62 BOOST_TEST(fixture.getFromDataID(dataContext, 0) == ptrFromData->getID());
63 BOOST_TEST(fixture.getToDataID(dataContext, 0) == ptrToData->getID());
64 BOOST_TEST(fixture.getProvidedDataID(dataContext) != ptrToData->getID());
65 BOOST_TEST(fixture.getProvidedDataID(dataContext) == ptrFromData->getID());
66 BOOST_TEST(dataContext.getMeshID() != ptrToMesh->getID());
67 BOOST_TEST(dataContext.getMeshID() == ptrFromMesh->getID());
68 BOOST_TEST(fixture.hasWriteMapping(dataContext));
69 BOOST_TEST(!fixture.hasReadMapping(dataContext));
70 BOOST_TEST(fixture.mappingContexts(dataContext)[0].fromMeshID == mappingContext.fromMeshID);
71 BOOST_TEST(fixture.mappingContexts(dataContext)[0].toMeshID == mappingContext.toMeshID);
72 BOOST_TEST(fixture.mappingContexts(dataContext)[0].mapping == mappingContext.mapping);
73}
74
75BOOST_AUTO_TEST_CASE(testDataContextMultipleWriteMapping)
76{
77 PRECICE_TEST(1_rank);
78
80
81 // Create mesh object for from mesh
82 int dimensions = 3;
83 mesh::PtrMesh ptrFromMesh = std::make_shared<mesh::Mesh>("ParticipantMesh", dimensions, testing::nextMeshID());
84 mesh::PtrData ptrFromData = ptrFromMesh->createData("MappedData", dimensions, 0_dataID);
85
86 ptrFromMesh->createVertex(Eigen::Vector3d(0.0, 0.0, 0.0));
87 ptrFromMesh->createVertex(Eigen::Vector3d(1.0, 0.0, 0.0));
88 ptrFromMesh->createVertex(Eigen::Vector3d(0.0, 0.0, 1.0));
89
90 // Create mesh object for to mesh
91 mesh::PtrMesh ptrToMesh = std::make_shared<mesh::Mesh>("OtherMesh", dimensions, testing::nextMeshID());
92 mesh::PtrData ptrToData = ptrToMesh->createData("MappedData", dimensions, 1_dataID);
93
94 ptrToMesh->createVertex(Eigen::Vector3d(0.0, 0.1, 0.0));
95 ptrToMesh->createVertex(Eigen::Vector3d(1.0, 0.1, 0.0));
96 ptrToMesh->createVertex(Eigen::Vector3d(0.0, 0.1, 1.0));
97
98 MeshContext toMeshContext1;
99 toMeshContext1.mesh = ptrToMesh;
100
101 WriteDataContext dataContext(ptrFromData, ptrFromMesh);
102
103 MappingContext mappingContext;
104 mappingContext.fromMeshID = ptrFromMesh->getID();
105 mappingContext.toMeshID = ptrToMesh->getID();
106
107 BOOST_TEST(ptrToData->getID() != ptrFromData->getID());
108 BOOST_TEST(ptrToMesh->getID() != ptrFromMesh->getID());
109
110 BOOST_TEST(!fixture.hasMapping(dataContext));
111 BOOST_TEST(fixture.getProvidedDataID(dataContext) == ptrFromData->getID());
112 BOOST_TEST(dataContext.getMeshID() == ptrFromMesh->getID());
113
114 // Add the first mapping we configured for this context
115 dataContext.appendMappingConfiguration(mappingContext, toMeshContext1);
116
117 // Add a second mapping targeting a different to mesh
118 // Create the object for to mesh and the data
119 mesh::PtrMesh ptrToMesh2 = std::make_shared<mesh::Mesh>("SecondOtherMesh", dimensions, testing::nextMeshID());
120 mesh::PtrData ptrToData2 = ptrToMesh2->createData("MappedData", dimensions, 2_dataID);
121
122 ptrToMesh2->createVertex(Eigen::Vector3d(0.0, 1.1, 0.0));
123 ptrToMesh2->createVertex(Eigen::Vector3d(2.0, 1.1, 0.0));
124 ptrToMesh2->createVertex(Eigen::Vector3d(0.0, 2.1, 4.0));
125
126 MeshContext toMeshContext2;
127 toMeshContext2.mesh = ptrToMesh2;
128
129 MappingContext mappingContext2;
130 mappingContext2.fromMeshID = ptrFromMesh->getID();
131 mappingContext2.toMeshID = ptrToMesh2->getID();
132
133 // the mapping configuration
134 dataContext.appendMappingConfiguration(mappingContext2, toMeshContext2);
135
136 // First, we repeat the checks from above in order to check that nothing changed
137 BOOST_TEST(fixture.hasMapping(dataContext));
138 BOOST_TEST(fixture.getProvidedDataID(dataContext) != ptrToData->getID());
139 BOOST_TEST(fixture.getProvidedDataID(dataContext) == ptrFromData->getID());
140 BOOST_TEST(dataContext.getMeshID() != ptrToMesh->getID());
141 BOOST_TEST(dataContext.getMeshID() == ptrFromMesh->getID());
142 BOOST_TEST(fixture.hasWriteMapping(dataContext));
143 BOOST_TEST(!fixture.hasReadMapping(dataContext));
144 // Test dedicated content of the first mapping configuration
145 BOOST_TEST(fixture.getFromDataID(dataContext, 0) == ptrFromData->getID());
146 BOOST_TEST(fixture.getToDataID(dataContext, 0) == ptrToData->getID());
147 BOOST_TEST(fixture.mappingContexts(dataContext)[0].fromMeshID == mappingContext.fromMeshID);
148 BOOST_TEST(fixture.mappingContexts(dataContext)[0].toMeshID == mappingContext.toMeshID);
149 BOOST_TEST(fixture.mappingContexts(dataContext)[0].mapping == mappingContext.mapping);
150
151 // Now, test the newly added mapping
152 BOOST_TEST(fixture.getProvidedDataID(dataContext) != ptrToData2->getID());
153 BOOST_TEST(dataContext.getMeshID() != ptrToMesh2->getID());
154 BOOST_TEST(fixture.hasWriteMapping(dataContext));
155 BOOST_TEST(!fixture.hasReadMapping(dataContext));
156 // Test dedicated content of the first mapping configuration
157 BOOST_TEST(fixture.getFromDataID(dataContext, 1) == ptrFromData->getID());
158 BOOST_TEST(fixture.getToDataID(dataContext, 1) == ptrToData2->getID());
159 BOOST_TEST(fixture.mappingContexts(dataContext)[1].fromMeshID == mappingContext2.fromMeshID);
160 BOOST_TEST(fixture.mappingContexts(dataContext)[1].toMeshID == mappingContext2.toMeshID);
161 BOOST_TEST(fixture.mappingContexts(dataContext)[1].mapping == mappingContext2.mapping);
162}
163
164BOOST_AUTO_TEST_CASE(testDataContextWriteBuffer)
165{
166 PRECICE_TEST(1_rank);
167
169
170 // Create mesh object for from mesh
171 int dimensions = 3;
172 int dataDimensions = 1;
173 int nValues = 3;
174 mesh::PtrMesh ptrMesh = std::make_shared<mesh::Mesh>("Mesh", dimensions, testing::nextMeshID());
175 mesh::PtrData ptrData = ptrMesh->createData("Data", dataDimensions, 0_dataID);
176
177 std::vector<VertexID> ids(nValues);
178
179 ids[0] = ptrMesh->createVertex(Eigen::Vector3d(0.0, 0.0, 0.0)).getID();
180 ids[1] = ptrMesh->createVertex(Eigen::Vector3d(1.0, 0.0, 0.0)).getID();
181 ids[2] = ptrMesh->createVertex(Eigen::Vector3d(0.0, 0.0, 1.0)).getID();
182
183 BOOST_TEST(ids[0] == 0);
184 BOOST_TEST(ids[1] == 1);
185 BOOST_TEST(ids[2] == 2);
186
187 std::vector<double> values(nValues);
188 for (int i = 0; i < nValues; i++) {
189 values[i] = i;
190 }
191
192 WriteDataContext dataContext(ptrData, ptrMesh);
193
194 BOOST_TEST(!fixture.hasMapping(dataContext));
195 BOOST_TEST(fixture.getProvidedDataID(dataContext) == ptrData->getID());
196 BOOST_TEST(dataContext.getMeshID() == ptrMesh->getID());
197
198 dataContext.resizeBufferTo(nValues);
199 dataContext.writeValuesIntoDataBuffer(ids, values);
200
201 // add another vertex
202 ids.emplace_back(ptrMesh->createVertex(Eigen::Vector3d(0.0, 1.0, 0.0)).getID());
203 values.emplace_back(nValues);
204
205 BOOST_TEST(ids[3] == 3);
206
207 dataContext.resizeBufferTo(nValues + 1);
208 dataContext.writeValuesIntoDataBuffer(ids, values);
209}
210
211BOOST_AUTO_TEST_CASE(testDataContextReadMapping)
212{
213 PRECICE_TEST(1_rank);
214
216
217 // Create mesh object
218 int dimensions = 3;
219 mesh::PtrMesh ptrToMesh = std::make_shared<mesh::Mesh>("ParticipantMesh", dimensions, testing::nextMeshID());
220 mesh::PtrData ptrToData = ptrToMesh->createData("MappedData", dimensions, 0_dataID);
221
222 ptrToMesh->createVertex(Eigen::Vector3d(0.0, 0.0, 0.0));
223 ptrToMesh->createVertex(Eigen::Vector3d(1.0, 0.0, 0.0));
224 ptrToMesh->createVertex(Eigen::Vector3d(0.0, 0.0, 1.0));
225
226 // Create mesh object for from mesh
227 mesh::PtrMesh ptrFromMesh = std::make_shared<mesh::Mesh>("OtherMesh", dimensions, testing::nextMeshID());
228 mesh::PtrData ptrFromData = ptrFromMesh->createData("MappedData", dimensions, 1_dataID);
229
230 ptrFromMesh->createVertex(Eigen::Vector3d(0.0, 0.1, 0.0));
231 ptrFromMesh->createVertex(Eigen::Vector3d(1.0, 0.1, 0.0));
232 ptrFromMesh->createVertex(Eigen::Vector3d(0.0, 0.1, 1.0));
233
234 MeshContext fromMeshContext;
235 fromMeshContext.mesh = ptrFromMesh;
236
237 ReadDataContext dataContext(ptrToData, ptrToMesh);
238
239 MappingContext mappingContext;
240 mappingContext.fromMeshID = ptrFromMesh->getID();
241 mappingContext.toMeshID = ptrToMesh->getID();
242
243 BOOST_TEST(ptrToData->getID() != ptrFromData->getID());
244 BOOST_TEST(ptrToMesh->getID() != ptrFromMesh->getID());
245
246 BOOST_TEST(!fixture.hasMapping(dataContext));
247 BOOST_TEST(fixture.getProvidedDataID(dataContext) == ptrToData->getID());
248 BOOST_TEST(dataContext.getMeshID() == ptrToMesh->getID());
249
250 // Add the mapping we configured for this context
251 // For read data contexts, there is only one context allowed
252 dataContext.appendMappingConfiguration(mappingContext, fromMeshContext);
253
254 // mapping is configured. Write mapping, therefore _providedData == _toData
255 BOOST_TEST(fixture.hasMapping(dataContext));
256 BOOST_TEST(fixture.getFromDataID(dataContext, 0) == ptrFromData->getID());
257 BOOST_TEST(fixture.getToDataID(dataContext, 0) == ptrToData->getID());
258 BOOST_TEST(fixture.getProvidedDataID(dataContext) == ptrToData->getID());
259 BOOST_TEST(fixture.getProvidedDataID(dataContext) != ptrFromData->getID());
260 BOOST_TEST(dataContext.getMeshID() == ptrToMesh->getID());
261 BOOST_TEST(dataContext.getMeshID() != ptrFromMesh->getID());
262 BOOST_TEST(!fixture.hasWriteMapping(dataContext));
263 BOOST_TEST(fixture.hasReadMapping(dataContext));
264 BOOST_TEST(fixture.mappingContexts(dataContext)[0].fromMeshID == mappingContext.fromMeshID);
265 BOOST_TEST(fixture.mappingContexts(dataContext)[0].toMeshID == mappingContext.toMeshID);
266 BOOST_TEST(fixture.mappingContexts(dataContext)[0].mapping == mappingContext.mapping);
267}
268
BOOST_AUTO_TEST_CASE(testDataContextWriteMapping)
BOOST_AUTO_TEST_SUITE(PreProcess)
BOOST_AUTO_TEST_SUITE_END()
#define PRECICE_TEST(...)
Definition Testing.hpp:27
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)
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.