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)
7  : cyclus::Region(ctx),
8  latitude(0.0),
9  longitude(0.0),
11  #if !CYCLUS_HAS_COIN
12  throw cyclus::Error("Growth Region requires that Cyclus & Cycamore be compiled "
13  "with COIN support.");
14  #endif
15 }
16 
18 
20  Demand& demand) {
21 
22 
23  cyclus::toolkit::PiecewiseFunctionFactory pff;
24  cyclus::toolkit::BasicFunctionFactory bff;
25  bool continuous = false;
26  int time;
27  std::string type, params;
28  Demand::const_iterator it;
29  for (it = demand.begin(); it != demand.end(); it++) {
30  time = it->first;
31  type = it->second.first;
32  params = it->second.second;
33  pff.AddFunction(bff.GetFunctionPtr(type, params), time, continuous);
34  continuous = true; // only the first entry is not continuous
35  }
36 
37  // register the commodity and demand
38  cyclus::toolkit::Commodity c(commod);
39  sdmanager_.RegisterCommodity(c, pff.GetFunctionPtr());
40 }
41 
43  cyclus::Region::EnterNotify();
44  std::set<cyclus::Agent*>::iterator ait;
45  for (ait = cyclus::Agent::children().begin();
46  ait != cyclus::Agent::children().end();
47  ++ait) {
48  Agent* a = *ait;
49  Register_(a);
50  }
51 
52  std::map<std::string, Demand>::iterator it;
53  for (it = commodity_demand.begin(); it != commodity_demand.end(); ++it) {
54  LOG(cyclus::LEV_INFO3, "greg") << "Adding demand for commodity "
55  << it->first;
56  AddCommodityDemand_(it->first, it->second);
57  }
59 }
60 
61 void GrowthRegion::DecomNotify(Agent* a) {
62  Unregister_(a);
63 }
64 
65 void GrowthRegion::Register_(cyclus::Agent* agent) {
66  using cyclus::toolkit::CommodityProducerManager;
67  using cyclus::toolkit::Builder;
68 #if CYCLUS_HAS_COIN
69  CommodityProducerManager* cpm_cast =
70  dynamic_cast<CommodityProducerManager*>(agent);
71  if (cpm_cast != NULL) {
72  LOG(cyclus::LEV_INFO3, "greg") << "Registering agent "
73  << agent->prototype() << agent->id()
74  << " as a commodity producer manager.";
75  sdmanager_.RegisterProducerManager(cpm_cast);
76  }
77 
78  Builder* b_cast = dynamic_cast<Builder*>(agent);
79  if (b_cast != NULL) {
80  LOG(cyclus::LEV_INFO3, "greg") << "Registering agent "
81  << agent->prototype() << agent->id()
82  << " as a builder.";
83  buildmanager_.Register(b_cast);
84  }
85 #else
86  throw cyclus::Error("Growth Region requires that Cyclus & Cycamore be compiled "
87  "with COIN support.");
88 #endif
89 }
90 
91 void GrowthRegion::Unregister_(cyclus::Agent* agent) {
92  using cyclus::toolkit::CommodityProducerManager;
93  using cyclus::toolkit::Builder;
94 #if CYCLUS_HAS_COIN
95  CommodityProducerManager* cpm_cast =
96  dynamic_cast<CommodityProducerManager*>(agent);
97  if (cpm_cast != NULL)
98  sdmanager_.UnregisterProducerManager(cpm_cast);
99 
100  Builder* b_cast = dynamic_cast<Builder*>(agent);
101  if (b_cast != NULL)
102  buildmanager_.Unregister(b_cast);
103 #else
104  throw cyclus::Error("Growth Region requires that Cyclus & Cycamore be compiled "
105  "with COIN support.");
106 #endif
107 }
108 
109 void GrowthRegion::Tick() {
110  double demand, supply, unmetdemand;
111  cyclus::toolkit::Commodity commod;
112  int time = context()->time();
113  std::map<std::string, Demand>::iterator it;
114  for (it = commodity_demand.begin(); it != commodity_demand.end(); ++it) {
115  commod = cyclus::toolkit::Commodity(it->first);
116  demand = sdmanager_.Demand(commod, time);
117  supply = sdmanager_.Supply(commod);
118  unmetdemand = demand - supply;
119 
120  LOG(cyclus::LEV_INFO3, "greg") << "GrowthRegion: " << prototype()
121  << " at time: " << time
122  << " has the following values regarding "
123  << " commodity: " << commod.name();
124  LOG(cyclus::LEV_INFO3, "greg") << " * demand = " << demand;
125  LOG(cyclus::LEV_INFO3, "greg") << " * supply = " << supply;
126  LOG(cyclus::LEV_INFO3, "greg") << " * unmet demand = " << unmetdemand;
127 
128  if (unmetdemand > 0) {
129  OrderBuilds(commod, unmetdemand);
130  }
131  }
132  cyclus::Region::Tick();
133 }
134 
135 void GrowthRegion::OrderBuilds(cyclus::toolkit::Commodity& commodity,
136  double unmetdemand) {
137 #if CYCLUS_HAS_COIN
138  using std::vector;
139  vector<cyclus::toolkit::BuildOrder> orders =
140  buildmanager_.MakeBuildDecision(commodity, unmetdemand);
141 
142  LOG(cyclus::LEV_INFO3, "greg")
143  << "The build orders have been determined. "
144  << orders.size()
145  << " different type(s) of prototypes will be built.";
146 
147  cyclus::toolkit::BuildOrder* order;
148  cyclus::Institution* instcast;
149  cyclus::Agent* agentcast;
150  for (int i = 0; i < orders.size(); i++) {
151  order = &orders.at(i);
152  instcast = dynamic_cast<cyclus::Institution*>(order->builder);
153  agentcast = dynamic_cast<cyclus::Agent*>(order->producer);
154  if (!instcast || !agentcast) {
155  throw cyclus::CastError("growth_region has tried to incorrectly "
156  "cast an already known entity.");
157  }
158 
159  LOG(cyclus::LEV_INFO3, "greg")
160  << "A build order for " << order->number
161  << " prototype(s) of type "
162  << dynamic_cast<cyclus::Agent*>(agentcast)->prototype()
163  << " from builder " << instcast->prototype()
164  << " is being placed.";
165 
166  for (int j = 0; j < order->number; j++) {
167  LOG(cyclus::LEV_DEBUG2, "greg") << "Ordering build number: " << j + 1;
168  context()->SchedBuild(instcast, agentcast->prototype());
169  }
170  }
171 #else
172  throw cyclus::Error("Growth Region requires that Cyclus & Cycamore be compiled "
173  "with COIN support.");
174 #endif
175 }
176 
178  std::string specification = this->spec();
179  context()
180  ->NewDatum("AgentPosition")
181  ->AddVal("Spec", specification)
182  ->AddVal("Prototype", this->prototype())
183  ->AddVal("AgentId", id())
184  ->AddVal("Latitude", latitude)
185  ->AddVal("Longitude", longitude)
186  ->Record();
187 }
188 
189 extern "C" cyclus::Agent* ConstructGrowthRegion(cyclus::Context* ctx) {
190  return new GrowthRegion(ctx);
191 }
192 
193 } // 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
double longitude
void Unregister_(cyclus::Agent *agent)
unregister a child
void RecordPosition()
Records an agent&#39;s latitude and longitude to the output db.
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::Position coordinates
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