CYCLUS
context.cc
Go to the documentation of this file.
1 #include "context.h"
2 
3 #include <vector>
4 #include <boost/uuid/uuid_generators.hpp>
5 
6 #include "error.h"
7 #include "exchange_solver.h"
8 #include "logger.h"
9 #include "sim_init.h"
10 #include "timer.h"
11 #include "version.h"
12 
13 namespace cyclus {
14 
15 double cy_eps = 1e-6;
16 double cy_eps_rsrc = 1e-6;
17 
19  : duration(0),
20  y0(0),
21  m0(0),
23  decay("manual"),
24  branch_time(-1),
25  explicit_inventory(false),
26  explicit_inventory_compact(false),
27  parent_sim(boost::uuids::nil_uuid()),
28  parent_type("init") {}
29 
30 SimInfo::SimInfo(int dur, int y0, int m0, std::string handle)
31  : duration(dur),
32  y0(y0),
33  m0(m0),
35  decay("manual"),
36  branch_time(-1),
37  handle(handle),
38  explicit_inventory(false),
40  parent_sim(boost::uuids::nil_uuid()),
41  parent_type("init") {}
42 
43 SimInfo::SimInfo(int dur, int y0, int m0, std::string handle, std::string d)
44  : duration(dur),
45  y0(y0),
46  m0(m0),
48  decay(d),
49  branch_time(-1),
50  handle(handle),
51  explicit_inventory(false),
53  parent_sim(boost::uuids::nil_uuid()),
54  parent_type("init") {}
55 
56 SimInfo::SimInfo(int dur, boost::uuids::uuid parent_sim,
57  int branch_time, std::string parent_type,
58  std::string handle)
59  : duration(dur),
60  y0(-1),
61  m0(-1),
63  decay("manual"),
64  parent_sim(parent_sim),
65  parent_type(parent_type),
66  branch_time(branch_time),
67  explicit_inventory(false),
69  handle(handle) {}
70 
72  : ti_(ti),
73  rec_(rec),
74  solver_(NULL),
75  trans_id_(0),
76  si_(0) {}
77 
79  if (solver_ != NULL) {
80  delete solver_;
81  }
82 
83  // initiate deletion of agents that don't have parents.
84  // dealloc will propagate through hierarchy as agents delete their children
85  std::vector<Agent*> to_del;
86  std::set<Agent*>::iterator it;
87  for (it = agent_list_.begin(); it != agent_list_.end(); ++it) {
88  if ((*it)->parent() == NULL) {
89  to_del.push_back(*it);
90  }
91  }
92  for (int i = 0; i < to_del.size(); ++i) {
93  DelAgent(to_del[i]);
94  }
95 }
96 
98  int n = agent_list_.erase(m);
99  if (n == 1) {
100  delete m;
101  m = NULL;
102  }
103 }
104 
105 void Context::SchedBuild(Agent* parent, std::string proto_name, int t) {
106  if (t == -1) {
107  t = time() + 1;
108  }
109  int pid = (parent != NULL) ? parent->id() : -1;
110  ti_->SchedBuild(parent, proto_name, t);
111  NewDatum("BuildSchedule")
112  ->AddVal("ParentId", pid)
113  ->AddVal("Prototype", proto_name)
114  ->AddVal("SchedTime", time())
115  ->AddVal("BuildTime", t)
116  ->Record();
117 }
118 
119 void Context::SchedDecom(Agent* m, int t) {
120  if (t == -1) {
121  t = time();
122  }
123  ti_->SchedDecom(m, t);
124  NewDatum("DecomSchedule")
125  ->AddVal("AgentId", m->id())
126  ->AddVal("SchedTime", time())
127  ->AddVal("DecomTime", t)
128  ->Record();
129 }
130 
131 boost::uuids::uuid Context::sim_id() {
132  return rec_->sim_id();
133 }
134 
135 void Context::AddPrototype(std::string name, Agent* p) {
136  AddPrototype(name, p, false);
137 }
138 
139 void Context::AddPrototype(std::string name, Agent* p, bool overwrite) {
140  if (!overwrite && protos_.find(name) != protos_.end()) {
141  throw KeyError("Prototype name " + name + " has already been added" +
142  " and cannot be overwritten.");
143  }
144 
145  protos_[name] = p;
146  // explicit snapshot required for in situ (non-xml) prototype addition
147  SimInit::SnapAgent(p);
148  NewDatum("Prototypes")
149  ->AddVal("Prototype", name)
150  ->AddVal("AgentId", p->id())
151  ->AddVal("Spec", p->spec())
152  ->Record();
153 
154  std::string spec = p->spec();
155  if (rec_ver_.count(spec) == 0) {
156  rec_ver_.insert(spec);
157  NewDatum("AgentVersions")
158  ->AddVal("Spec", spec)
159  ->AddVal("Version", p->version())
160  ->Record();
161  }
162 }
163 
165  recipes_[name] = c;
166  NewDatum("Recipes")
167  ->AddVal("Recipe", name)
168  ->AddVal("QualId", c->id())
169  ->Record();
170 }
171 
173  if (recipes_.count(name) == 0) {
174  throw KeyError("Invalid recipe name " + name);
175  }
176  return recipes_[name];
177 }
178 
180  NewDatum("Info")
181  ->AddVal("Handle", si.handle)
182  ->AddVal("InitialYear", si.y0)
183  ->AddVal("InitialMonth", si.m0)
184  ->AddVal("Duration", si.duration)
185  ->AddVal("ParentSimId", si.parent_sim)
186  ->AddVal("ParentType", si.parent_type)
187  ->AddVal("BranchTime", si.branch_time)
188  ->AddVal("CyclusVersion", std::string(version::core()))
189  ->AddVal("CyclusVersionDescribe", std::string(version::describe()))
190  ->AddVal("SqliteVersion", std::string(version::sqlite3()))
191  ->AddVal("Hdf5Version", std::string(version::hdf5()))
192  ->AddVal("BoostVersion", std::string(version::boost()))
193  ->AddVal("LibXML2Version", std::string(version::xml2()))
194  ->AddVal("CoinCBCVersion", std::string(version::coincbc()))
195  ->Record();
196 
197  NewDatum("DecayMode")
198  ->AddVal("Decay", si.decay)
199  ->Record();
200 
201  NewDatum("InfoExplicitInv")
202  ->AddVal("RecordInventory", si.explicit_inventory)
203  ->AddVal("RecordInventoryCompact", si.explicit_inventory_compact)
204  ->Record();
205 
206  // TODO: when the backends get uint64_t support, the static_cast here should
207  // be removed.
208  NewDatum("TimeStepDur")
209  ->AddVal("DurationSecs", static_cast<int>(si.dt))
210  ->Record();
211 
212  NewDatum("Epsilon")
213  ->AddVal("GenericEpsilon", si.eps)
214  ->AddVal("ResourceEpsilon", si.eps_rsrc)
215  ->Record();
216 
217  NewDatum("XMLPPInfo")
218  ->AddVal("LibXMLPlusPlusVersion", std::string(version::xmlpp()))
219  ->Record();
220 
221  si_ = si;
222  ti_->Initialize(this, si);
223 }
224 
226  return ti_->time();
227 }
228 
230  ti_->RegisterTimeListener(tl);
231 }
232 
234  ti_->UnregisterTimeListener(tl);
235 }
236 
237 Datum* Context::NewDatum(std::string title) {
238  return rec_->NewDatum(title);
239 }
240 
242  ti_->Snapshot();
243 }
244 
246  ti_->KillSim();
247 }
248 
249 } // namespace cyclus
virtual std::string version()
Definition: agent.h:65
Composition::Ptr GetRecipe(std::string name)
Retrieve a registered recipe.
Definition: context.cc:172
boost::uuids::uuid sim_id()
returns the unique id associated with this cyclus simulation.
Definition: recorder.cc:49
boost::shared_ptr< Composition > Ptr
Definition: composition.h:43
void AddRecipe(std::string name, Composition::Ptr c)
Adds a composition recipe to a simulation-wide accessible list.
Definition: context.cc:164
Definition: any.hpp:72
std::string handle
user-defined label associated with a particular simulation
Definition: context.h:74
int branch_time
timestep at which simulation branching occurs if any
Definition: context.h:96
double cy_eps
generic epsilon values
Definition: context.cc:15
const char * xmlpp()
Definition: version.cc:60
const char * xml2()
Definition: version.cc:56
double eps
Epsilon in the simulation.
Definition: context.h:102
void RegisterTimeListener(TimeListener *agent)
Registers an agent to receive tick/tock notifications every timestep.
Definition: timer.cc:165
boost::uuids::uuid sim_id()
See Recorder::sim_id documentation.
Definition: context.cc:131
std::string name(int nuc)
Definition: pyne.cc:2940
int duration
length of the simulation in timesteps (months)
Definition: context.h:80
void Snapshot()
Schedules a snapshot of simulation state to output database to occur at the beginning of the next tim...
Definition: timer.h:57
void UnregisterTimeListener(TimeListener *tl)
Removes an agent from receiving tick/tock notifications.
Definition: timer.cc:169
void UnregisterTimeListener(TimeListener *tl)
Removes an agent from receiving tick/tock notifications.
Definition: context.cc:233
int y0
start year for the simulation (e.g. 1973);
Definition: context.h:83
bool explicit_inventory
True if per-agent inventories should be explicitly queried/recorded every time step in a table (i...
Definition: context.h:109
const char * describe()
Definition: version.cc:27
std::string spec()
The string representation of the agent spec that uniquely identifies the concrete agent class/module...
Definition: agent.h:358
void InitSim(SimInfo si)
Initializes the simulation time parameters.
Definition: context.cc:179
~Context()
Clean up resources including destructing the solver and all agents the context is aware of...
Definition: context.cc:78
uint64_t dt
Duration in seconds of a single time step in the simulation.
Definition: context.h:99
Context(Timer *ti, Recorder *rec)
Creates a new context working with the specified timer and datum manager.
Definition: context.cc:71
virtual int time()
Returns the current simulation timestep.
Definition: context.cc:225
Used to specify and send a collection of key-value pairs to the Recorder for recording.
Definition: datum.h:15
const uint64_t kDefaultTimeStepDur
Definition: context.h:21
int parent(int nuc, unsigned int rx, std::string z="n")
Definition: pyne.cc:6621
struct pyne::decay decay
a struct matching the &#39;/decay/decays&#39; table in nuc_data.h5.
void Initialize(Context *ctx, SimInfo si)
Sets intial time-related parameters for the simulation.
Definition: timer.cc:222
boost::uuids::uuid parent_sim
id for the parent simulation if any
Definition: context.h:89
const char * core()
Definition: version.cc:31
const char * coincbc()
Definition: version.cc:69
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:12
void AddPrototype(std::string name, Agent *m)
Adds a prototype to a simulation-wide accessible list, a prototype can not be added more than once...
Definition: context.cc:135
std::string decay
"manual" if use of the decay function is allowed, "never" otherwise
Definition: context.h:77
double eps_rsrc
Epsilon for resources in the simulation.
Definition: context.h:105
int m0
start month for the simulation: Jan = 1, ..., Dec = 12
Definition: context.h:86
const char * boost()
Definition: version.cc:35
SimInfo()
constructs a SimInfo instance with default variables
Definition: context.cc:18
Collects and manages output data generation for the cyclus core and agents during a simulation...
Definition: recorder.h:45
double cy_eps_rsrc
epsilon values to be used by resources
Definition: context.cc:16
std::string parent_type
One of "init", "branch", "restart" indicating the relationship of this simulation to its parent simul...
Definition: context.h:93
void RegisterTimeListener(TimeListener *tl)
Registers an agent to receive tick/tock notifications every timestep.
Definition: context.cc:229
int time()
Returns the current time, in months since the simulation started.
Definition: timer.cc:211
The TimeListener class is an inheritable class for any Agent that requires knowlege of ticks and tock...
Definition: time_listener.h:23
Datum * NewDatum(std::string title)
Creates a new datum namespaced under the specified title.
Definition: recorder.cc:69
Container for a static simulation-global parameters that both describe the simulation and affect its ...
Definition: context.h:37
Code providing rudimentary logging capability for the Cyclus core.
bool explicit_inventory_compact
True if per-agent inventories should be explicitly queried/recorded every time step in a table (i...
Definition: context.h:114
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 KillSim()
Schedules the simulation to be terminated at the end of this timestep.
Definition: timer.h:60
void Record()
Record this datum to its Recorder.
Definition: datum.cc:24
virtual const int id() const
The agent instance&#39;s unique ID within a simulation.
Definition: agent.h:354
void SchedBuild(Agent *parent, std::string proto_name, int t)
Schedules the named prototype to be built for the specified parent at timestep t. ...
Definition: timer.cc:173
void Snapshot()
Schedules a snapshot of simulation state to output database to occur at the beginning of the next tim...
Definition: context.cc:241
void KillSim()
Schedules the simulation to be terminated at the end of this timestep.
Definition: context.cc:245
void SchedDecom(Agent *m, int time)
Schedules the given Agent to be decommissioned at the specified timestep t.
Definition: timer.cc:180
static void SnapAgent(Agent *m)
Records a snapshot of the agent&#39;s current internal state into the simulation&#39;s output database...
Definition: sim_init.cc:121
const char * hdf5()
Definition: version.cc:43
Datum * NewDatum(std::string title)
See Recorder::NewDatum documentation.
Definition: context.cc:237
For failed retrieval/insertion of key-based data into/from data structures.
Definition: error.h:47
const char * sqlite3()
Definition: version.cc:39
void SchedDecom(Agent *m, int time=-1)
Schedules the given Agent to be decommissioned at the specified timestep t.
Definition: context.cc:119
void DelAgent(Agent *m)
Destructs and cleans up m (and it&#39;s children recursively).
Definition: context.cc:97
void SchedBuild(Agent *parent, std::string proto_name, int t=-1)
Schedules the named prototype to be built for the specified parent at timestep t. ...
Definition: context.cc:105
Controls simulation timestepping and inter-timestep phases.
Definition: timer.h:22