preCICE v3.1.2
Loading...
Searching...
No Matches
SummationActionTest.cpp
Go to the documentation of this file.
1#include <Eigen/Core>
2#include <algorithm>
3#include <list>
4#include <memory>
5#include <string>
6#include <vector>
7#include "action/Action.hpp"
11#include "logging/Logger.hpp"
12#include "mesh/Data.hpp"
13#include "mesh/Mesh.hpp"
18#include "testing/Testing.hpp"
19#include "xml/XMLTag.hpp"
20
21using namespace precice;
22
23BOOST_AUTO_TEST_SUITE(ActionTests)
24BOOST_AUTO_TEST_SUITE(Summation)
25
26BOOST_AUTO_TEST_CASE(SummationOneDimensional)
27{
28 PRECICE_TEST(1_rank);
29 using namespace mesh;
30 PtrMesh mesh(new Mesh("Mesh", 3, testing::nextMeshID()));
31 int dimensions = 1;
32 PtrData sourceData1 = mesh->createData("SourceData1", dimensions, 0_dataID);
33 PtrData sourceData2 = mesh->createData("SourceData2", dimensions, 1_dataID);
34 PtrData sourceData3 = mesh->createData("SourceData3", dimensions, 2_dataID);
35 PtrData targetData = mesh->createData("TargetData", dimensions, 3_dataID);
36 std::vector<int> sourceDataIDs{sourceData1->getID(), sourceData2->getID(), sourceData3->getID()};
37 int targetDataID = targetData->getID();
38 mesh->createVertex(Eigen::Vector3d::Constant(0.0));
39 mesh->createVertex(Eigen::Vector3d::Constant(1.0));
40 mesh->createVertex(Eigen::Vector3d::Constant(2.0));
41
42 mesh->allocateDataValues();
43
44 Eigen::VectorXd v1(3), v2(3), v3(3);
45 v1 << 2.0, 3.0, 4.0;
46 v2 << 1.0, 2.0, 3.0;
47 v3 << 2.0, 3.0, 4.0;
48 sourceData1->setSampleAtTime(0, time::Sample{dimensions, v1});
49 sourceData2->setSampleAtTime(0, time::Sample{dimensions, v2});
50 sourceData3->setSampleAtTime(0, time::Sample{dimensions, v3});
51 // targetData->setSampleAtTime(0, time::Sample{dimensions,Eigen::VectorXd::Zero(targetValues.size())});
52
54 action::SummationAction::WRITE_MAPPING_POST, sourceDataIDs, targetDataID, mesh);
55
56 sum.performAction();
57
58 const auto &sourceValues1 = sourceData1->values();
59 const auto &sourceValues2 = sourceData2->values();
60 const auto &sourceValues3 = sourceData3->values();
61 const auto &targetValues = targetData->values();
62 BOOST_TEST(sourceValues1(0) == 2.0);
63 BOOST_TEST(sourceValues1(1) == 3.0);
64 BOOST_TEST(sourceValues1(2) == 4.0);
65 BOOST_TEST(sourceValues2(0) == 1.0);
66 BOOST_TEST(sourceValues2(1) == 2.0);
67 BOOST_TEST(sourceValues2(2) == 3.0);
68 BOOST_TEST(sourceValues3(0) == 2.0);
69 BOOST_TEST(sourceValues3(1) == 3.0);
70 BOOST_TEST(sourceValues3(2) == 4.0);
71 BOOST_TEST(targetValues(0) == 5.0);
72 BOOST_TEST(targetValues(1) == 8.0);
73 BOOST_TEST(targetValues(2) == 11.0);
74}
75
76BOOST_AUTO_TEST_CASE(SummationThreeDimensional)
77{
78 PRECICE_TEST(1_rank);
79 using namespace mesh;
80 int dimensions = 3;
81 PtrMesh mesh(new Mesh("Mesh", dimensions, testing::nextMeshID()));
82 PtrData sourceData1 = mesh->createData("SourceData1", dimensions, 0_dataID);
83 PtrData sourceData2 = mesh->createData("SourceData2", dimensions, 1_dataID);
84 PtrData targetData = mesh->createData("TargetData", dimensions, 2_dataID);
85 std::vector<int> sourceDataIDs{sourceData1->getID(), sourceData2->getID()};
86 int targetDataID = targetData->getID();
87 mesh->createVertex(Eigen::Vector3d::Constant(0.0));
88 mesh->createVertex(Eigen::Vector3d::Constant(1.0));
89 mesh->createVertex(Eigen::Vector3d::Constant(2.0));
90 mesh->allocateDataValues();
91
92 Eigen::VectorXd v1(9), v2(9);
93 v1 << 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0;
94 v2 << 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0;
95
96 sourceData1->setSampleAtTime(0, time::Sample{dimensions, v1});
97 sourceData2->setSampleAtTime(0, time::Sample{dimensions, v2});
98 // targetData->setSampleAtTime(0, time::Sample{dimensions,Eigen::VectorXd::Zero(targetValues.size())})
99
101 action::SummationAction::WRITE_MAPPING_POST, sourceDataIDs, targetDataID, mesh);
102
103 sum.performAction();
104
105 const auto &sourceValues1 = sourceData1->values();
106 const auto &sourceValues2 = sourceData2->values();
107 const auto &targetValues = targetData->values();
108 BOOST_TEST(sourceValues1(0) == 1.0);
109 BOOST_TEST(sourceValues2(0) == 2.0);
110 BOOST_TEST(targetValues(0) == 3.0);
111
112 BOOST_TEST(sourceValues1(1) == 2.0);
113 BOOST_TEST(sourceValues2(1) == 3.0);
114 BOOST_TEST(targetValues(1) == 5.0);
115
116 BOOST_TEST(sourceValues1(2) == 3.0);
117 BOOST_TEST(sourceValues2(2) == 4.0);
118 BOOST_TEST(targetValues(2) == 7.0);
119
120 BOOST_TEST(sourceValues1(3) == 4.0);
121 BOOST_TEST(sourceValues2(3) == 5.0);
122 BOOST_TEST(targetValues(3) == 9.0);
123
124 BOOST_TEST(sourceValues1(4) == 5.0);
125 BOOST_TEST(sourceValues2(4) == 6.0);
126 BOOST_TEST(targetValues(4) == 11.0);
127
128 BOOST_TEST(sourceValues1(5) == 6.0);
129 BOOST_TEST(sourceValues2(5) == 7.0);
130 BOOST_TEST(targetValues(5) == 13.0);
131
132 BOOST_TEST(sourceValues1(6) == 7.0);
133 BOOST_TEST(sourceValues2(6) == 8.0);
134 BOOST_TEST(targetValues(6) == 15.0);
135
136 BOOST_TEST(sourceValues1(7) == 8.0);
137 BOOST_TEST(sourceValues2(7) == 9.0);
138 BOOST_TEST(targetValues(7) == 17.0);
139
140 BOOST_TEST(sourceValues1(8) == 9.0);
141 BOOST_TEST(sourceValues2(8) == 10.0);
142 BOOST_TEST(targetValues(8) == 19.0);
143}
144
145BOOST_AUTO_TEST_CASE(SummationThreeDimensionalSubcycling)
146{
147 PRECICE_TEST(1_rank);
148 using namespace mesh;
149 int dimensions = 3;
150 PtrMesh mesh(new Mesh("Mesh", dimensions, testing::nextMeshID()));
151 PtrData sourceData1 = mesh->createData("SourceData1", dimensions, 0_dataID);
152 PtrData sourceData2 = mesh->createData("SourceData2", dimensions, 1_dataID);
153 PtrData targetData = mesh->createData("TargetData", dimensions, 2_dataID);
154 std::vector<int> sourceDataIDs{sourceData1->getID(), sourceData2->getID()};
155 int targetDataID = targetData->getID();
156 mesh->createVertex(Eigen::Vector3d::Constant(0.0));
157 mesh->createVertex(Eigen::Vector3d::Constant(1.0));
158 mesh->createVertex(Eigen::Vector3d::Constant(2.0));
159 mesh->allocateDataValues();
160
161 Eigen::VectorXd v1_05(9), v2_05(9), v1_1(9), v2_1(9);
162 v1_05 << 11.0, 22.0, 33.0, 44.0, 55.0, 66.0, 77.0, 88.0, 99.0;
163 v2_05 << 12.0, 23.0, 34.0, 45.0, 56.0, 67.0, 78.0, 89.0, 910.0;
164 v1_1 << 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0;
165 v2_1 << 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0;
166
167 sourceData1->setSampleAtTime(0.5, time::Sample{dimensions, v1_05});
168 sourceData2->setSampleAtTime(0.5, time::Sample{dimensions, v2_05});
169 sourceData1->setSampleAtTime(1.0, time::Sample{dimensions, v1_1});
170 sourceData2->setSampleAtTime(1.0, time::Sample{dimensions, v2_1});
171
173 action::SummationAction::WRITE_MAPPING_POST, sourceDataIDs, targetDataID, mesh);
174
175 sum.performAction();
176
177 const auto &sourceValues1 = sourceData1->values();
178 const auto &sourceValues2 = sourceData2->values();
179 const auto &targetValues = targetData->values();
180 BOOST_TEST(sourceValues1(0) == 1.0);
181 BOOST_TEST(sourceValues2(0) == 2.0);
182 BOOST_TEST(targetValues(0) == 3.0);
183
184 BOOST_TEST(sourceValues1(1) == 2.0);
185 BOOST_TEST(sourceValues2(1) == 3.0);
186 BOOST_TEST(targetValues(1) == 5.0);
187
188 BOOST_TEST(sourceValues1(2) == 3.0);
189 BOOST_TEST(sourceValues2(2) == 4.0);
190 BOOST_TEST(targetValues(2) == 7.0);
191
192 BOOST_TEST(sourceValues1(3) == 4.0);
193 BOOST_TEST(sourceValues2(3) == 5.0);
194 BOOST_TEST(targetValues(3) == 9.0);
195
196 BOOST_TEST(sourceValues1(4) == 5.0);
197 BOOST_TEST(sourceValues2(4) == 6.0);
198 BOOST_TEST(targetValues(4) == 11.0);
199
200 BOOST_TEST(sourceValues1(5) == 6.0);
201 BOOST_TEST(sourceValues2(5) == 7.0);
202 BOOST_TEST(targetValues(5) == 13.0);
203
204 BOOST_TEST(sourceValues1(6) == 7.0);
205 BOOST_TEST(sourceValues2(6) == 8.0);
206 BOOST_TEST(targetValues(6) == 15.0);
207
208 BOOST_TEST(sourceValues1(7) == 8.0);
209 BOOST_TEST(sourceValues2(7) == 9.0);
210 BOOST_TEST(targetValues(7) == 17.0);
211
212 BOOST_TEST(sourceValues1(8) == 9.0);
213 BOOST_TEST(sourceValues2(8) == 10.0);
214 BOOST_TEST(targetValues(8) == 19.0);
215
216 // Load and check data from 0.5
217
218 auto &loadedStample1 = sourceData1->stamples().front();
219 BOOST_TEST(loadedStample1.timestamp == 0.5);
220 sourceData1->values() = loadedStample1.sample.values;
221
222 auto &loadedStample2 = sourceData2->stamples().front();
223 BOOST_TEST(loadedStample2.timestamp == 0.5);
224 sourceData2->values() = loadedStample2.sample.values;
225
226 auto &loadedStample3 = targetData->stamples().front();
227 BOOST_TEST(loadedStample3.timestamp == 0.5);
228 targetData->values() = loadedStample3.sample.values;
229
230 BOOST_TEST(sourceValues1(0) == 11.0);
231 BOOST_TEST(sourceValues2(0) == 12.0);
232 BOOST_TEST(targetValues(0) == 23.0);
233
234 BOOST_TEST(sourceValues1(1) == 22.0);
235 BOOST_TEST(sourceValues2(1) == 23.0);
236 BOOST_TEST(targetValues(1) == 45.0);
237
238 BOOST_TEST(sourceValues1(2) == 33.0);
239 BOOST_TEST(sourceValues2(2) == 34.0);
240 BOOST_TEST(targetValues(2) == 67.0);
241
242 BOOST_TEST(sourceValues1(3) == 44.0);
243 BOOST_TEST(sourceValues2(3) == 45.0);
244 BOOST_TEST(targetValues(3) == 89.0);
245
246 BOOST_TEST(sourceValues1(4) == 55.0);
247 BOOST_TEST(sourceValues2(4) == 56.0);
248 BOOST_TEST(targetValues(4) == 111.0);
249
250 BOOST_TEST(sourceValues1(5) == 66.0);
251 BOOST_TEST(sourceValues2(5) == 67.0);
252 BOOST_TEST(targetValues(5) == 133.0);
253
254 BOOST_TEST(sourceValues1(6) == 77.0);
255 BOOST_TEST(sourceValues2(6) == 78.0);
256 BOOST_TEST(targetValues(6) == 155.0);
257
258 BOOST_TEST(sourceValues1(7) == 88.0);
259 BOOST_TEST(sourceValues2(7) == 89.0);
260 BOOST_TEST(targetValues(7) == 177.0);
261
262 BOOST_TEST(sourceValues1(8) == 99.0);
263 BOOST_TEST(sourceValues2(8) == 910.0);
264 BOOST_TEST(targetValues(8) == 1009.0);
265}
266
268{
269 PRECICE_TEST(1_rank);
270 std::string filename = testing::getPathToSources() + "/action/tests/SummationActionTest-testConfiguration-1.xml";
273 mesh::PtrMeshConfiguration meshConfig(new mesh::MeshConfiguration(tag, dataConfig));
274 action::ActionConfiguration config(tag, meshConfig);
276 BOOST_TEST(config.actions().size() == 1);
277 auto &action = config.actions().front();
278 BOOST_TEST(static_cast<bool>(action));
279}
280
281BOOST_AUTO_TEST_SUITE_END() // Summation
282BOOST_AUTO_TEST_SUITE_END() // ActionTest
BOOST_AUTO_TEST_SUITE(PreProcess)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(SummationOneDimensional)
#define PRECICE_TEST(...)
Definition Testing.hpp:27
Configures an Action subclass object.
const std::list< PtrAction > & actions() const
Returns the configured action.
Action that adds multiple source data into target data.
void performAction() final override
Adding data and applying them to target.
Performs and provides configuration for Data objects from XML files.
Represents an XML tag to be configured automatically.
Definition XMLTag.hpp:31
std::string getPathToSources()
Returns the base path to the sources.
Definition Testing.cpp:31
XMLTag getRootTag()
Returns an XMLTag::Listener that does nothing on callbacks.
Definition XMLTag.cpp:389
void configure(XMLTag &tag, const precice::xml::ConfigurationContext &context, std::string_view configurationFilename)
Configures the given configuration from file configurationFilename.
Definition XMLTag.cpp:395
Main namespace of the precice library.
Tightly coupled to the parameters of Participant()
Definition XMLTag.hpp:24