28 InitBase(b, r->
sim_id(), 0);
33 Warn<EXPERIMENTAL_WARNING>(
"restart capability is not finalized and fully" 34 " tested. Its behavior may change in future" 37 InitBase(b, sim_id, t);
45 int t, boost::uuids::uuid new_sim_id) {
46 throw Error(
"simulation branching feature not implemented");
52 std::vector<Cond> conds;
53 conds.push_back(
Cond(
"SimId",
"==", simid));
80 std::set<Agent*> mlist = ctx->agent_list_;
81 std::set<Agent*>::iterator it;
82 for (it = mlist.begin(); it != mlist.end(); ++it) {
93 ->AddVal(
"NextId", Agent::next_id_)
98 ->AddVal(
"NextId", ctx->trans_id_)
103 ->AddVal(
"NextId", Composition::next_id_)
108 ->AddVal(
"NextId", Resource::nextstate_id_)
113 ->AddVal(
"NextId", Resource::nextobj_id_)
118 ->AddVal(
"NextId", Product::next_qualid_)
125 m->Agent::Snapshot(
DbInit(m,
true));
131 Inventories::iterator it;
132 for (it = invs.begin(); it != invs.end(); ++it) {
134 std::vector<Resource::Ptr> inv = it->second;
135 for (
int i = 0; i < inv.size(); ++i) {
136 ctx->
NewDatum(
"AgentStateInventories")
138 ->AddVal(
"SimTime", ctx->
time())
139 ->AddVal(
"InventoryName", name)
140 ->
AddVal(
"ResourceId", inv[i]->state_id())
146 void SimInit::LoadInfo() {
148 int dur = qr.
GetVal<
int>(
"Duration");
149 int y0 = qr.
GetVal<
int>(
"InitialYear");
150 int m0 = qr.
GetVal<
int>(
"InitialMonth");
154 si_ =
SimInfo(dur, y0, m0, h, d);
156 si_.parent_sim = qr.
GetVal<boost::uuids::uuid>(
"ParentSimId");
158 qr = b_->
Query(
"TimeStepDur", NULL);
161 si_.dt = qr.
GetVal<
int>(
"DurationSecs");
163 qr = b_->
Query(
"Epsilon", NULL);
164 si_.eps = qr.
GetVal<
double>(
"GenericEpsilon");
165 si_.eps_rsrc = qr.GetVal<
double>(
"ResourceEpsilon");
167 qr = b_->
Query(
"InfoExplicitInv", NULL);
168 si_.explicit_inventory = qr.
GetVal<
bool>(
"RecordInventory");
169 si_.explicit_inventory_compact = qr.GetVal<
bool>(
"RecordInventoryCompact");
174 void SimInit::LoadRecipes() {
177 qr = b_->
Query(
"Recipes", NULL);
178 }
catch (std::exception err) {
182 for (
int i = 0; i < qr.
rows.size(); ++i) {
184 int stateid = qr.
GetVal<
int>(
"QualId", i);
194 map<string, double> commod_order;
197 for (
int i = 0; i < qr.
rows.size(); ++i) {
199 double order = qr.
GetVal<
double>(
"SolutionOrder", i);
200 commod_order[commod] = order;
202 }
catch (std::exception err) {
207 if (name ==
"greedy") {
211 throw ValueError(
"The name of the preconditioner was not recognized, " 212 "got '" + name +
"'.");
218 std::set<std::string> tables) {
223 string precon_name =
string(
"greedy");
225 string solver_info =
string(
"GreedySolverInfo");
226 if (0 < tables.count(solver_info)) {
228 if (qr.
rows.size() > 0) {
229 precon_name = qr.
GetVal<
string>(
"Preconditioner");
233 precon = LoadPreconditioner(precon_name);
234 if (precon == NULL) {
238 reinterpret_cast<GreedyPreconditioner*>(precon));
244 std::set<std::string> tables) {
251 if (0 < tables.count(solver_info)) {
253 timeout = qr.
GetVal<
double>(
"Timeout");
254 verbose = qr.
GetVal<
bool>(
"Verbose");
255 mps = qr.
GetVal<
bool>(
"Mps");
260 solver =
new ProgSolver(
"cbc", timeout, exclusive, verbose, mps);
263 throw cyclus::Error(
"Cyclus was not compiled with COIN support, cannot load solver.");
267 void SimInit::LoadSolverInfo() {
273 bool exclusive_orders;
277 set<string> tables = b_->
Tables();
278 string solver_info =
string(
"SolverInfo");
279 if (0 < tables.count(solver_info)) {
281 if (qr.
rows.size() > 0) {
282 solver_name = qr.
GetVal<
string>(
"Solver");
283 exclusive_orders = qr.
GetVal<
bool>(
"ExclusiveOrders");
287 if (solver_name ==
"greedy") {
288 solver = LoadGreedySolver(exclusive_orders, tables);
289 }
else if (solver_name ==
"coin-or") {
290 solver = LoadCoinSolver(exclusive_orders, tables);
292 throw ValueError(
"The name of the solver was not recognized, " 293 "got '" + solver_name +
"'.");
299 void SimInit::LoadPrototypes() {
301 for (
int i = 0; i < qr.
rows.size(); ++i) {
303 int agentid = qr.
GetVal<
int>(
"AgentId", i);
313 std::vector<Cond> conds;
314 conds.push_back(
Cond(
"AgentId",
"==", agentid));
319 m->Agent::InitFrom(&pi);
327 void SimInit::LoadInitialAgents() {
334 std::vector<Cond> conds;
335 conds.push_back(
Cond(
"EnterTime",
"<=", t_));
337 std::map<int, int> parentmap;
338 std::map<int, Agent*> unbuilt;
339 for (
int i = 0; i < qentry.
rows.size(); ++i) {
340 if (t_ > 0 && qentry.
GetVal<
int>(
"EnterTime", i) == t_) {
344 int id = qentry.
GetVal<
int>(
"AgentId", i);
345 std::vector<Cond> conds;
346 conds.push_back(
Cond(
"AgentId",
"==",
id));
347 conds.push_back(
Cond(
"ExitTime",
"<", t_));
350 if (qexit.
rows.size() != 0) {
353 }
catch (std::exception err) {}
363 m->prototype_ = proto;
365 m->enter_time_ = qentry.
GetVal<
int>(
"EnterTime", i);
367 parentmap[
id] = qentry.
GetVal<
int>(
"ParentId", i);
371 conds.push_back(
Cond(
"SimTime",
"==", t_));
374 m->Agent::InitFrom(&pi);
380 std::map<int, Agent*>::iterator it = unbuilt.begin();
381 std::vector<Agent*> enter_list;
382 while (unbuilt.size() > 0) {
384 Agent* m = it->second;
385 int parentid = parentmap[
id];
387 if (parentid == -1) {
392 enter_list.push_back(m);
393 }
else if (agents_.count(parentid) > 0) {
394 m->Connect(agents_[parentid]);
398 enter_list.push_back(m);
402 if (it == unbuilt.end()) {
403 it = unbuilt.begin();
409 for (
int i = 0; i < enter_list.size(); ++i) {
414 void SimInit::LoadInventories() {
415 std::map<int, Agent*>::iterator it;
416 for (it = agents_.begin(); it != agents_.end(); ++it) {
417 Agent* m = it->second;
418 std::vector<Cond> conds;
419 conds.push_back(
Cond(
"SimTime",
"==", t_));
420 conds.push_back(
Cond(
"AgentId",
"==", m->
id()));
423 qr = b_->
Query(
"AgentStateInventories", &conds);
424 }
catch (std::exception err) {
return;}
427 for (
int i = 0; i < qr.
rows.size(); ++i) {
429 int state_id = qr.
GetVal<
int>(
"ResourceId", i);
430 invs[inv_name].push_back(LoadResource(ctx_, b_, state_id));
436 void SimInit::LoadBuildSched() {
437 std::vector<Cond> conds;
438 conds.push_back(
Cond(
"BuildTime",
">", t_));
441 qr = b_->
Query(
"BuildSchedule", &conds);
442 }
catch (std::exception err) {
return;}
444 for (
int i = 0; i < qr.
rows.size(); ++i) {
445 int t = qr.
GetVal<
int>(
"BuildTime", i);
446 int parentid = qr.
GetVal<
int>(
"ParentId", i);
448 ctx_->
SchedBuild(agents_[parentid], proto, t);
452 void SimInit::LoadDecomSched() {
453 std::vector<Cond> conds;
454 conds.push_back(
Cond(
"DecomTime",
">=", t_));
457 qr = b_->
Query(
"DecomSchedule", &conds);
458 }
catch (std::exception err) {
return;}
460 for (
int i = 0; i < qr.
rows.size(); ++i) {
461 int t = qr.
GetVal<
int>(
"DecomTime", i);
462 int agentid = qr.
GetVal<
int>(
"AgentId", i);
467 void SimInit::LoadNextIds() {
468 std::vector<Cond> conds;
469 conds.push_back(
Cond(
"Time",
"==", t_));
471 for (
int i = 0; i < qr.
rows.size(); ++i) {
473 if (obj ==
"Agent") {
474 Agent::next_id_ = qr.
GetVal<
int>(
"NextId", i);
475 }
else if (obj ==
"Transaction") {
476 ctx_->trans_id_ = qr.
GetVal<
int>(
"NextId", i);
477 }
else if (obj ==
"Composition") {
478 Composition::next_id_ = qr.
GetVal<
int>(
"NextId", i);
479 }
else if (obj ==
"ResourceState") {
480 Resource::nextstate_id_ = qr.
GetVal<
int>(
"NextId", i);
481 }
else if (obj ==
"ResourceObj") {
482 Resource::nextobj_id_ = qr.
GetVal<
int>(
"NextId", i);
483 }
else if (obj ==
"Product") {
484 Product::next_qualid_ = qr.
GetVal<
int>(
"NextId", i);
486 throw IOError(
"Unexpected value in NextIds table: " + obj);
499 Material::Ptr m = ResCast<Material>(SimInit::LoadResource(&ctx, b, resid));
500 m->tracker_.DontTrack();
513 Product::Ptr p = ResCast<Product>(SimInit::LoadResource(&ctx, b, resid));
514 p->tracker_.DontTrack();
520 std::vector<Cond> conds;
521 conds.push_back(
Cond(
"ResourceId",
"==", state_id));
524 int obj_id = qr.
GetVal<
int>(
"ObjId");
528 r = LoadMaterial(ctx, b, state_id);
530 r = LoadProduct(ctx, b, state_id);
532 throw IOError(
"Invalid resource type in output database: " + type);
535 r->state_id_ = state_id;
542 std::vector<Cond> conds;
543 conds.push_back(
Cond(
"ResourceId",
"==", state_id));
545 int prev_decay = qr.
GetVal<
int>(
"PrevDecayTime");
549 conds.push_back(
Cond(
"ResourceId",
"==", state_id));
550 qr = b->
Query(
"Resources", &conds);
551 double qty = qr.
GetVal<
double>(
"Quantity");
552 int stateid = qr.
GetVal<
int>(
"QualId");
558 mat->prev_decay_time_ = prev_decay;
565 std::vector<Cond> conds;
566 conds.push_back(
Cond(
"QualId",
"==", stateid));
569 for (
int i = 0; i < qr.
rows.size(); ++i) {
570 int nucid = qr.
GetVal<
int>(
"NucId", i);
571 double mass_frac = qr.
GetVal<
double>(
"MassFrac", i);
572 cm[nucid] = mass_frac;
582 std::vector<Cond> conds;
583 conds.push_back(
Cond(
"ResourceId",
"==", state_id));
585 double qty = qr.
GetVal<
double>(
"Quantity");
586 int stateid = qr.
GetVal<
int>(
"QualId");
590 conds.push_back(
Cond(
"QualId",
"==", stateid));
591 qr = b->
Query(
"Products", &conds);
595 Product::qualids_[quality] = stateid;
virtual std::set< std::string > Tables()=0
Return a set of all table names currently in the database.
boost::uuids::uuid sim_id()
returns the unique id associated with this cyclus simulation.
boost::shared_ptr< Composition > Ptr
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...
double b(int nuc)
Computes the scattering length [cm] from the coherent and incoherent components.
void AddRecipe(std::string name, Composition::Ptr c)
Adds a composition recipe to a simulation-wide accessible list.
A generic mechanism to manually manage exceptions.
Meta data and results of a query.
The Region class is the abstract class/interface used by all region agents.
int branch_time
timestep at which simulation branching occurs if any
For values that are too big, too small, etc.
boost::shared_ptr< Material > Ptr
The ProgSolver provides the implementation for a mathematical programming solution to a resource exch...
Dummy * Clone()
Returns a newly created/allocated prototype that is an exact copy of this.
virtual const int id() const
The agent instance's unique ID within a simulation.
std::string name(int nuc)
static const ResourceType kType
boost::shared_ptr< Product > Ptr
std::map< Nuc, double > CompMap
a raw definition of nuclides and corresponding (dimensionless quantities).
T GetVal(std::string field, int row=0)
Convenience method for retrieving a value from a specific row and named field (column).
std::map< std::string, std::vector< Resource::Ptr > > Inventories
map<inventory_name, vector<resources_in_inventory> >.
virtual void InitInv(Inventories &inv)=0
Provides an agent's initial inventory of resources before a simulation begins.
void InitSim(SimInfo si)
Initializes the simulation time parameters.
virtual int time()
Returns the current simulation timestep.
static Ptr Create(Agent *creator, double quantity, Composition::Ptr c)
Creates a new material resource that is "live" and tracked.
for failed reading/writing to files, network connections, etc..
static Product::Ptr BuildProduct(QueryableBackend *b, int resid)
Convenience function for reconstructing an untracked product 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.
Wrapper class for QueryableBackends that injects a set of Cond's into every query before being execut...
virtual void EnterNotify()
Called to give the agent an opportunity to register for services (e.g.
static Ptr Create(Agent *creator, double quantity, std::string quality)
Creates a new product that is "live" and tracked.
boost::uuids::uuid parent_sim
id for the parent simulation if any
Context * context() const
Returns this agent's simulation context.
static const int kDefaultTimeout
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 AddPrototype(std::string name, Agent *m)
Adds a prototype to a simulation-wide accessible list, a prototype can not be added more than once...
A GreedyPreconditioner conditions an ExchangeGraph for a GreedySolver by ordering the RequestGroups a...
void Restart(QueryableBackend *b, boost::uuids::uuid sim_id, int t)
EXPERIMENTAL (might not work properly).
DbInit provides an interface for agents to record data to the output db that automatically injects th...
void Flush()
Flushes all buffered Datum objects and flushes all registered backends.
virtual void Snapshot(DbInit di)=0
Snapshots agent-internal state to the database via the DbInit var di.
static void Snapshot(Context *ctx)
Records a snapshot of the current state of the simulation being managed by ctx into the simulation's ...
Collects and manages output data generation for the cyclus core and agents during a simulation...
std::string parent_type
One of "init", "branch", "restart" indicating the relationship of this simulation to its parent simul...
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 ...
A simulation context provides access to necessary simulation-global functions and state...
boost::shared_ptr< Resource > Ptr
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.
The abstract base class used by all types of agents that live and interact in a simulation.
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
const double pi
pi = 3.14159265359
static Material::Ptr BuildMaterial(QueryableBackend *b, int resid)
Convenience function for reconstructing an untracked material object with the given resource state id...
static const ResourceType kType
std::vector< QueryRow > rows
ordered results of a query
virtual void InitFrom(QueryableBackend *b)
Intializes an agent's internal state from the database.
Represents a condition used to filter rows returned by a query.
a very simple interface for solving translated resource exchanges
void Record()
Record this datum to its Recorder.
static Agent * Make(Context *ctx, AgentSpec spec)
Returns a newly constructed agent for the given module spec.
ExchangeSolver * solver()
Returns the exchange solver associated with this context.
void Init(Recorder *r, QueryableBackend *b)
Initialize a simulation with data from b for simulation id in r.
Interface implemented by backends that support rudimentary querying.
static void SnapAgent(Agent *m)
Records a snapshot of the agent's current internal state into the simulation's output database...
Datum * NewDatum(std::string title)
See Recorder::NewDatum documentation.
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...
The GreedySolver provides the implementation for a "greedy" solution to a resource exchange graph...
void SchedDecom(Agent *m, int time=-1)
Schedules the given Agent to be decommissioned at the specified timestep t.
void DelAgent(Agent *m)
Destructs and cleans up m (and it's children recursively).
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. ...
Controls simulation timestepping and inter-timestep phases.