| helics 3.6.1

Recorder.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
8 #pragma once
9 #include "../application_api/Endpoints.hpp"
10 #include "../application_api/Subscriptions.hpp"
11 #include "helicsApp.hpp"
12
13 #include <deque>
14 #include <map>
15 #include <memory>
16 #include <set>
17 #include <string>
18 #include <tuple>
19 #include <utility>
20 #include <vector>
21
22 namespace helics {
23 class CloningFilter;
24
25 namespace apps {
27 class HELICS_CXX_EXPORT Recorder: public App {
28 public:
34 Recorder(std::string_view name, FederateInfo& fedInfo);
38 explicit Recorder(std::vector<std::string> args);
40 Recorder(int argc, char* argv[]);
41
48 Recorder(std::string_view name,
49 const std::shared_ptr<Core>& core,
50 const FederateInfo& fedInfo);
56 Recorder(std::string_view name, CoreApp& core, const FederateInfo& fedInfo);
61 Recorder(std::string_view name, const std::string& jsonString);
63 Recorder(Recorder&& other_recorder) = default;
65 Recorder& operator=(Recorder&& other_recorder) = default;
67 ~Recorder();
69 virtual void runTo(Time runToTime) override;
71 void addSubscription(std::string_view key);
73 void addEndpoint(std::string_view endpoint);
75 void addSourceEndpointClone(std::string_view sourceEndpoint);
77 void addDestEndpointClone(std::string_view destEndpoint);
81 void addCapture(std::string_view captureDesc);
83 void saveFile(const std::string& filename);
85 auto pointCount() const { return points.size(); }
87 auto messageCount() const { return messages.size(); }
93 std::tuple<Time, std::string_view, std::string> getValue(std::size_t index) const;
98 std::unique_ptr<Message> getMessage(std::size_t index) const;
99
100 private:
102 void initialSetup();
106 virtual void loadJsonFile(const std::string& jsonString,
107 bool enableFederateInterfaceRegistration) override;
109 virtual void loadTextFile(const std::string& textFile) override;
111 void writeJsonFile(const std::string& filename);
113 void writeTextFile(const std::string& filename);
114
115 virtual void initialize() override;
116 void generateInterfaces();
117 void captureForCurrentTime(Time currentTime, int iteration = 0);
118 void loadCaptureInterfaces();
119
121 std::shared_ptr<helicsCLI11App> buildArgParserApp();
123 void processArgs();
124
125 protected:
127 class ValueCapture {
128 public:
129 helics::Time time;
130 int index{-1};
131 int16_t iteration{0};
132 bool first{false};
133 std::string value;
134 ValueCapture() = default;
135 ValueCapture(helics::Time t1, int id1, std::string_view val):
136 time(t1), index(id1), value(val)
137 {
138 }
139 };
140
142 class ValueStats {
143 public:
144 helics::Time time{helics::Time::minVal()};
145 std::string lastVal;
146 std::string key;
147 int cnt{0};
148 };
149
150 bool allow_iteration{false};
151 bool verbose{false};
152 Time nextPrintTimeStep{
153 helics::timeZero};
154 std::unique_ptr<CloningFilter> cFilt;
155 std::vector<ValueCapture> points;
156 std::deque<Input> subscriptions;
157 std::vector<std::string> targets;
158 std::deque<Endpoint> endpoints;
159 std::unique_ptr<Endpoint> cloneEndpoint;
160 std::vector<std::unique_ptr<Message>> messages;
161 std::map<helics::InterfaceHandle, int> subids;
162 std::map<std::string_view, int> subkeys;
163 std::map<helics::InterfaceHandle, int> eptids; // translate subscription id to index
164 std::map<std::string_view, int> eptNames;
165 std::vector<ValueStats> vStat;
166 std::vector<std::string> captureInterfaces;
167 std::string mapfile;
168 };
169
170 } // namespace apps
171 } // namespace helics
helics::FederateInfo
Definition: FederateInfo.hpp:28
helics::apps::App
Definition: helicsApp.hpp:32
helics::apps::Recorder::ValueCapture
Definition: Recorder.hpp:127
helics::apps::Recorder::ValueStats
Definition: Recorder.hpp:142
helics::apps::Recorder
Definition: Recorder.hpp:27
helics::apps::Recorder::subscriptions
std::deque< Input > subscriptions
the actual subscription objects
Definition: Recorder.hpp:156
helics::apps::Recorder::pointCount
auto pointCount() const
Definition: Recorder.hpp:85
helics::apps::Recorder::cFilt
std::unique_ptr< CloningFilter > cFilt
a pointer to a clone filter
Definition: Recorder.hpp:154
helics::apps::Recorder::endpoints
std::deque< Endpoint > endpoints
the actual endpoint objects
Definition: Recorder.hpp:158
helics::apps::Recorder::eptNames
std::map< std::string_view, int > eptNames
translate endpoint name to index
Definition: Recorder.hpp:164
helics::apps::Recorder::operator=
Recorder & operator=(Recorder &&other_recorder)=default
helics::apps::Recorder::captureInterfaces
std::vector< std::string > captureInterfaces
storage for the interfaces to capture
Definition: Recorder.hpp:166
helics::apps::Recorder::points
std::vector< ValueCapture > points
lists of points that were captured
Definition: Recorder.hpp:155
helics::apps::Recorder::cloneEndpoint
std::unique_ptr< Endpoint > cloneEndpoint
the endpoint for cloned message delivery
Definition: Recorder.hpp:159
helics::apps::Recorder::Recorder
Recorder(Recorder &&other_recorder)=default
helics::apps::Recorder::targets
std::vector< std::string > targets
specified targets for the subscriptions
Definition: Recorder.hpp:157
helics::apps::Recorder::messages
std::vector< std::unique_ptr< Message > > messages
list of messages
Definition: Recorder.hpp:160
helics::apps::Recorder::subkeys
std::map< std::string_view, int > subkeys
translate subscription names to an index
Definition: Recorder.hpp:162
helics::apps::Recorder::vStat
std::vector< ValueStats > vStat
storage for statistics capture
Definition: Recorder.hpp:165
helics::apps::Recorder::subids
std::map< helics::InterfaceHandle, int > subids
map of the subscription ids
Definition: Recorder.hpp:161
helics::apps::Recorder::mapfile
std::string mapfile
file name for the on-line file updater
Definition: Recorder.hpp:167
helics::apps::Recorder::messageCount
auto messageCount() const
Definition: Recorder.hpp:87
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::timeZero
constexpr Time timeZero
Definition: helicsTime.hpp:31
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27