CYCLUS
Loading...
Searching...
No Matches
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
8namespace cyclus {
9
10// The code for this function was copied with minor adjustements from
11// XMLFileLoader::LoadInitialAgents
12void InitAgent(Agent* a, std::stringstream& config, Recorder* rec,
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()));
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
38int MockAgent::nextid_ = 0;
39
41 : ctx_(ctx),
42 rec_(rec),
43 back_(b),
44 source_(is_source),
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
58MockAgent MockAgent::commod(std::string commod) {
59 commod_ = commod;
60 return *this;
61}
62MockAgent MockAgent::recipe(std::string recipe) {
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
79std::string MockAgent::Finalize() {
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 ////////////
115MockSim::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
124MockSim::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
143MockSim::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
165void MockSim::DummyProto(std::string name) {
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
179void 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
199MockAgent MockSim::AddSource(std::string commod) {
200 MockAgent ms(&ctx_, &rec_, back_, true);
201 ms.commod(commod);
202 return ms;
203}
204
205MockAgent MockSim::AddSink(std::string commod) {
206 MockAgent ms(&ctx_, &rec_, back_, false);
207 ms.commod(commod);
208 return ms;
209}
210
211void MockSim::AddRecipe(std::string name, Composition::Ptr c) {
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
231
235
236SqliteBack& MockSim::db() { return *back_; }
237
238} // namespace cyclus
std::string Sanitize()
The abstract base class used by all types of agents that live and interact in a simulation.
Definition agent.h:49
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
virtual const int id() const
The agent instance's unique ID within a simulation.
Definition agent.h:352
boost::shared_ptr< Composition > Ptr
Definition composition.h:43
Wrapper class for QueryableBackends that injects a set of Cond's into every query before being execut...
Represents a condition used to filter rows returned by a query.
A simulation context provides access to necessary simulation-global functions and state.
Definition context.h:145
T * CreateAgent(std::string proto_name)
Create a new agent by cloning the named prototype.
Definition context.h:195
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:120
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:150
void AddRecipe(std::string name, Composition::Ptr c)
Adds a composition recipe to a simulation-wide accessible list.
Definition context.cc:179
void InitSim(SimInfo si)
Initializes the simulation time parameters.
Definition context.cc:263
DbInit provides an interface for agents to record data to the output db that automatically injects th...
Definition db_init.h:14
static Agent * Make(Context *ctx, AgentSpec spec)
Returns a newly constructed agent for the given module spec.
static const void SetNucDataPath()
Initializes the path to the cyclus_nuc_data.h5 file.
Definition env.h:98
A class for extracting information from a given XML parser.
Definition infile_tree.h:22
boost::shared_ptr< Material > Ptr
Definition material.h:75
MockAgent is a template for accumulating configuration information used to generate a source or sink ...
Definition mock_sim.h:25
MockAgent recipe(std::string recipe)
Sets the recipe to be offered/requested by the source/sink.
Definition mock_sim.cc:62
MockAgent lifetime(int duration)
Sets the lifetime in time steps of the source/sink before it is decommissioned.
Definition mock_sim.cc:74
MockAgent commod(std::string commod)
Sets the commodity to be offered/requested by the source/sink.
Definition mock_sim.cc:58
MockAgent start(int timestep)
Sets the time step in the simulation that the source/sink should be deployed.
Definition mock_sim.cc:70
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
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
std::string Finalize()
Finalize MUST be called after configuration is complete to actually create the source/sink agent (or ...
Definition mock_sim.cc:79
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
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
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 AddRecipe(std::string name, Composition::Ptr c)
AddRecipe adds a recipe to the mock simulation environment (i.e.
Definition mock_sim.cc:211
Agent * agent
the agent being tested by the mock simulation environment.
Definition mock_sim.h:230
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
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
SqliteBack & db()
Returns the underlying in-memory database containing results for the simulation.
Definition mock_sim.cc:236
int Run()
Runs the simulation.
Definition mock_sim.cc:215
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
Wrapper class for QueryableBackends that injects prefix in front of the title/table for every query b...
boost::shared_ptr< Product > Ptr
Definition product.h:24
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
void Flush()
Flushes all buffered Datum objects and flushes all registered backends.
Definition recorder.cc:92
void Close()
Flushes all buffered Datum objects and flushes all registered backends.
Definition recorder.cc:117
Container for a static simulation-global parameters that both describe the simulation and affect its ...
Definition context.h:45
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:556
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:542
An Recorder backend that writes data to an sqlite database.
Definition sqlite_back.h:17
void RunSim()
Runs the simulation.
Definition timer.cc:16
A helper class to hold xml file data and provide automatic validation.
Definition xml_parser.h:15
void Init(const std::stringstream &input)
initializes a parser with an xml snippet
Definition xml_parser.cc:49
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
void InitAgent(Agent *a, std::stringstream &config, Recorder *rec, SqliteBack *back)
Definition mock_sim.cc:12
unsigned int warn_limit
This is maximum number of times to issue a warning of each kind.
Definition error.cc:13
void PyStop(void)
Closes the current Python session.
Definition pyhooks.cc:73
void PyStart(void)
Initialize Python functionality, this is a no-op if Python was not installed along with Cyclus.
Definition pyhooks.cc:71
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters
static const double CY_LARGE_DOUBLE
maximum (+) value for a linear variable
Definition cyc_limits.h:44