| helics 3.6.1

TimeCoordinator.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 "../common/GuardedTypes.hpp"
10 #include "ActionMessage.hpp"
11 #include "BaseTimeCoordinator.hpp"
12
13 #include <atomic>
14 #include <functional>
15 #include <nlohmann/json_fwd.hpp>
16 #include <string>
17 #include <utility>
18 #include <vector>
19
20 namespace helics {
21
23 class tcoptions {
24 public:
25 Time timeDelta = Time::epsilon();
26 Time inputDelay = timeZero;
27 Time outputDelay = timeZero;
28 Time offset = timeZero;
29 Time period = timeZero;
30 // Time rtLag = timeZero;
31 // Time rtLead = timeZero;
32 // bool observer = false;
33 // bool realtime = false;
34 // bool source_only = false;
35 bool wait_for_current_time_updates = false;
36 bool uninterruptible = false;
37 bool restrictive_time_policy = false;
40 bool event_triggered = false;
41 int maxIterations = 50;
42 };
43
48 class TimeCoordinator: public BaseTimeCoordinator {
49 protected:
51 TimeData upstream;
52 TimeData total;
53 mutable TimeData lastSend;
54 // the variables for time coordination
55 Time time_granted{Time::minVal()};
56 Time time_requested{Time::maxVal()};
57 Time time_next{timeZero};
58 Time time_minminDe{timeZero};
59 Time time_minDe{timeZero};
60 Time time_allow{Time::minVal()};
61 Time time_exec{Time::maxVal()};
62 Time time_message{Time::maxVal()};
63 Time time_value{Time::maxVal()};
66 Time time_grantBase{Time::minVal()};
68 Time time_block{Time::maxVal()};
69 Time maxTime{Time::maxVal()};
71 shared_guarded_m<std::vector<GlobalFederateId>> dependent_federates;
73 shared_guarded_m<std::vector<GlobalFederateId>> dependency_federates;
75 std::vector<std::pair<Time, int32_t>> timeBlocks;
77 tcoptions info;
78 std::uint8_t currentRestrictionLevel{0};
79
80 public:
82 IterationRequest iterating{IterationRequest::NO_ITERATIONS};
83
85 bool hasInitUpdates{false};
87 bool hasIterationData{false};
90 bool needSendAll{false};
92 mutable bool triggered{false};
94 bool globalTime{false};
95
96 protected:
98 bool dynamicJoining{false};
99
100 std::atomic<int32_t> iteration{0};
101
102 public:
104 TimeCoordinator() = default;
106 explicit TimeCoordinator(std::function<void(const ActionMessage&)> userSendMessageFunction):
107 BaseTimeCoordinator(std::move(userSendMessageFunction))
108 {
109 }
110
112 void setProperty(int timeProperty, Time propertyVal);
114 void setProperty(int intProperty, int propertyVal);
116 void setOptionFlag(int optionFlag, bool value);
118 void setDynamicJoining() { dynamicJoining = true; }
119
121 Time getTimeProperty(int timeProperty) const;
123 bool getOptionFlag(int optionFlag) const;
125 int getIntegerProperty(int intProperty) const;
126
128 Time getGrantedTime() const { return time_granted; }
130 Time getRequestedTime() const { return time_requested; }
132 Time allowedSendTime() const { return time_granted + info.outputDelay; }
134 std::vector<GlobalFederateId> getDependencies() const;
136 std::vector<GlobalFederateId> getDependents() const
137 {
138 return *dependent_federates.lock_shared();
139 }
140 const auto& getBarriers() const { return timeBlocks; }
144 int32_t getCurrentIteration() const { return iteration.load(); }
148 virtual bool updateTimeFactors() override;
152 void updateValueTime(Time valueUpdateTime, bool allowRequestSend);
156 void updateMessageTime(Time messageUpdateTime, bool allowRequestSend);
157
158 void specifyNonGranting(bool value = true) { nonGranting = value; }
159
160 private:
162 bool updateNextExecutionTime();
165 void updateNextPossibleEventTime();
167 Time getNextPossibleTime() const;
168 Time generateAllowedTime(Time testTime) const;
169 /* return true,true if the message was sent and skip federate was detected
170 true,false if the message was sent and skip federate was not detected
171 false,false, if the message was not sent
172 */
173 std::pair<bool, bool> checkAndSendTimeRequest(ActionMessage& upd, GlobalFederateId skip) const;
174
175 void sendTimeRequest(GlobalFederateId triggerFed) const;
176 void updateTimeGrant();
178 bool transmitTimingMessages(ActionMessage& msg,
179 GlobalFederateId skipFed = GlobalFederateId{}) const;
180
181 TimeProcessingResult processTimeBlockMessage(const ActionMessage& cmd);
182
183 Time updateTimeBlocks(int32_t blockId, Time newTime);
184
185 public:
186 virtual TimeProcessingResult processTimeMessage(const ActionMessage& cmd) override;
187
191 void processConfigUpdateMessage(const ActionMessage& cmd);
195 virtual bool addDependency(GlobalFederateId fedID) override;
199 virtual bool addDependent(GlobalFederateId fedID) override;
202 virtual void removeDependency(GlobalFederateId fedID) override;
205 virtual void removeDependent(GlobalFederateId fedID) override;
206
208 virtual MessageProcessingResult
209 checkExecEntry(GlobalFederateId triggerFed = GlobalFederateId{}) override;
210
212 void sendUpdatedExecRequest(GlobalFederateId target = GlobalFederateId{},
213 GlobalFederateId minFed = GlobalFederateId{},
214 std::int32_t responseSequenceCounter = 0);
221 void timeRequest(Time nextTime,
222 IterationRequest iterate,
223 Time newValueTime,
224 Time newMessageTime);
225
226 virtual void enteringExecMode(IterationRequest mode) override;
228 MessageProcessingResult checkTimeGrant(GlobalFederateId triggerFed = GlobalFederateId{});
230 void localError();
232 virtual std::string printTimeStatus() const override;
234 void generateConfig(nlohmann::json& base) const;
235
237 virtual void generateDebuggingTimeInfo(nlohmann::json& base) const override;
238
240 std::pair<GlobalFederateId, Time> getMinGrantedDependency() const;
242 void enterInitialization();
244 void requestTimeCheck();
245
246 virtual Time getNextTime() const override;
247 };
248
249 } // namespace helics
gmlc::libguarded::shared_guarded
Definition: application_api/Federate.hpp:27
helics::ActionMessage
Definition: ActionMessage.hpp:30
helics::BaseTimeCoordinator
Definition: BaseTimeCoordinator.hpp:25
helics::BaseTimeCoordinator::nonGranting
bool nonGranting
Definition: BaseTimeCoordinator.hpp:42
helics::TimeCoordinator
Definition: TimeCoordinator.hpp:48
helics::TimeCoordinator::time_requested
Time time_requested
the most recent time requested
Definition: TimeCoordinator.hpp:56
helics::TimeCoordinator::printTimeStatus
virtual std::string printTimeStatus() const override
Definition: TimeCoordinator.cpp:880
helics::TimeCoordinator::time_message
Time time_message
the time of the earliest message event
Definition: TimeCoordinator.hpp:62
helics::TimeCoordinator::updateTimeFactors
virtual bool updateTimeFactors() override
Definition: TimeCoordinator.cpp:429
helics::TimeCoordinator::timeBlocks
std::vector< std::pair< Time, int32_t > > timeBlocks
blocks for a particular timeblocking link
Definition: TimeCoordinator.hpp:75
helics::TimeCoordinator::checkExecEntry
virtual MessageProcessingResult checkExecEntry(GlobalFederateId triggerFed=GlobalFederateId{}) override
Definition: TimeCoordinator.cpp:993
helics::TimeCoordinator::removeDependent
virtual void removeDependent(GlobalFederateId fedID) override
Definition: TimeCoordinator.cpp:923
helics::TimeCoordinator::getDependencies
std::vector< GlobalFederateId > getDependencies() const
Definition: TimeCoordinator.cpp:934
helics::TimeCoordinator::needSendAll
bool needSendAll
Definition: TimeCoordinator.hpp:90
helics::TimeCoordinator::processConfigUpdateMessage
void processConfigUpdateMessage(const ActionMessage &cmd)
Definition: TimeCoordinator.cpp:1472
helics::TimeCoordinator::setOptionFlag
void setOptionFlag(int optionFlag, bool value)
Definition: TimeCoordinator.cpp:1403
helics::TimeCoordinator::addDependency
virtual bool addDependency(GlobalFederateId fedID) override
Definition: TimeCoordinator.cpp:894
helics::TimeCoordinator::info
tcoptions info
basic time control information
Definition: TimeCoordinator.hpp:77
helics::TimeCoordinator::enterInitialization
void enterInitialization()
Definition: TimeCoordinator.cpp:304
helics::TimeCoordinator::updateMessageTime
void updateMessageTime(Time messageUpdateTime, bool allowRequestSend)
Definition: TimeCoordinator.cpp:395
helics::TimeCoordinator::upstream
TimeData upstream
the variables for time coordination
Definition: TimeCoordinator.hpp:51
helics::TimeCoordinator::updateValueTime
void updateValueTime(Time valueUpdateTime, bool allowRequestSend)
Definition: TimeCoordinator.cpp:210
helics::TimeCoordinator::hasInitUpdates
bool hasInitUpdates
flag indicating that a value or message was received during initialization stage
Definition: TimeCoordinator.hpp:85
helics::TimeCoordinator::dynamicJoining
bool dynamicJoining
if set to true the time coordinator is joining an ongoing co-simulation
Definition: TimeCoordinator.hpp:98
helics::TimeCoordinator::getRequestedTime
Time getRequestedTime() const
Definition: TimeCoordinator.hpp:130
helics::TimeCoordinator::checkTimeGrant
MessageProcessingResult checkTimeGrant(GlobalFederateId triggerFed=GlobalFederateId{})
Definition: TimeCoordinator.cpp:479
helics::TimeCoordinator::removeDependency
virtual void removeDependency(GlobalFederateId fedID) override
Definition: TimeCoordinator.cpp:912
helics::TimeCoordinator::time_block
Time time_block
a blocking time to not grant time >= the specified time
Definition: TimeCoordinator.hpp:68
helics::TimeCoordinator::getIntegerProperty
int getIntegerProperty(int intProperty) const
Definition: TimeCoordinator.cpp:1442
helics::TimeCoordinator::getMinGrantedDependency
std::pair< GlobalFederateId, Time > getMinGrantedDependency() const
Definition: TimeCoordinator.cpp:1203
helics::TimeCoordinator::iteration
std::atomic< int32_t > iteration
current number of iterations
Definition: TimeCoordinator.hpp:100
helics::TimeCoordinator::setProperty
void setProperty(int timeProperty, Time propertyVal)
Definition: TimeCoordinator.cpp:1367
helics::TimeCoordinator::enteringExecMode
virtual void enteringExecMode(IterationRequest mode) override
Definition: TimeCoordinator.cpp:24
helics::TimeCoordinator::time_next
Time time_next
the next possible internal event time
Definition: TimeCoordinator.hpp:57
helics::TimeCoordinator::TimeCoordinator
TimeCoordinator(std::function< void(const ActionMessage &)> userSendMessageFunction)
Definition: TimeCoordinator.hpp:106
helics::TimeCoordinator::timeRequest
void timeRequest(Time nextTime, IterationRequest iterate, Time newValueTime, Time newMessageTime)
Definition: TimeCoordinator.cpp:103
helics::TimeCoordinator::globalTime
bool globalTime
true if using a global time manager parent
Definition: TimeCoordinator.hpp:94
helics::TimeCoordinator::getGrantedTime
Time getGrantedTime() const
Definition: TimeCoordinator.hpp:128
helics::TimeCoordinator::hasIterationData
bool hasIterationData
flag indicating that a value or message was received during iteration
Definition: TimeCoordinator.hpp:87
helics::TimeCoordinator::triggered
bool triggered
indicator the federate was triggered recently
Definition: TimeCoordinator.hpp:92
helics::TimeCoordinator::getDependents
std::vector< GlobalFederateId > getDependents() const
Definition: TimeCoordinator.hpp:136
helics::TimeCoordinator::time_value
Time time_value
the time of the earliest value event
Definition: TimeCoordinator.hpp:63
helics::TimeCoordinator::time_allow
Time time_allow
the current allowable time
Definition: TimeCoordinator.hpp:60
helics::TimeCoordinator::dependency_federates
shared_guarded_m< std::vector< GlobalFederateId > > dependency_federates
these are to maintain an accessible record of dependency federates
Definition: TimeCoordinator.hpp:73
helics::TimeCoordinator::setDynamicJoining
void setDynamicJoining()
Definition: TimeCoordinator.hpp:118
helics::TimeCoordinator::sendUpdatedExecRequest
void sendUpdatedExecRequest(GlobalFederateId target=GlobalFederateId{}, GlobalFederateId minFed=GlobalFederateId{}, std::int32_t responseSequenceCounter=0)
Definition: TimeCoordinator.cpp:958
helics::TimeCoordinator::time_grantBase
Time time_grantBase
Definition: TimeCoordinator.hpp:66
helics::TimeCoordinator::generateConfig
void generateConfig(nlohmann::json &base) const
Definition: TimeCoordinator.cpp:244
helics::TimeCoordinator::dependent_federates
shared_guarded_m< std::vector< GlobalFederateId > > dependent_federates
these are to maintain an accessible record of dependent federates
Definition: TimeCoordinator.hpp:71
helics::TimeCoordinator::localError
void localError()
Definition: TimeCoordinator.cpp:60
helics::TimeCoordinator::generateDebuggingTimeInfo
virtual void generateDebuggingTimeInfo(nlohmann::json &base) const override
Definition: TimeCoordinator.cpp:269
helics::TimeCoordinator::addDependent
virtual bool addDependent(GlobalFederateId fedID) override
Definition: TimeCoordinator.cpp:903
helics::TimeCoordinator::TimeCoordinator
TimeCoordinator()=default
helics::TimeCoordinator::iterating
IterationRequest iterating
indicator that the coordinator should be iteratingif need be
Definition: TimeCoordinator.hpp:82
helics::TimeCoordinator::time_granted
Time time_granted
the most recent time granted
Definition: TimeCoordinator.hpp:55
helics::TimeCoordinator::getNextTime
virtual Time getNextTime() const override
Definition: TimeCoordinator.cpp:322
helics::TimeCoordinator::processTimeMessage
virtual TimeProcessingResult processTimeMessage(const ActionMessage &cmd) override
Definition: TimeCoordinator.cpp:1221
helics::TimeCoordinator::requestTimeCheck
void requestTimeCheck()
Definition: TimeCoordinator.cpp:327
helics::TimeCoordinator::getTimeProperty
Time getTimeProperty(int timeProperty) const
Definition: TimeCoordinator.cpp:1424
helics::TimeCoordinator::time_exec
Time time_exec
the time of the next targeted execution
Definition: TimeCoordinator.hpp:61
helics::TimeCoordinator::getOptionFlag
bool getOptionFlag(int optionFlag) const
Definition: TimeCoordinator.cpp:1454
helics::TimeCoordinator::time_minminDe
Time time_minminDe
the minimum of the minimum dependency event Time
Definition: TimeCoordinator.hpp:58
helics::TimeCoordinator::allowedSendTime
Time allowedSendTime() const
Definition: TimeCoordinator.hpp:132
helics::TimeCoordinator::getCurrentIteration
int32_t getCurrentIteration() const
Definition: TimeCoordinator.hpp:144
helics::TimeCoordinator::time_minDe
Time time_minDe
the minimum event time of the dependencies
Definition: TimeCoordinator.hpp:59
helics::TimeData
Definition: TimeDependencies.hpp:62
helics::tcoptions
Definition: TimeCoordinator.hpp:23
helics::tcoptions::event_triggered
bool event_triggered
Definition: TimeCoordinator.hpp:40
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::MessageProcessingResult
MessageProcessingResult
Definition: CoreTypes.hpp:75
helics::timeZero
constexpr Time timeZero
Definition: helicsTime.hpp:31
helics::IterationRequest
IterationRequest
Definition: CoreTypes.hpp:103
helics::IterationRequest::NO_ITERATIONS
@ NO_ITERATIONS
indicator that the iterations have completed
helics::TimeProcessingResult
TimeProcessingResult
Definition: TimeDependencies.hpp:42
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27