preCICE v3.1.2
Loading...
Searching...
No Matches
BaseCouplingScheme.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <Eigen/Core>
4#include <algorithm>
5#include <map>
6#include <memory>
7#include <set>
8#include <string>
9#include <vector>
10
11#include "Constants.hpp"
12#include "CouplingData.hpp"
13#include "CouplingScheme.hpp"
14#include "SharedPointer.hpp"
18#include "impl/TimeHandler.hpp"
19#include "io/TXTTableWriter.hpp"
20#include "logging/Logger.hpp"
21#include "m2n/M2N.hpp"
22#include "m2n/SharedPointer.hpp"
24
25namespace precice {
26namespace io {
27class TXTTableWriter;
28} // namespace io
29
30namespace cplscheme {
31class CouplingData;
32
62public:
66
68 double maxTime,
69 int maxTimeWindows,
70 double timeWindowSize,
72 int minIterations,
73 int maxIterations,
74 CouplingMode cplMode,
76
81 bool sendsInitializedData() const override final;
82
87 bool isInitialized() const override final;
88
90 bool addComputedTime(double timeToAdd) override final;
91
101 bool willDataBeExchanged(double lastSolverTimeStepSize) const override final;
102
107 bool hasDataBeenReceived() const override final;
108
113 double getTime() const override final;
114
115 double getTimeWindowStart() const override final;
116
121 int getTimeWindows() const override final;
122
132 bool hasTimeWindowSize() const override final;
133
140 double getTimeWindowSize() const override final;
141
148 double getNextTimeStepMaxSize() const override final;
149
151 bool isCouplingOngoing() const override final;
152
154 bool isTimeWindowComplete() const override final;
155
157 bool isActionRequired(Action action) const override final;
158
160 bool isActionFulfilled(Action action) const override final;
161
163 void markActionFulfilled(Action action) override final;
164
166 void requireAction(Action action) override final;
167
173 std::string printCouplingState() const override;
174
176 void finalize() override final;
177
184 void initialize(double startTime, int startTimeWindow) override final;
185
186 ChangedMeshes firstSynchronization(const ChangedMeshes &changes) override final;
187
188 void firstExchange() override final;
189
190 ChangedMeshes secondSynchronization() override final;
191
192 void secondExchange() override final;
193
196 int dataID,
197 bool suffices,
198 bool strict,
199 impl::PtrConvergenceMeasure measure,
200 bool doesLogging);
201
203 void setAcceleration(const acceleration::PtrAcceleration &acceleration);
204
209 bool doesFirstStep() const;
210
214 virtual bool hasAnySendData() = 0;
215
222
227 bool isImplicitCouplingScheme() const override;
228
234 bool hasConverged() const override;
235
236protected:
239
241 acceleration::PtrAcceleration _acceleration;
242
243 void sendNumberOfTimeSteps(const m2n::PtrM2N &m2n, const int numberOfTimeSteps);
244
245 void sendTimes(const m2n::PtrM2N &m2n, const Eigen::VectorXd &times);
246
253 void sendData(const m2n::PtrM2N &m2n, const DataMap &sendData);
254
255 int receiveNumberOfTimeSteps(const m2n::PtrM2N &m2n);
256
257 Eigen::VectorXd receiveTimes(const m2n::PtrM2N &m2n, int nTimeSteps);
258
265 void receiveData(const m2n::PtrM2N &m2n, const DataMap &receiveData);
266
277 void receiveDataForWindowEnd(const m2n::PtrM2N &m2n, const DataMap &receiveData);
278
285
299 PtrCouplingData addCouplingData(const mesh::PtrData &data, mesh::PtrMesh mesh, bool requiresInitialization, bool exchangeSubsteps, CouplingData::Direction direction);
300
305 bool isExplicitCouplingScheme() const;
306
311 void setTimeWindowSize(double timeWindowSize);
312
317 double getNextTimeWindowSize() const;
318
323 void setNextTimeWindowSize(double timeWindowSize);
324
329
334
339 bool receivesInitializedData() const;
340
349 void setTimeWindows(int timeWindows);
350
355 void sendConvergence(const m2n::PtrM2N &m2n);
356
362 void receiveConvergence(const m2n::PtrM2N &m2n);
363
370 void doImplicitStep();
371
375 void moveToNextWindow();
376
380 void storeIteration();
381
387
393
398 bool reachedEndOfTimeWindow() const;
399
401 bool requiresSubsteps() const override final;
402
404 ImplicitData implicitDataToReceive() const override;
405
407 std::string localParticipant() const override final;
408
409private:
412
413 mutable logging::Logger _log{"cplscheme::BaseCouplingScheme"};
414
416 double _maxTime;
417
420
423
426
429
432
435
438
440 int _iterations = -1;
441
444
446 bool _doesFirstStep = false;
447
450
453
456
459
461 bool _isInitialized = false;
462
464
466
468 bool _hasConverged = false;
469
472
475
478
491 bool strict;
494
496 {
497 return "Res" + measure->getAbbreviation() + "(" + couplingData->getDataName() + ")";
498 }
499 };
500
508
510
515
519 virtual void exchangeInitialData() = 0;
520
522
524 virtual void exchangeFirstData() = 0;
525
527 virtual void exchangeSecondData() = 0;
528
534
539
544
548 void advanceTXTWriters();
549
557 int timeWindows,
558 double time) const;
559
565
570 bool measureConvergence();
571
576
582 bool anyDataRequiresInitialization(DataMap &dataMap) const;
583
587 double getWindowEndTime() const;
588
592 double getWindowStartTime() const;
593};
594} // namespace cplscheme
595} // namespace precice
Abstract base class for standard coupling schemes.
void initializeWithZeroInitialData(const DataMap &receiveData)
Initializes storage in receiveData as zero.
std::string printCouplingState() const override
Returns coupling state information.
void setTimeWindowSize(double timeWindowSize)
Setter for _timeWindowSize.
std::vector< ConvergenceMeasureContext > _convergenceMeasures
All convergence measures of coupling iterations.
void determineInitialReceive(DataMap &receiveData)
Sets _receivesInitializedData, if receiveData requires initialization.
bool isInitialized() const override final
getter for _isInitialized
ChangedMeshes secondSynchronization() override final
std::string _localParticipant
Local participant name.
double _timeWindowSize
size of time window; _timeWindowSize <= _maxTime
void determineInitialSend(DataMap &sendData)
Sets _sendsInitializedData, if sendData requires initialization.
virtual void exchangeSecondData()=0
Exchanges the second set of data.
ChangedMeshes firstSynchronization(const ChangedMeshes &changes) override final
void receiveDataForWindowEnd(const m2n::PtrM2N &m2n, const DataMap &receiveData)
Like receiveData, but temporarily sets window time to end of window.
bool requiresSubsteps() const override final
Returns true if any send data of the scheme requires substeps.
bool _hasDataBeenReceived
True, if data has been received from other participant. Flag is used to make sure that coupling schem...
void notifyDataHasBeenReceived()
Used to set flag after data has been received using receiveData().
virtual void determineInitialDataExchange()=0
Determines which data is initialized and therefore has to be exchanged during initialize.
void addConvergenceMeasure(int dataID, bool suffices, bool strict, impl::PtrConvergenceMeasure measure, bool doesLogging)
Adds a measure to determine the convergence of coupling iterations.
bool _isInitialized
True, if coupling has been initialized.
bool isExplicitCouplingScheme() const
Function to determine whether coupling scheme is an explicit coupling scheme.
int _maxIterations
Limit of iterations during one time window. Continue to next time window, if _iterations == _maxItera...
void sendData(const m2n::PtrM2N &m2n, const DataMap &sendData)
Sends data sendDataIDs given in mapCouplingData with communication.
virtual void exchangeFirstData()=0
Functions needed for advance()
bool _doesFirstStep
True, if local participant is the one starting the explicit scheme.
bool _receivesInitializedData
True, if this participant has to receive initialized data.
void initialize(double startTime, int startTimeWindow) override final
Initializes the coupling scheme.
void storeIteration()
used for storing all Data at end of doImplicitStep for later reference.
bool sendsInitializedData() const override final
Getter for _sendsInitializedData.
void initializeTXTWriters()
Initialize txt writers for iterations and convergence tracking.
bool addComputedTime(double timeToAdd) override final
Adds newly computed time. Has to be called before every advance.
bool isImplicitCouplingScheme() const override
Function to determine whether coupling scheme is an implicit coupling scheme.
bool _hasConverged
True if implicit scheme converged.
void setTimeWindows(int timeWindows)
Setter for _timeWindows.
void sendConvergence(const m2n::PtrM2N &m2n)
sends convergence to other participant via m2n
int _minIterations
Lower limit of iterations during one time window. Prevents convergence if _iterations < _minIteration...
void requireAction(Action action) override final
Sets an action required to be performed by the accessor.
bool _sendsInitializedData
True, if this participant has to send initialized data.
bool _isTimeWindowComplete
True, if _time == _timeWindowStartTime + _timeWindowSize and (coupling has converged or _iterations =...
bool isCouplingOngoing() const override final
Returns true, when the coupled simulation is still ongoing.
int _totalIterations
Number of total iterations performed.
bool anyDataRequiresInitialization(DataMap &dataMap) const
Checks whether any CouplingData in dataMap requires initialization.
double getTime() const override final
getter for _time
Eigen::VectorXd receiveTimes(const m2n::PtrM2N &m2n, int nTimeSteps)
bool hasDataBeenReceived() const override final
getter for _hasDataBeenReceived
bool hasConverged() const override
Checks if the implicit cplscheme has converged.
std::shared_ptr< io::TXTTableWriter > _convergenceWriter
Writes out coupling convergence within all time windows.
double _maxTime
Maximum time being computed. End of simulation is reached, if getTime() == _maxTime.
double getTimeWindowSize() const override final
Returns the time window size, if one is given by the coupling scheme.
double getTimeWindowStart() const override final
void setAcceleration(const acceleration::PtrAcceleration &acceleration)
Set an acceleration technique.
bool reachedEndOfTimeWindow() const
Function to check whether end of time window is reached. Does not check for convergence.
int receiveNumberOfTimeSteps(const m2n::PtrM2N &m2n)
void moveToNextWindow()
finalizes this window's data and initializes data for next window.
int _maxTimeWindows
Number of time windows that have to be computed. End of simulation is reached, if _timeWindows == _ma...
void doImplicitStep()
perform a coupling iteration
std::string printBasicState(int timeWindows, double time) const
Prints the coupling state.
double getNextTimeStepMaxSize() const override final
Returns the maximal size of the next time step to be computed.
bool isTimeWindowComplete() const override final
Returns true, when the accessor can advance to the next time window.
std::string printActionsState() const
Prints the action state.
bool hasTimeWindowSize() const override final
Function to check whether time window size is defined by coupling scheme.
bool receivesInitializedData() const
Getter for _receivesInitializedData.
int getTimeWindows() const override final
getter for _timeWindows
ImplicitData implicitDataToReceive() const override
Returns a vector of implicit data to receive in the next advance.
void sendNumberOfTimeSteps(const m2n::PtrM2N &m2n, const int numberOfTimeSteps)
PtrCouplingData addCouplingData(const mesh::PtrData &data, mesh::PtrMesh mesh, bool requiresInitialization, bool exchangeSubsteps, CouplingData::Direction direction)
Adds CouplingData with given properties to this BaseCouplingScheme and returns a pointer to the Coupl...
int _timeWindows
number of completed time windows; _timeWindows <= _maxTimeWindows
void advanceTXTWriters()
Advance txt writers for iterations and convergence tracking.
acceleration::PtrAcceleration _acceleration
Acceleration method to speedup iteration convergence.
virtual DataMap & getAccelerationData()=0
interface to provide accelerated data, depending on coupling scheme being used
double getNextTimeWindowSize() const
Getter for _nextTimeWindowSize.
double _nextTimeWindowSize
time window size of next window (acts as buffer for time windows size provided by first participant,...
DataMap _allData
All send and receive data as a map "data ID -> data".
void sendTimes(const m2n::PtrM2N &m2n, const Eigen::VectorXd &times)
CouplingMode _couplingMode
Coupling mode used by coupling scheme.
bool willDataBeExchanged(double lastSolverTimeStepSize) const override final
Returns true, if data will be exchanged when calling advance().
bool isActionRequired(Action action) const override final
Returns true, if the given action has to be performed by the accessor.
std::string localParticipant() const override final
Returns the name of the local participant.
void receiveConvergence(const m2n::PtrM2N &m2n)
receives convergence from other participant via m2n
void setNextTimeWindowSize(double timeWindowSize)
Setter for _nextTimeWindowSize.
std::shared_ptr< io::TXTTableWriter > _iterationsWriter
Responsible for monitoring iteration count over time window.
virtual void exchangeInitialData()=0
implements functionality for initialize in base class.
int _iterations
Number of iterations in current time window. _iterations <= _maxIterations.
void markActionFulfilled(Action action) override final
Tells the coupling scheme that the accessor has performed the given action.
virtual void initializeReceiveDataStorage()=0
Functions needed for initialize()
bool isActionFulfilled(Action action) const override final
Returns true, if the given action has to be performed by the accessor.
void newConvergenceMeasurements()
Reset all convergence measurements after convergence.
BaseCouplingScheme(double maxTime, int maxTimeWindows, double timeWindowSize, std::string localParticipant, int minIterations, int maxIterations, CouplingMode cplMode, constants::TimesteppingMethod dtMethod)
void setDoesFirstStep(bool doesFirstStep)
Setter for _doesFirstStep.
void finalize() override final
Finalizes the coupling scheme.
bool measureConvergence()
Measure whether coupling scheme has converged or not.
void receiveData(const m2n::PtrM2N &m2n, const DataMap &receiveData)
Receives data receiveDataIDs given in mapCouplingData with communication.
void checkCompletenessRequiredActions()
If any required actions are open, an error message is issued.
bool doesFirstStep() const
Getter for _doesFirstStep.
Interface for all coupling schemes.
Action
Actions that are required by CouplingSchemes.
static const double UNDEFINED_TIME_WINDOW_SIZE
To be used, when the time window size is determined dynamically during the coupling.
Main namespace of the precice library.
STL namespace.
Holds meta information to perform a convergence measurement.