commodconverter
 All Classes Namespaces Files Functions Variables Macros
src/commodconverter.cc
Go to the documentation of this file.
1 // commodconverter.cc
2 // Implements the CommodConverter class
3 #include "commodconverter.h"
4 
5 namespace commodconverter {
6 
7 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
8 CommodConverter::CommodConverter(cyclus::Context* ctx)
9  : cyclus::Facility(ctx) {
10  cyclus::Warn<cyclus::EXPERIMENTAL_WARNING>("the CommodConverter is experimental.");
11  };
12 
13 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
14 // pragmas
15 
16 #pragma cyclus def schema commodconverter::CommodConverter
17 
18 #pragma cyclus def annotations commodconverter::CommodConverter
19 
20 #pragma cyclus def initinv commodconverter::CommodConverter
21 
22 #pragma cyclus def snapshotinv commodconverter::CommodConverter
23 
24 #pragma cyclus def infiletodb commodconverter::CommodConverter
25 
26 #pragma cyclus def snapshot commodconverter::CommodConverter
27 
28 #pragma cyclus def clone commodconverter::CommodConverter
29 
30 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
31 void CommodConverter::InitFrom(CommodConverter* m) {
32 
33  #pragma cyclus impl initfromcopy commodconverter::CommodConverter
34 
35  cyclus::toolkit::CommodityProducer::Copy(m);
36 }
37 
38 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
39 void CommodConverter::InitFrom(cyclus::QueryableBackend* b){
40 
41  #pragma cyclus impl initfromdb commodconverter::CommodConverter
42 
43  using cyclus::toolkit::Commodity;
44  Commodity commod = Commodity(out_commod);
45  cyclus::toolkit::CommodityProducer::Add(commod);
46  cyclus::toolkit::CommodityProducer::SetCapacity(commod, capacity);
47  cyclus::toolkit::CommodityProducer::SetCost(commod, cost);
48 }
49 
50 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
51 void CommodConverter::EnterNotify() {
52  Facility::EnterNotify();
53 
54  using cyclus::toolkit::Commodity;
55  Commodity commod = Commodity(out_commod);
56  cyclus::toolkit::CommodityProducer::Add(commod);
57  cyclus::toolkit::CommodityProducer::SetCapacity(commod, capacity);
58  cyclus::toolkit::CommodityProducer::SetCost(commod, cost);
59 }
60 
61 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
62 std::string CommodConverter::str() {
63  std::stringstream ss;
64  std::string ans;
65  if (cyclus::toolkit::CommodityProducer::
66  Produces(cyclus::toolkit::Commodity(out_commod_()))){
67  ans = "yes";
68  } else {
69  ans = "no";
70  }
71  ss << cyclus::Facility::str();
72  ss << " has facility parameters {" << "\n"
73  << " Input Commodity = " << in_commod_() << ",\n"
74  << " Output Commodity = " << out_commod_() << ",\n"
75  << " Process Time = " << process_time_() << ",\n"
76  << " Capacity = " << capacity_() << ",\n"
77  << " commod producer members: " << " produces "
78  << out_commod << "?:" << ans
79  << " capacity: " << cyclus::toolkit::CommodityProducer::Capacity(out_commod_())
80  << " cost: " << cyclus::toolkit::CommodityProducer::Cost(out_commod_())
81  << "'}";
82  return ss.str();
83 }
84 
85 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
86 void CommodConverter::Tick() {
87  LOG(cyclus::LEV_INFO3, "ComCnv") << prototype() << " is ticking {";
88  LOG(cyclus::LEV_INFO3, "ComCnv") << "}";
89 }
90 
91 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
92 void CommodConverter::Tock() {
93  LOG(cyclus::LEV_INFO3, "ComCnv") << prototype() << " is tocking {";
94  while( processing[ready()].count() > 0 ) {
95  Convert_(); // place processing into stocks
96  }
97  BeginProcessing_(); // place unprocessed inventory into processing
98  LOG(cyclus::LEV_INFO3, "ComCnv") << "}";
99 }
100 
101 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
102 std::set<cyclus::RequestPortfolio<cyclus::Material>::Ptr>
103 CommodConverter::GetMatlRequests() {
104  using cyclus::CapacityConstraint;
105  using cyclus::Material;
106  using cyclus::RequestPortfolio;
107  using cyclus::Request;
108 
109  std::set<RequestPortfolio<Material>::Ptr> ports;
110 
111  RequestPortfolio<Material>::Ptr port(new RequestPortfolio<Material>());
112  Material::Ptr mat = Request_();
113  double amt = mat->quantity();
114 
115  if (amt > cyclus::eps()) {
116  //CapacityConstraint<Material> cc(amt);
117  //port->AddConstraint(cc);
118 
119  port->AddRequest(mat, this, in_commod_());
120 
121  ports.insert(port);
122  }
123 
124  return ports;
125 }
126 
127 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
128 void CommodConverter::AcceptMatlTrades(
129  const std::vector< std::pair<cyclus::Trade<cyclus::Material>,
130  cyclus::Material::Ptr> >& responses) {
131  std::vector< std::pair<cyclus::Trade<cyclus::Material>,
132  cyclus::Material::Ptr> >::const_iterator it;
133  for (it = responses.begin(); it != responses.end(); ++it) {
134  AddMat_(it->second);
135  }
136 }
137 
138 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
139 std::set<cyclus::BidPortfolio<cyclus::Material>::Ptr>
140 CommodConverter::GetMatlBids(cyclus::CommodMap<cyclus::Material>::type&
141  commod_requests) {
142  using cyclus::BidPortfolio;
143  using cyclus::Material;
144 
145  std::set<BidPortfolio<Material>::Ptr> ports;
146 
147  std::set<std::string>::const_iterator it;
148  BidPortfolio<Material>::Ptr port = GetBids_(commod_requests,
149  out_commod,
150  &stocks);
151  if (!port->bids().empty()) {
152  ports.insert(port);
153  }
154 
155  return ports;
156 
157 }
158 
159 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
160 void CommodConverter::GetMatlTrades(
161  const std::vector< cyclus::Trade<cyclus::Material> >& trades,
162  std::vector<std::pair<cyclus::Trade<cyclus::Material>,
163  cyclus::Material::Ptr> >& responses) {
164  using cyclus::Material;
165  using cyclus::Trade;
166 
167  // for each trade, respond
168  std::vector< Trade<Material> >::const_iterator it;
169  for (it = trades.begin(); it != trades.end(); ++it) {
170  std::string commodity = it->request->commodity();
171  double qty = it->amt;
172  // create a material pointer representing what you can offer
173  Material::Ptr response = TradeResponse_(qty, &stocks);
174 
175  responses.push_back(std::make_pair(*it, response));
176  LOG(cyclus::LEV_INFO5, "ComCnv") << prototype()
177  << " just received an order"
178  << " for " << it->amt
179  << " of " << commodity;
180  }
181 
182 }
183 
184 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
185 void CommodConverter::AddMat_(cyclus::Material::Ptr mat) {
186  // Here we do not check that the recipe matches the input recipe.
187 
188  LOG(cyclus::LEV_INFO5, "ComCnv") << prototype() << " is initially holding "
189  << inventory.quantity() << " total.";
190 
191  try {
192  inventory.Push(mat);
193  } catch (cyclus::Error& e) {
194  e.msg(Agent::InformErrorMsg(e.msg()));
195  throw e;
196  }
197 
198  LOG(cyclus::LEV_INFO5, "ComCnv") << prototype() << " added " << mat->quantity()
199  << " of " << in_commod_()
200  << " to its inventory, which is holding "
201  << inventory.quantity() << " total.";
202 
203 }
204 
205 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
206 cyclus::Material::Ptr CommodConverter::Request_() {
207  double qty = std::max(0.0, current_capacity());
208  LOG(cyclus::LEV_INFO5, "ComCnv") << prototype()
209  << " just requested"
210  << current_capacity()
211  << " of commodity: " << in_commod
212  << " with recipe: " << in_recipe;
213  return cyclus::Material::CreateUntracked(qty,
214  context()->GetRecipe(in_recipe));
215 }
216 
217 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
218 cyclus::BidPortfolio<cyclus::Material>::Ptr CommodConverter::GetBids_(
219  cyclus::CommodMap<cyclus::Material>::type& commod_requests,
220  std::string commod,
221  cyclus::toolkit::ResourceBuff* buffer) {
222  using cyclus::Bid;
223  using cyclus::BidPortfolio;
224  using cyclus::CapacityConstraint;
225  using cyclus::Composition;
226  using cyclus::Converter;
227  using cyclus::Material;
228  using cyclus::Request;
229  using cyclus::ResCast;
230  using cyclus::toolkit::ResourceBuff;
231 
232  BidPortfolio<Material>::Ptr port(new BidPortfolio<Material>());
233 
234  if (commod_requests.count(commod) > 0 && buffer->quantity() > 0) {
235  std::vector<Request<Material>*>& requests = commod_requests.at(commod);
236 
237  // get offer composition
238  Material::Ptr back = ResCast<Material>(buffer->Pop(ResourceBuff::BACK));
239  Composition::Ptr comp = back->comp();
240  buffer->Push(back);
241 
242  std::vector<Request<Material>*>::iterator it;
243  for (it = requests.begin(); it != requests.end(); ++it) {
244  Request<Material>* req = *it;
245  double qty = std::min(req->target()->quantity(), buffer->quantity());
246  Material::Ptr offer = Material::CreateUntracked(qty, comp);
247  port->AddBid(req, offer, this);
248  }
249 
250  //CapacityConstraint<Material> cc(buffer->quantity());
251  //port->AddConstraint(cc);
252  }
253 
254  return port;
255 }
256 
257 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
258 cyclus::Material::Ptr CommodConverter::TradeResponse_(
259  double qty,
260  cyclus::toolkit::ResourceBuff* buffer) {
261  using cyclus::Material;
262  using cyclus::ResCast;
263 
264  std::vector<Material::Ptr> manifest;
265  try {
266  // pop amount from inventory and blob it into one material
267  manifest = ResCast<Material>(buffer->PopQty(qty));
268  } catch(cyclus::Error& e) {
269  e.msg(Agent::InformErrorMsg(e.msg()));
270  throw e;
271  }
272 
273  Material::Ptr response = manifest[0];
274  crctx_.RemoveRsrc(response);
275  for (int i = 1; i < manifest.size(); i++) {
276  crctx_.RemoveRsrc(manifest[i]);
277  response->Absorb(manifest[i]);
278  }
279  return response;
280 }
281 
282 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
283 void CommodConverter::BeginProcessing_(){
284  LOG(cyclus::LEV_DEBUG2, "ComCnv") << "CommodConverter " << prototype()
285  << " added resources to processing";
286  if( inventory.count() > 0 ){
287  try {
288  processing[context()->time()].Push(inventory.Pop());
289  } catch (cyclus::Error& e) {
290  e.msg(Agent::InformErrorMsg(e.msg()));
291  throw e;
292  }
293  }
294 }
295 
296 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
297 void CommodConverter::Convert_(){
298  using cyclus::Material;
299  using cyclus::ResCast;
300  LOG(cyclus::LEV_DEBUG2, "ComCnv") << "CommodConverter " << prototype()
301  << " removed a resource from processing.";
302 
303  if ( processing.find(ready())->second.count() > 0 ) {
304  try {
305  // pop one material from processing
306  Material::Ptr mat = ResCast<Material>(processing.find(ready())->second.Pop());
307  // if an out_recipe was provided, transmute it
308  if( out_recipe != "" ){
309  mat->Transmute(context()->GetRecipe(out_recipe));
310  }
311  // change its commod
312  crctx_.UpdateRsrc(out_commod_(), mat);
313  // put it in the stocks
314  stocks.Push(mat);
315  } catch (cyclus::Error& e) {
316  e.msg(Agent::InformErrorMsg(e.msg()));
317  throw e;
318  }
319  }
320 }
321 
322 
323 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
324 extern "C" cyclus::Agent* ConstructCommodConverter(cyclus::Context* ctx) {
325  return new CommodConverter(ctx);
326 }
327 
328 } // namespace commodconverter
cyclus::Agent * ConstructCommodConverter(cyclus::Context *ctx)
CommodConverter(cyclus::Context *ctx)
Constructor for CommodConverter Class.