helics  3.5.2
TimeDependencies.hpp
1 /*
2 Copyright (c) 2017-2024,
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 
11 #include "json/forwards.h"
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,
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:
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:
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 
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;
148  bool addDependent(GlobalFederateId gid);
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(); }
176 
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 
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;
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(Json::Value& output,
249  const TimeData& dep,
250  bool includeAggregates = true);
251 
252 void addTimeState(Json::Value& output, const TimeState state);
253 
254 void generateJsonOutputDependency(Json::Value& output, const DependencyInfo& dep);
255 } // namespace helics
Definition: ActionMessage.hpp:30
Definition: TimeDependencies.hpp:98
bool dependency
indicator that the dependency is an actual dependency
Definition: TimeDependencies.hpp:106
bool triggered
indicator that the dependency has been triggered in some way
Definition: TimeDependencies.hpp:109
bool nonGranting
indicator that the dependency is a non granting time coordinator
Definition: TimeDependencies.hpp:108
bool updateRequested
indicator that an update request is in process
Definition: TimeDependencies.hpp:110
GlobalFederateId fedID
identifier for the dependency
Definition: TimeDependencies.hpp:100
bool dependent
indicator the dependency is a dependent object
Definition: TimeDependencies.hpp:105
bool cyclic
Definition: TimeDependencies.hpp:102
DependencyInfo(GlobalFederateId id)
Definition: TimeDependencies.hpp:119
bool forwarding
indicator that the dependency is a forwarding time coordinator
Definition: TimeDependencies.hpp:107
Definition: GlobalFederateId.hpp:75
constexpr bool isValid() const
Definition: GlobalFederateId.hpp:112
Definition: TimeDependencies.hpp:62
std::int32_t responseSequenceCounter
Definition: TimeDependencies.hpp:80
Time Te
the next currently scheduled event
Definition: TimeDependencies.hpp:65
Time TeAlt
the second min event
Definition: TimeDependencies.hpp:67
std::int32_t grantedIteration
the iteration of the dependency when the local iteration was granted
Definition: TimeDependencies.hpp:82
std::uint8_t restrictionLevel
timing restriction level
Definition: TimeDependencies.hpp:76
bool update(const TimeData &update)
Definition: TimeDependencies.cpp:172
Time next
next possible message or value
Definition: TimeDependencies.hpp:64
bool interrupted
indicator that the federates next event is a timing interruption
Definition: TimeDependencies.hpp:73
std::int8_t timingVersion
version indicator
Definition: TimeDependencies.hpp:75
Time minDe
min dependency event time
Definition: TimeDependencies.hpp:66
std::int32_t timeoutCount
counter for timeout checking
Definition: TimeDependencies.hpp:78
Time lastGrant
storage for the previous grant time
Definition: TimeDependencies.hpp:68
bool hasData
indicator that data was sent in the current interval
Definition: TimeDependencies.hpp:72
GlobalFederateId minFedActual
the actual forwarded minimum federate object
Definition: TimeDependencies.hpp:70
bool delayedTiming
indicator that the dependency is using delayed timing
Definition: TimeDependencies.hpp:74
std::int32_t sequenceCounter
the sequence Counter of the request
Definition: TimeDependencies.hpp:79
GlobalFederateId minFed
identifier for the min dependency
Definition: TimeDependencies.hpp:69
Definition: TimeDependencies.hpp:129
bool checkIfReadyForExecEntry(bool iterating, bool waiting) const
Definition: TimeDependencies.cpp:502
void resetDependency(GlobalFederateId id)
Definition: TimeDependencies.cpp:432
bool addDependent(GlobalFederateId gid)
Definition: TimeDependencies.cpp:394
void resetDependentEvents(Time grantTime)
Definition: TimeDependencies.cpp:693
void resetIteratingExecRequests()
Definition: TimeDependencies.cpp:666
bool addDependency(GlobalFederateId gid)
Definition: TimeDependencies.cpp:350
bool verifySequenceCounter(Time tmin, std::int32_t sequenceCount)
Definition: TimeDependencies.cpp:636
bool empty() const
Definition: TimeDependencies.hpp:173
auto begin() const
Definition: TimeDependencies.hpp:164
const DependencyInfo * getDependencyInfo(GlobalFederateId gid) const
Definition: TimeDependencies.cpp:330
TimeProcessingResult updateTime(const ActionMessage &cmd)
Definition: TimeDependencies.cpp:454
void removeInterdependence(GlobalFederateId gid)
Definition: TimeDependencies.cpp:444
std::pair< int, std::string > checkForIssues(bool waiting) const
Definition: TimeDependencies.cpp:703
auto cbegin() const
Definition: TimeDependencies.hpp:168
bool checkIfAllDependenciesArePastExec(bool iterating) const
Definition: TimeDependencies.cpp:463
auto begin()
Definition: TimeDependencies.hpp:160
int activeDependencyCount() const
Definition: TimeDependencies.cpp:644
bool hasActiveTimeDependencies() const
Definition: TimeDependencies.cpp:629
auto size() const
Definition: TimeDependencies.hpp:158
auto end() const
Definition: TimeDependencies.hpp:166
bool isDependency(GlobalFederateId ofed) const
Definition: TimeDependencies.cpp:312
bool checkIfReadyForTimeGrant(bool iterating, Time desiredGrantTime, GrantDelayMode delayMode) const
Definition: TimeDependencies.cpp:552
void resetIteratingTimeRequests(Time requestTime)
Definition: TimeDependencies.cpp:680
auto cend() const
Definition: TimeDependencies.hpp:170
auto end()
Definition: TimeDependencies.hpp:162
void removeDependent(GlobalFederateId gid)
Definition: TimeDependencies.cpp:419
void removeDependency(GlobalFederateId gid)
Definition: TimeDependencies.cpp:381
bool isDependent(GlobalFederateId ofed) const
Definition: TimeDependencies.cpp:321
GlobalFederateId getMinDependency() const
Definition: TimeDependencies.cpp:651
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
constexpr Time cBigTime
Definition: helicsTime.hpp:37
constexpr Time timeZero
Definition: helicsTime.hpp:31
ConnectionType
Definition: TimeDependencies.hpp:33
GrantDelayMode
Definition: TimeDependencies.hpp:53
TimeProcessingResult
Definition: TimeDependencies.hpp:42
@ DELAY_PROCESSING
the message should be delayed and reprocessed later
@ PROCESSED_NEW_REQUEST
the message was used to update the current state and a new request was made
@ PROCESSED_AND_CHECK
the message was used to update the current state and additional checks should be made
@ NOT_PROCESSED
the message did not result in an update
TimeState
Definition: TimeDependencies.hpp:20
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27
constexpr Time negEpsilon
Definition: helicsTime.hpp:35