-
Notifications
You must be signed in to change notification settings - Fork 40
/
reactor.cc
581 lines (490 loc) · 16.5 KB
/
reactor.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#include "reactor.h"
using cyclus::Material;
using cyclus::Composition;
using cyclus::toolkit::ResBuf;
using cyclus::toolkit::MatVec;
using cyclus::KeyError;
using cyclus::ValueError;
using cyclus::Request;
namespace cycamore {
Reactor::Reactor(cyclus::Context* ctx)
: cyclus::Facility(ctx),
n_assem_batch(0),
assem_size(0),
n_assem_core(0),
n_assem_spent(0),
n_assem_fresh(0),
cycle_time(0),
refuel_time(0),
cycle_step(0),
power_cap(0),
power_name("power"),
discharged(false),
latitude(0.0),
longitude(0.0),
coordinates(latitude, longitude) {}
#pragma cyclus def clone cycamore::Reactor
#pragma cyclus def schema cycamore::Reactor
#pragma cyclus def annotations cycamore::Reactor
#pragma cyclus def infiletodb cycamore::Reactor
#pragma cyclus def snapshot cycamore::Reactor
#pragma cyclus def snapshotinv cycamore::Reactor
#pragma cyclus def initinv cycamore::Reactor
void Reactor::InitFrom(Reactor* m) {
#pragma cyclus impl initfromcopy cycamore::Reactor
cyclus::toolkit::CommodityProducer::Copy(m);
}
void Reactor::InitFrom(cyclus::QueryableBackend* b) {
#pragma cyclus impl initfromdb cycamore::Reactor
namespace tk = cyclus::toolkit;
tk::CommodityProducer::Add(tk::Commodity(power_name),
tk::CommodInfo(power_cap, power_cap));
for (int i = 0; i < side_products.size(); i++) {
tk::CommodityProducer::Add(tk::Commodity(side_products[i]),
tk::CommodInfo(side_product_quantity[i],
side_product_quantity[i]));
}
}
void Reactor::EnterNotify() {
cyclus::Facility::EnterNotify();
// If the user ommitted fuel_prefs, we set it to zeros for each fuel
// type. Without this segfaults could occur - yuck.
if (fuel_prefs.size() == 0) {
for (int i = 0; i < fuel_outcommods.size(); i++) {
fuel_prefs.push_back(cyclus::kDefaultPref);
}
}
// Test if any side products have been defined.
if (side_products.size() == 0){
hybrid_ = false;
}
// input consistency checking:
int n = recipe_change_times.size();
std::stringstream ss;
if (recipe_change_commods.size() != n) {
ss << "prototype '" << prototype() << "' has "
<< recipe_change_commods.size()
<< " recipe_change_commods vals, expected " << n << "\n";
}
if (recipe_change_in.size() != n) {
ss << "prototype '" << prototype() << "' has " << recipe_change_in.size()
<< " recipe_change_in vals, expected " << n << "\n";
}
if (recipe_change_out.size() != n) {
ss << "prototype '" << prototype() << "' has " << recipe_change_out.size()
<< " recipe_change_out vals, expected " << n << "\n";
}
n = pref_change_times.size();
if (pref_change_commods.size() != n) {
ss << "prototype '" << prototype() << "' has " << pref_change_commods.size()
<< " pref_change_commods vals, expected " << n << "\n";
}
if (pref_change_values.size() != n) {
ss << "prototype '" << prototype() << "' has " << pref_change_values.size()
<< " pref_change_values vals, expected " << n << "\n";
}
if (ss.str().size() > 0) {
throw cyclus::ValueError(ss.str());
}
RecordPosition();
}
bool Reactor::CheckDecommissionCondition() {
return core.count() == 0 && spent.count() == 0;
}
void Reactor::Tick() {
// The following code must go in the Tick so they fire on the time step
// following the cycle_step update - allowing for the all reactor events to
// occur and be recorded on the "beginning" of a time step. Another reason
// they
// can't go at the beginnin of the Tock is so that resource exchange has a
// chance to occur after the discharge on this same time step.
if (retired()) {
Record("RETIRED", "");
if (context()->time() == exit_time() + 1) { // only need to transmute once
if (decom_transmute_all == true) {
Transmute(ceil(static_cast<double>(n_assem_core)));
}
else {
Transmute(ceil(static_cast<double>(n_assem_core) / 2.0));
}
}
while (core.count() > 0) {
if (!Discharge()) {
break;
}
}
// in case a cycle lands exactly on our last time step, we will need to
// burn a batch from fresh inventory on this time step. When retired,
// this batch also needs to be discharged to spent fuel inventory.
while (fresh.count() > 0 && spent.space() >= assem_size) {
spent.Push(fresh.Pop());
}
if(CheckDecommissionCondition()) {
Decommission();
}
return;
}
if (cycle_step == cycle_time) {
Transmute();
Record("CYCLE_END", "");
}
if (cycle_step >= cycle_time && !discharged) {
discharged = Discharge();
}
if (cycle_step >= cycle_time) {
Load();
}
int t = context()->time();
// update preferences
for (int i = 0; i < pref_change_times.size(); i++) {
int change_t = pref_change_times[i];
if (t != change_t) {
continue;
}
std::string incommod = pref_change_commods[i];
for (int j = 0; j < fuel_incommods.size(); j++) {
if (fuel_incommods[j] == incommod) {
fuel_prefs[j] = pref_change_values[i];
break;
}
}
}
// update recipes
for (int i = 0; i < recipe_change_times.size(); i++) {
int change_t = recipe_change_times[i];
if (t != change_t) {
continue;
}
std::string incommod = recipe_change_commods[i];
for (int j = 0; j < fuel_incommods.size(); j++) {
if (fuel_incommods[j] == incommod) {
fuel_inrecipes[j] = recipe_change_in[i];
fuel_outrecipes[j] = recipe_change_out[i];
break;
}
}
}
}
std::set<cyclus::RequestPortfolio<Material>::Ptr> Reactor::GetMatlRequests() {
using cyclus::RequestPortfolio;
std::set<RequestPortfolio<Material>::Ptr> ports;
Material::Ptr m;
// second min expression reduces assembles to amount needed until
// retirement if it is near.
int n_assem_order = n_assem_core - core.count() + n_assem_fresh - fresh.count();
if (exit_time() != -1) {
// the +1 accounts for the fact that the reactor is alive and gets to
// operate during its exit_time time step.
int t_left = exit_time() - context()->time() + 1;
int t_left_cycle = cycle_time + refuel_time - cycle_step;
double n_cycles_left = static_cast<double>(t_left - t_left_cycle) /
static_cast<double>(cycle_time + refuel_time);
n_cycles_left = ceil(n_cycles_left);
int n_need = std::max(0.0, n_cycles_left * n_assem_batch - n_assem_fresh + n_assem_core - core.count());
n_assem_order = std::min(n_assem_order, n_need);
}
if (n_assem_order == 0) {
return ports;
} else if (retired()) {
return ports;
}
for (int i = 0; i < n_assem_order; i++) {
RequestPortfolio<Material>::Ptr port(new RequestPortfolio<Material>());
std::vector<Request<Material>*> mreqs;
for (int j = 0; j < fuel_incommods.size(); j++) {
std::string commod = fuel_incommods[j];
double pref = fuel_prefs[j];
Composition::Ptr recipe = context()->GetRecipe(fuel_inrecipes[j]);
m = Material::CreateUntracked(assem_size, recipe);
Request<Material>* r = port->AddRequest(m, this, commod, pref, true);
mreqs.push_back(r);
}
std::vector<double>::iterator result;
result = std::max_element(fuel_prefs.begin(), fuel_prefs.end());
int max_index = std::distance(fuel_prefs.begin(), result);
cyclus::toolkit::RecordTimeSeries<double>("demand"+fuel_incommods[max_index], this,
assem_size * n_assem_order) ;
port->AddMutualReqs(mreqs);
ports.insert(port);
}
return ports;
}
void Reactor::GetMatlTrades(
const std::vector<cyclus::Trade<Material> >& trades,
std::vector<std::pair<cyclus::Trade<Material>, Material::Ptr> >&
responses) {
using cyclus::Trade;
std::map<std::string, MatVec> mats = PopSpent();
for (int i = 0; i < trades.size(); i++) {
std::string commod = trades[i].request->commodity();
Material::Ptr m = mats[commod].back();
mats[commod].pop_back();
responses.push_back(std::make_pair(trades[i], m));
res_indexes.erase(m->obj_id());
}
PushSpent(mats); // return leftovers back to spent buffer
}
void Reactor::AcceptMatlTrades(const std::vector<
std::pair<cyclus::Trade<Material>, Material::Ptr> >& responses) {
std::vector<std::pair<cyclus::Trade<cyclus::Material>,
cyclus::Material::Ptr> >::const_iterator trade;
std::stringstream ss;
int nload = std::min((int)responses.size(), n_assem_core - core.count());
if (nload > 0) {
ss << nload << " assemblies";
Record("LOAD", ss.str());
}
for (trade = responses.begin(); trade != responses.end(); ++trade) {
std::string commod = trade->first.request->commodity();
Material::Ptr m = trade->second;
index_res(m, commod);
if (core.count() < n_assem_core) {
core.Push(m);
} else {
fresh.Push(m);
}
}
}
std::set<cyclus::BidPortfolio<Material>::Ptr> Reactor::GetMatlBids(
cyclus::CommodMap<Material>::type& commod_requests) {
using cyclus::BidPortfolio;
std::set<BidPortfolio<Material>::Ptr> ports;
bool gotmats = false;
std::map<std::string, MatVec> all_mats;
if (uniq_outcommods_.empty()) {
for (int i = 0; i < fuel_outcommods.size(); i++) {
uniq_outcommods_.insert(fuel_outcommods[i]);
}
}
std::set<std::string>::iterator it;
for (it = uniq_outcommods_.begin(); it != uniq_outcommods_.end(); ++it) {
std::string commod = *it;
std::vector<Request<Material>*>& reqs = commod_requests[commod];
if (reqs.size() == 0) {
continue;
} else if (!gotmats) {
all_mats = PeekSpent();
}
MatVec mats = all_mats[commod];
if (mats.size() == 0) {
continue;
}
BidPortfolio<Material>::Ptr port(new BidPortfolio<Material>());
for (int j = 0; j < reqs.size(); j++) {
Request<Material>* req = reqs[j];
double tot_bid = 0;
for (int k = 0; k < mats.size(); k++) {
Material::Ptr m = mats[k];
tot_bid += m->quantity();
port->AddBid(req, m, this, true);
if (tot_bid >= req->target()->quantity()) {
break;
}
}
}
double tot_qty = 0;
for (int j = 0; j < mats.size(); j++) {
tot_qty += mats[j]->quantity();
}
cyclus::CapacityConstraint<Material> cc(tot_qty);
port->AddConstraint(cc);
ports.insert(port);
}
return ports;
}
void Reactor::Tock() {
if (retired()) {
return;
}
if (cycle_step >= cycle_time + refuel_time && core.count() == n_assem_core) {
discharged = false;
cycle_step = 0;
}
if (cycle_step == 0 && core.count() == n_assem_core) {
Record("CYCLE_START", "");
}
if (cycle_step >= 0 && cycle_step < cycle_time &&
core.count() == n_assem_core) {
cyclus::toolkit::RecordTimeSeries<cyclus::toolkit::POWER>(this, power_cap);
cyclus::toolkit::RecordTimeSeries<double>("supplyPOWER", this, power_cap);
RecordSideProduct(true);
} else {
cyclus::toolkit::RecordTimeSeries<cyclus::toolkit::POWER>(this, 0);
cyclus::toolkit::RecordTimeSeries<double>("supplyPOWER", this, 0);
RecordSideProduct(false);
}
// "if" prevents starting cycle after initial deployment until core is full
// even though cycle_step is its initial zero.
if (cycle_step > 0 || core.count() == n_assem_core) {
cycle_step++;
}
}
void Reactor::Transmute() { Transmute(n_assem_batch); }
void Reactor::Transmute(int n_assem) {
MatVec old = core.PopN(std::min(n_assem, core.count()));
core.Push(old);
if (core.count() > old.size()) {
// rotate untransmuted mats back to back of buffer
core.Push(core.PopN(core.count() - old.size()));
}
std::stringstream ss;
ss << old.size() << " assemblies";
Record("TRANSMUTE", ss.str());
for (int i = 0; i < old.size(); i++) {
old[i]->Transmute(context()->GetRecipe(fuel_outrecipe(old[i])));
}
}
std::map<std::string, MatVec> Reactor::PeekSpent() {
std::map<std::string, MatVec> mapped;
MatVec mats = spent.PopN(spent.count());
spent.Push(mats);
for (int i = 0; i < mats.size(); i++) {
std::string commod = fuel_outcommod(mats[i]);
mapped[commod].push_back(mats[i]);
}
return mapped;
}
bool Reactor::Discharge() {
int npop = std::min(n_assem_batch, core.count());
if (n_assem_spent - spent.count() < npop) {
Record("DISCHARGE", "failed");
return false; // not enough room in spent buffer
}
std::stringstream ss;
ss << npop << " assemblies";
Record("DISCHARGE", ss.str());
spent.Push(core.PopN(npop));
std::map<std::string, MatVec> spent_mats;
for (int i = 0; i < fuel_outcommods.size(); i++) {
spent_mats = PeekSpent();
MatVec mats = spent_mats[fuel_outcommods[i]];
double tot_spent = 0;
for (int j = 0; j<mats.size(); j++){
Material::Ptr m = mats[j];
tot_spent += m->quantity();
}
cyclus::toolkit::RecordTimeSeries<double>("supply"+fuel_outcommods[i], this, tot_spent);
}
return true;
}
void Reactor::Load() {
int n = std::min(n_assem_core - core.count(), fresh.count());
if (n == 0) {
return;
}
std::stringstream ss;
ss << n << " assemblies";
Record("LOAD", ss.str());
core.Push(fresh.PopN(n));
}
std::string Reactor::fuel_incommod(Material::Ptr m) {
int i = res_indexes[m->obj_id()];
if (i >= fuel_incommods.size()) {
throw KeyError("cycamore::Reactor - no incommod for material object");
}
return fuel_incommods[i];
}
std::string Reactor::fuel_outcommod(Material::Ptr m) {
int i = res_indexes[m->obj_id()];
if (i >= fuel_outcommods.size()) {
throw KeyError("cycamore::Reactor - no outcommod for material object");
}
return fuel_outcommods[i];
}
std::string Reactor::fuel_inrecipe(Material::Ptr m) {
int i = res_indexes[m->obj_id()];
if (i >= fuel_inrecipes.size()) {
throw KeyError("cycamore::Reactor - no inrecipe for material object");
}
return fuel_inrecipes[i];
}
std::string Reactor::fuel_outrecipe(Material::Ptr m) {
int i = res_indexes[m->obj_id()];
if (i >= fuel_outrecipes.size()) {
throw KeyError("cycamore::Reactor - no outrecipe for material object");
}
return fuel_outrecipes[i];
}
double Reactor::fuel_pref(Material::Ptr m) {
int i = res_indexes[m->obj_id()];
if (i >= fuel_prefs.size()) {
return 0;
}
return fuel_prefs[i];
}
void Reactor::index_res(cyclus::Resource::Ptr m, std::string incommod) {
for (int i = 0; i < fuel_incommods.size(); i++) {
if (fuel_incommods[i] == incommod) {
res_indexes[m->obj_id()] = i;
return;
}
}
throw ValueError(
"cycamore::Reactor - received unsupported incommod material");
}
std::map<std::string, MatVec> Reactor::PopSpent() {
MatVec mats = spent.PopN(spent.count());
std::map<std::string, MatVec> mapped;
for (int i = 0; i < mats.size(); i++) {
std::string commod = fuel_outcommod(mats[i]);
mapped[commod].push_back(mats[i]);
}
// needed so we trade away oldest assemblies first
std::map<std::string, MatVec>::iterator it;
for (it = mapped.begin(); it != mapped.end(); ++it) {
std::reverse(it->second.begin(), it->second.end());
}
return mapped;
}
void Reactor::PushSpent(std::map<std::string, MatVec> leftover) {
std::map<std::string, MatVec>::iterator it;
for (it = leftover.begin(); it != leftover.end(); ++it) {
// undo reverse in PopSpent to make sure oldest assemblies come out first
std::reverse(it->second.begin(), it->second.end());
spent.Push(it->second);
}
}
void Reactor::RecordSideProduct(bool produce){
if (hybrid_){
double value;
for (int i = 0; i < side_products.size(); i++) {
if (produce){
value = side_product_quantity[i];
}
else {
value = 0;
}
context()
->NewDatum("ReactorSideProducts")
->AddVal("AgentId", id())
->AddVal("Time", context()->time())
->AddVal("Product", side_products[i])
->AddVal("Value", value)
->Record();
}
}
}
void Reactor::Record(std::string name, std::string val) {
context()
->NewDatum("ReactorEvents")
->AddVal("AgentId", id())
->AddVal("Time", context()->time())
->AddVal("Event", name)
->AddVal("Value", val)
->Record();
}
void Reactor::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* ConstructReactor(cyclus::Context* ctx) {
return new Reactor(ctx);
}
} // namespace cycamore