helics  3.5.2
Clone.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 
8 #pragma once
9 #include "../application_api/Endpoints.hpp"
10 #include "../application_api/Publications.hpp"
11 #include "../application_api/Subscriptions.hpp"
12 #include "helicsApp.hpp"
13 
14 #include <deque>
15 #include <map>
16 #include <memory>
17 #include <set>
18 #include <string>
19 #include <tuple>
20 #include <utility>
21 #include <vector>
22 
23 namespace helics {
24 class CloningFilter;
25 
26 namespace apps {
28  class HELICS_CXX_EXPORT Clone: public App {
29  public:
35  Clone(std::string_view appName, FederateInfo& fedInfo);
39  explicit Clone(std::vector<std::string> args);
41  Clone(int argc, char* argv[]);
42 
49  Clone(std::string_view appName,
50  const std::shared_ptr<Core>& core,
51  const FederateInfo& fedInfo);
52 
58  Clone(std::string_view appName, CoreApp& core, const FederateInfo& fedInfo);
59 
64  Clone(std::string_view appName, const std::string& jsonString);
66  Clone(Clone&& other_recorder) = default;
68  Clone& operator=(Clone&& record) = default;
70  ~Clone();
72  virtual void runTo(Time runToTime) override;
74  void saveFile(const std::string& filename = std::string{});
76  auto pointCount() const { return points.size(); }
78  auto messageCount() const { return messages.size(); }
83  std::tuple<Time, std::string, std::string> getValue(int index) const;
88  std::unique_ptr<Message> getMessage(int index) const;
89 
93  void setFederateToClone(std::string_view federateName);
96  void setOutputFile(std::string_view fileName) { outFileName = fileName; }
97 
98  private:
100  void addSubscription(std::string_view key);
101 
103  void addSourceEndpointClone(std::string_view sourceEndpoint);
104 
105  virtual void initialize() override;
106  void generateInterfaces();
107  void captureForCurrentTime(Time currentTime, int iteration = 0);
109  std::shared_ptr<helicsCLI11App> buildArgParserApp();
110 
112  void initialSetup();
114  void processArgs();
115 
116  protected:
118  class ValueCapture {
119  public:
120  helics::Time time;
121  int index{-1};
122  int16_t iteration{0};
123  bool first{false};
124  std::string value;
125  ValueCapture() = default;
126  ValueCapture(helics::Time t1, int id1, std::string_view val):
127  time(t1), index(id1), value(val)
128  {
129  }
130  };
131 
132  bool allow_iteration{false};
133  bool verbose{false};
134  bool fileSaved{false};
135  Time nextPrintTimeStep{
137  std::unique_ptr<CloningFilter> cFilt;
138  std::vector<ValueCapture> points;
139  std::deque<Input> subscriptions;
140  std::vector<std::string>
142  std::unique_ptr<Endpoint> cloneEndpoint;
143  std::vector<std::unique_ptr<Message>> messages;
144  std::map<helics::InterfaceHandle, int> subids;
145  std::map<std::string_view, int> subkeys;
146  std::map<helics::InterfaceHandle, int> eptids; // translate subscription id to index
147  std::map<std::string_view, int> eptNames;
148  std::string captureFederate;
149  std::string fedConfig;
150  std::string outFileName{"clone.json"};
151  std::vector<int> pubPointCount;
152  };
153 
154 } // namespace apps
155 } // namespace helics
Definition: FederateInfo.hpp:29
Definition: helicsApp.hpp:32
Definition: Clone.hpp:118
Definition: Clone.hpp:28
std::map< std::string_view, int > eptNames
translate endpoint name to index
Definition: Clone.hpp:147
Clone(Clone &&other_recorder)=default
std::unique_ptr< Endpoint > cloneEndpoint
the endpoint for cloned message delivery
Definition: Clone.hpp:142
std::vector< int > pubPointCount
a vector containing the counts of each publication
Definition: Clone.hpp:151
std::vector< ValueCapture > points
lists of points that were captured
Definition: Clone.hpp:138
std::deque< Input > subscriptions
the actual subscription objects
Definition: Clone.hpp:139
auto messageCount() const
Definition: Clone.hpp:78
std::string captureFederate
storage for the name of the federate to clone
Definition: Clone.hpp:148
std::unique_ptr< CloningFilter > cFilt
a pointer to a clone filter
Definition: Clone.hpp:137
Clone & operator=(Clone &&record)=default
void setOutputFile(std::string_view fileName)
Definition: Clone.hpp:96
std::string fedConfig
storage for the federateConfiguration
Definition: Clone.hpp:149
std::vector< std::string > cloneSubscriptionNames
string of the subscriptions of the cloned federate
Definition: Clone.hpp:141
std::vector< std::unique_ptr< Message > > messages
list of messages
Definition: Clone.hpp:143
auto pointCount() const
Definition: Clone.hpp:76
std::map< helics::InterfaceHandle, int > subids
map of the subscription ids
Definition: Clone.hpp:144
std::map< std::string_view, int > subkeys
translate subscription names to an index
Definition: Clone.hpp:145
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
constexpr Time timeZero
Definition: helicsTime.hpp:31
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27