preCICE v3.1.1
Loading...
Searching...
No Matches
TimeHandler.cpp
Go to the documentation of this file.
1#include "TimeHandler.hpp"
2
3#include <optional>
4
7#include "utils/assertion.hpp"
8
10
22
23// TimeHandler implementation
24
26 : _impl(std::make_unique<TimeHandler::Impl>())
27{
28}
29
30// Required by unique_ptr
32
34 : _impl(std::make_unique<TimeHandler::Impl>())
35{
36 _impl->_maxTime = maxTime;
37}
38
40 : _impl(std::make_unique<TimeHandler::Impl>(*other._impl))
41{
42}
43
45 : _impl(std::exchange(other._impl, nullptr))
46{
47}
48
50{
51 _impl = std::make_unique<TimeHandler::Impl>(*other._impl);
52 return *this;
53}
54
56{
57 _impl = std::exchange(other._impl, nullptr);
58 return *this;
59}
60
61void TimeHandler::resetTo(double timeStart)
62{
63 _impl->_windowStart = timeStart;
65}
66
68{
69 _impl->_windowProgress.add(dt);
70}
71
73{
74 _impl->_windowProgress = 0.0;
75}
76
77void TimeHandler::completeTimeWindow(double timeWindowSize)
78{
79 // Use the exact time window if possible
80 if (math::equals(windowProgress(), timeWindowSize)) {
81 _impl->_windowStart.add(timeWindowSize);
82 } else {
83 // Handle truncated time windows in case of reaching the end of the simulation
84 // This only happens when the final time window is truncated due
85 // time window size not being a divider of max-time.
86 PRECICE_ASSERT(reachedEnd(), "This can only happened if we reach max-time", timeWindowSize, time(), _impl->_maxTime.value_or(-1));
87 _impl->_windowStart.add(_impl->_windowProgress);
88 }
90}
91
92// Queries about reaching ends
93
94bool TimeHandler::reachedEndOfWindow(double timeWindowSize) const
95{
96 return math::equals(untilWindowEnd(timeWindowSize), 0.0);
97}
98
100{
101 if (!_impl->_maxTime) {
102 return false; // Directly return preventing lossy computations
103 }
104 return math::equals(untilTime(*_impl->_maxTime), 0.0);
105}
106
107// Time differences
108
109double TimeHandler::untilWindowEnd(double timeWindowSize) const
110{
111 double maxDt = untilProgress(timeWindowSize);
112 if (!_impl->_maxTime) {
113 return maxDt; // Return to prevent further lossy computations
114 }
115 // Truncate by max Time
116 return std::min(maxDt, untilEnd());
117}
118
119double TimeHandler::untilTime(double t) const
120{
121 // rearranged version of: maxtime - windowStart - windowProgress
122 return -(_impl->_windowStart + _impl->_windowProgress.value() - t).value();
123}
124
126{
127 if (!_impl->_maxTime) {
129 }
130 return untilTime(_impl->_maxTime.value());
131}
132
134{
135 return _impl->_windowProgress.value();
136}
137
138// Time points
139
141{
142 return _impl->_windowStart.value();
143}
144
145double TimeHandler::time() const
146{
147 return (_impl->_windowStart + _impl->_windowProgress.value()).value();
148}
149
150double TimeHandler::untilProgress(double windowSize) const
151{
152 // rearranged version of: windowSize - _windowProgress
153 return -(_impl->_windowProgress - windowSize).value();
154}
155
156} // namespace precice::cplscheme::impl
#define PRECICE_ASSERT(...)
Definition assertion.hpp:87
double untilTime(double t) const
Returns the time difference until the overall time reaches the given time.
void resetProgress()
Resets the progress of the time window back to 0.
void resetTo(double timeStart)
Resets the handler to the given time.
bool reachedEndOfWindow(double timeWindowSize) const
double untilProgress(double windowSize) const
double time() const
Returns the current time as a double.
double untilWindowEnd(double timeWindowSize) const
Returns the time distance to the possibly truncated end of the current time window.
void completeTimeWindow(double timeWindowSize)
TimeHandler & operator=(const TimeHandler &)
double windowStart() const
Returns the window start as a double.
double windowProgress() const
Returns the window progress as a double.
void progressBy(double dt)
Progress the time window by the given amount.
An accurate aggregator for doubles with usability in mind.
T exchange(T... args)
T max(T... args)
T min(T... args)
constexpr bool equals(const Eigen::MatrixBase< DerivedA > &A, const Eigen::MatrixBase< DerivedB > &B, double tolerance=NUMERICAL_ZERO_DIFFERENCE)
Compares two Eigen::MatrixBase for equality up to tolerance.
STL namespace.
utils::DoubleAggregator _windowStart
The aggregator for the start of a time window, handles timestep.
utils::DoubleAggregator _windowProgress
The aggregator progress inside a time window, handles substeps.
std::optional< double > _maxTime
The optional maximum time.