CYCAMORE
src/growth_region.cc
Go to the documentation of this file.
1 // Implements the GrowthRegion class
2 #include "growth_region.h"
3 
4 namespace cycamore {
5 
6 GrowthRegion::GrowthRegion(cyclus::Context* ctx) : cyclus::Region(ctx) { }
7 
9 
11  Demand& demand) {
12 
13 
14  cyclus::toolkit::PiecewiseFunctionFactory pff;
15  cyclus::toolkit::BasicFunctionFactory bff;
16  bool continuous = false;
17  int time;
18  std::string type, params;
19  Demand::const_iterator it;
20  for (it = demand.begin(); it != demand.end(); it++) {
21  time = it->first;
22  type = it->second.first;
23  params = it->second.second;
24  pff.AddFunction(bff.GetFunctionPtr(type, params), time, continuous);
25  continuous = true; // only the first entry is not continuous
26  }
27 
28  // register the commodity and demand
29  cyclus::toolkit::Commodity c(commod);
30  sdmanager_.RegisterCommodity(c, pff.GetFunctionPtr());
31 }
32 
34  cyclus::Region::EnterNotify();
35  std::set<cyclus::Agent*>::iterator ait;
36  for (ait = cyclus::Agent::children().begin();
37  ait != cyclus::Agent::children().end();
38  ++ait) {
39  Agent* a = *ait;
40  Register_(a);
41  }
42 
43  std::map<std::string, Demand>::iterator it;
44  for (it = commodity_demand.begin(); it != commodity_demand.end(); ++it) {
45  LOG(cyclus::LEV_INFO3, "greg") << "Adding demand for commodity "
46  << it->first;
47  AddCommodityDemand_(it->first, it->second);
48  }
49 }
50 
51 void GrowthRegion::DecomNotify(Agent* a) {
52  Unregister_(a);
53 }
54 
55 void GrowthRegion::Register_(cyclus::Agent* agent) {
56  using cyclus::toolkit::CommodityProducerManager;
57  using cyclus::toolkit::Builder;
58 
59  CommodityProducerManager* cpm_cast =
60  dynamic_cast<CommodityProducerManager*>(agent);
61  if (cpm_cast != NULL) {
62  LOG(cyclus::LEV_INFO3, "greg") << "Registering agent "
63  << agent->prototype() << agent->id()
64  << " as a commodity producer manager.";
65  sdmanager_.RegisterProducerManager(cpm_cast);
66  }
67 
68  Builder* b_cast = dynamic_cast<Builder*>(agent);
69  if (b_cast != NULL) {
70  LOG(cyclus::LEV_INFO3, "greg") << "Registering agent "
71  << agent->prototype() << agent->id()
72  << " as a builder.";
73  buildmanager_.Register(b_cast);
74  }
75 }
76 
77 void GrowthRegion::Unregister_(cyclus::Agent* agent) {
78  using cyclus::toolkit::CommodityProducerManager;
79  using cyclus::toolkit::Builder;
80 
81  CommodityProducerManager* cpm_cast =
82  dynamic_cast<CommodityProducerManager*>(agent);
83  if (cpm_cast != NULL)
84  sdmanager_.UnregisterProducerManager(cpm_cast);
85 
86  Builder* b_cast = dynamic_cast<Builder*>(agent);
87  if (b_cast != NULL)
88  buildmanager_.Unregister(b_cast);
89 }
90 
91 void GrowthRegion::Tick() {
92  double demand, supply, unmetdemand;
93  cyclus::toolkit::Commodity commod;
94  int time = context()->time();
95  std::map<std::string, Demand>::iterator it;
96  for (it = commodity_demand.begin(); it != commodity_demand.end(); ++it) {
97  commod = cyclus::toolkit::Commodity(it->first);
98  demand = sdmanager_.Demand(commod, time);
99  supply = sdmanager_.Supply(commod);
100  unmetdemand = demand - supply;
101 
102  LOG(cyclus::LEV_INFO3, "greg") << "GrowthRegion: " << prototype()
103  << " at time: " << time
104  << " has the following values regarding "
105  << " commodity: " << commod.name();
106  LOG(cyclus::LEV_INFO3, "greg") << " * demand = " << demand;
107  LOG(cyclus::LEV_INFO3, "greg") << " * supply = " << supply;
108  LOG(cyclus::LEV_INFO3, "greg") << " * unmet demand = " << unmetdemand;
109 
110  if (unmetdemand > 0) {
111  OrderBuilds(commod, unmetdemand);
112  }
113  }
114  cyclus::Region::Tick();
115 }
116 
117 void GrowthRegion::OrderBuilds(cyclus::toolkit::Commodity& commodity,
118  double unmetdemand) {
119  using std::vector;
120  vector<cyclus::toolkit::BuildOrder> orders =
121  buildmanager_.MakeBuildDecision(commodity, unmetdemand);
122 
123  LOG(cyclus::LEV_INFO3, "greg")
124  << "The build orders have been determined. "
125  << orders.size()
126  << " different type(s) of prototypes will be built.";
127 
128  cyclus::toolkit::BuildOrder* order;
129  cyclus::Institution* instcast;
130  cyclus::Agent* agentcast;
131  for (int i = 0; i < orders.size(); i++) {
132  order = &orders.at(i);
133  instcast = dynamic_cast<cyclus::Institution*>(order->builder);
134  agentcast = dynamic_cast<cyclus::Agent*>(order->producer);
135  if (!instcast || !agentcast) {
136  throw cyclus::CastError("growth_region has tried to incorrectly "
137  "cast an already known entity.");
138  }
139 
140  LOG(cyclus::LEV_INFO3, "greg")
141  << "A build order for " << order->number
142  << " prototype(s) of type "
143  << dynamic_cast<cyclus::Agent*>(agentcast)->prototype()
144  << " from builder " << instcast->prototype()
145  << " is being placed.";
146 
147  for (int j = 0; j < order->number; j++) {
148  LOG(cyclus::LEV_DEBUG2, "greg") << "Ordering build number: " << j + 1;
149  context()->SchedBuild(instcast, agentcast->prototype());
150  }
151  }
152 }
153 
154 extern "C" cyclus::Agent* ConstructGrowthRegion(cyclus::Context* ctx) {
155  return new GrowthRegion(ctx);
156 }
157 
158 } // namespace cycamore
virtual void Tick()
On each tick, the GrowthRegion queries its supply demand manager to determine if there exists some de...
virtual ~GrowthRegion()
The default destructor for the GrowthRegion.
GrowthRegion(cyclus::Context *ctx)
The default constructor for the GrowthRegion.
cyclus::Agent * ConstructGrowthRegion(cyclus::Context *ctx)
std::map< std::string, std::vector< std::pair< int, std::pair< std::string, std::string > > > > commodity_demand
void Unregister_(cyclus::Agent *agent)
unregister a child
void AddCommodityDemand_(std::string commod, Demand &demand)
add a demand for a commodity on which this region request that facilities be built ...
void OrderBuilds(cyclus::toolkit::Commodity &commodity, double unmetdemand)
orders builds given a commodity and an unmet demand for production capacity of that commodity ...
cyclus::toolkit::SupplyDemandManager sdmanager_
manager for Supply and demand
cycamore::GrowthRegion string
virtual void EnterNotify()
enter the simulation and register any children present
virtual void DecomNotify(Agent *m)
unregister a child
cyclus::toolkit::BuildingManager buildmanager_
manager for building things
std::vector< std::pair< int, std::pair< std::string, std::string > > > Demand
A container of (time, (demand type, demand parameters))
void Register_(cyclus::Agent *agent)
register a child