CYCAMORE
src/storage_tests.cc
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 
3 #include "storage_tests.h"
4 
5 namespace storage {
6 
7 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
8 void StorageTest::SetUp() {
9  src_facility_ = new Storage(tc_.get());
11  SetUpStorage();
12 }
13 
14 void StorageTest::TearDown() {
15  delete src_facility_;
16 }
17 
19  in_r1 = "in_r1";
20  in_c1.push_back("in_c1");
21  out_c1.push_back("out_c1");
22  residence_time = 10;
23  max_inv_size = 200;
24  throughput = 20;
26 
27  cyclus::CompMap v;
28  v[922350000] = 1;
29  v[922380000] = 2;
30  cyclus::Composition::Ptr recipe = cyclus::Composition::CreateFromAtom(v);
31  tc_.get()->AddRecipe(in_r1, recipe);
32 }
33 
42 }
43 
44 void StorageTest::TestInitState(Storage* fac){
45  EXPECT_EQ(residence_time, fac->residence_time);
46  EXPECT_EQ(max_inv_size, fac->max_inv_size);
47  EXPECT_EQ(throughput, fac->throughput);
48  EXPECT_EQ(in_r1, fac->in_recipe);
49 }
50 
51 void StorageTest::TestAddMat(Storage* fac,
52  cyclus::Material::Ptr mat){
53  double amt = mat->quantity();
54  double before = fac->inventory.quantity();
55  fac->AddMat_(mat);
56  double after = fac->inventory.quantity();
57  EXPECT_EQ(amt, after - before);
58 }
59 
60 void StorageTest::TestBuffers(Storage* fac, double inv,
61  double proc, double ready, double stocks){
62  double t = tc_.get()->time();
63 
64  EXPECT_EQ(inv, fac->inventory.quantity());
65  EXPECT_EQ(proc, fac->processing.quantity());
66  EXPECT_EQ(stocks, fac->stocks.quantity());
67  EXPECT_EQ(ready, fac->ready.quantity());
68 }
69 
70 void StorageTest::TestStocks(Storage* fac, cyclus::CompMap v){
71 
72  cyclus::toolkit::ResBuf<cyclus::Material>* buffer = &fac->stocks;
73  Material::Ptr final_mat = cyclus::ResCast<Material>(buffer->PopBack());
74  cyclus::CompMap final_comp = final_mat->comp()->atom();
75  EXPECT_EQ(final_comp,v);
76 
77 }
78 
79 void StorageTest::TestCurrentCap(Storage* fac, double inv){
80 
81  EXPECT_EQ(inv, fac->current_capacity());
82 }
83 
84 void StorageTest::TestReadyTime(Storage* fac, int t){
85 
86  EXPECT_EQ(t, fac->ready_time());
87 }
88 
89 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
90 TEST_F(StorageTest, clone) {
91  Storage* cloned_fac =
92  dynamic_cast<Storage*> (src_facility_->Clone());
93  TestInitState(cloned_fac);
94 }
95 
96 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
97 TEST_F(StorageTest, InitialState) {
98  // Test things about the initial state of the facility here
100 }
101 
102 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
103 TEST_F(StorageTest, CurrentCapacity){
105  max_inv_size = 1e299;
106  SetUpStorage();
108 }
109 
110 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
111 TEST_F(StorageTest, Print) {
112  EXPECT_NO_THROW(std::string s = src_facility_->str());
113  // Test Storage specific aspects of the print method here
114 }
115 
116 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
117 TEST_F(StorageTest, AddMats) {
118  double cap = max_inv_size;
119  cyclus::Material::Ptr mat = cyclus::NewBlankMaterial(0.5*cap);
121 
122  cyclus::Composition::Ptr rec = tc_.get()->GetRecipe(in_r1);
123  cyclus::Material::Ptr recmat = cyclus::Material::CreateUntracked(0.5*cap, rec);
124  TestAddMat(src_facility_, recmat);
125 }
126 
127 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
128 TEST_F(StorageTest, Tick) {
129  ASSERT_NO_THROW(src_facility_->Tick());
130  // Test Storage specific behaviors of the Tick function here
131 }
132 
133 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
134 TEST_F(StorageTest, Tock) {
135 
136  // initially, nothing in the buffers
137  TestBuffers(src_facility_,0,0,0,0);
138 
139  double cap = throughput;
140  cyclus::Composition::Ptr rec = tc_.get()->GetRecipe(in_r1);
141  cyclus::Material::Ptr mat = cyclus::Material::CreateUntracked(cap, rec);
143 
144  // affter add, the inventory has the material
145  TestBuffers(src_facility_,cap,0,0,0);
146 
147  EXPECT_NO_THROW(src_facility_->Tock());
148 
149  // after tock, the processing buffer has the material
150  TestBuffers(src_facility_,0,cap,0,0);
151 
152  EXPECT_EQ(0, tc_.get()->time());
153  for( int i = 1; i < residence_time-1; ++i){
154  tc_.get()->time(i);
155  EXPECT_NO_THROW(src_facility_->Tock());
156  TestBuffers(src_facility_,0,cap,0,0);
157  }
158 
159  tc_.get()->time(residence_time);
160  EXPECT_EQ(residence_time, tc_.get()->time());
162  src_facility_->Tock();
163  TestBuffers(src_facility_,0,0,0,cap);
164 
165  tc_.get()->time(residence_time+1);
167  src_facility_->Tock();
168  TestBuffers(src_facility_,0,0,0,cap);
169 
170 }
171 
172 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
173 TEST_F(StorageTest, NoProcessTime) {
174  // tests what happens when the process time is zero
175  residence_time = 0;
176  SetUpStorage();
177  EXPECT_EQ(0, residence_time);
178 
179  double cap = throughput;
180  cyclus::Composition::Ptr rec = tc_.get()->GetRecipe(in_r1);
181  cyclus::Material::Ptr mat = cyclus::Material::CreateUntracked(cap, rec);
183 
184  // affter add, the inventory has the material
185  TestBuffers(src_facility_,cap,0,0,0);
186 
187  EXPECT_NO_THROW(src_facility_->Tock());
188 
189  // affter tock, the stocks have the material
190  TestBuffers(src_facility_,0,0,0,cap);
191 }
192 
193 TEST_F(StorageTest, NoConvert) {
194 // Make sure no conversion occurs
195  double cap = throughput;
196  cyclus::Composition::Ptr rec = tc_.get()->GetRecipe(in_r1);
197  cyclus::Material::Ptr mat = cyclus::Material::CreateUntracked(cap, rec);
199 
200  EXPECT_NO_THROW(src_facility_->Tock());
201 
202  tc_.get()->time(residence_time);
204  EXPECT_NO_THROW(src_facility_->Tock());
205  TestBuffers(src_facility_,0,0,0,cap);
206  cyclus::CompMap in_rec;
207  in_rec[922350000] = 1;
208  in_rec[922380000] = 2;
209  TestStocks(src_facility_,in_rec);
210 }
211 
212 TEST_F(StorageTest, MultipleSmallBatches) {
213  // Add first small batch
214  double cap = throughput;
215  cyclus::Composition::Ptr rec = tc_.get()->GetRecipe(in_r1);
216  cyclus::Material::Ptr mat = cyclus::Material::CreateUntracked(0.2*cap, rec);
218 
219  // After add, material is in inventory
220  TestBuffers(src_facility_,0.2*cap,0,0,0);
221 
222  // Move first batch into processing
223  src_facility_->Tock();
224  TestBuffers(src_facility_,0,0.2*cap,0,0);
225 
226  // Add second small batch
227  tc_.get()->time(2);
228  src_facility_->Tock();
229  cyclus::Material::Ptr mat1 = cyclus::Material::CreateUntracked(0.3*cap, rec);
231  TestBuffers(src_facility_,0.3*cap,0.2*cap,0,0);
232 
233  // Move second batch into processing
234  EXPECT_NO_THROW(src_facility_->Tock());
235  TestBuffers(src_facility_,0,0.5*cap,0,0);
236 
237  // Move first batch to stocks
238  tc_.get()->time(residence_time);
240  EXPECT_NO_THROW(src_facility_->Tock());
241  TestBuffers(src_facility_,0,0.3*cap,0,0.2*cap);
242 
243  tc_.get()->time(residence_time+2);
244  EXPECT_NO_THROW(src_facility_->Tock());
245  TestBuffers(src_facility_,0,0,0,0.5*cap);
246 }
247 
248 
249 TEST_F(StorageTest, ChangeCapacity) {
250  // src_facility_->discrete_handling_(0);
251  max_inv_size = 10000;
252  // Set throughput, add first batch
253  throughput = 300;
254  SetUpStorage();
255  double cap1 = throughput;
256  cyclus::Composition::Ptr rec = tc_.get()->GetRecipe(in_r1);
257  cyclus::Material::Ptr mat1 = cyclus::Material::CreateUntracked(cap1, rec);
258  TestAddMat(src_facility_, mat1);
259  EXPECT_NO_THROW(src_facility_->Tock());
260  TestBuffers(src_facility_,0,cap1,0,0);
261 
262  // Increase throughput, add second and third batches
263  tc_.get()->time(2);
264  throughput = 500;
265  SetUpStorage();
266  double cap2 = throughput;
267  cyclus::Material::Ptr mat2 = cyclus::Material::CreateUntracked(cap2,rec);
268  TestAddMat(src_facility_, mat2);
269  EXPECT_NO_THROW(src_facility_->Tock());
270  TestBuffers(src_facility_,0,cap2+cap1,0,0);
271  tc_.get()->time(3);
272  cyclus::Material::Ptr mat3 = cyclus::Material::CreateUntracked(cap2,rec);
273  TestAddMat(src_facility_, mat3);
274  EXPECT_NO_THROW(src_facility_->Tock());
275  TestBuffers(src_facility_,0,cap2*2+cap1,0,0);
276 
277  // Move first batch to stocks
278  tc_.get()->time(residence_time);
279  EXPECT_NO_THROW(src_facility_->Tock());
280  TestBuffers(src_facility_,0,2*cap2,0,cap1);
281 
282  // Decrease throughput and move portion of second batch to stocks
283  throughput = 400;
284  SetUpStorage();
285  tc_.get()->time(residence_time+2);
286  EXPECT_EQ(400, throughput);
287  EXPECT_NO_THROW(src_facility_->Tock());
288  TestBuffers(src_facility_,0,cap2,100,cap1+400);
289 
290  // Continue to move second batch // and portion of third
291  tc_.get()->time(residence_time+3);
292  EXPECT_NO_THROW(src_facility_->Tock());
293  TestBuffers(src_facility_,0,0,200,cap1+cap2+300);
294 
295  // Move remainder of third batch
296  tc_.get()->time(residence_time+4);
297  EXPECT_NO_THROW(src_facility_->Tock());
298  TestBuffers(src_facility_,0,0,0,cap1+cap2+cap2);
299 
300 }
301 
302 TEST_F(StorageTest, TwoBatchSameTime) {
303  // Add first small batch
304  double cap = throughput;
305  cyclus::Composition::Ptr rec = tc_.get()->GetRecipe(in_r1);
306  cyclus::Material::Ptr mat = cyclus::Material::CreateUntracked(0.2*cap, rec);
308 
309  // After add, material is in inventory
310  TestBuffers(src_facility_,0.2*cap,0,0,0);
311  cyclus::Material::Ptr mat1 = cyclus::Material::CreateUntracked(0.2*cap, rec);
312  TestAddMat(src_facility_, mat1);
313  TestBuffers(src_facility_,0.4*cap,0,0,0);
314 
315  EXPECT_NO_THROW(src_facility_->Tock());
316  TestBuffers(src_facility_,0,0.4*cap,0,0);
317 
318  // Move material to stocks
319  tc_.get()->time(residence_time);
320  EXPECT_NO_THROW(src_facility_->Tock());
321  TestBuffers(src_facility_,0,0,0,0.4*cap);
322 }
323 
324 TEST_F(StorageTest,ChangeProcessTime){
325  // Initialize process time variable and add first batch
326  int proc_time1 = residence_time;
327  double cap = throughput;
328  cyclus::Composition::Ptr rec = tc_.get()->GetRecipe(in_r1);
329  cyclus::Material::Ptr mat = cyclus::Material::CreateUntracked(cap, rec);
331  TestBuffers(src_facility_,cap,0,0,0);
332 
333  // Move material to processing
334  EXPECT_NO_THROW(src_facility_->Tock());
335  TestBuffers(src_facility_,0,cap,0,0);
336 
337  // Add second batch
338  cyclus::Material::Ptr mat1 = cyclus::Material::CreateUntracked(cap,rec);
339  tc_.get()->time(8);
341  TestBuffers(src_facility_,cap,cap,0,0);
342  EXPECT_NO_THROW(src_facility_->Tock());
343  TestBuffers(src_facility_,0,2*cap,0,0);
344 
345  // Increase process time
346  residence_time = proc_time1+5;
347  SetUpStorage();
348  // src_facility_->residence_time = proc_time1+5;
349  EXPECT_EQ(residence_time,proc_time1+5);
350  EXPECT_EQ(residence_time,15);
351  int proc_time2 = residence_time;
352 
353  // Make sure material doesn't move before new process time
354  for( int i=proc_time1; i < proc_time2 - 1; ++i){
355  tc_.get()->time(i);
356  EXPECT_NO_THROW(src_facility_->Tock());
357  TestBuffers(src_facility_,0,2*cap,0,0);
358  }
359 
360  // Move first batch to stocks
361  tc_.get()->time(proc_time2);
362  EXPECT_NO_THROW(src_facility_->Tock());
363  TestBuffers(src_facility_,0,cap,0,cap);
364 
365  // Decrease process time
366  residence_time = proc_time2-3;
367  SetUpStorage();
368  int proc_time3 = residence_time;
369 
370  // Move second batch to stocks
371  tc_.get()->time(proc_time3 +8);
372  EXPECT_NO_THROW(src_facility_->Tock());
373  TestBuffers(src_facility_,0,0,0,2*cap);
374 
375 }
376 
377 TEST_F(StorageTest,DifferentRecipe){
378  // Initialize material with different recipe than in_recipe
379  double cap = throughput;
380  cyclus::CompMap v;
381  v[922350000] = 3;
382  v[922380000] = 1;
383  cyclus::Composition::Ptr rec = cyclus::Composition::CreateFromAtom(v);
384  cyclus::Material::Ptr mat = cyclus::Material::CreateUntracked(cap, rec);
385 
386  // Move material through the facility
388  TestBuffers(src_facility_,cap,0,0,0);
389  EXPECT_NO_THROW(src_facility_->Tock());
390  TestBuffers(src_facility_,0,cap,0,0);
391  tc_.get()->time(residence_time);
392  EXPECT_NO_THROW(src_facility_->Tock());
393  TestBuffers(src_facility_,0,0,0,cap);
394 }
395 
396 TEST_F(StorageTest, BehaviorTest){
397  // Verify Storage behavior
398 
399  std::string config =
400  " <in_commods> <val>spent_fuel</val> </in_commods> "
401  " <out_commods> <val>dry_spent</val> </out_commods> "
402  " <residence_time>1</residence_time>"
403  " <max_inv_size>10</max_inv_size>";
404 
405  int simdur = 3;
406 
407  cyclus::MockSim sim(cyclus::AgentSpec (":cycamore:Storage"), config, simdur);
408 
409  sim.AddSource("spent_fuel").Finalize();
410  sim.AddSink("dry_spent").Finalize();
411 
412  int id = sim.Run();
413 
414  // return all transactions where our sstorage facility is the sender
415  std::vector<cyclus::Cond> conds;
416  conds.push_back(cyclus::Cond("Commodity", "==", std::string("dry_spent")));
417  cyclus::QueryResult qr = sim.db().Query("Transactions", &conds);
418  int n_trans = qr.rows.size();
419  EXPECT_EQ(1, n_trans) << "expected 1 transactions, got " << n_trans;
420 
421 }
422 
423 TEST_F(StorageTest, MultipleCommods){
424  // Verify Storage accepting Multiple Commods
425 
426  std::string config =
427  " <in_commods> <val>spent_fuel</val>"
428  " <val>spent_fuel2</val> </in_commods>"
429  " <in_commod_prefs> <val>1</val>"
430  " <val>1</val> </in_commod_prefs>"
431  " <out_commods> <val>dry_spent</val> </out_commods> "
432  " <max_inv_size>10</max_inv_size>";
433 
434  int simdur = 2;
435 
436  cyclus::MockSim sim(cyclus::AgentSpec (":cycamore:Storage"), config, simdur);
437 
438  sim.AddSource("spent_fuel").capacity(5).Finalize();
439  sim.AddSource("spent_fuel2").capacity(5).Finalize();
440  sim.AddSink("dry_spent").Finalize();
441 
442  int id = sim.Run();
443 
444  // return all transactions where our sstorage facility is the acceptor
445  std::vector<cyclus::Cond> conds;
446  conds.push_back(cyclus::Cond("Commodity", "==", std::string("spent_fuel")));
447  cyclus::QueryResult qr = sim.db().Query("Transactions", &conds);
448  int n_trans = qr.rows.size();
449  EXPECT_EQ(1, n_trans) << "expected 1 transactions, got " << n_trans;
450 
451  std::vector<cyclus::Cond> conds2;
452  conds2.push_back(cyclus::Cond("Commodity", "==", std::string("spent_fuel2")));
453  cyclus::QueryResult qr2 = sim.db().Query("Transactions", &conds2);
454  int n_trans2 = qr2.rows.size();
455  EXPECT_EQ(1, n_trans2) << "expected 1 transactions, got " << n_trans;
456 }
457 
458 TEST_F(StorageTest, PositionInitialize){
459  // Verify Storage behavior
460 
461  std::string config =
462  " <in_commods> <val>spent_fuel</val> </in_commods> "
463  " <out_commods> <val>dry_spent</val> </out_commods> "
464  " <residence_time>1</residence_time>"
465  " <max_inv_size>10</max_inv_size>";
466 
467  int simdur = 3;
468 
469  cyclus::MockSim sim(cyclus::AgentSpec (":cycamore:Storage"), config, simdur);
470 
471  sim.AddSource("spent_fuel").Finalize();
472  sim.AddSink("dry_spent").Finalize();
473 
474  int id = sim.Run();
475 
476  cyclus::QueryResult qr = sim.db().Query("AgentPosition", NULL);
477  EXPECT_EQ(qr.GetVal<double>("Latitude"), 0.0);
478  EXPECT_EQ(qr.GetVal<double>("Longitude"), 0.0);
479 }
480 
481 TEST_F(StorageTest, Longitude){
482  // Verify Storage behavior
483 
484  std::string config =
485  " <in_commods> <val>spent_fuel</val> </in_commods> "
486  " <out_commods> <val>dry_spent</val> </out_commods> "
487  " <residence_time>1</residence_time>"
488  " <max_inv_size>10</max_inv_size>"
489  " <latitude>50.0</latitude> "
490  " <longitude>35.0</longitude> ";
491 
492  int simdur = 3;
493 
494  cyclus::MockSim sim(cyclus::AgentSpec (":cycamore:Storage"), config, simdur);
495 
496  sim.AddSource("spent_fuel").Finalize();
497  sim.AddSink("dry_spent").Finalize();
498 
499  int id = sim.Run();
500 
501  cyclus::QueryResult qr = sim.db().Query("AgentPosition", NULL);
502  EXPECT_EQ(qr.GetVal<double>("Latitude"), 50.0);
503  EXPECT_EQ(qr.GetVal<double>("Longitude"), 35.0);
504 }
505 
506 } // namespace storage
507 
508 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
509 cyclus::Agent* StorageConstructor(cyclus::Context* ctx) {
510  return new storage::Storage(ctx);
511 }
512 
513 // required to get functionality in cyclus agent unit tests library
514 #ifndef CYCLUS_AGENT_TESTS_CONNECTED
515 int ConnectAgentTests();
517 #define CYCLUS_AGENT_TESTS_CONNECTED cyclus_agent_tests_connected
518 #endif // CYCLUS_AGENT_TESTS_CONNECTED
519 
520 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
521 INSTANTIATE_TEST_CASE_P(StorageFac, FacilityTests,
522  ::testing::Values(&StorageConstructor));
523 
524 INSTANTIATE_TEST_CASE_P(StorageFac, AgentTests,
525  ::testing::Values(&StorageConstructor));
virtual cyclus::Agent * Clone()
void TestCurrentCap(storage::Storage *fac, double inv)
void TestInitState(storage::Storage *fac)
void TestAddMat(storage::Storage *fac, cyclus::Material::Ptr mat)
virtual std::string str()
A verbose printer for the Storage Facility.
static int cyclus_agent_tests_connected
void TestStocks(storage::Storage *fac, cyclus::CompMap v)
virtual void Tock()
The handleTick function specific to the Storage.
void TestBuffers(storage::Storage *fac, double inv, double proc, double ready, double stocks)
std::vector< std::string > in_c1
cyclus::Agent * StorageConstructor(cyclus::Context *ctx)
void TestReadyTime(storage::Storage *fac, int t)
virtual void Tick()
The handleTick function specific to the Storage.
cycamore::GrowthRegion string
INSTANTIATE_TEST_CASE_P(StorageFac, FacilityTests, ::testing::Values(&StorageConstructor))
TEST_F(StorageTest, clone)
This Facility is intended to hold materials for a user specified amount of time in order to model a s...
std::vector< std::string > out_c1
int ConnectAgentTests()
std::vector< std::string > in_commods
std::vector< std::string > out_commods