| helics 3.6.1

BaseTimeCoordinator.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 "ActionMessage.hpp"
10 #include "CoreFederateInfo.hpp"
11 #include "TimeDependencies.hpp"
12 #include "nlohmann/json_fwd.hpp"
13
14 #include <atomic>
15 #include <functional>
16 #include <string>
17 #include <utility>
18 #include <vector>
19
20 namespace helics {
21
25 class BaseTimeCoordinator {
26 protected:
27 TimeDependencies dependencies;
29 std::function<void(const ActionMessage&)> sendMessageFunction;
31
32 GlobalFederateId mSourceId{0};
33 std::int32_t sequenceCounter{0};
34 bool noParent{true};
35 bool federatesOnly{false};
36 bool checkingExec{false};
37 bool executionMode{false};
39 bool restrictive_time_policy{false};
42 bool nonGranting{false};
43 bool delayedTiming{false};
44 bool disconnected{false};
45
46 public:
47 static constexpr std::int32_t TIME_COORDINATOR_VERSION{1};
48 BaseTimeCoordinator();
49 explicit BaseTimeCoordinator(std::function<void(const ActionMessage&)> userSendMessageFunction);
50 virtual ~BaseTimeCoordinator() = default;
52 void setMessageSender(std::function<void(const ActionMessage&)> userSendMessageFunction);
53
54 void setRestrictivePolicy(bool policy) { restrictive_time_policy = policy; }
56 std::vector<GlobalFederateId> getDependencies() const;
58 std::vector<GlobalFederateId> getDependents() const;
60 Time getLastGrant(GlobalFederateId fedId) const;
62 void setSourceId(GlobalFederateId sourceId) { mSourceId = sourceId; }
63 GlobalFederateId sourceId() const { return mSourceId; }
67 virtual bool updateTimeFactors() = 0;
68
72 const DependencyInfo* getDependencyInfo(GlobalFederateId ofed) const;
74 bool isDependency(GlobalFederateId ofed) const;
76 bool empty() const { return dependencies.empty(); }
77
78 protected:
80 ActionMessage generateTimeRequest(const TimeData& dep,
81 GlobalFederateId fed,
82 std::int32_t responseCode) const;
84 void sendTimingInfo();
85
86 public:
90 virtual TimeProcessingResult processTimeMessage(const ActionMessage& cmd);
91
93 void processDependencyUpdateMessage(const ActionMessage& cmd);
97 virtual bool addDependency(GlobalFederateId fedID);
101 virtual bool addDependent(GlobalFederateId fedID);
104 virtual void removeDependency(GlobalFederateId fedID);
107 virtual void removeDependent(GlobalFederateId fedID);
110 virtual void resetDependency(GlobalFederateId fedID);
111
112 void setAsChild(GlobalFederateId fedID);
113 void setAsParent(GlobalFederateId fedID);
114 void setVersion(GlobalFederateId fedID, std::int8_t version);
115 GlobalFederateId getParent() const;
116
118 void disconnect();
120 virtual MessageProcessingResult
121 checkExecEntry(GlobalFederateId triggerFed = GlobalFederateId{}) = 0;
122
126 virtual void enteringExecMode(IterationRequest mode = IterationRequest::NO_ITERATIONS);
127
129 virtual std::string printTimeStatus() const = 0;
131 virtual void generateDebuggingTimeInfo(nlohmann::json& base) const;
132
134 bool hasActiveTimeDependencies() const;
135
137 int dependencyCount() const;
139 GlobalFederateId getMinDependency() const;
140
143 nlohmann::json grantTimeoutCheck(const ActionMessage& cmd);
145 virtual Time getNextTime() const = 0;
146 };
147 } // namespace helics
helics::ActionMessage
Definition: ActionMessage.hpp:30
helics::BaseTimeCoordinator
Definition: BaseTimeCoordinator.hpp:25
helics::BaseTimeCoordinator::getDependencyInfo
const DependencyInfo * getDependencyInfo(GlobalFederateId ofed) const
Definition: BaseTimeCoordinator.cpp:238
helics::BaseTimeCoordinator::executionMode
bool executionMode
Definition: BaseTimeCoordinator.hpp:37
helics::BaseTimeCoordinator::enteringExecMode
virtual void enteringExecMode(IterationRequest mode=IterationRequest::NO_ITERATIONS)
Definition: BaseTimeCoordinator.cpp:45
helics::BaseTimeCoordinator::noParent
bool noParent
indicator that the coordinator does not have parents
Definition: BaseTimeCoordinator.hpp:34
helics::BaseTimeCoordinator::addDependency
virtual bool addDependency(GlobalFederateId fedID)
Definition: BaseTimeCoordinator.cpp:163
helics::BaseTimeCoordinator::generateDebuggingTimeInfo
virtual void generateDebuggingTimeInfo(nlohmann::json &base) const
Definition: BaseTimeCoordinator.cpp:125
helics::BaseTimeCoordinator::resetDependency
virtual void resetDependency(GlobalFederateId fedID)
Definition: BaseTimeCoordinator.cpp:233
helics::BaseTimeCoordinator::addDependent
virtual bool addDependent(GlobalFederateId fedID)
Definition: BaseTimeCoordinator.cpp:177
helics::BaseTimeCoordinator::hasActiveTimeDependencies
bool hasActiveTimeDependencies() const
Definition: BaseTimeCoordinator.cpp:271
helics::BaseTimeCoordinator::grantTimeoutCheck
nlohmann::json grantTimeoutCheck(const ActionMessage &cmd)
Definition: BaseTimeCoordinator.cpp:143
helics::BaseTimeCoordinator::sendTimingInfo
void sendTimingInfo()
Definition: BaseTimeCoordinator.cpp:286
helics::BaseTimeCoordinator::checkExecEntry
virtual MessageProcessingResult checkExecEntry(GlobalFederateId triggerFed=GlobalFederateId{})=0
helics::BaseTimeCoordinator::processDependencyUpdateMessage
void processDependencyUpdateMessage(const ActionMessage &cmd)
Definition: BaseTimeCoordinator.cpp:413
helics::BaseTimeCoordinator::isDependency
bool isDependency(GlobalFederateId ofed) const
Definition: BaseTimeCoordinator.cpp:158
helics::BaseTimeCoordinator::dependencies
TimeDependencies dependencies
Definition: BaseTimeCoordinator.hpp:27
helics::BaseTimeCoordinator::getLastGrant
Time getLastGrant(GlobalFederateId fedId) const
Definition: BaseTimeCoordinator.cpp:265
helics::BaseTimeCoordinator::disconnect
void disconnect()
Definition: BaseTimeCoordinator.cpp:80
helics::BaseTimeCoordinator::empty
bool empty() const
Definition: BaseTimeCoordinator.hpp:76
helics::BaseTimeCoordinator::sendMessageFunction
std::function< void(const ActionMessage &)> sendMessageFunction
callback used to send the messages
Definition: BaseTimeCoordinator.hpp:29
helics::BaseTimeCoordinator::generateTimeRequest
ActionMessage generateTimeRequest(const TimeData &dep, GlobalFederateId fed, std::int32_t responseCode) const
Definition: BaseTimeCoordinator.cpp:306
helics::BaseTimeCoordinator::getDependents
std::vector< GlobalFederateId > getDependents() const
Definition: BaseTimeCoordinator.cpp:254
helics::BaseTimeCoordinator::restrictive_time_policy
bool restrictive_time_policy
flag indicating that a restrictive time policy should be used
Definition: BaseTimeCoordinator.hpp:39
helics::BaseTimeCoordinator::mSourceId
GlobalFederateId mSourceId
the identifier for inserting into the source id field of any generated messages;
Definition: BaseTimeCoordinator.hpp:32
helics::BaseTimeCoordinator::setSourceId
void setSourceId(GlobalFederateId sourceId)
Definition: BaseTimeCoordinator.hpp:62
helics::BaseTimeCoordinator::getMinDependency
GlobalFederateId getMinDependency() const
Definition: BaseTimeCoordinator.cpp:281
helics::BaseTimeCoordinator::processTimeMessage
virtual TimeProcessingResult processTimeMessage(const ActionMessage &cmd)
Definition: BaseTimeCoordinator.cpp:385
helics::BaseTimeCoordinator::updateTimeFactors
virtual bool updateTimeFactors()=0
helics::BaseTimeCoordinator::removeDependent
virtual void removeDependent(GlobalFederateId fedID)
Definition: BaseTimeCoordinator.cpp:228
helics::BaseTimeCoordinator::sequenceCounter
std::int32_t sequenceCounter
storage for sequence counter
Definition: BaseTimeCoordinator.hpp:33
helics::BaseTimeCoordinator::federatesOnly
bool federatesOnly
indicator that the forwarder only operates with federates
Definition: BaseTimeCoordinator.hpp:35
helics::BaseTimeCoordinator::printTimeStatus
virtual std::string printTimeStatus() const =0
helics::BaseTimeCoordinator::getNextTime
virtual Time getNextTime() const =0
helics::BaseTimeCoordinator::setMessageSender
void setMessageSender(std::function< void(const ActionMessage &)> userSendMessageFunction)
Definition: BaseTimeCoordinator.cpp:36
helics::BaseTimeCoordinator::getDependencies
std::vector< GlobalFederateId > getDependencies() const
Definition: BaseTimeCoordinator.cpp:243
helics::BaseTimeCoordinator::removeDependency
virtual void removeDependency(GlobalFederateId fedID)
Definition: BaseTimeCoordinator.cpp:223
helics::BaseTimeCoordinator::dependencyCount
int dependencyCount() const
Definition: BaseTimeCoordinator.cpp:276
helics::BaseTimeCoordinator::nonGranting
bool nonGranting
Definition: BaseTimeCoordinator.hpp:42
helics::DependencyInfo
Definition: TimeDependencies.hpp:98
helics::GlobalFederateId
Definition: GlobalFederateId.hpp:75
helics::TimeData
Definition: TimeDependencies.hpp:62
helics::TimeDependencies
Definition: TimeDependencies.hpp:129
helics::TimeDependencies::empty
bool empty() const
Definition: TimeDependencies.hpp:173
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::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