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