forked from cyclus/cycamore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenrichment.cc
506 lines (443 loc) · 18.2 KB
/
enrichment.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// Implements the Enrichment class
#include "enrichment.h"
#include <algorithm>
#include <cmath>
#include <limits>
#include <sstream>
#include <vector>
#include <boost/lexical_cast.hpp>
namespace cycamore {
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Enrichment::Enrichment(cyclus::Context* ctx)
: cyclus::Facility(ctx),
tails_assay(0),
swu_capacity(0),
max_enrich(1),
initial_feed(0),
feed_commod(""),
feed_recipe(""),
product_commod(""),
tails_commod(""),
order_prefs(true),
latitude(0.0),
longitude(0.0),
coordinates(latitude, longitude) {}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Enrichment::~Enrichment() {}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
std::string Enrichment::str() {
std::stringstream ss;
ss << cyclus::Facility::str() << " with enrichment facility parameters:"
<< " * SWU capacity: " << SwuCapacity()
<< " * Tails assay: " << tails_assay << " * Feed assay: " << FeedAssay()
<< " * Input cyclus::Commodity: " << feed_commod
<< " * Output cyclus::Commodity: " << product_commod
<< " * Tails cyclus::Commodity: " << tails_commod;
return ss.str();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Enrichment::Build(cyclus::Agent* parent) {
using cyclus::Material;
Facility::Build(parent);
if (initial_feed > 0) {
inventory.Push(Material::Create(this, initial_feed,
context()->GetRecipe(feed_recipe)));
}
LOG(cyclus::LEV_DEBUG2, "EnrFac") << "Enrichment "
<< " entering the simuluation: ";
LOG(cyclus::LEV_DEBUG2, "EnrFac") << str();
RecordPosition();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Enrichment::Tick() {
current_swu_capacity = SwuCapacity();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Enrichment::Tock() {
using cyclus::toolkit::RecordTimeSeries;
LOG(cyclus::LEV_INFO4, "EnrFac") << prototype() << " used "
<< intra_timestep_swu_ << " SWU";
RecordTimeSeries<cyclus::toolkit::ENRICH_SWU>(this, intra_timestep_swu_);
LOG(cyclus::LEV_INFO4, "EnrFac") << prototype() << " used "
<< intra_timestep_feed_ << " feed";
RecordTimeSeries<cyclus::toolkit::ENRICH_FEED>(this, intra_timestep_feed_);
RecordTimeSeries<double>("demand"+feed_commod, this, intra_timestep_feed_);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
std::set<cyclus::RequestPortfolio<cyclus::Material>::Ptr>
Enrichment::GetMatlRequests() {
using cyclus::Material;
using cyclus::RequestPortfolio;
using cyclus::Request;
std::set<RequestPortfolio<Material>::Ptr> ports;
RequestPortfolio<Material>::Ptr port(new RequestPortfolio<Material>());
Material::Ptr mat = Request_();
double amt = mat->quantity();
if (amt > cyclus::eps_rsrc()) {
port->AddRequest(mat, this, feed_commod);
ports.insert(port);
}
return ports;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool SortBids(cyclus::Bid<cyclus::Material>* i,
cyclus::Bid<cyclus::Material>* j) {
cyclus::Material::Ptr mat_i = i->offer();
cyclus::Material::Ptr mat_j = j->offer();
cyclus::toolkit::MatQuery mq_i(mat_i);
cyclus::toolkit::MatQuery mq_j(mat_j);
return ((mq_i.mass(922350000) / mq_i.qty()) <=
(mq_j.mass(922350000) / mq_j.qty()));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Sort offers of input material to have higher preference for more
// U-235 content
void Enrichment::AdjustMatlPrefs(
cyclus::PrefMap<cyclus::Material>::type& prefs) {
using cyclus::Bid;
using cyclus::Material;
using cyclus::Request;
if (order_prefs == false) {
return;
}
cyclus::PrefMap<cyclus::Material>::type::iterator reqit;
// Loop over all requests
for (reqit = prefs.begin(); reqit != prefs.end(); ++reqit) {
std::vector<Bid<Material>*> bids_vector;
std::map<Bid<Material>*, double>::iterator mit;
for (mit = reqit->second.begin(); mit != reqit->second.end(); ++mit) {
Bid<Material>* bid = mit->first;
bids_vector.push_back(bid);
}
std::sort(bids_vector.begin(), bids_vector.end(), SortBids);
// Assign preferences to the sorted vector
double n_bids = bids_vector.size();
bool u235_mass = 0;
for (int bidit = 0; bidit < bids_vector.size(); bidit++) {
int new_pref = bidit + 1;
// For any bids with U-235 qty=0, set pref to zero.
if (!u235_mass) {
cyclus::Material::Ptr mat = bids_vector[bidit]->offer();
cyclus::toolkit::MatQuery mq(mat);
if (mq.mass(922350000) == 0) {
new_pref = -1;
} else {
u235_mass = true;
}
}
(reqit->second)[bids_vector[bidit]] = new_pref;
} // each bid
} // each Material Request
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Enrichment::AcceptMatlTrades(
const std::vector<std::pair<cyclus::Trade<cyclus::Material>,
cyclus::Material::Ptr> >& responses) {
// see
// http://stackoverflow.com/questions/5181183/boostshared-ptr-and-inheritance
std::vector<std::pair<cyclus::Trade<cyclus::Material>,
cyclus::Material::Ptr> >::const_iterator it;
for (it = responses.begin(); it != responses.end(); ++it) {
AddMat_(it->second);
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
std::set<cyclus::BidPortfolio<cyclus::Material>::Ptr> Enrichment::GetMatlBids(
cyclus::CommodMap<cyclus::Material>::type& out_requests) {
using cyclus::Bid;
using cyclus::BidPortfolio;
using cyclus::CapacityConstraint;
using cyclus::Converter;
using cyclus::Material;
using cyclus::Request;
using cyclus::toolkit::MatVec;
using cyclus::toolkit::RecordTimeSeries;
std::set<BidPortfolio<Material>::Ptr> ports;
RecordTimeSeries<double>("supply" + tails_commod, this, tails.quantity());
RecordTimeSeries<double>("supply" + product_commod, this, inventory.quantity());
if ((out_requests.count(tails_commod) > 0) && (tails.quantity() > 0)) {
BidPortfolio<Material>::Ptr tails_port(new BidPortfolio<Material>());
std::vector<Request<Material>*>& tails_requests =
out_requests[tails_commod];
std::vector<Request<Material>*>::iterator it;
for (it = tails_requests.begin(); it != tails_requests.end(); ++it) {
// offer bids for all tails material, keeping discrete quantities
// to preserve possible variation in composition
MatVec mats = tails.PopN(tails.count());
tails.Push(mats);
for (int k = 0; k < mats.size(); k++) {
Material::Ptr m = mats[k];
Request<Material>* req = *it;
tails_port->AddBid(req, m, this);
}
}
// overbidding (bidding on every offer)
// add an overall capacity constraint
CapacityConstraint<Material> tails_constraint(tails.quantity());
tails_port->AddConstraint(tails_constraint);
LOG(cyclus::LEV_INFO5, "EnrFac") << prototype()
<< " adding tails capacity constraint of "
<< tails.capacity();
ports.insert(tails_port);
}
if ((out_requests.count(product_commod) > 0) && (inventory.quantity() > 0)) {
BidPortfolio<Material>::Ptr commod_port(new BidPortfolio<Material>());
std::vector<Request<Material>*>& commod_requests =
out_requests[product_commod];
std::vector<Request<Material>*>::iterator it;
for (it = commod_requests.begin(); it != commod_requests.end(); ++it) {
Request<Material>* req = *it;
Material::Ptr mat = req->target();
double request_enrich = cyclus::toolkit::UraniumAssayMass(mat);
if (ValidReq(req->target()) &&
((request_enrich < max_enrich) ||
(cyclus::AlmostEq(request_enrich, max_enrich)))) {
Material::Ptr offer = Offer_(req->target());
commod_port->AddBid(req, offer, this);
}
}
Converter<Material>::Ptr sc(new SWUConverter(FeedAssay(), tails_assay));
Converter<Material>::Ptr nc(new NatUConverter(FeedAssay(), tails_assay));
CapacityConstraint<Material> swu(swu_capacity, sc);
CapacityConstraint<Material> natu(inventory.quantity(), nc);
commod_port->AddConstraint(swu);
commod_port->AddConstraint(natu);
LOG(cyclus::LEV_INFO5, "EnrFac")
<< prototype() << " adding a swu constraint of " << swu.capacity();
LOG(cyclus::LEV_INFO5, "EnrFac")
<< prototype() << " adding a natu constraint of " << natu.capacity();
ports.insert(commod_port);
}
return ports;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool Enrichment::ValidReq(const cyclus::Material::Ptr mat) {
cyclus::toolkit::MatQuery q(mat);
double u235 = q.atom_frac(922350000);
double u238 = q.atom_frac(922380000);
return (u238 > 0 && u235 / (u235 + u238) > tails_assay);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Enrichment::GetMatlTrades(
const std::vector<cyclus::Trade<cyclus::Material> >& trades,
std::vector<std::pair<cyclus::Trade<cyclus::Material>,
cyclus::Material::Ptr> >& responses) {
using cyclus::Material;
using cyclus::Trade;
intra_timestep_swu_ = 0;
intra_timestep_feed_ = 0;
std::vector<Trade<Material>>::const_iterator it;
for (it = trades.begin(); it != trades.end(); ++it) {
double qty = it->amt;
std::string commod_type = it->bid->request()->commodity();
Material::Ptr response;
// Figure out whether material is tails or enriched,
// if tails then make transfer of material
if (commod_type == tails_commod) {
LOG(cyclus::LEV_INFO5, "EnrFac")
<< prototype() << " just received an order"
<< " for " << it->amt << " of " << tails_commod;
double pop_qty = std::min(qty, tails.quantity());
response = tails.Pop(pop_qty, cyclus::eps_rsrc());
} else {
LOG(cyclus::LEV_INFO5, "EnrFac")
<< prototype() << " just received an order"
<< " for " << it->amt << " of " << product_commod;
response = Enrich_(it->bid->offer(), qty);
}
responses.push_back(std::make_pair(*it, response));
}
if (cyclus::IsNegative(tails.quantity())) {
std::stringstream ss;
ss << "is being asked to provide more than its current inventory.";
throw cyclus::ValueError(Agent::InformErrorMsg(ss.str()));
}
if (cyclus::IsNegative(current_swu_capacity)) {
throw cyclus::ValueError("EnrFac " + prototype() +
" is being asked to provide more than" +
" its SWU capacity.");
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Enrichment::AddMat_(cyclus::Material::Ptr mat) {
// Elements and isotopes other than U-235, U-238 are sent directly to tails
cyclus::CompMap cm = mat->comp()->atom();
bool extra_u = false;
bool other_elem = false;
for (cyclus::CompMap::const_iterator it = cm.begin(); it != cm.end(); ++it) {
if (pyne::nucname::znum(it->first) == 92) {
if (pyne::nucname::anum(it->first) != 235 &&
pyne::nucname::anum(it->first) != 238 && it->second > 0) {
extra_u = true;
}
} else if (it->second > 0) {
other_elem = true;
}
}
if (extra_u) {
cyclus::Warn<cyclus::VALUE_WARNING>(
"More than 2 isotopes of U. "
"Istopes other than U-235, U-238 are sent directly to tails.");
}
if (other_elem) {
cyclus::Warn<cyclus::VALUE_WARNING>(
"Non-uranium elements are "
"sent directly to tails.");
}
LOG(cyclus::LEV_INFO5, "EnrFac") << prototype() << " is initially holding "
<< inventory.quantity() << " total.";
try {
inventory.Push(mat);
} catch (cyclus::Error& e) {
e.msg(Agent::InformErrorMsg(e.msg()));
throw e;
}
LOG(cyclus::LEV_INFO5, "EnrFac")
<< prototype() << " added " << mat->quantity() << " of " << feed_commod
<< " to its inventory, which is holding " << inventory.quantity()
<< " total.";
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
cyclus::Material::Ptr Enrichment::Request_() {
double qty = std::max(0.0, inventory.capacity() - inventory.quantity());
return cyclus::Material::CreateUntracked(qty,
context()->GetRecipe(feed_recipe));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
cyclus::Material::Ptr Enrichment::Offer_(cyclus::Material::Ptr mat) {
cyclus::toolkit::MatQuery q(mat);
cyclus::CompMap comp;
comp[922350000] = q.atom_frac(922350000);
comp[922380000] = q.atom_frac(922380000);
return cyclus::Material::CreateUntracked(
mat->quantity(), cyclus::Composition::CreateFromAtom(comp));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
cyclus::Material::Ptr Enrichment::Enrich_(cyclus::Material::Ptr mat,
double qty) {
using cyclus::Material;
using cyclus::ResCast;
using cyclus::toolkit::Assays;
using cyclus::toolkit::UraniumAssayMass;
using cyclus::toolkit::SwuRequired;
using cyclus::toolkit::FeedQty;
using cyclus::toolkit::TailsQty;
// get enrichment parameters
Assays assays(FeedAssay(), UraniumAssayMass(mat), tails_assay);
double swu_req = SwuRequired(qty, assays);
double natu_req = FeedQty(qty, assays);
// Determine the composition of the natural uranium
// (ie. U-235+U-238/TotalMass)
double pop_qty = inventory.quantity();
Material::Ptr natu_matl = inventory.Pop(pop_qty, cyclus::eps_rsrc());
inventory.Push(natu_matl);
cyclus::toolkit::MatQuery mq(natu_matl);
std::set<cyclus::Nuc> nucs;
nucs.insert(922350000);
nucs.insert(922380000);
double natu_frac = mq.mass_frac(nucs);
double feed_req = natu_req / natu_frac;
// Take into account the above mentioned special case.
if (feed_req > pop_qty) {
// All of the available feed is to be used.
feed_req = pop_qty;
// Amount of U235 and U238 in the available feed.
double u235_u238_available = pop_qty * natu_frac;
double factor = (assays.Product() - assays.Tails())
/ (assays.Feed() - assays.Tails());
// Reevaluate how much product can be produced.
qty = u235_u238_available / factor;
// The SWU needs to be recalculated as well because all non-U235 and
// non-U238 elements/isotopes are directly sent to the tails and do
// not contribute to the SWU.
swu_req = SwuRequired(qty, assays);
}
// pop amount from inventory and blob it into one material
Material::Ptr r;
try {
// required so popping doesn't take out too much
if (cyclus::AlmostEq(feed_req, inventory.quantity())) {
r = cyclus::toolkit::Squash(inventory.PopN(inventory.count()));
} else {
r = inventory.Pop(feed_req, cyclus::eps_rsrc());
}
} catch (cyclus::Error& e) {
NatUConverter nc(FeedAssay(), tails_assay);
std::stringstream ss;
ss << " tried to remove " << feed_req << " from its inventory of size "
<< inventory.quantity()
<< " and the conversion of the material into natu is "
<< nc.convert(mat);
throw cyclus::ValueError(Agent::InformErrorMsg(ss.str()));
}
// "enrich" it, but pull out the composition and quantity we require from the
// blob
cyclus::Composition::Ptr comp = mat->comp();
Material::Ptr response = r->ExtractComp(qty, comp);
tails.Push(r);
current_swu_capacity -= swu_req;
intra_timestep_swu_ += swu_req;
intra_timestep_feed_ += feed_req;
RecordEnrichment_(feed_req, swu_req);
LOG(cyclus::LEV_INFO5, "EnrFac") << prototype()
<< " has performed an enrichment: ";
LOG(cyclus::LEV_INFO5, "EnrFac") << " * Feed Qty: " << feed_req;
LOG(cyclus::LEV_INFO5, "EnrFac") << " * Feed Assay: "
<< assays.Feed() * 100;
LOG(cyclus::LEV_INFO5, "EnrFac") << " * Product Qty: " << qty;
LOG(cyclus::LEV_INFO5, "EnrFac") << " * Product Assay: "
<< assays.Product() * 100;
LOG(cyclus::LEV_INFO5, "EnrFac") << " * Tails Qty: "
<< TailsQty(qty, assays);
LOG(cyclus::LEV_INFO5, "EnrFac") << " * Tails Assay: "
<< assays.Tails() * 100;
LOG(cyclus::LEV_INFO5, "EnrFac") << " * SWU: " << swu_req;
LOG(cyclus::LEV_INFO5, "EnrFac") << " * Current SWU capacity: "
<< current_swu_capacity;
return response;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Enrichment::RecordEnrichment_(double natural_u, double swu) {
using cyclus::Context;
using cyclus::Agent;
LOG(cyclus::LEV_DEBUG1, "EnrFac") << prototype()
<< " has enriched a material:";
LOG(cyclus::LEV_DEBUG1, "EnrFac") << " * Amount: " << natural_u;
LOG(cyclus::LEV_DEBUG1, "EnrFac") << " * SWU: " << swu;
Context* ctx = Agent::context();
ctx->NewDatum("Enrichments")
->AddVal("AgentId", id())
->AddVal("Time", ctx->time())
->AddVal("Natural_Uranium", natural_u)
->AddVal("SWU", swu)
->Record();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
double Enrichment::FeedAssay() {
using cyclus::Material;
if (inventory.empty()) {
return 0;
}
double pop_qty = inventory.quantity();
cyclus::Material::Ptr fission_matl =
inventory.Pop(pop_qty, cyclus::eps_rsrc());
inventory.Push(fission_matl);
return cyclus::toolkit::UraniumAssayMass(fission_matl);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Enrichment::RecordPosition() {
std::string specification = this->spec();
context()
->NewDatum("AgentPosition")
->AddVal("Spec", specification)
->AddVal("Prototype", this->prototype())
->AddVal("AgentId", id())
->AddVal("Latitude", latitude)
->AddVal("Longitude", longitude)
->Record();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
extern "C" cyclus::Agent* ConstructEnrichment(cyclus::Context* ctx) {
return new Enrichment(ctx);
}
} // namespace cycamore