CYCLUS
timeseries.h
Go to the documentation of this file.
1 #ifndef CYCLUS_SRC_TOOLKIT_TIMESERIES_H_
2 #define CYCLUS_SRC_TOOLKIT_TIMESERIES_H_
3 
4 #include <functional>
5 #include <map>
6 #include <string>
7 #include <vector>
8 
9 #include "boost/variant.hpp"
10 #include "boost/variant/get.hpp"
11 
12 #include "agent.h"
13 #include "any.hpp"
14 #include "context.h"
15 
16 namespace cyclus {
17 namespace toolkit {
18 
19 /// Time series types to be used in the RecordTimeSeries() functions.
20 /// These types have the following units which *must* be adhered to strictly:
21 /// - POWER [MWe]
22 /// - ENRICH_SWU [kg SWU]
23 /// - ENRICH_FEED [kg]
24 enum TimeSeriesType : int{
28 };
29 
30 /// Stores global information for the time series call backs.
31 typedef boost::variant<
32  std::function<void(cyclus::Agent*, int, bool, std::string)>,
33  std::function<void(cyclus::Agent*, int, int, std::string)>,
34  std::function<void(cyclus::Agent*, int, float, std::string)>,
35  std::function<void(cyclus::Agent*, int, double, std::string)>,
36  std::function<void(cyclus::Agent*, int, std::string, std::string)>
38 
39 extern std::map<std::string, std::vector<time_series_listener_t>> TIME_SERIES_LISTENERS;
40 
41 /// Records a per-time step quantity for a given type
42 template <TimeSeriesType T>
43 void RecordTimeSeries(cyclus::Agent* agent, double value,
44  std::string units = "");
45 
46 /// Records a per-time step quantity for a string
47 template <typename T>
48 void RecordTimeSeries(std::string tsname, cyclus::Agent* agent, T value,
49  std::string units = "") {
50  std::string tblname = "TimeSeries" + tsname;
51  int time = agent->context()->time();
52  agent->context()
53  ->NewDatum(tblname)
54  ->AddVal("AgentId", agent->id())
55  ->AddVal("Time", time)
56  ->AddVal("Value", value)
57  ->AddVal("Units", units)
58  ->Record();
59  std::vector<time_series_listener_t> vec = TIME_SERIES_LISTENERS[tsname];
60  for (auto f=vec.begin(); f != vec.end(); ++f){
61  std::function<void(cyclus::Agent*, int, T, std::string)> fn = boost::get<std::function<void(cyclus::Agent*, int, T, std::string)> >(*f);
62  fn(agent, time, value, tsname);
63  }
64  PyCallListeners(tsname, agent, agent->context(), time, value);
65 }
66 
67 } // namespace toolkit
68 } // namespace cyclus
69 
70 #endif // CYCLUS_SRC_TOOLKIT_TIMESERIES_H_
virtual const int id() const
The agent instance&#39;s unique ID within a simulation.
Definition: agent.h:354
std::map< std::string, std::vector< time_series_listener_t > > TIME_SERIES_LISTENERS
Definition: timeseries.cc:6
void RecordTimeSeries(cyclus::Agent *agent, double value, std::string units="")
Records a per-time step quantity for a given type.
virtual int time()
Returns the current simulation timestep.
Definition: context.cc:227
Context * context() const
Returns this agent&#39;s simulation context.
Definition: agent.h:369
Datum * AddVal(const char *field, boost::spirit::hold_any val, std::vector< int > *shape=NULL)
Add an arbitrary field-value pair to the datum.
Definition: datum.cc:22
TimeSeriesType
Time series types to be used in the RecordTimeSeries() functions.
Definition: timeseries.h:24
The abstract base class used by all types of agents that live and interact in a simulation.
Definition: agent.h:51
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
void Record()
Record this datum to its Recorder.
Definition: datum.cc:35
void PyCallListeners(std::string tsname, Agent *agent, void *cpp_ctx, int time, boost::spirit::hold_any value)
Calls the Python listeners.
Definition: pyhooks.cc:144
Datum * NewDatum(std::string title)
See Recorder::NewDatum documentation.
Definition: context.cc:239
boost::variant< std::function< void(cyclus::Agent *, int, bool, std::string)>, std::function< void(cyclus::Agent *, int, int, std::string)>, std::function< void(cyclus::Agent *, int, float, std::string)>, std::function< void(cyclus::Agent *, int, double, std::string)>, std::function< void(cyclus::Agent *, int, std::string, std::string)> > time_series_listener_t
Stores global information for the time series call backs.
Definition: timeseries.h:37