CYCAMORE
Loading...
Searching...
No Matches
build/cycamore/source.cc
Go to the documentation of this file.
1
2#line 1 "/cycamore/src/source.cc"
3#include "source.h"
4
5#include <sstream>
6#include <limits>
7
8#include <boost/lexical_cast.hpp>
9
10namespace cycamore {
11
12Source::Source(cyclus::Context* ctx)
13 : cyclus::Facility(ctx),
14 throughput(std::numeric_limits<double>::max()),
15 inventory_size(std::numeric_limits<double>::max()),
16 latitude(0.0),
17 longitude(0.0),
18 package(cyclus::Package::unpackaged_name()),
19 transport_unit(cyclus::TransportUnit::unrestricted_name()),
21
23
24void Source::InitFrom(Source* m) { cyclus::Facility::InitFrom(m);
25 int rawcycpp_shape_outcommod[1] = {-1};
26 cycpp_shape_outcommod = std::vector<int>(rawcycpp_shape_outcommod, rawcycpp_shape_outcommod + 1);
27 int rawcycpp_shape_outrecipe[1] = {-1};
28 cycpp_shape_outrecipe = std::vector<int>(rawcycpp_shape_outrecipe, rawcycpp_shape_outrecipe + 1);
29 int rawcycpp_shape_inventory_size[1] = {-1};
30 cycpp_shape_inventory_size = std::vector<int>(rawcycpp_shape_inventory_size, rawcycpp_shape_inventory_size + 1);
31 int rawcycpp_shape_throughput[1] = {-1};
32 cycpp_shape_throughput = std::vector<int>(rawcycpp_shape_throughput, rawcycpp_shape_throughput + 1);
33 int rawcycpp_shape_package[1] = {-1};
34 cycpp_shape_package = std::vector<int>(rawcycpp_shape_package, rawcycpp_shape_package + 1);
35 int rawcycpp_shape_transport_unit[1] = {-1};
36 cycpp_shape_transport_unit = std::vector<int>(rawcycpp_shape_transport_unit, rawcycpp_shape_transport_unit + 1);
37 int rawcycpp_shape_latitude[1] = {-1};
38 cycpp_shape_latitude = std::vector<int>(rawcycpp_shape_latitude, rawcycpp_shape_latitude + 1);
39 int rawcycpp_shape_longitude[1] = {-1};
40 cycpp_shape_longitude = std::vector<int>(rawcycpp_shape_longitude, rawcycpp_shape_longitude + 1);
41 int rawcycpp_shape_inventory[2] = {-1, -1};
42 cycpp_shape_inventory = std::vector<int>(rawcycpp_shape_inventory, rawcycpp_shape_inventory + 2);
43 outcommod = m->outcommod;
44 outrecipe = m->outrecipe;
45 inventory_size = m->inventory_size;
46 throughput = m->throughput;
47 package = m->package;
48 transport_unit = m->transport_unit;
49 latitude = m->latitude;
50 longitude = m->longitude;
51 inventory.capacity(m->inventory.capacity());
52#line 24 "/cycamore/src/source.cc"
53 cyclus::toolkit::CommodityProducer::Copy(m);
54}
55
56void Source::InitFrom(cyclus::QueryableBackend* b) { cyclus::Facility::InitFrom(b);
57 int rawcycpp_shape_outcommod[1] = {-1};
58 cycpp_shape_outcommod = std::vector<int>(rawcycpp_shape_outcommod, rawcycpp_shape_outcommod + 1);
59 int rawcycpp_shape_outrecipe[1] = {-1};
60 cycpp_shape_outrecipe = std::vector<int>(rawcycpp_shape_outrecipe, rawcycpp_shape_outrecipe + 1);
61 int rawcycpp_shape_inventory_size[1] = {-1};
62 cycpp_shape_inventory_size = std::vector<int>(rawcycpp_shape_inventory_size, rawcycpp_shape_inventory_size + 1);
63 int rawcycpp_shape_throughput[1] = {-1};
64 cycpp_shape_throughput = std::vector<int>(rawcycpp_shape_throughput, rawcycpp_shape_throughput + 1);
65 int rawcycpp_shape_package[1] = {-1};
66 cycpp_shape_package = std::vector<int>(rawcycpp_shape_package, rawcycpp_shape_package + 1);
67 int rawcycpp_shape_transport_unit[1] = {-1};
68 cycpp_shape_transport_unit = std::vector<int>(rawcycpp_shape_transport_unit, rawcycpp_shape_transport_unit + 1);
69 int rawcycpp_shape_latitude[1] = {-1};
70 cycpp_shape_latitude = std::vector<int>(rawcycpp_shape_latitude, rawcycpp_shape_latitude + 1);
71 int rawcycpp_shape_longitude[1] = {-1};
72 cycpp_shape_longitude = std::vector<int>(rawcycpp_shape_longitude, rawcycpp_shape_longitude + 1);
73 int rawcycpp_shape_inventory[2] = {-1, -1};
74 cycpp_shape_inventory = std::vector<int>(rawcycpp_shape_inventory, rawcycpp_shape_inventory + 2);
75 cyclus::QueryResult qr = b->Query("Info", NULL);
76 outcommod = qr.GetVal<std::string>("outcommod");
77 outrecipe = qr.GetVal<std::string>("outrecipe");
78 inventory_size = qr.GetVal<double>("inventory_size");
79 throughput = qr.GetVal<double>("throughput");
80 package = qr.GetVal<std::string>("package");
81 transport_unit = qr.GetVal<std::string>("transport_unit");
82 latitude = qr.GetVal<double>("latitude");
83 longitude = qr.GetVal<double>("longitude");
84 inventory.capacity(1e+300);
85#line 29 "/cycamore/src/source.cc"
86 namespace tk = cyclus::toolkit;
87 tk::CommodityProducer::Add(tk::Commodity(outcommod),
88 tk::CommodInfo(throughput, throughput));
89}
90
91std::string Source::str() {
92 namespace tk = cyclus::toolkit;
93 std::stringstream ss;
94 std::string ans;
95 if (cyclus::toolkit::CommodityProducer::Produces(
96 cyclus::toolkit::Commodity(outcommod))) {
97 ans = "yes";
98 } else {
99 ans = "no";
100 }
101 ss << cyclus::Facility::str() << " supplies commodity '" << outcommod
102 << "' with recipe '" << outrecipe << "' at a throughput of "
103 << throughput << " kg per time step "
104 << " commod producer members: "
105 << " produces " << outcommod << "?: " << ans
106 << " throughput: " << cyclus::toolkit::CommodityProducer::Capacity(outcommod)
107 << " with package type: " << package
108 << " and transport unit type: " << transport_unit
109 << " cost: " << cyclus::toolkit::CommodityProducer::Cost(outcommod);
110 return ss.str();
111}
112
114 cyclus::Facility::EnterNotify();
116}
117
118void Source::Build(cyclus::Agent* parent) {
119 Facility::Build(parent);
120
121 using cyclus::CompMap;
122 using cyclus::Composition;
123 using cyclus::Material;
124
125 // create all source inventory and place into buf
126 cyclus::Material::Ptr all_inv;
127 Composition::Ptr blank_comp = Composition::CreateFromMass(CompMap());
128 all_inv = (outrecipe.empty() || context() == NULL) ? Material::Create(this, inventory_size, blank_comp) : Material::Create(this, inventory_size, context()->GetRecipe(outrecipe));
129#line 74 "/cycamore/src/source.cc"
130 inventory.Push(all_inv);
131
132}
133
134std::set<cyclus::BidPortfolio<cyclus::Material>::Ptr> Source::GetMatlBids(
135 cyclus::CommodMap<cyclus::Material>::type& commod_requests) {
136 using cyclus::BidPortfolio;
137 using cyclus::CapacityConstraint;
138 using cyclus::Material;
139 using cyclus::Package;
140 using cyclus::Request;
141 using cyclus::TransportUnit;
142
143 double max_qty = std::min(throughput, inventory.quantity());
144 cyclus::toolkit::RecordTimeSeries<double>("supply"+outcommod, this,
145 max_qty);
146 LOG(cyclus::LEV_INFO3, "Source") << prototype() << " is bidding up to "
147 << max_qty << " kg of " << outcommod;
148 LOG(cyclus::LEV_INFO5, "Source") << "stats: " << str();
149
150 std::set<BidPortfolio<Material>::Ptr> ports;
151 if (max_qty < cyclus::eps()) {
152 return ports;
153 } else if (commod_requests.count(outcommod) == 0) {
154 return ports;
155 }
156
157 BidPortfolio<Material>::Ptr port(new BidPortfolio<Material>());
158 std::vector<Request<Material>*>& requests = commod_requests[outcommod];
159 std::vector<Request<Material>*>::iterator it;
160 for (it = requests.begin(); it != requests.end(); ++it) {
161 Request<Material>* req = *it;
162 Material::Ptr target = req->target();
163 double qty = std::min(target->quantity(), max_qty);
164
165 // calculate packaging
166 std::vector<double> bids = context()->GetPackage(package)->GetFillMass(qty);
167
168 // calculate transport units
169 int shippable_pkgs = context()->GetTransportUnit(transport_unit)
170 ->MaxShippablePackages(bids.size());
171 if (shippable_pkgs < bids.size()) {
172 bids.erase(bids.begin() + shippable_pkgs, bids.end());
173 }
174
175 std::vector<double>::iterator bit;
176 for (bit = bids.begin(); bit != bids.end(); ++bit) {
177 Material::Ptr m;
178 m = outrecipe.empty() ? Material::CreateUntracked(*bit, target->comp()) : Material::CreateUntracked(*bit, context()->GetRecipe(outrecipe));
179#line 125 "/cycamore/src/source.cc"
180 port->AddBid(req, m, this);
181 }
182 }
183
184 CapacityConstraint<Material> cc(max_qty);
185 port->AddConstraint(cc);
186 ports.insert(port);
187 return ports;
188}
189
191 const std::vector<cyclus::Trade<cyclus::Material> >& trades,
192 std::vector<std::pair<cyclus::Trade<cyclus::Material>,
193 cyclus::Material::Ptr> >& responses) {
194 using cyclus::Material;
195 using cyclus::Trade;
196
197 int shippable_trades = context()->GetTransportUnit(transport_unit)
198 ->MaxShippablePackages(trades.size());
199
200 std::vector<Trade<Material> >::const_iterator it;
201 for (it = trades.begin(); it != trades.end(); ++it) {
202 if (shippable_trades > 0) {
203 double qty = it->amt;
204
205 Material::Ptr m = inventory.Pop(qty);
206
207 std::vector<Material::Ptr> m_pkgd = m->Package<Material>(context()->GetPackage(package));
208
209 if (m->quantity() > cyclus::eps()) {
210 // If not all material is packaged successfully, return the excess
211 // amount to the inventory
212 inventory.Push(m);
213 }
214
215 Material::Ptr response;
216 if (m_pkgd.size() > 0) {
217 // Because we responded (in GetMatlBids) with individual package-sized
218 // bids, each packaged vector is guaranteed to have no more than one
219 // package in it. This single packaged resource is our response
220 response = m_pkgd[0];
221 shippable_trades -= 1;
222 } else {
223 // If packaging failed, respond with a zero (empty) material
224 response = Material::CreateUntracked(0, m->comp());
225 }
226
227 if (outrecipe.empty() && response->comp() != it->request->target()->comp()) {
228 response->Transmute(it->request->target()->comp());
229 }
230
231 responses.push_back(std::make_pair(*it, response));
232 LOG(cyclus::LEV_INFO5, "Source") << prototype() << " sent an order"
233 << " for " << response->quantity() << " of " << outcommod;
234 }
235 }
236}
237
239 std::string specification = this->spec();
240 context()
241 ->NewDatum("AgentPosition")
242 ->AddVal("Spec", specification)
243 ->AddVal("Prototype", this->prototype())
244 ->AddVal("AgentId", id())
245 ->AddVal("Latitude", latitude)
246 ->AddVal("Longitude", longitude)
247 ->Record();
248}
249
250extern "C" cyclus::Agent* ConstructSource(cyclus::Context* ctx) {
251 return new Source(ctx);
252}
253
254} // namespace cycamore
This facility acts as a source of material with a fixed throughput (per time step) capacity and a lif...
std::vector< int > cycpp_shape_longitude
std::vector< int > cycpp_shape_latitude
virtual void Build(cyclus::Agent *parent)
— Facility Members — perform module-specific tasks when entering the simulation
std::vector< int > cycpp_shape_inventory_size
std::vector< int > cycpp_shape_package
std::vector< int > cycpp_shape_inventory
std::vector< int > cycpp_shape_transport_unit
virtual std::set< cyclus::BidPortfolio< cyclus::Material >::Ptr > GetMatlBids(cyclus::CommodMap< cyclus::Material >::type &commod_requests)
virtual void GetMatlTrades(const std::vector< cyclus::Trade< cyclus::Material > > &trades, std::vector< std::pair< cyclus::Trade< cyclus::Material >, cyclus::Material::Ptr > > &responses)
std::vector< int > cycpp_shape_throughput
Source(cyclus::Context *ctx)
cyclus::toolkit::ResBuf< cyclus::Material > inventory
virtual void InitFrom(Source *m)
std::vector< int > cycpp_shape_outcommod
virtual std::string str()
std::vector< int > cycpp_shape_outrecipe
cyclus::Agent * ConstructSource(cyclus::Context *ctx)
cyclus::toolkit::Position coordinates