helics  3.5.2
Player.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 "../application_api/Endpoints.hpp"
10 #include "../application_api/HelicsPrimaryTypes.hpp"
11 #include "../application_api/Publications.hpp"
12 #include "helicsApp.hpp"
13 
14 #include <deque>
15 #include <memory>
16 #include <set>
17 #include <string>
18 #include <unordered_map>
19 #include <vector>
20 
21 namespace helics {
22 namespace apps {
23  struct ValueSetter {
24  Time time{Time::minVal()};
25  int iteration = 0;
26  int index{-1};
27  std::string type;
28  std::string pubName;
29  defV value;
30  };
31 
32  struct MessageHolder {
33  Time sendTime{Time::minVal()};
34  int index{-1};
35  Message mess;
36  };
37 
43  class HELICS_CXX_EXPORT Player: public App {
44  public:
46  Player() = default;
50  explicit Player(std::vector<std::string> args);
55  Player(int argc, char* argv[]);
60  explicit Player(std::string_view name, const FederateInfo& fi);
66  Player(std::string_view name, const std::shared_ptr<Core>& core, const FederateInfo& fi);
72  Player(std::string_view name, CoreApp& core, const FederateInfo& fi);
78  Player(std::string_view appName, const std::string& configString);
79 
81  Player(Player&& other_player) = default;
83  Player& operator=(Player&& fed) = default;
84 
91  virtual void initialize() override;
92 
96  virtual void runTo(Time stopTime_input) override;
97 
103  void addPublication(std::string_view name,
104  DataType type,
105  std::string_view pubUnits = std::string_view());
106 
111  template<class valType>
112  typename std::enable_if_t<helicsType<valType>() != DataType::HELICS_CUSTOM>
113  addPublication(std::string_view key, std::string_view pubUnits = std::string_view())
114  {
115  if (!useLocal) {
116  publications.emplace_back(
117  InterfaceVisibility::GLOBAL, fed.get(), key, helicsType<valType>(), pubUnits);
118  } else {
119  publications.emplace_back(fed.get(), key, helicsType<valType>(), pubUnits);
120  }
121 
122  pubids[key] = static_cast<int>(publications.size()) - 1;
123  }
124 
129  void addEndpoint(std::string_view endpointName,
130  std::string_view endpointType = std::string_view());
136  template<class valType>
137  void addPoint(Time pubTime, std::string_view key, const valType& val)
138  {
139  points.resize(points.size() + 1);
140  points.back().time = pubTime;
141  points.back().pubName = key;
142  points.back().value = val;
143  }
144 
151  template<class valType>
152  void addPoint(Time pubTime, int iteration, std::string_view key, const valType& val)
153  {
154  points.resize(points.size() + 1);
155  points.back().time = pubTime;
156  points.back().iteration = iteration;
157  points.back().pubName = key;
158  points.back().value = val;
159  }
166  void addMessage(Time sendTime,
167  std::string_view src,
168  std::string_view dest,
169  std::string_view payload);
170 
178  void addMessage(Time sendTime,
179  Time actionTime,
180  std::string_view src,
181  std::string_view dest,
182  std::string_view payload);
183 
185  auto pointCount() const { return points.size(); }
187  auto messageCount() const { return messages.size(); }
189  auto publicationCount() const { return publications.size(); }
191  auto endpointCount() const { return endpoints.size(); }
193  const auto& getPoint(int index) const { return points[index]; }
195  const auto& getMessage(int index) const { return messages[index]; }
196 
197  private:
198  std::unique_ptr<helicsCLI11App> generateParser();
200  void processArgs();
201 
203  void initialSetup();
207  virtual void loadJsonFile(const std::string& jsonString,
208  bool enableFederateInterfaceRegistration) override;
210  virtual void loadTextFile(const std::string& filename) override;
212  void sortTags();
214  void generatePublications();
216  void generateEndpoints();
218  void cleanUpPointList();
219 
221  void sendInformation(Time sendTime, int iteration = 0);
222 
227  helics::Time extractTime(std::string_view str, int lineNumber = 0) const;
228 
229  private:
230  std::vector<ValueSetter> points;
231  std::vector<MessageHolder> messages;
232  std::unordered_map<std::string, std::string> tags;
233  std::set<std::string> epts;
234  std::deque<Publication> publications;
235  std::deque<Endpoint> endpoints;
236  std::unordered_map<std::string_view, int> pubids;
237  std::unordered_map<std::string_view, int> eptids;
238  helics::DataType defType =
239  helics::DataType::HELICS_STRING;
240  size_t pointIndex = 0;
241  size_t messageIndex = 0;
242  time_units units = time_units::sec;
243  double timeMultiplier =
244  1.0;
245  Time nextPrintTimeStep =
247  };
248 } // namespace apps
249 } // namespace helics
Definition: FederateInfo.hpp:29
Definition: core-data.hpp:29
Definition: helicsApp.hpp:32
Definition: Player.hpp:43
std::enable_if_t< helicsType< valType >) !=DataType::HELICS_CUSTOM > addPublication(std::string_view key, std::string_view pubUnits=std::string_view())
Definition: Player.hpp:113
auto endpointCount() const
Definition: Player.hpp:191
auto publicationCount() const
Definition: Player.hpp:189
auto pointCount() const
Definition: Player.hpp:185
const auto & getPoint(int index) const
Definition: Player.hpp:193
auto messageCount() const
Definition: Player.hpp:187
const auto & getMessage(int index) const
Definition: Player.hpp:195
Player & operator=(Player &&fed)=default
void addPoint(Time pubTime, std::string_view key, const valType &val)
Definition: Player.hpp:137
Player(Player &&other_player)=default
void addPoint(Time pubTime, int iteration, std::string_view key, const valType &val)
Definition: Player.hpp:152
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
DataType
Definition: helicsTypes.hpp:273
constexpr Time timeZero
Definition: helicsTime.hpp:31
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27
std::variant< double, int64_t, std::string, std::complex< double >, std::vector< double >, std::vector< std::complex< double > >, NamedPoint > defV
Definition: HelicsPrimaryTypes.hpp:37
Definition: Player.hpp:32
Definition: Player.hpp:23