| helics 3.6.1

Player.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 "../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[]);
61 explicit Player(std::string_view name, const FederateInfo& fedInfo);
67 Player(std::string_view name,
68 const std::shared_ptr<Core>& core,
69 const FederateInfo& fedInfo);
75 Player(std::string_view name, CoreApp& core, const FederateInfo& fedInfo);
81 Player(std::string_view appName, const std::string& configString);
82
84 Player(Player&& other_player) = default;
86 Player& operator=(Player&& fed) = default;
87
94 virtual void initialize() override;
95
99 virtual void runTo(Time stopTime_input) override;
100
106 void addPublication(std::string_view name,
107 DataType type,
108 std::string_view pubUnits = std::string_view());
109
114 template<class valType>
115 typename std::enable_if_t<helicsType<valType>() != DataType::HELICS_CUSTOM>
116 addPublication(std::string_view key, std::string_view pubUnits = std::string_view())
117 {
118 if (!useLocal) {
119 publications.emplace_back(
120 InterfaceVisibility::GLOBAL, fed.get(), key, helicsType<valType>(), pubUnits);
121 } else {
122 publications.emplace_back(fed.get(), key, helicsType<valType>(), pubUnits);
123 }
124
125 pubids[key] = static_cast<int>(publications.size()) - 1;
126 }
127
132 void addEndpoint(std::string_view endpointName,
133 std::string_view endpointType = std::string_view());
139 template<class valType>
140 void addPoint(Time pubTime, std::string_view key, const valType& val)
141 {
142 points.resize(points.size() + 1);
143 points.back().time = pubTime;
144 points.back().pubName = key;
145 points.back().value = val;
146 }
147
154 template<class valType>
155 void addPoint(Time pubTime, int iteration, std::string_view key, const valType& val)
156 {
157 points.resize(points.size() + 1);
158 points.back().time = pubTime;
159 points.back().iteration = iteration;
160 points.back().pubName = key;
161 points.back().value = val;
162 }
169 void addMessage(Time sendTime,
170 std::string_view src,
171 std::string_view dest,
172 std::string_view payload);
173
181 void addMessage(Time sendTime,
182 Time actionTime,
183 std::string_view src,
184 std::string_view dest,
185 std::string_view payload);
186
188 auto pointCount() const { return points.size(); }
190 auto messageCount() const { return messages.size(); }
192 auto publicationCount() const { return publications.size(); }
194 auto endpointCount() const { return endpoints.size(); }
196 const auto& getPoint(int index) const { return points[index]; }
198 const auto& getMessage(int index) const { return messages[index]; }
199
200 private:
201 std::unique_ptr<helicsCLI11App> generateParser();
203 void processArgs();
204
206 void initialSetup();
210 virtual void loadJsonFile(const std::string& jsonString,
211 bool enableFederateInterfaceRegistration) override;
213 virtual void loadTextFile(const std::string& filename) override;
215 void sortTags();
217 void generatePublications();
219 void generateEndpoints();
221 void cleanUpPointList();
222
224 void sendInformation(Time sendTime, int iteration = 0);
225
230 helics::Time extractTime(std::string_view str, int lineNumber = 0) const;
231
232 private:
233 std::vector<ValueSetter> points;
234 std::vector<MessageHolder> messages;
235 std::unordered_map<std::string, std::string> tags;
236 std::set<std::string> epts;
237 std::deque<Publication> publications;
238 std::deque<Endpoint> endpoints;
239 std::unordered_map<std::string_view, int> pubids;
240 std::unordered_map<std::string_view, int> eptids;
241 helics::DataType defType =
242 helics::DataType::HELICS_STRING;
243 size_t pointIndex = 0;
244 size_t messageIndex = 0;
245 time_units units = time_units::sec;
246 double timeMultiplier =
247 1.0;
248 Time nextPrintTimeStep =
249 helics::timeZero;
250 };
251 } // namespace apps
252 } // namespace helics
helics::FederateInfo
Definition: FederateInfo.hpp:28
helics::Message
Definition: core-data.hpp:29
helics::apps::App
Definition: helicsApp.hpp:32
helics::apps::Player
Definition: Player.hpp:43
helics::apps::Player::addPublication
std::enable_if_t< helicsType< valType >) !=DataType::HELICS_CUSTOM > addPublication(std::string_view key, std::string_view pubUnits=std::string_view())
Definition: Player.hpp:116
helics::apps::Player::endpointCount
auto endpointCount() const
Definition: Player.hpp:194
helics::apps::Player::publicationCount
auto publicationCount() const
Definition: Player.hpp:192
helics::apps::Player::Player
Player()=default
helics::apps::Player::pointCount
auto pointCount() const
Definition: Player.hpp:188
helics::apps::Player::getPoint
const auto & getPoint(int index) const
Definition: Player.hpp:196
helics::apps::Player::messageCount
auto messageCount() const
Definition: Player.hpp:190
helics::apps::Player::getMessage
const auto & getMessage(int index) const
Definition: Player.hpp:198
helics::apps::Player::operator=
Player & operator=(Player &&fed)=default
helics::apps::Player::addPoint
void addPoint(Time pubTime, std::string_view key, const valType &val)
Definition: Player.hpp:140
helics::apps::Player::Player
Player(Player &&other_player)=default
helics::apps::Player::addPoint
void addPoint(Time pubTime, int iteration, std::string_view key, const valType &val)
Definition: Player.hpp:155
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::DataType
DataType
Definition: helicsTypes.hpp:273
helics::timeZero
constexpr Time timeZero
Definition: helicsTime.hpp:31
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27
helics::defV
std::variant< double, int64_t, std::string, std::complex< double >, std::vector< double >, std::vector< std::complex< double > >, NamedPoint > defV
Definition: HelicsPrimaryTypes.hpp:37
helics::apps::MessageHolder
Definition: Player.hpp:32
helics::apps::ValueSetter
Definition: Player.hpp:23