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