helics  2.8.1
Source.hpp
1 /*
2 Copyright (c) 2017-2021,
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 <map>
15 #include <memory>
16 #include <set>
17 #include <string>
18 #include <vector>
19 
20 namespace helics {
21 namespace apps {
23  struct SourceObject {
24  Publication pub;
25  Time period;
26  Time nextTime{timeZero};
27  int generatorIndex{-1};
28  std::string generatorName;
29  SourceObject() = default;
30  SourceObject(const Publication& p, Time per): pub(p), period(per) {}
31  // source type
32  };
33 
36  class HELICS_CXX_EXPORT SignalGenerator {
37  protected:
38  Time lastTime{timeZero};
39  Time keyTime{timeZero};
40 
41  public:
42  SignalGenerator() = default;
43  virtual ~SignalGenerator() = default;
45  virtual void set(const std::string& parameter, double val);
47  virtual void setString(const std::string& parameter, const std::string& val);
50  virtual defV generate(Time signalTime) = 0;
52  void setTime(Time indexTime) { keyTime = indexTime; }
53  };
54 
60  class HELICS_CXX_EXPORT Source: public App {
61  public:
63  Source() = default;
67  explicit Source(std::vector<std::string> args);
72  Source(int argc, char* argv[]);
77  Source(const std::string& name, const FederateInfo& fi);
83  Source(const std::string& name, const std::shared_ptr<Core>& core, const FederateInfo& fi);
84 
90  Source(const std::string& name, CoreApp& core, const FederateInfo& fi);
95  Source(const std::string& name, const std::string& configString);
96 
98  Source(Source&& other_source) = default;
99 
101  Source& operator=(Source&& fed) = default;
102 
106  virtual void initialize() override;
107 
111  virtual void runTo(Time stopTime_input) override;
112 
120  void addPublication(const std::string& key,
121  const std::string& generator,
122  data_type type,
123  Time period,
124  const std::string& units = std::string());
125 
132  void addPublication(const std::string& key,
133  data_type type,
134  Time period,
135  const std::string& units = std::string())
136  {
137  addPublication(key, std::string(), type, period, units);
138  }
142  int addSignalGenerator(const std::string& name, const std::string& type);
144  void setStartTime(const std::string& key, Time startTime);
146  void setPeriod(const std::string& key, Time period);
148  void linkPublicationToGenerator(const std::string& key, const std::string& generator);
150  void linkPublicationToGenerator(const std::string& key, int genIndex);
152  std::shared_ptr<SignalGenerator> getGenerator(int index);
153 
154  private:
156  void processArgs();
160  virtual void loadJsonFile(const std::string& jsonString) override;
162  Time runSource(SourceObject& obj, Time currentTime);
164  Time runSourceLoop(Time currentTime);
165 
166  private:
167  std::vector<SourceObject> sources;
168  std::vector<std::shared_ptr<SignalGenerator>> generators;
169  std::map<std::string, int> generatorLookup;
170  std::vector<Endpoint> endpoints;
171  std::map<std::string, int> pubids;
172  Time defaultPeriod = 1.0;
173  };
174 } // namespace apps
175 } // namespace helics
helics::timeZero
constexpr Time timeZero
Definition: helics-time.hpp:31
helics::apps::Source::getGenerator
std::shared_ptr< SignalGenerator > getGenerator(int index)
Definition: Source.cpp:301
loadJsonTime
helics::Time loadJsonTime(const Json::Value &timeElement, time_units defaultUnits=time_units::sec)
Definition: JsonProcessingFunctions.cpp:64
helics::apps::SourceObject
Definition: Source.hpp:23
helics::apps::Source::addSignalGenerator
int addSignalGenerator(const std::string &name, const std::string &type)
Definition: Source.cpp:285
helics::apps::Source::addPublication
void addPublication(const std::string &key, data_type type, Time period, const std::string &units=std::string())
Definition: Source.hpp:132
helics::apps::Source::initialize
virtual void initialize() override
Definition: Source.cpp:180
helics::apps::SignalGenerator::set
virtual void set(const std::string &parameter, double val)
Definition: Source.cpp:30
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helics-time.hpp:27
helics::apps::Source::linkPublicationToGenerator
void linkPublicationToGenerator(const std::string &key, const std::string &generator)
Definition: Source.cpp:327
helics::helicsCLI11App
Definition: helicsCLI11.hpp:41
getKey
std::string getKey(const Json::Value &element)
Definition: JsonProcessingFunctions.cpp:90
helics::apps::App::masterFileName
std::string masterFileName
the name of the master file used to do the construction
Definition: helicsApp.hpp:129
helics::apps::App::loadJsonFileConfiguration
void loadJsonFileConfiguration(const std::string &appName, const std::string &jsonString)
Definition: helicsApp.cpp:159
helics::InvalidParameter
Definition: core-exceptions.hpp:48
helics::typeNameStringRef
const std::string & typeNameStringRef(data_type type)
Definition: helicsTypes.cpp:27
helics::apps::Source::Source
Source()=default
helics::apps::SignalGenerator::setString
virtual void setString(const std::string &parameter, const std::string &val)
Definition: Source.cpp:32
helics::FederateInfo
Definition: FederateInfo.hpp:20
helics::apps::Source::runTo
virtual void runTo(Time stopTime_input) override
Definition: Source.cpp:219
helics::apps::Source
Definition: Source.hpp:60
loadJson
Json::Value loadJson(const std::string &jsonString)
Definition: JsonProcessingFunctions.cpp:24
helics::Federate::modes::startup
@ startup
when created the federate is in startup state
helics_flag_source_only
@ helics_flag_source_only
Definition: helics_enums.h:92
helics::apps::Source::addPublication
void addPublication(const std::string &key, const std::string &generator, data_type type, Time period, const std::string &units=std::string())
Definition: Source.cpp:256
fed
@ fed
special logging command for message coming from a fed
Definition: loggingHelper.hpp:32
helics::apps::SignalGenerator
Definition: Source.hpp:36
helics::defV
mpark::variant< double, int64_t, std::string, std::complex< double >, std::vector< double >, std::vector< std::complex< double > >, NamedPoint > defV
Definition: HelicsPrimaryTypes.hpp:34
helics::apps::Source::setPeriod
void setPeriod(const std::string &key, Time period)
Definition: Source.cpp:318
helics::Publication
Definition: Publications.hpp:23
helics::apps::Source::setStartTime
void setStartTime(const std::string &key, Time startTime)
Definition: Source.cpp:310
helics::Publication::publish
void publish(double val)
Definition: Publications.cpp:70
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::data_type
data_type
Definition: helicsTypes.hpp:275
helics::apps::SignalGenerator::setTime
void setTime(Time indexTime)
Definition: Source.hpp:52
helics::apps::App::fed
std::shared_ptr< CombinationFederate > fed
the federate created for the Player
Definition: helicsApp.hpp:127
helics::apps::App
Definition: helicsApp.hpp:28
helics::timeEpsilon
constexpr Time timeEpsilon
Definition: helics-time.hpp:33
helics::apps::App::loadFile
void loadFile(const std::string &filename)
Definition: helicsApp.cpp:124
helics::Federate::modes::executing
@ executing
entered after the enterExectuationState call has returned