commodconverter
 All Classes Namespaces Files Functions Variables Macros
build/CommodConverter/commodconverter.cc
Go to the documentation of this file.
1 #line 1 "/Users/khuff/repos/cyclus/commodconverter/src/commodconverter.cc"
2 // commodconverter.cc
3 // Implements the CommodConverter class
4 #include "commodconverter.h"
5 
6 namespace commodconverter {
7 
8 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9 CommodConverter::CommodConverter(cyclus::Context* ctx)
10  : cyclus::Facility(ctx) {
11  cyclus::Warn<cyclus::EXPERIMENTAL_WARNING>("the CommodConverter is experimental.");
12  };
13 
14 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
15 // pragmas
16 
17 std::string CommodConverter::schema() {
18  return ""
19  "<interleave>\n"
20  "<element name=\"in_commod\">\n"
21  " <data type=\"string\" />\n"
22  "</element>\n"
23  "<element name=\"out_commod\">\n"
24  " <data type=\"string\" />\n"
25  "</element>\n"
26  "<optional>\n"
27  " <element name=\"in_recipe\">\n"
28  " <data type=\"string\" />\n"
29  " </element>\n"
30  "</optional>\n"
31  "<optional>\n"
32  " <element name=\"out_recipe\">\n"
33  " <data type=\"string\" />\n"
34  " </element>\n"
35  "</optional>\n"
36  "<optional>\n"
37  " <element name=\"process_time\">\n"
38  " <data type=\"int\" />\n"
39  " </element>\n"
40  "</optional>\n"
41  "<optional>\n"
42  " <element name=\"max_inv_size\">\n"
43  " <data type=\"double\" />\n"
44  " </element>\n"
45  "</optional>\n"
46  "</interleave>\n"
47  ;
48 };
49 #line 17 "/Users/khuff/repos/cyclus/commodconverter/src/commodconverter.cc"
50 
52  Json::Value root;
53  Json::Reader reader;
54  bool parsed_ok = reader.parse(
55  "{\"vars\":{\"in_commod\":{\"doc\":\"commodity accepted by"
56  " this "
57  "facility\",\"type\":\"std::string\",\"tooltip\":\"input co"
58  "mmodity\",\"index\":0},\"out_commod\":{\"doc\":\"commodity"
59  " produced by this "
60  "facility\",\"type\":\"std::string\",\"tooltip\":\"output c"
61  "ommodity\",\"index\":1},\"in_recipe\":{\"default\":\"\",\"do"
62  "c\":\"recipe accepted by this "
63  "facility\",\"type\":\"std::string\",\"tooltip\":\"input re"
64  "cipe\",\"index\":2},\"out_recipe\":{\"default\":\"\",\"doc\":"
65  "\"recipe to be generated by this "
66  "facility\",\"type\":\"std::string\",\"tooltip\":\"output r"
67  "ecipe\",\"index\":3},\"process_time\":{\"default\":0,\"doc"
68  "\":\"the time it takes to convert a received "
69  "commodity "
70  "(timesteps).\",\"type\":\"int\",\"tooltip\":\"process time"
71  " (timesteps)\",\"index\":4},\"max_inv_size\":{\"default\""
72  ":1e+299,\"doc\":\"the amount of material that can be "
73  "in storage at one time "
74  "(kg).\",\"type\":\"double\",\"tooltip\":\"maximum "
75  "inventory size (kg)\",\"index\":5}},\"doc\":\"A "
76  "commodconverter facility converts from one "
77  "commodity to another, with an optional delay.\"}", root);
78  if (!parsed_ok) {
79  throw cyclus::ValueError("failed to parse annotations for commodconverter::CommodConverter.");
80  }
81  return root;
82 };
83 #line 19 "/Users/khuff/repos/cyclus/commodconverter/src/commodconverter.cc"
84 
85 void CommodConverter::InitInv(cyclus::Inventories& inv) {
86 };
87 #line 21 "/Users/khuff/repos/cyclus/commodconverter/src/commodconverter.cc"
88 
89 cyclus::Inventories CommodConverter::SnapshotInv() {
90  cyclus::Inventories invs;
91  return invs;
92 };
93 #line 23 "/Users/khuff/repos/cyclus/commodconverter/src/commodconverter.cc"
94 
95 void CommodConverter::InfileToDb(cyclus::InfileTree* tree, cyclus::DbInit di) {
96  cyclus::Facility::InfileToDb(tree, di);
97  tree = tree->SubTree("config/*");
98  cyclus::InfileTree* sub;
99  int i;
100  int n;
101  in_commod = cyclus::Query<std::string>(tree, "in_commod");
102  out_commod = cyclus::Query<std::string>(tree, "out_commod");
103  in_recipe = cyclus::OptionalQuery<std::string>(tree, "in_recipe", "");
104  out_recipe = cyclus::OptionalQuery<std::string>(tree, "out_recipe", "");
105  process_time = cyclus::OptionalQuery<int>(tree, "process_time", 0);
106  max_inv_size = cyclus::OptionalQuery<double>(tree, "max_inv_size", 1e+299);
107  di.NewDatum("Info")
108  ->AddVal("in_commod", in_commod)
109  ->AddVal("out_commod", out_commod)
110  ->AddVal("in_recipe", in_recipe)
111  ->AddVal("out_recipe", out_recipe)
112  ->AddVal("process_time", process_time)
113  ->AddVal("max_inv_size", max_inv_size)
114  ->Record();
115 };
116 #line 25 "/Users/khuff/repos/cyclus/commodconverter/src/commodconverter.cc"
117 
118 void CommodConverter::Snapshot(cyclus::DbInit di) {
119  di.NewDatum("Info")
120  ->AddVal("in_commod", in_commod)
121  ->AddVal("out_commod", out_commod)
122  ->AddVal("in_recipe", in_recipe)
123  ->AddVal("out_recipe", out_recipe)
124  ->AddVal("process_time", process_time)
125  ->AddVal("max_inv_size", max_inv_size)
126  ->Record();
127 };
128 #line 27 "/Users/khuff/repos/cyclus/commodconverter/src/commodconverter.cc"
129 
130 cyclus::Agent* CommodConverter::Clone() {
132  m->InitFrom(this);
133  return m;
134 };
135 #line 29 "/Users/khuff/repos/cyclus/commodconverter/src/commodconverter.cc"
136 
137 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
138 void CommodConverter::InitFrom(CommodConverter* m) { cyclus::Facility::InitFrom(m);
139  in_commod = m->in_commod;
140  out_commod = m->out_commod;
141  in_recipe = m->in_recipe;
142  out_recipe = m->out_recipe;
145 #line 34 "/Users/khuff/repos/cyclus/commodconverter/src/commodconverter.cc"
146 
147  cyclus::toolkit::CommodityProducer::Copy(m);
148 }
149 
150 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
151 void CommodConverter::InitFrom(cyclus::QueryableBackend* b){ cyclus::Facility::InitFrom(b);
152  cyclus::QueryResult qr = b->Query("Info", NULL);
153  in_commod = qr.GetVal<std::string>("in_commod");
154  out_commod = qr.GetVal<std::string>("out_commod");
155  in_recipe = qr.GetVal<std::string>("in_recipe");
156  out_recipe = qr.GetVal<std::string>("out_recipe");
157  process_time = qr.GetVal<int>("process_time");
158  max_inv_size = qr.GetVal<double>("max_inv_size");
159 #line 42 "/Users/khuff/repos/cyclus/commodconverter/src/commodconverter.cc"
160 
161  using cyclus::toolkit::Commodity;
162  Commodity commod = Commodity(out_commod);
163  cyclus::toolkit::CommodityProducer::Add(commod);
164  cyclus::toolkit::CommodityProducer::SetCapacity(commod, capacity);
165  cyclus::toolkit::CommodityProducer::SetCost(commod, cost);
166 }
167 
168 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
170  Facility::EnterNotify();
171 
172  using cyclus::toolkit::Commodity;
173  Commodity commod = Commodity(out_commod);
174  cyclus::toolkit::CommodityProducer::Add(commod);
175  cyclus::toolkit::CommodityProducer::SetCapacity(commod, capacity);
176  cyclus::toolkit::CommodityProducer::SetCost(commod, cost);
177 }
178 
179 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
180 std::string CommodConverter::str() {
181  std::stringstream ss;
182  std::string ans;
183  if (cyclus::toolkit::CommodityProducer::
184  Produces(cyclus::toolkit::Commodity(out_commod_()))){
185  ans = "yes";
186  } else {
187  ans = "no";
188  }
189  ss << cyclus::Facility::str();
190  ss << " has facility parameters {" << "\n"
191  << " Input Commodity = " << in_commod_() << ",\n"
192  << " Output Commodity = " << out_commod_() << ",\n"
193  << " Process Time = " << process_time_() << ",\n"
194  << " Capacity = " << capacity_() << ",\n"
195  << " commod producer members: " << " produces "
196  << out_commod << "?:" << ans
197  << " capacity: " << cyclus::toolkit::CommodityProducer::Capacity(out_commod_())
198  << " cost: " << cyclus::toolkit::CommodityProducer::Cost(out_commod_())
199  << "'}";
200  return ss.str();
201 }
202 
203 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
205  LOG(cyclus::LEV_INFO3, "ComCnv") << prototype() << " is ticking {";
206  LOG(cyclus::LEV_INFO3, "ComCnv") << "}";
207 }
208 
209 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
211  LOG(cyclus::LEV_INFO3, "ComCnv") << prototype() << " is tocking {";
212  while( processing[ready()].count() > 0 ) {
213  Convert_(); // place processing into stocks
214  }
215  BeginProcessing_(); // place unprocessed inventory into processing
216  LOG(cyclus::LEV_INFO3, "ComCnv") << "}";
217 }
218 
219 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
220 std::set<cyclus::RequestPortfolio<cyclus::Material>::Ptr>
222  using cyclus::CapacityConstraint;
223  using cyclus::Material;
224  using cyclus::RequestPortfolio;
225  using cyclus::Request;
226 
227  std::set<RequestPortfolio<Material>::Ptr> ports;
228 
229  RequestPortfolio<Material>::Ptr port(new RequestPortfolio<Material>());
230  Material::Ptr mat = Request_();
231  double amt = mat->quantity();
232 
233  if (amt > cyclus::eps()) {
234  //CapacityConstraint<Material> cc(amt);
235  //port->AddConstraint(cc);
236 
237  port->AddRequest(mat, this, in_commod_());
238 
239  ports.insert(port);
240  }
241 
242  return ports;
243 }
244 
245 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
247  const std::vector< std::pair<cyclus::Trade<cyclus::Material>,
248  cyclus::Material::Ptr> >& responses) {
249  std::vector< std::pair<cyclus::Trade<cyclus::Material>,
250  cyclus::Material::Ptr> >::const_iterator it;
251  for (it = responses.begin(); it != responses.end(); ++it) {
252  AddMat_(it->second);
253  }
254 }
255 
256 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
257 std::set<cyclus::BidPortfolio<cyclus::Material>::Ptr>
258 CommodConverter::GetMatlBids(cyclus::CommodMap<cyclus::Material>::type&
259  commod_requests) {
260  using cyclus::BidPortfolio;
261  using cyclus::Material;
262 
263  std::set<BidPortfolio<Material>::Ptr> ports;
264 
265  std::set<std::string>::const_iterator it;
266  BidPortfolio<Material>::Ptr port = GetBids_(commod_requests,
267  out_commod,
268  &stocks);
269  if (!port->bids().empty()) {
270  ports.insert(port);
271  }
272 
273  return ports;
274 
275 }
276 
277 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
279  const std::vector< cyclus::Trade<cyclus::Material> >& trades,
280  std::vector<std::pair<cyclus::Trade<cyclus::Material>,
281  cyclus::Material::Ptr> >& responses) {
282  using cyclus::Material;
283  using cyclus::Trade;
284 
285  // for each trade, respond
286  std::vector< Trade<Material> >::const_iterator it;
287  for (it = trades.begin(); it != trades.end(); ++it) {
288  std::string commodity = it->request->commodity();
289  double qty = it->amt;
290  // create a material pointer representing what you can offer
291  Material::Ptr response = TradeResponse_(qty, &stocks);
292 
293  responses.push_back(std::make_pair(*it, response));
294  LOG(cyclus::LEV_INFO5, "ComCnv") << prototype()
295  << " just received an order"
296  << " for " << it->amt
297  << " of " << commodity;
298  }
299 
300 }
301 
302 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
303 void CommodConverter::AddMat_(cyclus::Material::Ptr mat) {
304  // Here we do not check that the recipe matches the input recipe.
305 
306  LOG(cyclus::LEV_INFO5, "ComCnv") << prototype() << " is initially holding "
307  << inventory.quantity() << " total.";
308 
309  try {
310  inventory.Push(mat);
311  } catch (cyclus::Error& e) {
312  e.msg(Agent::InformErrorMsg(e.msg()));
313  throw e;
314  }
315 
316  LOG(cyclus::LEV_INFO5, "ComCnv") << prototype() << " added " << mat->quantity()
317  << " of " << in_commod_()
318  << " to its inventory, which is holding "
319  << inventory.quantity() << " total.";
320 
321 }
322 
323 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
324 cyclus::Material::Ptr CommodConverter::Request_() {
325  double qty = std::max(0.0, current_capacity());
326  LOG(cyclus::LEV_INFO5, "ComCnv") << prototype()
327  << " just requested"
328  << current_capacity()
329  << " of commodity: " << in_commod
330  << " with recipe: " << in_recipe;
331  return cyclus::Material::CreateUntracked(qty,
332  context()->GetRecipe(in_recipe));
333 }
334 
335 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
336 cyclus::BidPortfolio<cyclus::Material>::Ptr CommodConverter::GetBids_(
337  cyclus::CommodMap<cyclus::Material>::type& commod_requests,
338  std::string commod,
339  cyclus::toolkit::ResourceBuff* buffer) {
340  using cyclus::Bid;
341  using cyclus::BidPortfolio;
342  using cyclus::CapacityConstraint;
343  using cyclus::Composition;
344  using cyclus::Converter;
345  using cyclus::Material;
346  using cyclus::Request;
347  using cyclus::ResCast;
348  using cyclus::toolkit::ResourceBuff;
349 
350  BidPortfolio<Material>::Ptr port(new BidPortfolio<Material>());
351 
352  if (commod_requests.count(commod) > 0 && buffer->quantity() > 0) {
353  std::vector<Request<Material>*>& requests = commod_requests.at(commod);
354 
355  // get offer composition
356  Material::Ptr back = ResCast<Material>(buffer->Pop(ResourceBuff::BACK));
357  Composition::Ptr comp = back->comp();
358  buffer->Push(back);
359 
360  std::vector<Request<Material>*>::iterator it;
361  for (it = requests.begin(); it != requests.end(); ++it) {
362  Request<Material>* req = *it;
363  double qty = std::min(req->target()->quantity(), buffer->quantity());
364  Material::Ptr offer = Material::CreateUntracked(qty, comp);
365  port->AddBid(req, offer, this);
366  }
367 
368  //CapacityConstraint<Material> cc(buffer->quantity());
369  //port->AddConstraint(cc);
370  }
371 
372  return port;
373 }
374 
375 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
376 cyclus::Material::Ptr CommodConverter::TradeResponse_(
377  double qty,
378  cyclus::toolkit::ResourceBuff* buffer) {
379  using cyclus::Material;
380  using cyclus::ResCast;
381 
382  std::vector<Material::Ptr> manifest;
383  try {
384  // pop amount from inventory and blob it into one material
385  manifest = ResCast<Material>(buffer->PopQty(qty));
386  } catch(cyclus::Error& e) {
387  e.msg(Agent::InformErrorMsg(e.msg()));
388  throw e;
389  }
390 
391  Material::Ptr response = manifest[0];
392  crctx_.RemoveRsrc(response);
393  for (int i = 1; i < manifest.size(); i++) {
394  crctx_.RemoveRsrc(manifest[i]);
395  response->Absorb(manifest[i]);
396  }
397  return response;
398 }
399 
400 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
402  LOG(cyclus::LEV_DEBUG2, "ComCnv") << "CommodConverter " << prototype()
403  << " added resources to processing";
404  if( inventory.count() > 0 ){
405  try {
406  processing[context()->time()].Push(inventory.Pop());
407  } catch (cyclus::Error& e) {
408  e.msg(Agent::InformErrorMsg(e.msg()));
409  throw e;
410  }
411  }
412 }
413 
414 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
416  using cyclus::Material;
417  using cyclus::ResCast;
418  LOG(cyclus::LEV_DEBUG2, "ComCnv") << "CommodConverter " << prototype()
419  << " removed a resource from processing.";
420 
421  if ( processing.find(ready())->second.count() > 0 ) {
422  try {
423  // pop one material from processing
424  Material::Ptr mat = ResCast<Material>(processing.find(ready())->second.Pop());
425  // if an out_recipe was provided, transmute it
426  if( out_recipe != "" ){
427  mat->Transmute(context()->GetRecipe(out_recipe));
428  }
429  // change its commod
430  crctx_.UpdateRsrc(out_commod_(), mat);
431  // put it in the stocks
432  stocks.Push(mat);
433  } catch (cyclus::Error& e) {
434  e.msg(Agent::InformErrorMsg(e.msg()));
435  throw e;
436  }
437  }
438 }
439 
440 
441 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
442 extern "C" cyclus::Agent* ConstructCommodConverter(cyclus::Context* ctx) {
443  return new CommodConverter(ctx);
444 }
445 
446 } // namespace commodconverter
double current_capacity() const
current maximum amount that can be added to processing
cyclus::BidPortfolio< cyclus::Material >::Ptr GetBids_(cyclus::CommodMap< cyclus::Material >::type &commod_requests, std::string commod, cyclus::toolkit::ResourceBuff *buffer)
gathers information about bids
virtual void InfileToDb(cyclus::InfileTree *tree, cyclus::DbInit di)
virtual void InitFrom(commodconverter::CommodConverter *m)
The Prime Directive Generates code that handles all input file reading and restart operations (e...
virtual void GetMatlTrades(const std::vector< cyclus::Trade< cyclus::Material > > &trades, std::vector< std::pair< cyclus::Trade< cyclus::Material >, cyclus::Material::Ptr > > &responses)
respond to each trade with a material of out_commod and out_recipe
cyclus::toolkit::CommodityRecipeContext crctx_
cyclus::Material::Ptr TradeResponse_(double qty, cyclus::toolkit::ResourceBuff *buffer)
suggests, based on the buffer, a material response to an offer
virtual std::set< cyclus::RequestPortfolio< cyclus::Material >::Ptr > GetMatlRequests()
The CommodConverter request Materials of its given commodity.
cyclus::Agent * ConstructCommodConverter(cyclus::Context *ctx)
void Convert_()
Convert one ready resource in processing.
virtual void AcceptMatlTrades(const std::vector< std::pair< cyclus::Trade< cyclus::Material >, cyclus::Material::Ptr > > &responses)
The CommodConverter place accepted trade Materials in their Inventory.
virtual std::set< cyclus::BidPortfolio< cyclus::Material >::Ptr > GetMatlBids(cyclus::CommodMap< cyclus::Material >::type &commod_requests)
Responds to each request for this facility's commodity.
void BeginProcessing_()
Move all unprocessed inventory to processing.
virtual std::string str()
A verbose printer for the CommodConverter.
virtual void Tock()
The handleTick function specific to the CommodConverter.
void AddMat_(cyclus::Material::Ptr mat)
adds a material into the incoming commodity inventory
int ready()
returns the time key for ready materials
virtual void InitInv(cyclus::Inventories &inv)
cyclus::Material::Ptr Request_()
generates a request for this facility given its current state.
CommodConverter(cyclus::Context *ctx)
Constructor for CommodConverter Class.
This Facility is intended to convert a resource from one commodity to another.
virtual void Tick()
The handleTick function specific to the CommodConverter.
std::map< int, cyclus::toolkit::ResourceBuff > processing
map from ready time to resource buffers