helics  3.0.1
ForwardingTimeCoordinator.hpp
1 /*
2 Copyright (c) 2017-2021,
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 
13 #include "json/forwards.h"
14 #include <atomic>
15 #include <functional>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 namespace helics {
21 
27  private:
28  // the variables for time coordination
29  DependencyInfo upstream;
30  DependencyInfo downstream;
31 
32  // Core::local_federate_id parent = invalid_fed_id; //!< the id for the parent object which
33  // should also be a ForwardingTimeCoordinator
34  TimeDependencies dependencies;
35  std::function<void(const ActionMessage&)> sendMessageFunction;
37 
38  public:
41  GlobalFederateId delayedFederate{};
43  bool checkingExec{false};
44  bool executionMode{false};
45  bool iterating{false};
46  bool ignoreMinFed{false};
47  bool restrictive_time_policy{false};
49  bool noParent{false};
51  private:
52  bool federatesOnly{false};
53  public:
54  ForwardingTimeCoordinator() = default;
55 
57  void setMessageSender(std::function<void(const ActionMessage&)> userSendMessageFunction)
58  {
59  sendMessageFunction = std::move(userSendMessageFunction);
60  }
61 
63  std::vector<GlobalFederateId> getDependencies() const;
65  std::vector<GlobalFederateId> getDependents() const;
66 
70  void updateTimeFactors();
71 
77  bool isDependency(GlobalFederateId ofed) const;
79  bool empty() const { return dependencies.empty(); }
80 
81  private:
82  void transmitTimingMessagesUpstream(ActionMessage& msg) const;
83  void transmitTimingMessagesDownstream(ActionMessage& msg,
84  GlobalFederateId skipFed = GlobalFederateId{}) const;
86  ActionMessage generateTimeRequest(const DependencyInfo& dep, GlobalFederateId fed) const;
87 
88  public:
92  bool processTimeMessage(const ActionMessage& cmd);
93 
95  void processDependencyUpdateMessage(const ActionMessage& cmd);
99  bool addDependency(GlobalFederateId fedID);
103  bool addDependent(GlobalFederateId fedID);
106  void removeDependency(GlobalFederateId fedID);
109  void removeDependent(GlobalFederateId fedID);
110 
111  void setAsChild(GlobalFederateId fedID);
112  void setAsParent(GlobalFederateId fedID);
113 
115  void disconnect();
118 
121  void enteringExecMode();
122 
124  std::string printTimeStatus() const;
126  void generateDebuggingTimeInfo(Json::Value& base) const;
127 
129  bool hasActiveTimeDependencies() const;
131  Time getNextTime() const { return downstream.next; }
133  int dependencyCount() const;
136 };
137 } // namespace helics
helics::timeZero
constexpr Time timeZero
Definition: helicsTime.hpp:31
helics::ForwardingTimeCoordinator::getNextTime
Time getNextTime() const
Definition: ForwardingTimeCoordinator.hpp:131
helics::ForwardingTimeCoordinator::restrictive_time_policy
bool restrictive_time_policy
flag indicating that a restrictive time policy should be used
Definition: ForwardingTimeCoordinator.hpp:48
helics::GlobalFederateId::baseValue
constexpr BaseType baseValue() const
Definition: GlobalFederateId.hpp:80
helics::ForwardingTimeCoordinator::removeDependent
void removeDependent(GlobalFederateId fedID)
Definition: ForwardingTimeCoordinator.cpp:199
helics::TimeDependencies::removeDependent
void removeDependent(GlobalFederateId id)
Definition: TimeDependencies.cpp:303
helics::ForwardingTimeCoordinator::addDependent
bool addDependent(GlobalFederateId fedID)
Definition: ForwardingTimeCoordinator.cpp:173
helics::ForwardingTimeCoordinator::updateTimeFactors
void updateTimeFactors()
Definition: ForwardingTimeCoordinator.cpp:83
helics::ForwardingTimeCoordinator::addDependency
bool addDependency(GlobalFederateId fedID)
Definition: ForwardingTimeCoordinator.cpp:168
helics::ForwardingTimeCoordinator::getDependencyInfo
const DependencyInfo * getDependencyInfo(GlobalFederateId ofed) const
Definition: ForwardingTimeCoordinator.cpp:204
helics::ForwardingTimeCoordinator::removeDependency
void removeDependency(GlobalFederateId fedID)
Definition: ForwardingTimeCoordinator.cpp:194
helics::ForwardingTimeCoordinator::generateDebuggingTimeInfo
void generateDebuggingTimeInfo(Json::Value &base) const
Definition: ForwardingTimeCoordinator.cpp:130
helics::TimeDependencies
Definition: TimeDependencies.hpp:75
helics_definitions.hpp
base helics enumerations for C++ API's, a namespace wrapper for the definitions defined in helics_enu...
helics::TimeDependencies::getMinDependency
GlobalFederateId getMinDependency() const
Definition: TimeDependencies.cpp:363
helics::ForwardingTimeCoordinator::source_id
GlobalFederateId source_id
the identifier for inserting into the source id field of any generated messages;
Definition: ForwardingTimeCoordinator.hpp:40
helics::TimeDependencies::removeDependency
void removeDependency(GlobalFederateId id)
Definition: TimeDependencies.cpp:265
helics::DependencyInfo::dependent
bool dependent
indicator the dependency is a dependent object
Definition: TimeDependencies.hpp:60
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27
helics::TimeData::Te
Time Te
the next currently scheduled event
Definition: TimeDependencies.hpp:39
helics::GlobalFederateId
Definition: GlobalFederateId.hpp:68
helics::ActionMessage
Definition: ActionMessage.hpp:30
helics::parent_broker_id
constexpr GlobalBrokerId parent_broker_id
Definition: GlobalFederateId.hpp:60
helics::ForwardingTimeCoordinator::ignoreMinFed
bool ignoreMinFed
Definition: ForwardingTimeCoordinator.hpp:46
helics::TimeDependencies::isDependency
bool isDependency(GlobalFederateId ofed) const
Definition: TimeDependencies.cpp:202
iteration_requested_flag
@ iteration_requested_flag
indicator that an iteration has been requested
Definition: flagOperations.hpp:16
helics::ForwardingTimeCoordinator
Definition: ForwardingTimeCoordinator.hpp:26
helics::ForwardingTimeCoordinator::processTimeMessage
bool processTimeMessage(const ActionMessage &cmd)
Definition: ForwardingTimeCoordinator.cpp:364
helics::appendMessage
int appendMessage(ActionMessage &m, const ActionMessage &newMessage)
Definition: ActionMessage.cpp:919
checkActionFlag
bool checkActionFlag(uint16_t flags, FlagIndex flag)
Definition: flagOperations.hpp:85
helics::TimeDependencies::size
auto size() const
Definition: TimeDependencies.hpp:100
helics::ForwardingTimeCoordinator::checkExecEntry
MessageProcessingResult checkExecEntry()
Definition: ForwardingTimeCoordinator.cpp:246
helics::ForwardingTimeCoordinator::executionMode
bool executionMode
flag that the coordinator has entered the execution Mode
Definition: ForwardingTimeCoordinator.hpp:44
helics::ForwardingTimeCoordinator::processDependencyUpdateMessage
void processDependencyUpdateMessage(const ActionMessage &cmd)
Definition: ForwardingTimeCoordinator.cpp:380
child_flag
constexpr uint16_t child_flag
overload of extra_flag4 indicating a message is from a child object
Definition: flagOperations.hpp:49
helics::TimeData::next
Time next
next possible message or value
Definition: TimeDependencies.hpp:38
helics::TimeDependencies::activeDependencyCount
int activeDependencyCount() const
Definition: TimeDependencies.cpp:356
helics::ForwardingTimeCoordinator::empty
bool empty() const
Definition: ForwardingTimeCoordinator.hpp:79
helics::TimeDependencies::getDependencyInfo
const DependencyInfo * getDependencyInfo(GlobalFederateId id) const
Definition: TimeDependencies.cpp:220
helics::ActionMessage::messageID
int32_t messageID
8 – message ID for a variety of purposes
Definition: ActionMessage.hpp:36
helics::ForwardingTimeCoordinator::setMessageSender
void setMessageSender(std::function< void(const ActionMessage &)> userSendMessageFunction)
Definition: ForwardingTimeCoordinator.hpp:57
helics::TimeDependencies::begin
auto begin()
Definition: TimeDependencies.hpp:102
helics::ForwardingTimeCoordinator::enteringExecMode
void enteringExecMode()
Definition: ForwardingTimeCoordinator.cpp:22
helics::ForwardingTimeCoordinator::hasActiveTimeDependencies
bool hasActiveTimeDependencies() const
Definition: ForwardingTimeCoordinator.cpp:231
setActionFlag
void setActionFlag(FlagContainer &M, FlagIndex flag)
Definition: flagOperations.hpp:77
helics::ActionMessage::payload
SmallBuffer payload
buffer to contain the data payload
Definition: ActionMessage.hpp:48
helics::ActionMessage::dest_id
GlobalFederateId dest_id
20 fed_id for a targeted message
Definition: ActionMessage.hpp:39
helics::TimeDependencies::addDependency
bool addDependency(GlobalFederateId id)
Definition: TimeDependencies.cpp:240
helics::DependencyInfo
Definition: TimeDependencies.hpp:53
helics::MessageProcessingResult::ERROR_RESULT
@ ERROR_RESULT
indicator that an error has occurred
parent_flag
constexpr uint16_t parent_flag
overload of extra_flag3 indicating the message is from a parent object
Definition: flagOperations.hpp:46
helics::ForwardingTimeCoordinator::disconnect
void disconnect()
Definition: ForwardingTimeCoordinator.cpp:47
helics::ForwardingTimeCoordinator::hasDelayedTimingFederate
bool hasDelayedTimingFederate
indicator that federate is using delayed timing
Definition: ForwardingTimeCoordinator.hpp:50
helics::TimeDependencies::addDependent
bool addDependent(GlobalFederateId id)
Definition: TimeDependencies.cpp:278
helics::ForwardingTimeCoordinator::noParent
bool noParent
indicator that the coordinator does not have parents
Definition: ForwardingTimeCoordinator.hpp:49
helics::TimeDependencies::checkIfReadyForExecEntry
bool checkIfReadyForExecEntry(bool iterating) const
Definition: TimeDependencies.cpp:337
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::ForwardingTimeCoordinator::checkingExec
bool checkingExec
flag indicating that the coordinator is trying to enter the exec mode
Definition: ForwardingTimeCoordinator.hpp:43
helics::TimeDependencies::hasActiveTimeDependencies
bool hasActiveTimeDependencies() const
Definition: TimeDependencies.cpp:349
helics::ForwardingTimeCoordinator::getDependencies
std::vector< GlobalFederateId > getDependencies() const
Definition: ForwardingTimeCoordinator.cpp:209
helics::ForwardingTimeCoordinator::iterating
bool iterating
flag indicating that the min dependency is iterating
Definition: ForwardingTimeCoordinator.hpp:45
helics::TimeData::update
bool update(const TimeData &update)
Definition: TimeDependencies.cpp:96
helics::DependencyInfo::fedID
GlobalFederateId fedID
identifier for the dependency
Definition: TimeDependencies.hpp:55
helics::TimeData::minDe
Time minDe
min dependency event time
Definition: TimeDependencies.hpp:40
helics::DependencyInfo::dependency
bool dependency
indicator that the dependency is an actual dependency
Definition: TimeDependencies.hpp:61
helics::ForwardingTimeCoordinator::dependencyCount
int dependencyCount() const
Definition: ForwardingTimeCoordinator.cpp:236
helics::ForwardingTimeCoordinator::getDependents
std::vector< GlobalFederateId > getDependents() const
Definition: ForwardingTimeCoordinator.cpp:220
helics::MessageProcessingResult::CONTINUE_PROCESSING
@ CONTINUE_PROCESSING
the current loop should continue
helics::TimeDependencies::empty
bool empty() const
Definition: TimeDependencies.hpp:115
helics::TimeData::minFed
GlobalFederateId minFed
identifier for the min dependency
Definition: TimeDependencies.hpp:42
flagOperations.hpp
helics::ForwardingTimeCoordinator::isDependency
bool isDependency(GlobalFederateId ofed) const
Definition: ForwardingTimeCoordinator.cpp:163
helics::MessageProcessingResult
MessageProcessingResult
Definition: CoreTypes.hpp:64
helics::ForwardingTimeCoordinator::printTimeStatus
std::string printTimeStatus() const
Definition: ForwardingTimeCoordinator.cpp:155
helics::MessageProcessingResult::NEXT_STEP
@ NEXT_STEP
indicator that the iterations have completed
helics::ForwardingTimeCoordinator::getMinDependency
GlobalFederateId getMinDependency() const
Definition: ForwardingTimeCoordinator.cpp:241
helics::ActionMessage::source_id
GlobalFederateId source_id
12 – for federate_id or route_id
Definition: ActionMessage.hpp:37