preCICE v3.1.2
Loading...
Searching...
No Matches
SerializedStamplesTest.cpp
Go to the documentation of this file.
5#include "mesh/Mesh.hpp"
8#include "testing/Testing.hpp"
10
11using namespace precice;
12using namespace precice::com;
14
15BOOST_AUTO_TEST_SUITE(CommunicationTests)
16
17BOOST_AUTO_TEST_SUITE(SerializedStamples)
18
19BOOST_AUTO_TEST_CASE(SerializeValues)
20{
21 std::vector<int> vertexOffsets{4, 8, 8, 10};
22
23 const int meshDimensions = 3;
24 const int dataDimensions = 1;
25 const int nValues = 4;
26 const int nTimeSteps = 3;
27
28 mesh::PtrMesh dummyMesh(new mesh::Mesh("DummyMesh", 3, testing::nextMeshID()));
29 dummyMesh->setVertexOffsets(vertexOffsets);
30
31 mesh::PtrData fromData(new mesh::Data("from", -1, dataDimensions));
32 mesh::PtrData toData(new mesh::Data("to", -1, dataDimensions));
33
34 Eigen::VectorXd insert0(nValues);
35 insert0 << 1.0, 2.0, 3.0, 4.0;
36 Eigen::VectorXd insert05(nValues);
37 insert05 << 10.0, 20.0, 30.0, 40.0;
38 Eigen::VectorXd insert1(nValues);
39 insert1 << 100.0, 200.0, 300.0, 400.0;
40
41 cplscheme::PtrCouplingData fromDataPtr = makeCouplingData(fromData, dummyMesh);
42 cplscheme::PtrCouplingData toDataPtr = makeCouplingData(toData, dummyMesh);
43
44 fromDataPtr->setSampleAtTime(0, time::Sample{dataDimensions, insert0});
45 fromDataPtr->setSampleAtTime(0.5, time::Sample{dataDimensions, insert05});
46 fromDataPtr->setSampleAtTime(1, time::Sample{dataDimensions, insert1});
47
48 const auto serialized = serialize::SerializedStamples::serialize(fromDataPtr);
49
50 Eigen::VectorXd expectedSerialized(nTimeSteps * nValues);
51 expectedSerialized << 1.0, 10.0, 100.0, 2.0, 20.0, 200.0, 3.0, 30.0, 300.0, 4.0, 40.0, 400.0;
52
53 for (int i = 0; i < nTimeSteps * nValues; i++) {
54 BOOST_TEST(testing::equals(serialized.values()(i), expectedSerialized(i)));
55 }
56}
57
58BOOST_AUTO_TEST_CASE(DeserializeValues)
59{
60 std::vector<int> vertexOffsets{4, 8, 8, 10};
61
62 const int meshDimensions = 3;
63 const int nValues = 4;
64 const int nTimeSteps = 3;
65
66 Eigen::VectorXd serializedValues(nTimeSteps * nValues);
67 serializedValues << 1.0, 10.0, 100.0, 2.0, 20.0, 200.0, 3.0, 30.0, 300.0, 4.0, 40.0, 400.0;
68
69 mesh::PtrMesh dummyMesh(new mesh::Mesh("DummyMesh", 3, testing::nextMeshID()));
70 dummyMesh->setVertexOffsets(vertexOffsets);
71
72 mesh::PtrData toData(new mesh::Data("to", -1, 1));
73 cplscheme::PtrCouplingData toDataPtr = makeCouplingData(toData, dummyMesh);
74 toDataPtr->sample().values = Eigen::VectorXd(nValues);
75 toDataPtr->sample().setZero();
76
77 toDataPtr->setSampleAtTime(0, toDataPtr->sample());
78
79 Eigen::VectorXd timeStamps(nTimeSteps);
80 timeStamps << 0, 0.5, 1;
81
82 auto serialized = serialize::SerializedStamples::empty(timeStamps, toDataPtr);
83
84 serialized.values() = serializedValues;
85
86 serialized.deserializeInto(timeStamps, toDataPtr);
87
88 std::vector<Eigen::VectorXd> expectedValues;
89
90 Eigen::VectorXd insert0(nValues);
91 insert0 << 1.0, 2.0, 3.0, 4.0;
92 Eigen::VectorXd insert05(nValues);
93 insert05 << 10.0, 20.0, 30.0, 40.0;
94 Eigen::VectorXd insert1(nValues);
95 insert1 << 100.0, 200.0, 300.0, 400.0;
96
97 expectedValues.push_back(insert0);
98 expectedValues.push_back(insert05);
99 expectedValues.push_back(insert1);
100
101 BOOST_TEST(toDataPtr->timeStepsStorage().stamples().size() == nTimeSteps);
102 for (int t = 0; t < nTimeSteps; t++) {
103 BOOST_TEST(testing::equals(toDataPtr->timeStepsStorage().stamples()[t].timestamp, timeStamps[t]));
104 for (int i = 0; i < nValues; i++) {
105 BOOST_TEST(testing::equals(toDataPtr->timeStepsStorage().stamples()[t].sample.values(i), expectedValues[t](i)));
106 }
107 }
108}
109
110BOOST_AUTO_TEST_CASE(SerializeValuesAndGradients)
111{
112 std::vector<int> vertexOffsets{4, 8, 8, 10};
113
114 const int meshDimensions = 3;
115 const int dataDimensions = 1;
116 const int nValues = 4;
117 const int nTimeSteps = 3;
118
119 mesh::PtrMesh dummyMesh(new mesh::Mesh("DummyMesh", meshDimensions, testing::nextMeshID()));
120 dummyMesh->setVertexOffsets(vertexOffsets);
121
122 mesh::PtrData fromData(new mesh::Data("from", -1, dataDimensions));
123 fromData->requireDataGradient();
124
125 Eigen::VectorXd insert0(nValues);
126 insert0 << 1.0, 2.0, 3.0, 4.0;
127 Eigen::VectorXd insert05(nValues);
128 insert05 << 10.0, 20.0, 30.0, 40.0;
129 Eigen::VectorXd insert1(nValues);
130 insert1 << 100.0, 200.0, 300.0, 400.0;
131 Eigen::MatrixXd insertGradients0(nValues, meshDimensions);
132 insertGradients0 << 1.0, 2.0, 3.0,
133 4.0, 1.0, 2.0,
134 3.0, 4.0, 1.0,
135 2.0, 3.0, 4.0;
136 Eigen::MatrixXd insertGradients05(nValues, meshDimensions);
137 insertGradients05 << 11.0, 21.0, 31.0,
138 41.0, 11.0, 21.0,
139 31.0, 41.0, 11.0,
140 21.0, 31.0, 41.0;
141 Eigen::MatrixXd insertGradients1(nValues, meshDimensions);
142 insertGradients1 << 111.0, 211.0, 311.0,
143 411.0, 111.0, 211.0,
144 311.0, 411.0, 111.0,
145 211.0, 311.0, 411.0;
146
147 cplscheme::PtrCouplingData fromDataPtr = makeCouplingData(fromData, dummyMesh);
148
149 fromDataPtr->setSampleAtTime(0, time::Sample{dataDimensions, insert0, insertGradients0});
150 fromDataPtr->setSampleAtTime(0.5, time::Sample{dataDimensions, insert05, insertGradients05});
151 fromDataPtr->setSampleAtTime(1, time::Sample{dataDimensions, insert1, insertGradients1});
152
153 const auto serialized = serialize::SerializedStamples::serialize(fromDataPtr);
154
155 Eigen::VectorXd expectedSerializedValues(nTimeSteps * nValues);
156 expectedSerializedValues << 1.0, 10.0, 100.0, 2.0, 20.0, 200.0, 3.0, 30.0, 300.0, 4.0, 40.0, 400.0;
157
158 Eigen::VectorXd expectedSerializedGradients(nTimeSteps * nValues * meshDimensions);
159 expectedSerializedGradients << 1.0, 11.0, 111.0, 4.0, 41.0, 411.0, 3.0, 31.0, 311.0, 2.0, 21.0, 211.0, 2.0, 21.0, 211.0, 1.0, 11.0, 111.0, 4.0, 41.0, 411.0, 3.0, 31.0, 311.0, 3.0, 31.0, 311.0, 2.0, 21.0, 211.0, 1.0, 11.0, 111.0, 4.0, 41.0, 411.0;
160
161 for (int i = 0; i < nTimeSteps * nValues; i++) {
162 BOOST_TEST(testing::equals(serialized.values()(i), expectedSerializedValues(i)));
163 }
164
165 for (int i = 0; i < nTimeSteps * meshDimensions * nValues; i++) {
166 BOOST_TEST(testing::equals(serialized.gradients()(i), expectedSerializedGradients(i)));
167 }
168}
169
170BOOST_AUTO_TEST_CASE(DeserializeValuesAndGradients)
171{
172 std::vector<int> vertexOffsets{4, 8, 8, 10};
173
174 const int meshDimensions = 3;
175 const int nValues = 4;
176 const int nTimeSteps = 3;
177
178 Eigen::VectorXd serializedValues(nTimeSteps * nValues);
179 serializedValues << 1.0, 10.0, 100.0, 2.0, 20.0, 200.0, 3.0, 30.0, 300.0, 4.0, 40.0, 400.0;
180
181 Eigen::VectorXd serializedGradients(nTimeSteps * nValues * meshDimensions);
182 serializedGradients << 1.0, 11.0, 111.0, 4.0, 41.0, 411.0, 3.0, 31.0, 311.0, 2.0, 21.0, 211.0, 2.0, 21.0, 211.0, 1.0, 11.0, 111.0, 4.0, 41.0, 411.0, 3.0, 31.0, 311.0, 3.0, 31.0, 311.0, 2.0, 21.0, 211.0, 1.0, 11.0, 111.0, 4.0, 41.0, 411.0;
183
184 mesh::PtrMesh dummyMesh(new mesh::Mesh("DummyMesh", 3, testing::nextMeshID()));
185 dummyMesh->setVertexOffsets(vertexOffsets);
186
187 mesh::PtrData toData(new mesh::Data("to", -1, 1));
188 toData->requireDataGradient();
189
190 cplscheme::PtrCouplingData toDataPtr = makeCouplingData(toData, dummyMesh);
191 toDataPtr->sample().values = Eigen::VectorXd(nValues);
192 toDataPtr->sample().gradients = Eigen::MatrixXd(nValues, meshDimensions);
193 toDataPtr->sample().setZero();
194
195 toDataPtr->setSampleAtTime(0, toDataPtr->sample());
196
197 Eigen::VectorXd timeStamps(nTimeSteps);
198 timeStamps << 0, 0.5, 1;
199
200 auto serialized = serialize::SerializedStamples::empty(timeStamps, toDataPtr);
201
202 serialized.values() = serializedValues;
203 serialized.gradients() = serializedGradients;
204
205 serialized.deserializeInto(timeStamps, toDataPtr);
206
207 std::vector<Eigen::VectorXd> expectedValues;
208
209 Eigen::VectorXd insert0(nValues);
210 insert0 << 1.0, 2.0, 3.0, 4.0;
211 Eigen::VectorXd insert05(nValues);
212 insert05 << 10.0, 20.0, 30.0, 40.0;
213 Eigen::VectorXd insert1(nValues);
214 insert1 << 100.0, 200.0, 300.0, 400.0;
215
216 expectedValues.push_back(insert0);
217 expectedValues.push_back(insert05);
218 expectedValues.push_back(insert1);
219
220 std::vector<Eigen::MatrixXd> expectedGradients;
221
222 Eigen::MatrixXd insertGradients0(nValues, meshDimensions);
223 insertGradients0 << 1.0, 2.0, 3.0,
224 4.0, 1.0, 2.0,
225 3.0, 4.0, 1.0,
226 2.0, 3.0, 4.0;
227 Eigen::MatrixXd insertGradients05(nValues, meshDimensions);
228 insertGradients05 << 11.0, 21.0, 31.0,
229 41.0, 11.0, 21.0,
230 31.0, 41.0, 11.0,
231 21.0, 31.0, 41.0;
232 Eigen::MatrixXd insertGradients1(nValues, meshDimensions);
233 insertGradients1 << 111.0, 211.0, 311.0,
234 411.0, 111.0, 211.0,
235 311.0, 411.0, 111.0,
236 211.0, 311.0, 411.0;
237
238 expectedGradients.push_back(insertGradients0);
239 expectedGradients.push_back(insertGradients05);
240 expectedGradients.push_back(insertGradients1);
241
242 BOOST_TEST(toDataPtr->timeStepsStorage().stamples().size() == nTimeSteps);
243 for (int t = 0; t < nTimeSteps; t++) {
244 BOOST_TEST(testing::equals(toDataPtr->timeStepsStorage().stamples()[t].timestamp, timeStamps[t]));
245 for (int i = 0; i < nValues; i++) {
246 BOOST_TEST(testing::equals(toDataPtr->timeStepsStorage().stamples()[t].sample.values(i), expectedValues[t](i)));
247 }
248 for (int i = 0; i < meshDimensions * nValues; i++) {
249 BOOST_TEST(testing::equals(toDataPtr->timeStepsStorage().stamples()[t].sample.gradients(i), expectedGradients[t](i)));
250 }
251 }
252}
253
254BOOST_AUTO_TEST_SUITE_END() // SerializedStamples
255BOOST_AUTO_TEST_SUITE_END() // CommunicationTests
BOOST_AUTO_TEST_SUITE(PreProcess)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(SerializeValues)
static SerializedStamples empty(Eigen::VectorXd timeStamps, const cplscheme::PtrCouplingData data)
Create SerializedStamples with allocated buffers according to size of CouplingData.
static SerializedStamples serialize(const cplscheme::PtrCouplingData data)
Serializes a given CouplingData into SerializedStamples.
Describes a set of data values belonging to the vertices of a mesh.
Definition Data.hpp:29
Container and creator for meshes.
Definition Mesh.hpp:39
contains the data communication abstraction layer.
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
cplscheme::PtrCouplingData makeCouplingData(mesh::PtrData data, mesh::PtrMesh mesh, bool exchangeSubsteps)
Definition helper.hpp:10
Main namespace of the precice library.
T push_back(T... args)