CYCAMORE
Loading...
Searching...
No Matches
src/source.cc
Go to the documentation of this file.
1#include "source.h"
2
3#include <sstream>
4#include <limits>
5
6#include <boost/lexical_cast.hpp>
7
8namespace cycamore {
9
10Source::Source(cyclus::Context* ctx)
11 : cyclus::Facility(ctx),
12 throughput(std::numeric_limits<double>::max()),
13 inventory_size(std::numeric_limits<double>::max()),
14 latitude(0.0),
15 longitude(0.0),
16 package(cyclus::Package::unpackaged_name()),
17 transport_unit(cyclus::TransportUnit::unrestricted_name()),
19
20Source::~Source() {}
21
22void Source::InitFrom(Source* m) {
23 #pragma cyclus impl initfromcopy cycamore::Source
24 cyclus::toolkit::CommodityProducer::Copy(m);
25}
26
27void Source::InitFrom(cyclus::QueryableBackend* b) {
28 #pragma cyclus impl initfromdb cycamore::Source
29 namespace tk = cyclus::toolkit;
30 tk::CommodityProducer::Add(tk::Commodity(outcommod),
31 tk::CommodInfo(throughput, throughput));
32}
33
34std::string Source::str() {
35 namespace tk = cyclus::toolkit;
36 std::stringstream ss;
37 std::string ans;
38 if (cyclus::toolkit::CommodityProducer::Produces(
39 cyclus::toolkit::Commodity(outcommod))) {
40 ans = "yes";
41 } else {
42 ans = "no";
43 }
44 ss << cyclus::Facility::str() << " supplies commodity '" << outcommod
45 << "' with recipe '" << outrecipe << "' at a throughput of "
46 << throughput << " kg per time step "
47 << " commod producer members: "
48 << " produces " << outcommod << "?: " << ans
49 << " throughput: " << cyclus::toolkit::CommodityProducer::Capacity(outcommod)
50 << " with package type: " << package
51 << " and transport unit type: " << transport_unit
52 << " cost: " << cyclus::toolkit::CommodityProducer::Cost(outcommod);
53 return ss.str();
54}
55
56void Source::EnterNotify() {
57 cyclus::Facility::EnterNotify();
59}
60
61void Source::Build(cyclus::Agent* parent) {
62 Facility::Build(parent);
63
64 using cyclus::CompMap;
65 using cyclus::Composition;
66 using cyclus::Material;
67
68 // create all source inventory and place into buf
69 cyclus::Material::Ptr all_inv;
70 Composition::Ptr blank_comp = Composition::CreateFromMass(CompMap());
71 all_inv = (outrecipe.empty() || context() == NULL) ? \
72 Material::Create(this, inventory_size, blank_comp) : \
73 Material::Create(this, inventory_size, context()->GetRecipe(outrecipe));
74 inventory.Push(all_inv);
75
76}
77
78std::set<cyclus::BidPortfolio<cyclus::Material>::Ptr> Source::GetMatlBids(
79 cyclus::CommodMap<cyclus::Material>::type& commod_requests) {
80 using cyclus::BidPortfolio;
81 using cyclus::CapacityConstraint;
82 using cyclus::Material;
83 using cyclus::Package;
84 using cyclus::Request;
85 using cyclus::TransportUnit;
86
87 double max_qty = std::min(throughput, inventory.quantity());
88 cyclus::toolkit::RecordTimeSeries<double>("supply"+outcommod, this,
89 max_qty);
90 LOG(cyclus::LEV_INFO3, "Source") << prototype() << " is bidding up to "
91 << max_qty << " kg of " << outcommod;
92 LOG(cyclus::LEV_INFO5, "Source") << "stats: " << str();
93
94 std::set<BidPortfolio<Material>::Ptr> ports;
95 if (max_qty < cyclus::eps()) {
96 return ports;
97 } else if (commod_requests.count(outcommod) == 0) {
98 return ports;
99 }
100
101 BidPortfolio<Material>::Ptr port(new BidPortfolio<Material>());
102 std::vector<Request<Material>*>& requests = commod_requests[outcommod];
103 std::vector<Request<Material>*>::iterator it;
104 for (it = requests.begin(); it != requests.end(); ++it) {
105 Request<Material>* req = *it;
106 Material::Ptr target = req->target();
107 double qty = std::min(target->quantity(), max_qty);
108
109 // calculate packaging
110 std::vector<double> bids = context()->GetPackage(package)->GetFillMass(qty);
111
112 // calculate transport units
113 int shippable_pkgs = context()->GetTransportUnit(transport_unit)
114 ->MaxShippablePackages(bids.size());
115 if (shippable_pkgs < bids.size()) {
116 bids.erase(bids.begin() + shippable_pkgs, bids.end());
117 }
118
119 std::vector<double>::iterator bit;
120 for (bit = bids.begin(); bit != bids.end(); ++bit) {
121 Material::Ptr m;
122 m = outrecipe.empty() ? \
123 Material::CreateUntracked(*bit, target->comp()) : \
124 Material::CreateUntracked(*bit, context()->GetRecipe(outrecipe));
125 port->AddBid(req, m, this);
126 }
127 }
128
129 CapacityConstraint<Material> cc(max_qty);
130 port->AddConstraint(cc);
131 ports.insert(port);
132 return ports;
133}
134
135void Source::GetMatlTrades(
136 const std::vector<cyclus::Trade<cyclus::Material> >& trades,
137 std::vector<std::pair<cyclus::Trade<cyclus::Material>,
138 cyclus::Material::Ptr> >& responses) {
139 using cyclus::Material;
140 using cyclus::Trade;
141
142 int shippable_trades = context()->GetTransportUnit(transport_unit)
143 ->MaxShippablePackages(trades.size());
144
145 std::vector<Trade<Material> >::const_iterator it;
146 for (it = trades.begin(); it != trades.end(); ++it) {
147 if (shippable_trades > 0) {
148 double qty = it->amt;
149
150 Material::Ptr m = inventory.Pop(qty);
151
152 std::vector<Material::Ptr> m_pkgd = m->Package<Material>(context()->GetPackage(package));
153
154 if (m->quantity() > cyclus::eps()) {
155 // If not all material is packaged successfully, return the excess
156 // amount to the inventory
157 inventory.Push(m);
158 }
159
160 Material::Ptr response;
161 if (m_pkgd.size() > 0) {
162 // Because we responded (in GetMatlBids) with individual package-sized
163 // bids, each packaged vector is guaranteed to have no more than one
164 // package in it. This single packaged resource is our response
165 response = m_pkgd[0];
166 shippable_trades -= 1;
167 } else {
168 // If packaging failed, respond with a zero (empty) material
169 response = Material::CreateUntracked(0, m->comp());
170 }
171
172 if (outrecipe.empty() && response->comp() != it->request->target()->comp()) {
173 response->Transmute(it->request->target()->comp());
174 }
175
176 responses.push_back(std::make_pair(*it, response));
177 LOG(cyclus::LEV_INFO5, "Source") << prototype() << " sent an order"
178 << " for " << response->quantity() << " of " << outcommod;
179 }
180 }
181}
182
183void Source::RecordPosition() {
184 std::string specification = this->spec();
185 context()
186 ->NewDatum("AgentPosition")
187 ->AddVal("Spec", specification)
188 ->AddVal("Prototype", this->prototype())
189 ->AddVal("AgentId", id())
190 ->AddVal("Latitude", latitude)
191 ->AddVal("Longitude", longitude)
192 ->Record();
193}
194
195extern "C" cyclus::Agent* ConstructSource(cyclus::Context* ctx) {
196 return new Source(ctx);
197}
198
199} // namespace cycamore
Source(cyclus::Context *ctx)
void RecordPosition()
Records an agent's latitude and longitude to the output db.
cyclus::Agent * ConstructSource(cyclus::Context *ctx)
cyclus::toolkit::Position coordinates