CYCLUS
Loading...
Searching...
No Matches
mock_sim.h
Go to the documentation of this file.
1#ifndef CYCLUS_SRC_MOCK_SIM_H_
2#define CYCLUS_SRC_MOCK_SIM_H_
3
4#include "cyclus.h"
5#include "sqlite_back.h"
6#include "timer.h"
7
8namespace cyclus {
9
10class Source;
11class Sink;
12
13/// MockAgent is a template for accumulating configuration information used to
14/// generate a source or sink facility in a MockSimulation. All parameters
15/// other than commod have defaults. After all desired configuration is
16/// completed, the Finalize function MUST be called. All configure functions
17/// return the MockAgent itself to enable chaining. Default configuration is:
18///
19/// * no recipe - sources provide requested material, sinks take anything
20/// * infinite per time step capacity
21/// * start/deploy on time step zero
22/// * infinite lifetime.
23///
24/// For examples on how to use MockAgent, see the MockSim API documentation.
25class MockAgent {
26 public:
27 /// Initializes a MockAgent to create a source (is_source == true) or a
28 /// sink (is_source == false) in the provided simulation context. rec must be
29 /// the recorder used to initialize ctx and b must be a backend registered
30 /// with rec.
31 MockAgent(Context* ctx, Recorder* rec, SqliteBack* b, bool is_source);
32
33 /// Sets the commodity to be offered/requested by the source/sink.
34 MockAgent commod(std::string commod);
35
36 /// Sets the recipe to be offered/requested by the source/sink.
37 MockAgent recipe(std::string recipe);
38
39 /// Sets the per time step capacity/throughput limit for provided/received
40 /// material in kg for the source/sink.
41 MockAgent capacity(double cap);
42
43 /// Sets the time step in the simulation that the source/sink should be
44 /// deployed.
45 MockAgent start(int timestep);
46
47 /// Sets the lifetime in time steps of the source/sink before it is
48 /// decommissioned.
49 MockAgent lifetime(int duration);
50
51 /// Finalize MUST be called after configuration is complete to actually
52 /// create the source/sink agent (or schedule it to be built). The
53 /// auto-generated prototype name of the created prototype is returned to
54 /// support querying based on specific sources/sinks.
55 std::string Finalize();
56
57 private:
58 static int nextid_;
59 bool source_;
60 std::string commod_;
61 std::string recipe_;
62 double cap_;
63 int start_;
64 int lifetime_;
65 std::string proto_;
66 Context* ctx_;
67 Recorder* rec_;
68 SqliteBack* back_;
69};
70
71/// MockSim is a helper for running full simulations entirely in-code to test
72/// archetypes/agents without having to deal with input files, output database
73/// files, and other pieces of the full Cyclus stack. This is especially
74/// convenient for writing unit-like tests (e.g. using gtest) for your
75/// archetype's in-simulation behavior. Initialize the MockSim indicating the
76/// archetype you want to test and the simulation duration. Then add any
77/// number of sources and/or sinks to transact with your agent. They can have
78/// specific recipes (or not) and their deployment and lifetime (before
79/// decommissioning) can be specified too. Here is an example using the
80/// agents:Source archetype in Cyclus as the tested agent:
81///
82/// @code
83///
84/// cyclus::CompMap m;
85/// m[922350000] = .05;
86/// m[922380000] = .95;
87/// cyclus::Composition::Ptr fresh = cyclus::Composition::CreateFromMass(m);
88///
89/// std::string config =
90/// "<commod>enriched_u</commod>"
91/// "<recipe_name>fresh_fuel</recipe_name>"
92/// "<capacity>10</capacity>";
93///
94/// int dur = 10;
95/// cyclus::MockSim sim(cyclus::AgentSpec(":agents:Source"), config, dur);
96/// sim.AddSink("enriched_u").Finalize();
97/// sim.AddRecipe("fresh_fuel", fresh);
98/// int src_id = sim.Run(); // capture the agent ID of the facility being tested
99///
100/// @endcode
101///
102/// Querying the results can be accomplished by getting a reference to the
103/// in-memory database generated. Not all data that is present in normal
104/// full-stack simulations is available. However, most of the key core tables
105/// are fully available. Namely, the Transactions, Composition, Resources,
106/// ResCreators, AgentEntry, and AgentExit tables are available. Any
107/// custom-tables created by the tested archetype will also be available. Here
108/// is a sample query and test you might write if using the googletest
109/// framework:
110///
111/// @code
112///
113/// // return all transactions where our source facility is the sender
114/// std::vector<cyclus::Cond> conds;
115/// conds.push_back("SenderId", "==", src_id);
116/// cyclus::QueryResult qr = sim.db().Query("Transactions", &conds);
117/// int n_trans = qr.rows.size();
118/// EXPECT_EQ(10, n_trans) << "expected 10 transactions, got " << n_trans;
119///
120/// // reconstruct the material object for the first transaction
121/// int res_id = qr.GetVal<int>("ResourceId", 0);
122/// cyclus::Material::Ptr m = sim.GetMaterial(res_id);
123/// EXPECT_DOUBLE_EQ(10, m->quantity());
124///
125/// // confirm composition is as expected
126/// cyclus::toolkit::MatQuery mq(m);
127/// EXPECT_DOUBLE_EQ(0.5, mq.mass(922350000));
128/// EXPECT_DOUBLE_EQ(9.5, mq.mass(922380000));
129///
130/// @endcode
131class MockSim {
132 public:
133 /// Creates and initializes a new empty mock simulation environment where
134 /// duration is the length of the simulation in time steps. An agent must be
135 /// specified before running an empty-constructed MockSim (i.e. manually
136 /// construct an agent using MockSim::context() and setting MockSim::agent.
137 MockSim(int duration);
138
139 /// Creates and initializes a new mock simulation environment to test the
140 /// archetype identified by spec. config should contain the
141 /// archetype-specific xml snippet excluding the wrapping "<config>" and
142 /// "<[AgentName]>" tags. duration is the length of the simulation in time
143 /// steps.
144 MockSim(AgentSpec spec, std::string config, int duration);
145
146 /// Creates and initializes a new mock simulation environment to test the
147 /// archetype identified by spec. config should contain the
148 /// archetype-specific xml snippet excluding the wrapping "<config>" and
149 /// "<[AgentName]>" tags. duration is the length of the simulation in time
150 /// steps. 'lifetime' is the lifetime of the agent being tested.
151 MockSim(AgentSpec spec, std::string config, int duration, int lifetime);
152
153 ~MockSim();
154
155 /// AddSource adds a source facility that can offer/provide material to the
156 /// archetype being tested. commod specifies the commodity the source will
157 /// offer on. AddSource can be called multiple times to generate many sources
158 /// for the simulation. The returned MockAgent object has several functions
159 /// that can be called to configure the source's behavior further. Don't
160 /// forget to call the MockAgent object's "Finalize" function when you are
161 /// done configuring it. MockAgent's functions support chaining:
162 ///
163 /// @code
164 ///
165 /// cyclus::MockSim sim(...);
166 /// sim.AddSource("fresh_mox")
167 /// .start(7).lifetime(3).recipe("mox_fuel")
168 /// .Finalize();
169 ///
170 /// @endcode
171 MockAgent AddSource(std::string commod);
172
173 /// AddSink adds a sink facility that can request+receive material from the
174 /// archetype being tested. commod specifies the commodity the sink will
175 /// request on. AddSink can be called multiple times to generate many sinks
176 /// for the simulation. The returned MockAgent object has several functions
177 /// that can be called to configure the sink's behavior further. Don't
178 /// forget to call the MockAgent object's "Finalize" function when you are
179 /// done configuring it. MockAgent's functions support chaining:
180 ///
181 /// @code
182 ///
183 /// cyclus::MockSim sim(...);
184 /// sim.AddSink("spent_mox")
185 /// .start(7).lifetime(3).recipe("spent_fresh_mox")
186 /// .Finalize();
187 ///
188 /// @endcode
189 MockAgent AddSink(std::string commod);
190
191 /// AddRecipe adds a recipe to the mock simulation environment (i.e. to the
192 /// simulation context). Any recipes that your archetype expects to find in
193 /// the simulation context must be added this way; if the xml configuration
194 /// snippet for the archetype being tested contains a recipe name, add it
195 /// with this function.
196 void AddRecipe(std::string name, Composition::Ptr c);
197
198 /// Adds a dummy prototype to the simulation that can be used by
199 /// institutions and other agents for deployment/decommission testing.
200 void DummyProto(std::string name);
201
202 /// Adds a dummy prototype to the simulation that can be used by
203 /// institutions and other agents for demand-driven deployment testing.
204 void DummyProto(std::string name, std::string commod, double capacity);
205
206 /// Runs the simulation. This can only be called once. After the simulation
207 /// has been run, this MockSim object CANNOT be reused to run other
208 /// simulations. Run returns the agent ID for the agent being tested for
209 /// use in queries.
210 int Run();
211
212 /// Reconstructs a material object from the simulation results database with
213 /// the given resource state id.
214 Material::Ptr GetMaterial(int resid);
215
216 /// Reconstructs a product object from the simulation results database with
217 /// the given resource state id.
218 Product::Ptr GetProduct(int resid);
219
220 /// Returns the underlying in-memory database containing results for
221 /// the simulation. Run must be called before the database will contain
222 /// anything.
223 SqliteBack& db();
224
225 /// Returns the context for the mock simulation environment.
226 Context* context() { return &ctx_; }
227
228 /// the agent being tested by the mock simulation environment.
230
231 private:
232 Context ctx_;
233 Timer ti_;
234 Recorder rec_;
235 SqliteBack* back_;
236};
237
238} // namespace cyclus
239
240#endif // CYCLUS_SRC_MOCK_SIM_H_
The abstract base class used by all types of agents that live and interact in a simulation.
Definition agent.h:50
boost::shared_ptr< Composition > Ptr
Definition composition.h:43
A simulation context provides access to necessary simulation-global functions and state.
Definition context.h:146
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:226
MockAgent AddSink(std::string commod)
AddSink adds a sink facility that can request+receive material from the archetype being tested.
Definition mock_sim.cc:199
void AddRecipe(std::string name, Composition::Ptr c)
AddRecipe adds a recipe to the mock simulation environment (i.e.
Definition mock_sim.cc:205
Agent * agent
the agent being tested by the mock simulation environment.
Definition mock_sim.h:229
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:162
Material::Ptr GetMaterial(int resid)
Reconstructs a material object from the simulation results database with the given resource state id.
Definition mock_sim.cc:222
SqliteBack & db()
Returns the underlying in-memory database containing results for the simulation.
Definition mock_sim.cc:230
Context * context()
Returns the context for the mock simulation environment.
Definition mock_sim.h:226
int Run()
Runs the simulation.
Definition mock_sim.cc:209
MockAgent AddSource(std::string commod)
AddSource adds a source facility that can offer/provide material to the archetype being tested.
Definition mock_sim.cc:193
boost::shared_ptr< Product > Ptr
Definition product.h:23
Collects and manages output data generation for the cyclus core and agents during a simulation.
Definition recorder.h:45
An Recorder backend that writes data to an sqlite database.
Definition sqlite_back.h:17
Controls simulation timestepping and inter-timestep phases.
Definition timer.h:22
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14