| helics 3.6.1

TimeDependencies.hpp
1 /*
2 Copyright (c) 2017-2025,
3 Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Sustainable
4 Energy, LLC. See the top-level NOTICE for additional details. All rights reserved.
5 SPDX-License-Identifier: BSD-3-Clause
6 */
7 #pragma once
8
9 #include "basic_CoreTypes.hpp"
10 #include "nlohmann/json_fwd.hpp"
11
12 #include <string>
13 #include <utility>
14 #include <vector>
15
16 namespace helics {
17 class ActionMessage;
18
20 enum class TimeState : std::uint8_t {
21 initialized = 0,
22 exec_requested_require_iteration = 1,
23 exec_requested_iterative = 2,
24 exec_requested = 3,
25 time_granted = 5,
26 time_requested_require_iteration = 6,
27 time_requested_iterative = 7,
28 time_requested = 8,
29 error = 10
30 };
31
33 enum class ConnectionType : std::uint8_t {
34 INDEPENDENT = 0,
35 PARENT = 1,
36 CHILD = 2,
37 SELF = 3,
38 NONE = 4,
39 };
40
42 enum class TimeProcessingResult : std::uint8_t {
43 NOT_PROCESSED = 0,
44 PROCESSED = 1,
46 PROCESSED_AND_CHECK = 2,
48 PROCESSED_NEW_REQUEST = 3,
49 DELAY_PROCESSING = 5
50 };
51
53 enum class GrantDelayMode : std::uint8_t { NONE = 0, INTERRUPTED = 1, WAITING = 2 };
54
55 inline GrantDelayMode getDelayMode(bool waiting, bool interrupted)
56 {
57 return waiting ? GrantDelayMode::WAITING :
58 (interrupted ? GrantDelayMode::INTERRUPTED : GrantDelayMode::NONE);
59 }
60
61 // helper class containing the basic timeData
62 class TimeData {
63 public:
64 Time next{negEpsilon};
65 Time Te{timeZero};
66 Time minDe{timeZero};
67 Time TeAlt{timeZero};
68 Time lastGrant{timeZero};
69 GlobalFederateId minFed{};
70 GlobalFederateId minFedActual{};
71 TimeState mTimeState{TimeState::initialized};
72 bool hasData{false};
73 bool interrupted{false};
74 bool delayedTiming{false};
75 std::int8_t timingVersion{-2};
76 std::uint8_t restrictionLevel{0};
77
78 std::int32_t timeoutCount{0};
79 std::int32_t sequenceCounter{0};
80 std::int32_t responseSequenceCounter{0};
82 std::int32_t grantedIteration{0};
83 TimeData() = default;
84 TimeData(const TimeData&) = default;
85 TimeData& operator=(const TimeData&) = default;
86 TimeData(TimeData&&) = default;
87 TimeData& operator=(TimeData&&) = default;
88 explicit TimeData(Time start,
89 TimeState startState = TimeState::initialized,
90 std::uint8_t resLevel = 0U):
91 next{start}, Te{start}, minDe{start}, TeAlt{start}, mTimeState{startState},
92 restrictionLevel{resLevel} {};
94 bool update(const TimeData& update);
95 };
96
98 class DependencyInfo: public TimeData {
99 public:
100 GlobalFederateId fedID{};
101
102 bool cyclic{false};
104 ConnectionType connection{ConnectionType::INDEPENDENT};
105 bool dependent{false};
106 bool dependency{false};
107 bool forwarding{false};
108 bool nonGranting{false};
109 bool triggered{false};
110 bool updateRequested{false};
111 // Time forwardEvent{Time::maxVal()}; //!< a predicted event
113 DependencyInfo() = default;
114 DependencyInfo(const DependencyInfo&) = default;
115 DependencyInfo& operator=(const DependencyInfo&) = default;
116 DependencyInfo(DependencyInfo&&) = default;
117 DependencyInfo& operator=(DependencyInfo&&) = default;
119 explicit DependencyInfo(GlobalFederateId id): fedID(id), forwarding{id.isBroker()} {}
120
121 template<class... Args>
122 explicit DependencyInfo(Time start, Args&&... args):
123 TimeData(start, std::forward<Args>(args)...)
124 {
125 }
126 };
127
129 class TimeDependencies {
130 private:
131 std::vector<DependencyInfo> dependencies;
132 mutable GlobalFederateId mDelayedDependency{};
133
134 public:
136 TimeDependencies() = default;
138 bool isDependency(GlobalFederateId ofed) const;
140 bool isDependent(GlobalFederateId ofed) const;
144 bool addDependency(GlobalFederateId gid);
146 void removeDependency(GlobalFederateId gid);
148 bool addDependent(GlobalFederateId gid);
150 void removeDependent(GlobalFederateId gid);
152 void resetDependency(GlobalFederateId id);
154 void removeInterdependence(GlobalFederateId gid);
156 TimeProcessingResult updateTime(const ActionMessage& cmd);
158 auto size() const { return dependencies.size(); }
160 auto begin() { return dependencies.begin(); }
162 auto end() { return dependencies.end(); }
164 auto begin() const { return dependencies.cbegin(); }
166 auto end() const { return dependencies.cend(); }
168 auto cbegin() const { return dependencies.cbegin(); }
170 auto cend() const { return dependencies.cend(); }
171
173 bool empty() const { return dependencies.empty(); }
175 const DependencyInfo* getDependencyInfo(GlobalFederateId gid) const;
176
178 DependencyInfo* getDependencyInfo(GlobalFederateId gid);
179
181 bool checkIfReadyForExecEntry(bool iterating, bool waiting) const;
183 bool checkIfAllDependenciesArePastExec(bool iterating) const;
189 bool checkIfReadyForTimeGrant(bool iterating,
190 Time desiredGrantTime,
191 GrantDelayMode delayMode) const;
192
194 void resetIteratingExecRequests();
197 void resetIteratingTimeRequests(Time requestTime);
199 void resetDependentEvents(Time grantTime);
201 bool hasActiveTimeDependencies() const;
203 bool verifySequenceCounter(Time tmin, std::int32_t sequenceCount);
205 int activeDependencyCount() const;
207 GlobalFederateId getMinDependency() const;
208
209 void setDependencyVector(const std::vector<DependencyInfo>& deps) { dependencies = deps; }
212 std::pair<int, std::string> checkForIssues(bool waiting) const;
213
214 bool hasDelayedDependency() const { return mDelayedDependency.isValid(); }
215 GlobalFederateId delayedDependency() const { return mDelayedDependency; }
216 };
217
218 inline bool checkSequenceCounter(const DependencyInfo& dep, Time tmin, std::int32_t sq)
219 {
220 return (!dep.dependency || !dep.dependent || dep.timingVersion <= 0 || dep.next > tmin ||
221 dep.next >= cBigTime || dep.responseSequenceCounter == sq);
222 }
223
224 const DependencyInfo& getExecEntryMinFederate(const TimeDependencies& dependencies,
225 GlobalFederateId self,
226 ConnectionType ignoreType = ConnectionType::NONE,
227 GlobalFederateId ignore = GlobalFederateId{});
228 static constexpr GlobalFederateId NoIgnoredFederates{};
229
230 TimeData generateMinTimeUpstream(const TimeDependencies& dependencies,
231 bool restricted,
232 GlobalFederateId self,
233 GlobalFederateId ignore,
234 std::int32_t responseCode);
235
236 TimeData generateMinTimeDownstream(const TimeDependencies& dependencies,
237 bool restricted,
238 GlobalFederateId self,
239 GlobalFederateId ignore,
240 std::int32_t responseCode);
241
242 TimeData generateMinTimeTotal(const TimeDependencies& dependencies,
243 bool restricted,
244 GlobalFederateId self,
245 GlobalFederateId ignore,
246 std::int32_t responseCode);
247
248 void generateJsonOutputTimeData(nlohmann::json& output,
249 const TimeData& dep,
250 bool includeAggregates = true);
251
252 void addTimeState(nlohmann::json& output, const TimeState state);
253
254 void generateJsonOutputDependency(nlohmann::json& output, const DependencyInfo& dep);
255 } // namespace helics
helics::ActionMessage
Definition: ActionMessage.hpp:30
helics::DependencyInfo
Definition: TimeDependencies.hpp:98
helics::DependencyInfo::dependency
bool dependency
indicator that the dependency is an actual dependency
Definition: TimeDependencies.hpp:106
helics::DependencyInfo::triggered
bool triggered
indicator that the dependency has been triggered in some way
Definition: TimeDependencies.hpp:109
helics::DependencyInfo::nonGranting
bool nonGranting
indicator that the dependency is a non granting time coordinator
Definition: TimeDependencies.hpp:108
helics::DependencyInfo::updateRequested
bool updateRequested
indicator that an update request is in process
Definition: TimeDependencies.hpp:110
helics::DependencyInfo::DependencyInfo
DependencyInfo()=default
helics::DependencyInfo::fedID
GlobalFederateId fedID
identifier for the dependency
Definition: TimeDependencies.hpp:100
helics::DependencyInfo::dependent
bool dependent
indicator the dependency is a dependent object
Definition: TimeDependencies.hpp:105
helics::DependencyInfo::cyclic
bool cyclic
Definition: TimeDependencies.hpp:102
helics::DependencyInfo::DependencyInfo
DependencyInfo(GlobalFederateId id)
Definition: TimeDependencies.hpp:119
helics::DependencyInfo::forwarding
bool forwarding
indicator that the dependency is a forwarding time coordinator
Definition: TimeDependencies.hpp:107
helics::GlobalFederateId
Definition: GlobalFederateId.hpp:75
helics::GlobalFederateId::isValid
constexpr bool isValid() const
Definition: GlobalFederateId.hpp:112
helics::TimeData
Definition: TimeDependencies.hpp:62
helics::TimeData::responseSequenceCounter
std::int32_t responseSequenceCounter
Definition: TimeDependencies.hpp:80
helics::TimeData::Te
Time Te
the next currently scheduled event
Definition: TimeDependencies.hpp:65
helics::TimeData::TeAlt
Time TeAlt
the second min event
Definition: TimeDependencies.hpp:67
helics::TimeData::grantedIteration
std::int32_t grantedIteration
the iteration of the dependency when the local iteration was granted
Definition: TimeDependencies.hpp:82
helics::TimeData::restrictionLevel
std::uint8_t restrictionLevel
timing restriction level
Definition: TimeDependencies.hpp:76
helics::TimeData::update
bool update(const TimeData &update)
Definition: TimeDependencies.cpp:173
helics::TimeData::next
Time next
next possible message or value
Definition: TimeDependencies.hpp:64
helics::TimeData::interrupted
bool interrupted
indicator that the federates next event is a timing interruption
Definition: TimeDependencies.hpp:73
helics::TimeData::timingVersion
std::int8_t timingVersion
version indicator
Definition: TimeDependencies.hpp:75
helics::TimeData::minDe
Time minDe
min dependency event time
Definition: TimeDependencies.hpp:66
helics::TimeData::timeoutCount
std::int32_t timeoutCount
counter for timeout checking
Definition: TimeDependencies.hpp:78
helics::TimeData::lastGrant
Time lastGrant
storage for the previous grant time
Definition: TimeDependencies.hpp:68
helics::TimeData::hasData
bool hasData
indicator that data was sent in the current interval
Definition: TimeDependencies.hpp:72
helics::TimeData::minFedActual
GlobalFederateId minFedActual
the actual forwarded minimum federate object
Definition: TimeDependencies.hpp:70
helics::TimeData::delayedTiming
bool delayedTiming
indicator that the dependency is using delayed timing
Definition: TimeDependencies.hpp:74
helics::TimeData::sequenceCounter
std::int32_t sequenceCounter
the sequence Counter of the request
Definition: TimeDependencies.hpp:79
helics::TimeData::minFed
GlobalFederateId minFed
identifier for the min dependency
Definition: TimeDependencies.hpp:69
helics::TimeDependencies
Definition: TimeDependencies.hpp:129
helics::TimeDependencies::checkIfReadyForExecEntry
bool checkIfReadyForExecEntry(bool iterating, bool waiting) const
Definition: TimeDependencies.cpp:503
helics::TimeDependencies::resetDependency
void resetDependency(GlobalFederateId id)
Definition: TimeDependencies.cpp:433
helics::TimeDependencies::addDependent
bool addDependent(GlobalFederateId gid)
Definition: TimeDependencies.cpp:395
helics::TimeDependencies::resetDependentEvents
void resetDependentEvents(Time grantTime)
Definition: TimeDependencies.cpp:694
helics::TimeDependencies::resetIteratingExecRequests
void resetIteratingExecRequests()
Definition: TimeDependencies.cpp:667
helics::TimeDependencies::addDependency
bool addDependency(GlobalFederateId gid)
Definition: TimeDependencies.cpp:351
helics::TimeDependencies::verifySequenceCounter
bool verifySequenceCounter(Time tmin, std::int32_t sequenceCount)
Definition: TimeDependencies.cpp:637
helics::TimeDependencies::TimeDependencies
TimeDependencies()=default
helics::TimeDependencies::empty
bool empty() const
Definition: TimeDependencies.hpp:173
helics::TimeDependencies::begin
auto begin() const
Definition: TimeDependencies.hpp:164
helics::TimeDependencies::getDependencyInfo
const DependencyInfo * getDependencyInfo(GlobalFederateId gid) const
Definition: TimeDependencies.cpp:331
helics::TimeDependencies::updateTime
TimeProcessingResult updateTime(const ActionMessage &cmd)
Definition: TimeDependencies.cpp:455
helics::TimeDependencies::removeInterdependence
void removeInterdependence(GlobalFederateId gid)
Definition: TimeDependencies.cpp:445
helics::TimeDependencies::checkForIssues
std::pair< int, std::string > checkForIssues(bool waiting) const
Definition: TimeDependencies.cpp:704
helics::TimeDependencies::cbegin
auto cbegin() const
Definition: TimeDependencies.hpp:168
helics::TimeDependencies::checkIfAllDependenciesArePastExec
bool checkIfAllDependenciesArePastExec(bool iterating) const
Definition: TimeDependencies.cpp:464
helics::TimeDependencies::begin
auto begin()
Definition: TimeDependencies.hpp:160
helics::TimeDependencies::activeDependencyCount
int activeDependencyCount() const
Definition: TimeDependencies.cpp:645
helics::TimeDependencies::hasActiveTimeDependencies
bool hasActiveTimeDependencies() const
Definition: TimeDependencies.cpp:630
helics::TimeDependencies::size
auto size() const
Definition: TimeDependencies.hpp:158
helics::TimeDependencies::end
auto end() const
Definition: TimeDependencies.hpp:166
helics::TimeDependencies::isDependency
bool isDependency(GlobalFederateId ofed) const
Definition: TimeDependencies.cpp:313
helics::TimeDependencies::checkIfReadyForTimeGrant
bool checkIfReadyForTimeGrant(bool iterating, Time desiredGrantTime, GrantDelayMode delayMode) const
Definition: TimeDependencies.cpp:553
helics::TimeDependencies::resetIteratingTimeRequests
void resetIteratingTimeRequests(Time requestTime)
Definition: TimeDependencies.cpp:681
helics::TimeDependencies::cend
auto cend() const
Definition: TimeDependencies.hpp:170
helics::TimeDependencies::end
auto end()
Definition: TimeDependencies.hpp:162
helics::TimeDependencies::removeDependent
void removeDependent(GlobalFederateId gid)
Definition: TimeDependencies.cpp:420
helics::TimeDependencies::removeDependency
void removeDependency(GlobalFederateId gid)
Definition: TimeDependencies.cpp:382
helics::TimeDependencies::isDependent
bool isDependent(GlobalFederateId ofed) const
Definition: TimeDependencies.cpp:322
helics::TimeDependencies::getMinDependency
GlobalFederateId getMinDependency() const
Definition: TimeDependencies.cpp:652
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::cBigTime
constexpr Time cBigTime
Definition: helicsTime.hpp:37
helics::timeZero
constexpr Time timeZero
Definition: helicsTime.hpp:31
helics::ConnectionType
ConnectionType
Definition: TimeDependencies.hpp:33
helics::GrantDelayMode
GrantDelayMode
Definition: TimeDependencies.hpp:53
helics::TimeProcessingResult
TimeProcessingResult
Definition: TimeDependencies.hpp:42
helics::TimeProcessingResult::DELAY_PROCESSING
@ DELAY_PROCESSING
the message should be delayed and reprocessed later
helics::TimeProcessingResult::PROCESSED_NEW_REQUEST
@ PROCESSED_NEW_REQUEST
the message was used to update the current state and a new request was made
helics::TimeProcessingResult::PROCESSED_AND_CHECK
@ PROCESSED_AND_CHECK
the message was used to update the current state and additional checks should be made
helics::TimeProcessingResult::PROCESSED
@ PROCESSED
helics::TimeProcessingResult::NOT_PROCESSED
@ NOT_PROCESSED
the message did not result in an update
helics::TimeState
TimeState
Definition: TimeDependencies.hpp:20
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27
helics::negEpsilon
constexpr Time negEpsilon
Definition: helicsTime.hpp:35