helics  3.6.1
Source.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/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[]);
82  Source(std::string_view name, const FederateInfo& fedInfo);
88  Source(std::string_view name,
89  const std::shared_ptr<Core>& core,
90  const FederateInfo& fedInfo);
91 
97  Source(std::string_view name, CoreApp& core, const FederateInfo& fedInfo);
102  Source(std::string_view name, const std::string& configString);
103 
105  Source(Source&& other_source) = default;
106 
108  Source& operator=(Source&& fed) = default;
109 
113  virtual void initialize() override;
114 
118  virtual void runTo(Time stopTime_input) override;
119 
127  void addPublication(std::string_view key,
128  std::string_view generator,
129  DataType type,
130  Time period,
131  std::string_view units = std::string_view());
132 
139  void addPublication(std::string_view key,
140  DataType type,
141  Time period,
142  std::string_view units = std::string_view())
143  {
144  addPublication(key, std::string_view(), type, period, units);
145  }
149  int addSignalGenerator(std::string_view name, std::string_view type);
151  void setStartTime(std::string_view key, Time startTime);
153  void setPeriod(std::string_view key, Time period);
155  void linkPublicationToGenerator(std::string_view key, std::string_view generator);
157  void linkPublicationToGenerator(std::string_view key, int genIndex);
159  std::shared_ptr<SignalGenerator> getGenerator(int index);
160 
161  private:
163  void initialSetup();
165  void processArgs();
169  virtual void loadJsonFile(const std::string& jsonString,
170  bool enableFederateInterfaceRegistration) override;
172  Time runSource(SourceObject& obj, Time currentTime);
174  Time runSourceLoop(Time currentTime);
175 
176  private:
177  std::deque<SourceObject> sources;
178  std::vector<std::shared_ptr<SignalGenerator>> generators;
179  std::map<std::string_view, int> generatorLookup;
180  std::vector<Endpoint> endpoints;
181  std::map<std::string_view, int> pubids;
182  Time defaultPeriod = 1.0;
183  };
184 } // namespace apps
185 } // namespace helics
Definition: FederateInfo.hpp:28
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:139
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