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