helics  3.3.0
Source.hpp
1 /*
2 Copyright (c) 2017-2022,
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 processArgs();
164  virtual void loadJsonFile(const std::string& jsonString) override;
166  Time runSource(SourceObject& obj, Time currentTime);
168  Time runSourceLoop(Time currentTime);
169 
170  private:
171  std::deque<SourceObject> sources;
172  std::vector<std::shared_ptr<SignalGenerator>> generators;
173  std::map<std::string_view, int> generatorLookup;
174  std::vector<Endpoint> endpoints;
175  std::map<std::string_view, int> pubids;
176  Time defaultPeriod = 1.0;
177  };
178 } // namespace apps
179 } // namespace helics
helics::timeZero
constexpr Time timeZero
Definition: helicsTime.hpp:31
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:34
SignalGenerators.hpp
helics::apps::Source::addPublication
void addPublication(std::string_view key, DataType type, Time period, std::string_view units=std::string_view())
Definition: Source.hpp:136
helics::apps::Source::getGenerator
std::shared_ptr< SignalGenerator > getGenerator(int index)
Definition: Source.cpp:306
helics::apps::SignalGenerator::set
virtual void set(std::string_view parameter, double val)
Definition: Source.cpp:30
helics::apps::SourceObject
Definition: Source.hpp:24
helics::apps::Source::setStartTime
void setStartTime(std::string_view key, Time startTime)
Definition: Source.cpp:315
helics::apps::Source::addSignalGenerator
int addSignalGenerator(std::string_view name, std::string_view type)
Definition: Source.cpp:290
helics::apps::Source::initialize
virtual void initialize() override
Definition: Source.cpp:185
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27
HELICS_FLAG_SOURCE_ONLY
@ HELICS_FLAG_SOURCE_ONLY
Definition: helics_enums.h:107
helics::helicsCLI11App
Definition: helicsCLI11.hpp:42
helics::apps::App::masterFileName
std::string masterFileName
the name of the master file used to do the construction
Definition: helicsApp.hpp:132
helics::DataType
DataType
Definition: helicsTypes.hpp:273
helics::apps::App::loadJsonFileConfiguration
void loadJsonFileConfiguration(const std::string &appName, const std::string &jsonString)
Definition: helicsApp.cpp:158
helics::InvalidParameter
Definition: core-exceptions.hpp:48
helics::apps::Source::linkPublicationToGenerator
void linkPublicationToGenerator(std::string_view key, std::string_view generator)
Definition: Source.cpp:332
helics::apps::Source::Source
Source()=default
helics::FederateInfo
Definition: FederateInfo.hpp:24
helics::apps::Source::runTo
virtual void runTo(Time stopTime_input) override
Definition: Source.cpp:224
helics::apps::Source
Definition: Source.hpp:64
helics::apps::SignalGenerator::setString
virtual void setString(std::string_view parameter, std::string_view val)
Definition: Source.cpp:32
helics::apps::SignalGenerator
Definition: Source.hpp:37
helics::Publication
Definition: Publications.hpp:25
helics::Publication::publish
void publish(double val)
Definition: Publications.cpp:71
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::apps::SignalGenerator::setTime
void setTime(Time indexTime)
Definition: Source.hpp:55
helics::apps::App::fed
std::shared_ptr< CombinationFederate > fed
the federate created for the Player
Definition: helicsApp.hpp:130
helics::apps::Source::addPublication
void addPublication(std::string_view key, std::string_view generator, DataType type, Time period, std::string_view units=std::string_view())
Definition: Source.cpp:261
helics::apps::Source::setPeriod
void setPeriod(std::string_view key, Time period)
Definition: Source.cpp:323
helics::apps::App
Definition: helicsApp.hpp:29
helics::typeNameStringRef
const std::string & typeNameStringRef(DataType type)
Definition: helicsTypes.cpp:58
helics::timeEpsilon
constexpr Time timeEpsilon
Definition: helicsTime.hpp:33
helics::Federate::Modes::STARTUP
@ STARTUP
helics::apps::App::loadFile
void loadFile(const std::string &filename)
Definition: helicsApp.cpp:123
helics::Federate::Modes::EXECUTING
@ EXECUTING