28 InitBase(b, r->
sim_id(), 0);
34 "restart capability is not finalized and fully"
35 " tested. Its behavior may change in future"
38 InitBase(b, sim_id, t);
39 si_.parent_sim = sim_id;
40 si_.parent_type =
"restart";
47 boost::uuids::uuid new_sim_id) {
48 throw Error(
"simulation branching feature not implemented");
51void SimInit::InitBase(
QueryableBackend* b, boost::uuids::uuid simid,
int t) {
54 std::vector<Cond> conds;
55 conds.push_back(
Cond(
"SimId",
"==", simid));
82 std::set<Agent*> mlist = ctx->agent_list_;
83 std::set<Agent*>::iterator it;
84 for (it = mlist.begin(); it != mlist.end(); ++it) {
94 ->
AddVal(
"Object", std::string(
"Agent"))
95 ->
AddVal(
"NextId", Agent::next_id_)
99 ->
AddVal(
"Object", std::string(
"Transaction"))
100 ->
AddVal(
"NextId", ctx->trans_id_)
104 ->
AddVal(
"Object", std::string(
"Composition"))
105 ->
AddVal(
"NextId", Composition::next_id_)
109 ->
AddVal(
"Object", std::string(
"ResourceState"))
110 ->
AddVal(
"NextId", Resource::nextstate_id_)
114 ->
AddVal(
"Object", std::string(
"ResourceObj"))
115 ->
AddVal(
"NextId", Resource::nextobj_id_)
119 ->
AddVal(
"Object", std::string(
"Product"))
120 ->
AddVal(
"NextId", Product::next_qualid_)
127 m->Agent::Snapshot(
DbInit(m,
true));
133 Inventories::iterator it;
134 for (it = invs.begin(); it != invs.end(); ++it) {
135 std::string name = it->first;
136 std::vector<Resource::Ptr> inv = it->second;
137 for (
int i = 0; i < inv.size(); ++i) {
138 ctx->
NewDatum(
"AgentStateInventories")
141 ->
AddVal(
"InventoryName", name)
142 ->
AddVal(
"ResourceId", inv[i]->state_id())
148void SimInit::LoadInfo() {
150 int dur = qr.
GetVal<
int>(
"Duration");
151 int y0 = qr.
GetVal<
int>(
"InitialYear");
152 int m0 = qr.
GetVal<
int>(
"InitialMonth");
153 std::string h = qr.
GetVal<std::string>(
"Handle");
155 std::string d = dq.
GetVal<std::string>(
"Decay");
156 si_ =
SimInfo(dur, y0, m0, h, d);
158 si_.seed = qr.
GetVal<
int>(
"Seed");
159 si_.stride = qr.
GetVal<
int>(
"Stride");
161 si_.parent_sim = qr.
GetVal<boost::uuids::uuid>(
"ParentSimId");
163 qr = b_->
Query(
"TimeStepDur", NULL);
166 si_.dt = qr.
GetVal<
int>(
"DurationSecs");
168 qr = b_->
Query(
"Epsilon", NULL);
169 si_.eps = qr.
GetVal<
double>(
"GenericEpsilon");
170 si_.eps_rsrc = qr.GetVal<
double>(
"ResourceEpsilon");
172 qr = b_->
Query(
"InfoExplicitInv", NULL);
173 si_.explicit_inventory = qr.
GetVal<
bool>(
"RecordInventory");
174 si_.explicit_inventory_compact = qr.GetVal<
bool>(
"RecordInventoryCompact");
179void SimInit::LoadRecipes() {
182 qr = b_->
Query(
"Recipes", NULL);
183 }
catch (std::exception err) {
187 for (
int i = 0; i < qr.rows.size(); ++i) {
188 std::string recipe = qr.GetVal<std::string>(
"Recipe", i);
189 int stateid = qr.GetVal<
int>(
"QualId", i);
191 ctx_->AddRecipe(recipe, c);
195void SimInit::LoadPackages() {
198 qr = b_->Query(
"Packages", NULL);
199 }
catch (std::exception err) {
203 for (
int i = 0; i < qr.rows.size(); ++i) {
204 std::string
name = qr.GetVal<std::string>(
"PackageName", i);
205 double fill_min = qr.GetVal<
double>(
"FillMin", i);
206 double fill_max = qr.GetVal<
double>(
"FillMax", i);
207 std::string strategy = qr.GetVal<std::string>(
"Strategy", i);
210 ctx_->AddPackage(name, fill_min, fill_max, strategy);
217void SimInit::LoadTransportUnits() {
220 qr = b_->Query(
"TransportUnits", NULL);
221 }
catch (std::exception err) {
225 for (
int i = 0; i < qr.rows.size(); ++i) {
226 std::string
name = qr.GetVal<std::string>(
"TransportUnitName", i);
227 int fill_min = qr.GetVal<
int>(
"FillMin", i);
228 int fill_max = qr.GetVal<
int>(
"FillMax", i);
229 std::string strategy = qr.GetVal<std::string>(
"Strategy", i);
232 ctx_->AddTransportUnit(name, fill_min, fill_max, strategy);
239void* SimInit::LoadPreconditioner(std::string name) {
243 map<string, double> commod_order;
245 QueryResult qr = b_->Query(
"CommodPriority", NULL);
246 for (
int i = 0; i < qr.rows.size(); ++i) {
247 std::string commod = qr.GetVal<
string>(
"Commodity", i);
248 double order = qr.GetVal<
double>(
"SolutionOrder", i);
249 commod_order[commod] = order;
251 }
catch (std::exception err) {
256 if (name ==
"greedy") {
261 "The name of the preconditioner was not recognized, "
269 std::set<std::string> tables) {
272 ExchangeSolver* solver;
274 string precon_name =
string(
"greedy");
276 string solver_info =
string(
"GreedySolverInfo");
277 if (0 < tables.count(solver_info)) {
278 QueryResult qr = b_->Query(solver_info, NULL);
279 if (qr.rows.size() > 0) {
280 precon_name = qr.GetVal<
string>(
"Preconditioner");
284 precon = LoadPreconditioner(precon_name);
285 if (precon == NULL) {
286 solver =
new GreedySolver(exclusive);
288 solver =
new GreedySolver(exclusive,
289 reinterpret_cast<GreedyPreconditioner*
>(precon));
295 std::set<std::string> tables) {
297 ExchangeSolver* solver;
301 std::string solver_info =
"CoinSolverInfo";
302 if (0 < tables.count(solver_info)) {
303 QueryResult qr = b_->Query(solver_info, NULL);
304 timeout = qr.GetVal<
double>(
"Timeout");
305 verbose = qr.GetVal<
bool>(
"Verbose");
306 mps = qr.GetVal<
bool>(
"Mps");
311 solver =
new ProgSolver(
"cbc", timeout, exclusive, verbose, mps);
315 "Cyclus was not compiled with COIN support, cannot load solver.");
319void SimInit::LoadSolverInfo() {
323 ExchangeSolver* solver;
325 bool exclusive_orders;
329 set<string> tables = b_->Tables();
330 string solver_info =
string(
"SolverInfo");
331 if (0 < tables.count(solver_info)) {
332 QueryResult qr = b_->Query(solver_info, NULL);
333 if (qr.rows.size() > 0) {
334 solver_name = qr.GetVal<
string>(
"Solver");
335 exclusive_orders = qr.GetVal<
bool>(
"ExclusiveOrders");
339 if (solver_name ==
"greedy") {
340 solver = LoadGreedySolver(exclusive_orders, tables);
341 }
else if (solver_name ==
"coin-or") {
342 solver = LoadCoinSolver(exclusive_orders, tables);
345 "The name of the solver was not recognized, "
350 ctx_->solver(solver);
353void SimInit::LoadPrototypes() {
354 QueryResult qr = b_->Query(
"Prototypes", NULL);
355 for (
int i = 0; i < qr.rows.size(); ++i) {
356 std::string proto = qr.GetVal<std::string>(
"Prototype", i);
357 int agentid = qr.GetVal<
int>(
"AgentId", i);
358 std::string impl = qr.GetVal<std::string>(
"Spec", i);
359 AgentSpec spec(impl);
367 std::vector<Cond> conds;
368 conds.push_back(Cond(
"AgentId",
"==", agentid));
369 CondInjector ci(b_, conds);
370 PrefixInjector
pi(&ci,
"AgentState");
373 m->Agent::InitFrom(&pi);
375 pi = PrefixInjector(&ci,
"AgentState" + spec.Sanitize());
377 ctx_->AddPrototype(proto, m);
381void SimInit::LoadInitialAgents() {
388 std::vector<Cond> conds;
389 conds.push_back(Cond(
"EnterTime",
"<=", t_));
390 QueryResult qentry = b_->Query(
"AgentEntry", &conds);
391 std::map<int, int> parentmap;
392 std::map<int, Agent*> unbuilt;
393 for (
int i = 0; i < qentry.rows.size(); ++i) {
394 if (t_ > 0 && qentry.GetVal<
int>(
"EnterTime", i) == t_) {
398 int id = qentry.GetVal<
int>(
"AgentId", i);
399 std::vector<Cond> conds;
400 conds.push_back(Cond(
"AgentId",
"==",
id));
401 conds.push_back(Cond(
"ExitTime",
"<", t_));
403 QueryResult qexit = b_->Query(
"AgentExit", &conds);
404 if (qexit.rows.size() != 0) {
407 }
catch (std::exception err) {
412 std::string proto = qentry.GetVal<std::string>(
"Prototype", i);
413 std::string impl = qentry.GetVal<std::string>(
"Spec", i);
414 AgentSpec spec(impl);
418 m->prototype_ = proto;
420 m->enter_time_ = qentry.GetVal<
int>(
"EnterTime", i);
422 parentmap[
id] = qentry.GetVal<
int>(
"ParentId", i);
426 conds.push_back(Cond(
"SimTime",
"==", t_));
427 CondInjector ci(b_, conds);
428 PrefixInjector
pi(&ci,
"AgentState");
429 m->Agent::InitFrom(&pi);
430 pi = PrefixInjector(&ci,
"AgentState" + spec.Sanitize());
435 std::map<int, Agent*>::iterator it = unbuilt.begin();
436 std::vector<Agent*> enter_list;
437 while (unbuilt.size() > 0) {
439 Agent* m = it->second;
440 int parentid = parentmap[
id];
442 if (parentid == -1) {
447 enter_list.push_back(m);
448 }
else if (agents_.count(parentid) > 0) {
449 m->Connect(agents_[parentid]);
453 enter_list.push_back(m);
457 if (it == unbuilt.end()) {
458 it = unbuilt.begin();
464 for (
int i = 0; i < enter_list.size(); ++i) {
465 enter_list[i]->EnterNotify();
469void SimInit::LoadInventories() {
470 std::map<int, Agent*>::iterator it;
471 for (it = agents_.begin(); it != agents_.end(); ++it) {
472 Agent* m = it->second;
473 std::vector<Cond> conds;
474 conds.push_back(Cond(
"SimTime",
"==", t_));
475 conds.push_back(Cond(
"AgentId",
"==", m->id()));
478 qr = b_->Query(
"AgentStateInventories", &conds);
479 }
catch (std::exception err) {
484 for (
int i = 0; i < qr.rows.size(); ++i) {
485 std::string inv_name = qr.GetVal<std::string>(
"InventoryName", i);
486 int state_id = qr.GetVal<
int>(
"ResourceId", i);
487 invs[inv_name].push_back(LoadResource(ctx_, b_, state_id));
493void SimInit::LoadBuildSched() {
494 std::vector<Cond> conds;
495 conds.push_back(Cond(
"BuildTime",
">", t_));
498 qr = b_->Query(
"BuildSchedule", &conds);
499 }
catch (std::exception err) {
503 for (
int i = 0; i < qr.rows.size(); ++i) {
504 int t = qr.GetVal<
int>(
"BuildTime", i);
505 int parentid = qr.GetVal<
int>(
"ParentId", i);
506 std::string proto = qr.GetVal<std::string>(
"Prototype", i);
507 ctx_->SchedBuild(agents_[parentid], proto, t);
511void SimInit::LoadDecomSched() {
512 std::vector<Cond> conds;
513 conds.push_back(Cond(
"DecomTime",
">=", t_));
516 qr = b_->Query(
"DecomSchedule", &conds);
517 }
catch (std::exception err) {
521 for (
int i = 0; i < qr.rows.size(); ++i) {
522 int t = qr.GetVal<
int>(
"DecomTime", i);
523 int agentid = qr.GetVal<
int>(
"AgentId", i);
524 ctx_->SchedDecom(agents_[agentid], t);
528void SimInit::LoadNextIds() {
529 std::vector<Cond> conds;
530 conds.push_back(Cond(
"Time",
"==", t_));
531 QueryResult qr = b_->Query(
"NextIds", &conds);
532 for (
int i = 0; i < qr.rows.size(); ++i) {
533 std::string obj = qr.GetVal<std::string>(
"Object", i);
534 if (obj ==
"Agent") {
535 Agent::next_id_ = qr.GetVal<
int>(
"NextId", i);
536 }
else if (obj ==
"Transaction") {
537 ctx_->trans_id_ = qr.GetVal<
int>(
"NextId", i);
538 }
else if (obj ==
"Composition") {
539 Composition::next_id_ = qr.GetVal<
int>(
"NextId", i);
540 }
else if (obj ==
"ResourceState") {
541 Resource::nextstate_id_ = qr.GetVal<
int>(
"NextId", i);
542 }
else if (obj ==
"ResourceObj") {
543 Resource::nextobj_id_ = qr.GetVal<
int>(
"NextId", i);
544 }
else if (obj ==
"Product") {
545 Product::next_qualid_ = qr.GetVal<
int>(
"NextId", i);
547 throw IOError(
"Unexpected value in NextIds table: " + obj);
561 m->tracker_.DontTrack();
575 p->tracker_.DontTrack();
582 std::vector<Cond> conds;
583 conds.push_back(
Cond(
"ResourceId",
"==", state_id));
586 int obj_id = qr.
GetVal<
int>(
"ObjId");
590 r = LoadMaterial(ctx, b, state_id);
592 r = LoadProduct(ctx, b, state_id);
594 throw IOError(
"Invalid resource type in output database: " + type);
597 r->state_id_ = state_id;
605 std::vector<Cond> conds;
606 conds.push_back(Cond(
"ResourceId",
"==", state_id));
607 QueryResult qr =
b->Query(
"MaterialInfo", &conds);
608 int prev_decay = qr.GetVal<
int>(
"PrevDecayTime");
612 conds.push_back(Cond(
"ResourceId",
"==", state_id));
613 qr =
b->Query(
"Resources", &conds);
614 double qty = qr.GetVal<
double>(
"Quantity");
615 int stateid = qr.GetVal<
int>(
"QualId");
619 Agent* dummy =
new Dummy(ctx);
621 mat->prev_decay_time_ = prev_decay;
622 ctx->DelAgent(dummy);
628 std::vector<Cond> conds;
629 conds.push_back(Cond(
"QualId",
"==", stateid));
630 QueryResult qr =
b->Query(
"Compositions", &conds);
632 for (
int i = 0; i < qr.rows.size(); ++i) {
633 int nucid = qr.GetVal<
int>(
"NucId", i);
634 double mass_frac = qr.GetVal<
double>(
"MassFrac", i);
635 cm[nucid] = mass_frac;
646 std::vector<Cond> conds;
647 conds.push_back(Cond(
"ResourceId",
"==", state_id));
648 QueryResult qr =
b->Query(
"Resources", &conds);
649 double qty = qr.GetVal<
double>(
"Quantity");
650 int stateid = qr.GetVal<
int>(
"QualId");
654 conds.push_back(Cond(
"QualId",
"==", stateid));
655 qr =
b->Query(
"Products", &conds);
656 std::string quality = qr.GetVal<std::string>(
"Quality");
659 Product::qualids_[quality] = stateid;
661 Agent* dummy =
new Dummy(ctx);
663 ctx->DelAgent(dummy);
The abstract base class used by all types of agents that live and interact in a simulation.
Context * context() const
Returns this agent's simulation context.
virtual const int id() const
The agent instance's unique ID within a simulation.
virtual void Snapshot(DbInit di)=0
Snapshots agent-internal state to the database via the DbInit var di.
const int enter_time() const
Returns the time step at which this agent's Build function was called (-1 if the agent has never been...
virtual Inventories SnapshotInv()=0
Snapshots an agent's resource inventories to the database.
static Ptr CreateFromMass(CompMap v)
Creates a new composition from v with its components having appropriate mass-based ratios.
boost::shared_ptr< Composition > Ptr
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.
Datum * NewDatum(std::string title)
See Recorder::NewDatum documentation.
virtual int time()
Returns the current simulation timestep.
void InitSim(SimInfo si)
Initializes the simulation time parameters.
Datum * AddVal(const char *field, boost::spirit::hold_any val, std::vector< int > *shape=NULL)
Add an arbitrary field-value pair to the datum.
void Record()
Record this datum to its Recorder.
DbInit provides an interface for agents to record data to the output db that automatically injects th...
Dummy * Clone()
Returns a newly created/allocated prototype that is an exact copy of this.
static Agent * Make(Context *ctx, AgentSpec spec)
Returns a newly constructed agent for the given module spec.
A generic mechanism to manually manage exceptions.
a very simple interface for solving translated resource exchanges
static const ResourceType kType
static Ptr Create(Agent *creator, double quantity, Composition::Ptr c, std::string package_name=Package::unpackaged_name(), double unit_value=kUnsetUnitValue)
Creates a new material resource that is "live" and tracked.
boost::shared_ptr< Material > Ptr
static Ptr & unpackaged()
static std::string unpackaged_name()
boost::shared_ptr< Product > Ptr
static const ResourceType kType
static Ptr Create(Agent *creator, double quantity, std::string quality, std::string package_name=Package::unpackaged_name(), double unit_value=kUnsetUnitValue)
Creates a new product that is "live" and tracked.
static const int kDefaultTimeout
Meta data and results of a query.
T GetVal(std::string field, int row=0)
Convenience method for retrieving a value from a specific row and named field (column).
Interface implemented by backends that support rudimentary querying.
virtual QueryResult Query(std::string table, std::vector< Cond > *conds)=0
Return a set of rows from the specificed table that match all given conditions.
Collects and manages output data generation for the cyclus core and agents during a simulation.
boost::uuids::uuid sim_id()
returns the unique id associated with this cyclus simulation.
void Flush()
Flushes all buffered Datum objects and flushes all registered backends.
Region(Context *ctx)
Default constructor for Region Class.
boost::shared_ptr< Resource > Ptr
Container for a static simulation-global parameters that both describe the simulation and affect its ...
static Product::Ptr BuildProduct(QueryableBackend *b, int resid)
Convenience function for reconstructing an untracked product object with the given resource state id ...
void Restart(QueryableBackend *b, boost::uuids::uuid sim_id, int t)
EXPERIMENTAL (might not work properly).
void Init(Recorder *r, QueryableBackend *b)
Initialize a simulation with data from b for simulation id in r.
static void SnapAgent(Agent *m)
Records a snapshot of the agent's current internal state into the simulation's output database.
static void Snapshot(Context *ctx)
Records a snapshot of the current state of the simulation being managed by ctx into the simulation's ...
static Material::Ptr BuildMaterial(QueryableBackend *b, int resid)
Convenience function for reconstructing an untracked material object with the given resource state id...
void Branch(QueryableBackend *b, boost::uuids::uuid prev_sim_id, int t, boost::uuids::uuid new_sim_id)
NOT IMPLEMENTED.
Controls simulation timestepping and inter-timestep phases.
static std::string unrestricted_name()
static Ptr & unrestricted()
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
std::map< Nuc, double > CompMap
a raw definition of nuclides and corresponding (dimensionless quantities).
std::vector< typename T::Ptr > ResCast(std::vector< Resource::Ptr > rs)
Casts a vector of Resources into a vector of a specific resource type T.
std::map< std::string, std::vector< Resource::Ptr > > Inventories
map<inventory_name, vector<resources_in_inventory> >.
void Warn(const std::string &msg)
Issue a warning with the approriate message, accoring to the current warning settings.
std::string name(int nuc)
double b(int nuc)
Computes the scattering length [cm] from the coherent and incoherent components.
const double pi
pi = 3.14159265359