preCICE v3.1.2
Loading...
Searching...
No Matches
ParallelImplicitCouplingSchemeTest.cpp
Go to the documentation of this file.
1#include <Eigen/Core>
2#include <algorithm>
3#include <map>
4#include <memory>
5#include <string>
6#include <utility>
7#include <vector>
18#include "logging/LogMacros.hpp"
20#include "math/differences.hpp"
21#include "mesh/Data.hpp"
22#include "mesh/Mesh.hpp"
29#include "testing/Testing.hpp"
31#include "xml/XMLTag.hpp"
32
33using namespace precice;
34using namespace precice::cplscheme;
35
36BOOST_AUTO_TEST_SUITE(CplSchemeTests)
37
46
47BOOST_FIXTURE_TEST_SUITE(ParallelImplicitCouplingSchemeTests, ParallelImplicitCouplingSchemeFixture)
48
49#ifndef PRECICE_NO_MPI
50
51BOOST_AUTO_TEST_CASE(testParseConfigurationWithRelaxation)
52{
53 PRECICE_TEST(1_rank);
54 using namespace mesh;
55
56 std::string path(_pathToTests + "parallel-implicit-cplscheme-relax-const-config.xml");
57
59 PtrDataConfiguration dataConfig(new DataConfiguration(root));
60 PtrMeshConfiguration meshConfig(new MeshConfiguration(root, dataConfig));
62 new m2n::M2NConfiguration(root));
64 CouplingSchemeConfiguration cplSchemeConfig(root, meshConfig, m2nConfig, participantConfig);
65
67 BOOST_CHECK(cplSchemeConfig._accelerationConfig->getAcceleration().get());
68}
69
70BOOST_AUTO_TEST_CASE(testInitializeData)
71{
72 PRECICE_TEST("Participant0"_on(1_rank), "Participant1"_on(1_rank), Require::Events);
74 options.useOnlyPrimaryCom = true;
75 auto m2n = context.connectPrimaryRanks("Participant0", "Participant1", options);
76
78
79 int dimensions = 3;
80
81 // Create a data configuration, to simplify configuration of data
83 dataConfig->addData("Data0", mesh::Data::typeName::SCALAR);
84 dataConfig->addData("Data1", mesh::Data::typeName::VECTOR);
85
86 mesh::MeshConfiguration meshConfig(root, dataConfig);
87 mesh::PtrMesh mesh(new mesh::Mesh("Mesh", 3, testing::nextMeshID()));
88 const auto dataID0 = mesh->createData("Data0", 1, 0_dataID)->getID();
89 const auto dataID1 = mesh->createData("Data1", 3, 1_dataID)->getID();
90 mesh->createVertex(Eigen::Vector3d::Zero());
91 mesh->allocateDataValues();
92 meshConfig.insertMeshToMeshDimensionsMap(mesh->getName(), mesh->getDimensions());
93 meshConfig.addMesh(mesh);
94
95 // Create all parameters necessary to create a ParallelImplicitCouplingScheme object
96 double maxTime = 1.0;
97 int maxTimeWindows = 3;
98 const double timeWindowSize = 0.1;
99 const double timeStepSize = timeWindowSize; // solver is not subcycling
100 std::string nameParticipant0("Participant0");
101 std::string nameParticipant1("Participant1");
102 int sendDataIndex = -1;
103 int receiveDataIndex = -1;
104 bool dataRequiresInitialization = false;
105 if (context.isNamed(nameParticipant0)) {
106 sendDataIndex = dataID0;
107 receiveDataIndex = dataID1;
108 dataRequiresInitialization = true;
109 } else {
110 sendDataIndex = dataID1;
111 receiveDataIndex = dataID0;
112 dataRequiresInitialization = true;
113 }
114
115 // Create the coupling scheme object
116 const int minIterations = 1;
117 const int maxIterations = 3;
118 ParallelCouplingScheme cplScheme(maxTime, maxTimeWindows, timeWindowSize, nameParticipant0, nameParticipant1, context.name, m2n, constants::FIXED_TIME_WINDOW_SIZE, BaseCouplingScheme::Implicit, minIterations, maxIterations);
119
121 cplScheme.addDataToSend(mesh->data(sendDataIndex), mesh, dataRequiresInitialization, true);
122 CouplingData *sendCouplingData = Fixture::getSendData(cplScheme, sendDataIndex);
123 cplScheme.addDataToReceive(mesh->data(receiveDataIndex), mesh, dataRequiresInitialization, true);
124 CouplingData *receiveCouplingData = Fixture::getReceiveData(cplScheme, receiveDataIndex);
126
127 if (context.isNamed(nameParticipant0)) {
128 BOOST_TEST(testing::equals(receiveCouplingData->values(), Eigen::Vector3d(0.0, 0.0, 0.0)));
129 BOOST_TEST(receiveCouplingData->values().size() == 3);
130 BOOST_TEST(testing::equals(sendCouplingData->values()(0), 0.0));
131 BOOST_TEST(sendCouplingData->values().size() == 1);
132 BOOST_TEST(Fixture::isImplicitCouplingScheme(cplScheme));
133 BOOST_TEST(cplScheme.isActionRequired(CouplingScheme::Action::InitializeData));
134 sendCouplingData->setSampleAtTime(0, time::Sample{1, Eigen::VectorXd::Constant(1, 4.0)});
135 cplScheme.markActionFulfilled(CouplingScheme::Action::InitializeData);
136 cplScheme.initialize(0.0, 0);
137 BOOST_TEST(cplScheme.hasDataBeenReceived());
138 BOOST_TEST(testing::equals(receiveCouplingData->values(), Eigen::Vector3d(1.0, 2.0, 3.0)));
139 BOOST_TEST(receiveCouplingData->getPreviousIterationSize() == 3);
140 BOOST_TEST(testing::equals(receiveCouplingData->previousIteration(), Eigen::Vector3d(0.0, 0.0, 0.0)));
141 BOOST_TEST(sendCouplingData->getPreviousIterationSize() == 1);
142 BOOST_TEST(testing::equals(sendCouplingData->previousIteration()(0), 4.0));
143 while (cplScheme.isCouplingOngoing()) {
144 if (cplScheme.isActionRequired(CouplingScheme::Action::WriteCheckpoint)) {
145 cplScheme.markActionFulfilled(CouplingScheme::Action::WriteCheckpoint);
146 }
147 BOOST_TEST(cplScheme.getNextTimeStepMaxSize() == timeStepSize);
148 sendCouplingData->setSampleAtTime(cplScheme.getTime() + timeStepSize, time::Sample{1, Eigen::VectorXd::Constant(1, 4.0)});
149 cplScheme.addComputedTime(timeStepSize);
150 cplScheme.firstSynchronization({});
151 cplScheme.firstExchange();
152 cplScheme.secondSynchronization();
153 cplScheme.secondExchange();
154 BOOST_TEST(cplScheme.hasDataBeenReceived());
155 if (cplScheme.isActionRequired(CouplingScheme::Action::ReadCheckpoint)) {
156 cplScheme.markActionFulfilled(CouplingScheme::Action::ReadCheckpoint);
157 }
158 }
159 } else {
160 BOOST_TEST(context.isNamed(nameParticipant1));
161 BOOST_TEST(cplScheme.isActionRequired(CouplingScheme::Action::InitializeData));
162 Eigen::VectorXd v(3);
163 v << 1.0, 2.0, 3.0;
164 sendCouplingData->setSampleAtTime(0, time::Sample{3, v});
165 cplScheme.markActionFulfilled(CouplingScheme::Action::InitializeData);
166 BOOST_TEST(testing::equals(receiveCouplingData->values()(0), 0.0));
167 BOOST_TEST(receiveCouplingData->values().size() == 1);
168 BOOST_TEST(testing::equals(sendCouplingData->values(), Eigen::Vector3d(1.0, 2.0, 3.0)));
169 BOOST_TEST(sendCouplingData->values().size() == 3);
170 cplScheme.initialize(0.0, 0);
171 BOOST_TEST(cplScheme.hasDataBeenReceived());
172 BOOST_TEST(testing::equals(receiveCouplingData->values()(0), 4.0));
173 BOOST_TEST(receiveCouplingData->getPreviousIterationSize() == 1);
174 BOOST_TEST(testing::equals(receiveCouplingData->previousIteration()(0), 0.0));
175 BOOST_TEST(testing::equals(sendCouplingData->values(), Eigen::Vector3d(1.0, 2.0, 3.0)));
176 BOOST_TEST(sendCouplingData->getPreviousIterationSize() == 3);
177 BOOST_TEST(testing::equals(sendCouplingData->previousIteration(), Eigen::Vector3d(1.0, 2.0, 3.0)));
178 while (cplScheme.isCouplingOngoing()) {
179 if (cplScheme.isActionRequired(CouplingScheme::Action::WriteCheckpoint)) {
180 cplScheme.markActionFulfilled(CouplingScheme::Action::WriteCheckpoint);
181 }
182 BOOST_TEST(cplScheme.getNextTimeStepMaxSize() == timeStepSize);
183 sendCouplingData->setSampleAtTime(cplScheme.getTime() + timeStepSize, time::Sample{3, v});
184 cplScheme.addComputedTime(timeStepSize);
185 cplScheme.firstSynchronization({});
186 cplScheme.firstExchange();
187 cplScheme.secondSynchronization();
188 cplScheme.secondExchange();
189 BOOST_TEST(cplScheme.hasDataBeenReceived());
190 if (cplScheme.isActionRequired(CouplingScheme::Action::ReadCheckpoint)) {
191 cplScheme.markActionFulfilled(CouplingScheme::Action::ReadCheckpoint);
192 }
193 }
194 }
195 cplScheme.finalize();
196}
197
198#endif // not PRECICE_NO_MPI
199
BOOST_AUTO_TEST_SUITE(PreProcess)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(testParseConfigurationWithRelaxation)
#define PRECICE_TEST(...)
Definition Testing.hpp:27
Performs XML configuration of a participant.
ChangedMeshes secondSynchronization() override final
ChangedMeshes firstSynchronization(const ChangedMeshes &changes) override final
void initialize(double startTime, int startTimeWindow) override final
Initializes the coupling scheme.
bool addComputedTime(double timeToAdd) override final
Adds newly computed time. Has to be called before every advance.
bool isCouplingOngoing() const override final
Returns true, when the coupled simulation is still ongoing.
double getTime() const override final
getter for _time
bool hasDataBeenReceived() const override final
getter for _hasDataBeenReceived
double getNextTimeStepMaxSize() const override final
Returns the maximal size of the next time step to be computed.
bool isActionRequired(Action action) const override final
Returns true, if the given action has to be performed by the accessor.
void markActionFulfilled(Action action) override final
Tells the coupling scheme that the accessor has performed the given action.
void finalize() override final
Finalizes the coupling scheme.
void addDataToReceive(const mesh::PtrData &data, mesh::PtrMesh mesh, bool requiresInitialization, bool exchangeSubsteps)
Adds data to be received on data exchange.
void addDataToSend(const mesh::PtrData &data, mesh::PtrMesh mesh, bool requiresInitialization, bool exchangeSubsteps)
Adds data to be sent on data exchange and possibly be modified during coupling iterations.
void determineInitialDataExchange() override
Determines which data is initialized and therefore has to be exchanged during initialize.
const Eigen::VectorXd & previousIteration() const
returns data value from previous iteration
Eigen::VectorXd & values()
Returns a reference to the data values.
void setSampleAtTime(double time, time::Sample sample)
Add sample at given time to _timeStepsStorage.
int getPreviousIterationSize() const
returns size of previous iteration
acceleration::PtrAccelerationConfiguration _accelerationConfig
Coupling scheme for parallel coupling, i.e. simultaneous execution of two coupled participants.
Configuration for communication channels between solvers.
Performs and provides configuration for Data objects from XML files.
void addMesh(const mesh::PtrMesh &mesh)
void insertMeshToMeshDimensionsMap(const std::string &mesh, int dimensions)
Initialize the map between meshes and dimensions, for unit tests that directly create mesh objects wi...
Container and creator for meshes.
Definition Mesh.hpp:39
Represents an XML tag to be configured automatically.
Definition XMLTag.hpp:31
T get(T... args)
contains implementations of coupling schemes for coupled simulations.
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
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