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