CYCLUS
mock_sim.cc
Go to the documentation of this file.
1 #include "mock_sim.h"
2 
3 #include "cyclus.h"
4 #include "sim_init.h"
5 #include "pyhooks.h"
6 #include <sstream>
7 
8 namespace cyclus {
9 
10 // The code for this function was copied with minor adjustements from
11 // XMLFileLoader::LoadInitialAgents
12 void InitAgent(Agent* a, std::stringstream& config, Recorder* rec,
13  SqliteBack* back) {
14  XMLParser parser_;
15  parser_.Init(config);
16  InfileTree xqe(parser_);
17 
18  a->Agent::InfileToDb(&xqe, DbInit(a, true));
19  a->InfileToDb(&xqe, DbInit(a));
20  rec->Flush();
21 
22  std::vector<Cond> conds;
23  conds.push_back(Cond("SimId", "==", rec->sim_id()));
24  conds.push_back(Cond("SimTime", "==", static_cast<int>(0)));
25  conds.push_back(Cond("AgentId", "==", a->id()));
26  CondInjector ci(back, conds);
27 
28  // call manually without agent impl injected
29  PrefixInjector pi(&ci, "AgentState");
30  a->Agent::InitFrom(&pi);
31 
32  pi = PrefixInjector(&ci, "AgentState" + AgentSpec(a->spec()).Sanitize());
33  a->InitFrom(&pi);
34 }
35 
36 ///////// MockAgent ////////////
37 
38 int MockAgent::nextid_ = 0;
39 
40 MockAgent::MockAgent(Context* ctx, Recorder* rec, SqliteBack* b, bool is_source)
41  : ctx_(ctx),
42  rec_(rec),
43  back_(b),
44  source_(is_source),
45  cap_(1e299),
46  lifetime_(-1),
47  start_(0) {
48  std::stringstream ss;
49  if (is_source) {
50  ss << "source";
51  } else {
52  ss << "sink";
53  }
54  ss << nextid_++;
55  proto_ = ss.str();
56 }
57 
59  commod_ = commod;
60  return *this;
61 }
63  recipe_ = recipe;
64  return *this;
65 }
67  cap_ = cap;
68  return *this;
69 }
71  start_ = t;
72  return *this;
73 }
75  lifetime_ = duration;
76  return *this;
77 }
78 
80  AgentSpec spec(":agents:Sink");
81  if (source_) {
82  spec = AgentSpec(":agents:Source");
83  }
84 
85  std::stringstream xml;
86  xml << "<facility><name>" << proto_ << "</name><lifetime>" << lifetime_
87  << "</lifetime><config><SrcSnkAgent>";
88 
89  if (source_) {
90  xml << "<commod>" << commod_ << "</commod>";
91  } else {
92  xml << "<in_commods><val>" << commod_ << "</val></in_commods>";
93  }
94  xml << "<capacity>" << cap_ << "</capacity>";
95  if (recipe_ != "") {
96  xml << "<recipe_name>" << recipe_ << "</recipe_name>";
97  }
98  xml << "</SrcSnkAgent></config></facility>";
99 
100  Agent* a = DynamicModule::Make(ctx_, spec);
101  InitAgent(a, xml, rec_, back_);
102 
103  ctx_->AddPrototype(proto_, a);
104 
105  if (start_ == 0) {
106  a = ctx_->CreateAgent<Agent>(proto_);
107  a->Build(NULL);
108  } else {
109  ctx_->SchedBuild(NULL, proto_, start_);
110  }
111  return proto_;
112 }
113 
114 ///////// MockSim ////////////
115 MockSim::MockSim(int duration)
116  : ctx_(&ti_, &rec_), back_(NULL), agent(NULL) {
118  warn_limit = 0;
119  back_ = new SqliteBack(":memory:");
120  rec_.RegisterBackend(back_);
121  ctx_.InitSim(SimInfo(duration));
122 }
123 
124 MockSim::MockSim(AgentSpec spec, std::string config, int duration)
125  : ctx_(&ti_, &rec_), back_(NULL), agent(NULL) {
127  warn_limit = 0;
128  back_ = new SqliteBack(":memory:");
129  rec_.RegisterBackend(back_);
130  ctx_.InitSim(SimInfo(duration));
131 
132  Agent* a = DynamicModule::Make(&ctx_, spec);
133 
134  std::stringstream xml;
135  xml << "<facility><name>agent_being_tested</name><config><foo>" << config
136  << "</foo></config></facility>";
137  InitAgent(a, xml, &rec_, back_);
138 
139  ctx_.AddPrototype(a->prototype(), a);
140  agent = ctx_.CreateAgent<Agent>(a->prototype());
141 }
142 
143 MockSim::MockSim(AgentSpec spec, std::string config, int duration, int lifetime)
144  : ctx_(&ti_, &rec_), back_(NULL), agent(NULL) {
146  warn_limit = 0;
147  back_ = new SqliteBack(":memory:");
148  rec_.RegisterBackend(back_);
149  ctx_.InitSim(SimInfo(duration));
150 
151  Agent* a = DynamicModule::Make(&ctx_, spec);
152 
153  std::stringstream xml;
154  xml << "<facility>"
155  << "<lifetime>" << lifetime << "</lifetime>"
156  << "<name>agent_being_tested</name>"
157  << "<config><foo>" << config << "</foo></config>"
158  << "</facility>";
159  InitAgent(a, xml, &rec_, back_);
160 
161  ctx_.AddPrototype(a->prototype(), a);
162  agent = ctx_.CreateAgent<Agent>(a->prototype());
163 }
164 
166  Agent* a = DynamicModule::Make(&ctx_, AgentSpec(":agents:Source"));
167 
168  std::stringstream xml;
169  xml << "<facility><name>" << name << "</name>"
170  << "<config><foo>"
171  << "<commod>alongunusedcommodityname</commod>"
172  << "<capacity>0</capacity>"
173  << "</foo></config></facility>";
174 
175  InitAgent(a, xml, &rec_, back_);
176  ctx_.AddPrototype(name, a);
177 };
178 
179 void MockSim::DummyProto(std::string name, std::string commod, double capacity) {
180  Agent* a = DynamicModule::Make(&ctx_, AgentSpec(":agents:Source"));
181 
182  std::stringstream xml;
183  xml << "<facility><name>" << name << "</name>"
184  << "<config><foo>"
185  << "<commod>" << "commod" << "</commod>"
186  << "<capacity>" << capacity << "</capacity>"
187  << "</foo></config></facility>";
188 
189  InitAgent(a, xml, &rec_, back_);
190  ctx_.AddPrototype(name, a);
191 };
192 
194  warn_limit = 42;
195  rec_.Close();
196  delete back_;
197 }
198 
200  MockAgent ms(&ctx_, &rec_, back_, true);
201  ms.commod(commod);
202  return ms;
203 }
204 
206  MockAgent ms(&ctx_, &rec_, back_, false);
207  ms.commod(commod);
208  return ms;
209 }
210 
212  ctx_.AddRecipe(name, c);
213 }
214 
216  // Initialize Python functionality
217  PyStart();
218 
219  agent->Build(NULL);
220  int id = agent->id();
221  ti_.RunSim();
222  rec_.Flush();
223 
224  PyStop();
225  return id;
226 }
227 
229  return SimInit::BuildMaterial(back_, resid);
230 }
231 
233  return SimInit::BuildProduct(back_, resid);
234 }
235 
236 SqliteBack& MockSim::db() { return *back_; }
237 
238 } // namespace cyclus
int Run()
Runs the simulation.
Definition: mock_sim.cc:215
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 Close()
Flushes all buffered Datum objects and flushes all registered backends.
Definition: recorder.cc:117
double b(int nuc)
Computes the scattering length [cm] from the coherent and incoherent components.
Definition: pyne.cc:11180
void AddRecipe(std::string name, Composition::Ptr c)
Adds a composition recipe to a simulation-wide accessible list.
Definition: context.cc:166
An Recorder backend that writes data to an sqlite database.
Definition: sqlite_back.h:17
MockAgent AddSource(std::string commod)
AddSource adds a source facility that can offer/provide material to the archetype being tested...
Definition: mock_sim.cc:199
Material::Ptr GetMaterial(int resid)
Reconstructs a material object from the simulation results database with the given resource state id...
Definition: mock_sim.cc:228
virtual void InfileToDb(InfileTree *qe, DbInit di)
Translates info for the agent from an input file to the database by reading parameters from the passe...
Definition: agent.cc:36
const std::string prototype() const
Returns the agent&#39;s prototype.
Definition: agent.h:347
A class for extracting information from a given XML parser.
Definition: infile_tree.h:22
boost::shared_ptr< Material > Ptr
Definition: material.h:75
virtual const int id() const
The agent instance&#39;s unique ID within a simulation.
Definition: agent.h:354
std::string name(int nuc)
Definition: pyne.cc:2940
MockAgent is a template for accumulating configuration information used to generate a source or sink ...
Definition: mock_sim.h:25
boost::shared_ptr< Product > Ptr
Definition: product.h:24
MockAgent(Context *ctx, Recorder *rec, SqliteBack *b, bool is_source)
Initializes a MockAgent to create a source (is_source == true) or a sink (is_source == false) in the ...
Definition: mock_sim.cc:40
void DummyProto(std::string name)
Adds a dummy prototype to the simulation that can be used by institutions and other agents for deploy...
Definition: mock_sim.cc:165
MockSim(int duration)
Creates and initializes a new empty mock simulation environment where duration is the length of the s...
Definition: mock_sim.cc:115
A helper class to hold xml file data and provide automatic validation.
Definition: xml_parser.h:16
virtual void Build(Agent *parent)
Called when the agent enters the smiulation as an active participant and is only ever called once...
Definition: agent.cc:153
unsigned int warn_limit
This is maximum number of times to issue a warning of each kind.
Definition: error.cc:13
Product::Ptr GetProduct(int resid)
Reconstructs a product object from the simulation results database with the given resource state id...
Definition: mock_sim.cc:232
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:181
static const void SetNucDataPath()
Initializes the path to the cyclus_nuc_data.h5 file.
Definition: env.h:98
static Product::Ptr BuildProduct(QueryableBackend *b, int resid)
Convenience function for reconstructing an untracked product object with the given resource state id ...
Definition: sim_init.cc:505
Wrapper class for QueryableBackends that injects a set of Cond&#39;s into every query before being execut...
void AddRecipe(std::string name, Composition::Ptr c)
AddRecipe adds a recipe to the mock simulation environment (i.e.
Definition: mock_sim.cc:211
MockAgent lifetime(int duration)
Sets the lifetime in time steps of the source/sink before it is decommissioned.
Definition: mock_sim.cc:74
T * CreateAgent(std::string proto_name)
Create a new agent by cloning the named prototype.
Definition: context.h:180
void PyStop(void)
Closes the current Python session.
Definition: pyhooks.cc:117
MockAgent commod(std::string commod)
Sets the commodity to be offered/requested by the source/sink.
Definition: mock_sim.cc:58
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:137
DbInit provides an interface for agents to record data to the output db that automatically injects th...
Definition: db_init.h:14
MockAgent AddSink(std::string commod)
AddSink adds a sink facility that can request+receive material from the archetype being tested...
Definition: mock_sim.cc:205
void Flush()
Flushes all buffered Datum objects and flushes all registered backends.
Definition: recorder.cc:92
Collects and manages output data generation for the cyclus core and agents during a simulation...
Definition: recorder.h:45
void RegisterBackend(RecBackend *b)
Registers b to receive Datum notifications for all Datum objects collected by the Recorder and to rec...
Definition: recorder.cc:113
MockAgent recipe(std::string recipe)
Sets the recipe to be offered/requested by the source/sink.
Definition: mock_sim.cc:62
std::string Finalize()
Finalize MUST be called after configuration is complete to actually create the source/sink agent (or ...
Definition: mock_sim.cc:79
MockAgent start(int timestep)
Sets the time step in the simulation that the source/sink should be deployed.
Definition: mock_sim.cc:70
void PyStart(void)
Initialize Python functionality, this is a no-op if Python was not installed along with Cyclus...
Definition: pyhooks.cc:115
void RunSim()
Runs the simulation.
Definition: timer.cc:16
Wrapper class for QueryableBackends that injects prefix in front of the title/table for every query b...
Container for a static simulation-global parameters that both describe the simulation and affect its ...
Definition: context.h:39
A simulation context provides access to necessary simulation-global functions and state...
Definition: context.h:130
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
MockAgent capacity(double cap)
Sets the per time step capacity/throughput limit for provided/received material in kg for the source/...
Definition: mock_sim.cc:66
const double pi
pi = 3.14159265359
Definition: pyne.cc:10356
static Material::Ptr BuildMaterial(QueryableBackend *b, int resid)
Convenience function for reconstructing an untracked material object with the given resource state id...
Definition: sim_init.cc:491
Agent * agent
the agent being tested by the mock simulation environment.
Definition: mock_sim.h:230
int id(int nuc)
Definition: pyne.cc:2716
virtual void InitFrom(QueryableBackend *b)
Intializes an agent&#39;s internal state from the database.
Definition: agent.cc:45
Represents a condition used to filter rows returned by a query.
static Agent * Make(Context *ctx, AgentSpec spec)
Returns a newly constructed agent for the given module spec.
void InitAgent(Agent *a, std::stringstream &config, Recorder *rec, SqliteBack *back)
Definition: mock_sim.cc:12
void Init(const std::stringstream &input)
initializes a parser with an xml snippet
Definition: xml_parser.cc:41
SqliteBack & db()
Returns the underlying in-memory database containing results for the simulation.
Definition: mock_sim.cc:236
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:107