helics  3.5.2
Source.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/HelicsPrimaryTypes.hpp"
11 #include "../application_api/Publications.hpp"
12 #include "helicsApp.hpp"
13 
14 #include <deque>
15 #include <map>
16 #include <memory>
17 #include <set>
18 #include <string>
19 #include <vector>
20 
21 namespace helics {
22 namespace apps {
24  struct SourceObject {
25  Publication pub;
26  Time period;
27  Time nextTime{timeZero};
28  int generatorIndex{-1};
29  std::string generatorName;
30  SourceObject() = default;
31  SourceObject(const Publication& p, Time per): pub(p), period(per) {}
32  // source type
33  };
34 
37  class HELICS_CXX_EXPORT SignalGenerator {
38  protected:
39  Time lastTime{timeZero};
40  Time keyTime{timeZero};
41  std::string mName;
42 
43  public:
44  SignalGenerator() = default;
45  explicit SignalGenerator(std::string_view name): mName{name} {};
46  virtual ~SignalGenerator() = default;
48  virtual void set(std::string_view parameter, double val);
50  virtual void setString(std::string_view parameter, std::string_view val);
53  virtual defV generate(Time signalTime) = 0;
55  void setTime(Time indexTime) { keyTime = indexTime; }
56  const std::string& getName() { return mName; }
57  };
58 
64  class HELICS_CXX_EXPORT Source: public App {
65  public:
67  Source() = default;
71  explicit Source(std::vector<std::string> args);
76  Source(int argc, char* argv[]);
81  Source(std::string_view name, const FederateInfo& fi);
87  Source(std::string_view name, const std::shared_ptr<Core>& core, const FederateInfo& fi);
88 
94  Source(std::string_view name, CoreApp& core, const FederateInfo& fi);
99  Source(std::string_view name, const std::string& configString);
100 
102  Source(Source&& other_source) = default;
103 
105  Source& operator=(Source&& fed) = default;
106 
110  virtual void initialize() override;
111 
115  virtual void runTo(Time stopTime_input) override;
116 
124  void addPublication(std::string_view key,
125  std::string_view generator,
126  DataType type,
127  Time period,
128  std::string_view units = std::string_view());
129 
136  void addPublication(std::string_view key,
137  DataType type,
138  Time period,
139  std::string_view units = std::string_view())
140  {
141  addPublication(key, std::string_view(), type, period, units);
142  }
146  int addSignalGenerator(std::string_view name, std::string_view type);
148  void setStartTime(std::string_view key, Time startTime);
150  void setPeriod(std::string_view key, Time period);
152  void linkPublicationToGenerator(std::string_view key, std::string_view generator);
154  void linkPublicationToGenerator(std::string_view key, int genIndex);
156  std::shared_ptr<SignalGenerator> getGenerator(int index);
157 
158  private:
160  void initialSetup();
162  void processArgs();
166  virtual void loadJsonFile(const std::string& jsonString,
167  bool enableFederateInterfaceRegistration) override;
169  Time runSource(SourceObject& obj, Time currentTime);
171  Time runSourceLoop(Time currentTime);
172 
173  private:
174  std::deque<SourceObject> sources;
175  std::vector<std::shared_ptr<SignalGenerator>> generators;
176  std::map<std::string_view, int> generatorLookup;
177  std::vector<Endpoint> endpoints;
178  std::map<std::string_view, int> pubids;
179  Time defaultPeriod = 1.0;
180  };
181 } // namespace apps
182 } // namespace helics
Definition: FederateInfo.hpp:29
Definition: Publications.hpp:25
Definition: helicsApp.hpp:32
Definition: Source.hpp:37
void setTime(Time indexTime)
Definition: Source.hpp:55
virtual defV generate(Time signalTime)=0
Definition: Source.hpp:64
void addPublication(std::string_view key, DataType type, Time period, std::string_view units=std::string_view())
Definition: Source.hpp:136
Source & operator=(Source &&fed)=default
Source(Source &&other_source)=default
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: Source.hpp:24