CYCAMORE
Loading...
Searching...
No Matches
build/cycamore/sink_tests.cc
Go to the documentation of this file.
1#include <gtest/gtest.h>
2
3#include "facility_tests.h"
4#include "agent_tests.h"
5#include "resource_helpers.h"
6#include "infile_tree.h"
7#include "xml_parser.h"
8
9#include "sink_tests.h"
10
11// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
13 src_facility = new cycamore::Sink(tc_.get());
14 trader = tc_.trader();
16 SetUpSink();
17}
18
19// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
21 delete src_facility;
22}
23
24// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
26 commod1_ = "acommod";
27 commod2_ = "bcommod";
28 commod3_ = "ccommod";
29 capacity_ = 5;
30 inv_ = capacity_ * 2;
31 qty_ = capacity_ * 0.5;
32 ncommods_ = 2;
33}
34
35// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
42
43// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
44TEST_F(SinkTest, InitialState) {
45 EXPECT_DOUBLE_EQ(0.0, src_facility->InventorySize());
46 EXPECT_DOUBLE_EQ(capacity_, src_facility->Capacity());
47 EXPECT_DOUBLE_EQ(inv_, src_facility->MaxInventorySize());
48 EXPECT_DOUBLE_EQ(capacity_, src_facility->SpaceAvailable());
49 EXPECT_DOUBLE_EQ(0.0, src_facility->InventorySize());
50 std::string arr[] = {commod1_, commod2_};
51 std::vector<std::string> vexp (arr, arr + sizeof(arr) / sizeof(arr[0]) );
52 EXPECT_EQ(vexp, src_facility->input_commodities());
53
54 src_facility->EnterNotify();
55 double pref[] = {cyclus::kDefaultPref, cyclus::kDefaultPref};
56 std::vector<double> vpref (pref, pref + sizeof(pref) / sizeof(pref[0]) );
57 EXPECT_EQ(vpref, src_facility->input_commodity_preferences());
58}
59
60// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
62 using cycamore::Sink;
63 Sink* cloned_fac = dynamic_cast<cycamore::Sink*>
64 (src_facility->Clone());
65
66 EXPECT_DOUBLE_EQ(0.0, cloned_fac->InventorySize());
67 EXPECT_DOUBLE_EQ(capacity_, cloned_fac->Capacity());
68 EXPECT_DOUBLE_EQ(inv_, cloned_fac->MaxInventorySize());
69 EXPECT_DOUBLE_EQ(capacity_, cloned_fac->SpaceAvailable());
70 std::string arr[] = {commod1_, commod2_};
71 std::vector<std::string> vexp (arr, arr + sizeof(arr) / sizeof(arr[0]) );
72 EXPECT_EQ(vexp, cloned_fac->input_commodities());
73
74 delete cloned_fac;
75}
76
77// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
78TEST_F(SinkTest, DISABLED_XMLInit) {
79 std::stringstream ss;
80 ss << "<start>"
81 << "<name>fooname</name>"
82 << "<config>"
83 << "<UNSPECIFIED>"
84 << "<input>"
85 << " <commodities>"
86 << " <incommodity>" << commod1_ << "</incommodity>"
87 << " <incommodity>" << commod2_ << "</incommodity>"
88 << " </commodities>"
89 << " <input_capacity>" << capacity_ << "</input_capacity>"
90 << " <inventorysize>" << inv_ << "</inventorysize>"
91 << "</input>"
92 << "</UNSPECIFIED>"
93 << "</config>"
94 << "</start>";
95
96 cyclus::XMLParser p;
97 p.Init(ss);
98 cyclus::InfileTree engine(p);
99 cycamore::Sink fac(tc_.get());
100
101 // EXPECT_NO_THROW(fac.InitFrom(&engine););
102 std::string arr[] = {commod1_, commod2_};
103 std::vector<std::string> vexp (arr, arr + sizeof(arr) / sizeof(arr[0]) );
104 EXPECT_EQ(vexp, fac.input_commodities());
105 EXPECT_DOUBLE_EQ(capacity_, fac.Capacity());
106 EXPECT_DOUBLE_EQ(inv_, fac.MaxInventorySize());
107 EXPECT_DOUBLE_EQ(capacity_, fac.SpaceAvailable());
108 EXPECT_DOUBLE_EQ(0.0, fac.InventorySize());
109}
110
111// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
112TEST_F(SinkTest, Requests) {
113 using cyclus::Request;
114 using cyclus::RequestPortfolio;
115 using cyclus::CapacityConstraint;
116 using cyclus::Material;
117
118 std::string arr[] = {commod1_, commod2_};
119 std::vector<std::string> commods (arr, arr + sizeof(arr) / sizeof(arr[0]) );
120
121 src_facility->EnterNotify();
122 std::set<RequestPortfolio<Material>::Ptr> ports =
123 src_facility->GetMatlRequests();
124
125 ASSERT_EQ(ports.size(), 1);
126 ASSERT_EQ(ports.begin()->get()->qty(), capacity_);
127 const std::vector<Request<Material>*>& requests =
128 ports.begin()->get()->requests();
129 ASSERT_EQ(requests.size(), 2);
130
131 for (int i = 0; i < ncommods_; ++i) {
132 Request<Material>* req = *(requests.begin() + i);
133 EXPECT_EQ(req->requester(), src_facility);
134 EXPECT_EQ(req->commodity(), commods[i]);
135 }
136
137 const std::set< CapacityConstraint<Material> >& constraints =
138 ports.begin()->get()->constraints();
139 EXPECT_EQ(constraints.size(), 0);
140}
141
142// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
143TEST_F(SinkTest, EmptyRequests) {
144 using cyclus::Material;
145 using cyclus::RequestPortfolio;
146
147 src_facility->Capacity(0);
148 std::set<RequestPortfolio<Material>::Ptr> ports =
149 src_facility->GetMatlRequests();
150 EXPECT_TRUE(ports.empty());
151}
152
153// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
154TEST_F(SinkTest, Accept) {
155 using cyclus::Bid;
156 using cyclus::Material;
157 using cyclus::Request;
158 using cyclus::Trade;
159 using test_helpers::get_mat;
160
161 double qty = qty_ * 2;
162 std::vector< std::pair<Trade<Material>,
163 Material::Ptr> > responses;
164
165 Request<Material>* req1 =
166 Request<Material>::Create(get_mat(922350000, qty_), src_facility,
167 commod1_);
168 Bid<Material>* bid1 = Bid<Material>::Create(req1, get_mat(), trader);
169
170 Request<Material>* req2 =
171 Request<Material>::Create(get_mat(922350000, qty_), src_facility,
172 commod2_);
173 Bid<Material>* bid2 =
174 Bid<Material>::Create(req2, get_mat(922350000, qty_), trader);
175
176 Trade<Material> trade1(req1, bid1, qty_);
177 responses.push_back(std::make_pair(trade1, get_mat(922350000, qty_)));
178 Trade<Material> trade2(req2, bid2, qty_);
179 responses.push_back(std::make_pair(trade2, get_mat(922350000, qty_)));
180
181 EXPECT_DOUBLE_EQ(0.0, src_facility->InventorySize());
182 src_facility->AcceptMatlTrades(responses);
183 EXPECT_DOUBLE_EQ(qty, src_facility->InventorySize());
184}
185// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
186TEST_F(SinkTest, InRecipe){
187// Create a context
188 using cyclus::RequestPortfolio;
189 using cyclus::Material;
190 using cyclus::Request;
191 cyclus::Recorder rec;
192 cyclus::Timer ti;
193 cyclus::Context ctx(&ti, &rec);
194
195 // define some test material in the context
196 cyclus::CompMap m;
197 m[922350000] = 1;
198 m[922580000] = 2;
199
200 cyclus::Composition::Ptr c = cyclus::Composition::CreateFromMass(m);
201 ctx.AddRecipe("some_u",c) ;
202
203 // create a sink facility to interact with the DRE
204 cycamore::Sink* snk = new cycamore::Sink(&ctx);
205 snk->AddCommodity("some_u");
206 snk->EnterNotify();
207
208 std::set<RequestPortfolio<Material>::Ptr> ports =
209 snk->GetMatlRequests();
210 ASSERT_EQ(ports.size(), 1);
211
212 const std::vector<Request<Material>*>& requests =
213 ports.begin()->get()->requests();
214 ASSERT_EQ(requests.size(), 1);
215
216 Request<Material>* req = *requests.begin();
217 EXPECT_EQ(req->requester(), snk);
218 EXPECT_EQ(req->commodity(),"some_u");
219}
220
221// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
222TEST_F(SinkTest, BidPrefs) {
223 using cyclus::QueryResult;
224 using cyclus::Cond;
225
226 std::string config =
227 " <in_commods>"
228 " <val>commods_1</val>"
229 " <val>commods_2</val>"
230 " </in_commods>"
231 " <in_commod_prefs>"
232 " <val>10</val> "
233 " <val>1</val> "
234 " </in_commod_prefs>"
235 " <capacity>1</capacity>"
236 " <input_capacity>1.0</input_capacity> ";
237
238 int simdur = 1;
239 cyclus::MockSim sim(cyclus::AgentSpec
240 (":cycamore:Sink"), config, simdur);
241
242 sim.AddSource("commods_1")
243 .capacity(1)
244 .Finalize();
245
246 sim.AddSource("commods_2")
247 .capacity(1)
248 .Finalize();
249
250 int id = sim.Run();
251
252 std::vector<Cond> conds;
253 conds.push_back(Cond("Commodity", "==", std::string("commods_1")));
254 QueryResult qr = sim.db().Query("Transactions", &conds);
255
256 // should trade only with #1 since it has highier priority
257 EXPECT_EQ(1, qr.rows.size());
258
259 std::vector<Cond> conds2;
260 conds2.push_back(Cond("Commodity", "==", std::string("commods_2")));
261 QueryResult qr2 = sim.db().Query("Transactions", &conds2);
262
263 // should trade only with #1 since it has highier priority
264 EXPECT_EQ(0, qr2.rows.size());
265
266}
267// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
269 EXPECT_NO_THROW(std::string s = src_facility->str());
270}
271
272TEST_F(SinkTest, PositionInitialize) {
273 using cyclus::QueryResult;
274
275 std::string config =
276 " <in_commods>"
277 " <val>commods_1</val>"
278 " <val>commods_2</val>"
279 " </in_commods>"
280 " <in_commod_prefs>"
281 " <val>10</val> "
282 " <val>1</val> "
283 " </in_commod_prefs>"
284 " <capacity>1</capacity>"
285 " <input_capacity>1.0</input_capacity> ";
286
287 int simdur = 1;
288 cyclus::MockSim sim(cyclus::AgentSpec
289 (":cycamore:Sink"), config, simdur);
290
291 sim.AddSource("commods_1")
292 .capacity(1)
293 .Finalize();
294
295 sim.AddSource("commods_2")
296 .capacity(1)
297 .Finalize();
298
299 int id = sim.Run();
300
301 QueryResult qr = sim.db().Query("AgentPosition", NULL);
302 EXPECT_EQ(qr.GetVal<double>("Latitude"), 0.0);
303 EXPECT_EQ(qr.GetVal<double>("Longitude"), 0.0);
304}
305
306TEST_F(SinkTest, PositionInitialize2) {
307 using cyclus::QueryResult;
308
309 std::string config =
310 " <in_commods>"
311 " <val>commods_1</val>"
312 " <val>commods_2</val>"
313 " </in_commods>"
314 " <in_commod_prefs>"
315 " <val>10</val> "
316 " <val>1</val> "
317 " </in_commod_prefs>"
318 " <capacity>1</capacity>"
319 " <input_capacity>1.0</input_capacity> "
320 " <latitude>50.0</latitude> "
321 " <longitude>35.0</longitude> ";
322
323 int simdur = 1;
324 cyclus::MockSim sim(cyclus::AgentSpec
325 (":cycamore:Sink"), config, simdur);
326
327 sim.AddSource("commods_1")
328 .capacity(1)
329 .Finalize();
330
331 sim.AddSource("commods_2")
332 .capacity(1)
333 .Finalize();
334
335 int id = sim.Run();
336
337 QueryResult qr = sim.db().Query("AgentPosition", NULL);
338 EXPECT_EQ(qr.GetVal<double>("Longitude"), 35.0);
339 EXPECT_EQ(qr.GetVal<double>("Latitude"), 50.0);
340
341}
342
343// A random number pulled from a uniform integer distribution can be
344// implemented as the request size
345TEST_F(SinkTest, RandomUniformSize) {
346 using cyclus::QueryResult;
347
348 std::string config =
349 " <in_commods>"
350 " <val>commods_1</val>"
351 " </in_commods>"
352 " <capacity>10</capacity>"
353 " <random_size_type>UniformReal</random_size_type> ";
354
355 int simdur = 1;
356 cyclus::MockSim sim(cyclus::AgentSpec
357 (":cycamore:Sink"), config, simdur);
358 sim.AddSource("commods_1").capacity(10).Finalize();
359 int id = sim.Run();
360
361 QueryResult qr = sim.db().Query("Resources", NULL);
362 EXPECT_EQ(qr.rows.size(), 1);
363 // Given the PRNG with default seed, the resource should have mass 9.41273
364 EXPECT_NEAR(qr.GetVal<double>("Quantity"), 9.41273, 0.0001);
365}
366
367// A random number pulled from a normal int distribution with default mean and
368// stddev can be implemented as the request size
369TEST_F(SinkTest, RandomNormalSize) {
370 using cyclus::QueryResult;
371
372 std::string config =
373 " <in_commods>"
374 " <val>commods_1</val>"
375 " </in_commods>"
376 " <capacity>10</capacity>"
377 " <random_size_type>NormalReal</random_size_type> ";
378
379 int simdur = 1;
380 cyclus::MockSim sim(cyclus::AgentSpec
381 (":cycamore:Sink"), config, simdur);
382 sim.AddSource("commods_1").capacity(10).Finalize();
383 int id = sim.Run();
384
385 QueryResult qr = sim.db().Query("Resources", NULL);
386 EXPECT_EQ(qr.rows.size(), 1);
387 // Given the PRNG with default seed, the resource should have mass 9.60929
388 EXPECT_NEAR(qr.GetVal<double>("Quantity"), 9.60929, 0.0001);
389}
390
391// A random number pulled from a normal int distribution with user-defined mean
392// and stddev can be implemented as the request size
393TEST_F(SinkTest, RandomNormalSizeWithMeanSttdev) {
394 using cyclus::QueryResult;
395
396 std::string config =
397 " <in_commods>"
398 " <val>commods_1</val>"
399 " </in_commods>"
400 " <capacity>10</capacity>"
401 " <random_size_type>NormalReal</random_size_type> "
402 " <random_size_mean>0.5</random_size_mean> "
403 " <random_size_stddev>0.2</random_size_stddev> ";
404
405 int simdur = 1;
406 cyclus::MockSim sim(cyclus::AgentSpec
407 (":cycamore:Sink"), config, simdur);
408 sim.AddSource("commods_1").capacity(10).Finalize();
409 int id = sim.Run();
410
411 QueryResult qr = sim.db().Query("Resources", NULL);
412 EXPECT_EQ(qr.rows.size(), 1);
413 // Given the PRNG with default seed, the resource should have mass 1.52979
414 EXPECT_NEAR(qr.GetVal<double>("Quantity"), 1.52979, 0.0001);
415}
416
417// A random number pulled from a uniform integer distribution can be
418// implemented as the buying frequency
419TEST_F(SinkTest, RandomUniformFreq) {
420 using cyclus::QueryResult;
421
422 std::string config =
423 " <in_commods>"
424 " <val>commods_1</val>"
425 " </in_commods>"
426 " <capacity>10</capacity>"
427 " <random_frequency_type>UniformInt</random_frequency_type> "
428 " <random_frequency_min>2</random_frequency_min> "
429 " <random_frequency_max>4</random_frequency_max> ";
430
431 int simdur = 3;
432 cyclus::MockSim sim(cyclus::AgentSpec
433 (":cycamore:Sink"), config, simdur);
434 sim.AddSource("commods_1").capacity(10).Finalize();
435 int id = sim.Run();
436
437 QueryResult qr = sim.db().Query("Transactions", NULL);
438 // only one transaction has occurred
439 EXPECT_EQ(qr.rows.size(), 1);
440 // Get the time from the first transaction in the database (0th entry)
441 int trans_time = qr.GetVal<int>("Time", 0);
442 // Given the PRNG with default seed , this time should be time step 2
443 EXPECT_EQ(trans_time, 2);
444}
445
446// A random number pulled from a normal int distribution with default mean and
447// stddev can be implemented as the buying frequency
448TEST_F(SinkTest, RandomNormalFreq) {
449 using cyclus::QueryResult;
450
451 std::string config =
452 " <in_commods>"
453 " <val>commods_1</val>"
454 " </in_commods>"
455 " <capacity>10</capacity>"
456 " <random_frequency_type>NormalInt</random_frequency_type> ";
457
458 int simdur = 3;
459 cyclus::MockSim sim(cyclus::AgentSpec
460 (":cycamore:Sink"), config, simdur);
461 sim.AddSource("commods_1").capacity(10).Finalize();
462 int id = sim.Run();
463
464 QueryResult qr = sim.db().Query("Transactions", NULL);
465 // only one transaction has occurred
466 EXPECT_EQ(qr.rows.size(), 1);
467 // Get the time from the first transaction in the database (0th entry)
468 int trans_time = qr.GetVal<int>("Time", 0);
469 // Given the PRNG with default seed , this time should be time step 2
470 EXPECT_EQ(trans_time, 2);
471}
472
473// A random number pulled from a normal int distribution with user-defined mean
474// and stddev can be implemented as the buying frequency
475TEST_F(SinkTest, RandomNormalFreqWithMeanSttdev) {
476 using cyclus::QueryResult;
477
478 std::string config =
479 " <in_commods>"
480 " <val>commods_1</val>"
481 " </in_commods>"
482 " <capacity>10</capacity>"
483 " <random_frequency_type>NormalInt</random_frequency_type> "
484 " <random_frequency_mean>2</random_frequency_mean> "
485 " <random_frequency_stddev>0.2</random_frequency_stddev> ";
486
487 int simdur = 3;
488 cyclus::MockSim sim(cyclus::AgentSpec
489 (":cycamore:Sink"), config, simdur);
490 sim.AddSource("commods_1").capacity(10).Finalize();
491 int id = sim.Run();
492
493 QueryResult qr = sim.db().Query("Transactions", NULL);
494 // only one transaction has occurred
495 EXPECT_EQ(qr.rows.size(), 1);
496 // Get the time from the first transaction in the database (0th entry)
497 int trans_time = qr.GetVal<int>("Time", 0);
498 // Given the PRNG with default seed, this time should be time step 2
499 EXPECT_EQ(trans_time, 2);
500}
501
502// Check that multiple buying cycles set by random number execute as expected
503TEST_F(SinkTest, RandomNormalFreqMultipleCycles) {
504 using cyclus::QueryResult;
505
506 std::string config =
507 " <in_commods>"
508 " <val>commods_1</val>"
509 " </in_commods>"
510 " <capacity>10</capacity>"
511 " <random_frequency_type>NormalInt</random_frequency_type> "
512 " <random_frequency_mean>4</random_frequency_mean> "
513 " <random_frequency_stddev>1</random_frequency_stddev> ";
514
515 int simdur = 12;
516 cyclus::MockSim sim(cyclus::AgentSpec
517 (":cycamore:Sink"), config, simdur);
518 sim.AddSource("commods_1").capacity(10).Finalize();
519 int id = sim.Run();
520
521 QueryResult qr = sim.db().Query("Transactions", NULL);
522 // three transaction should have occurred
523 EXPECT_EQ(3, qr.rows.size());
524 // check multiple cycles execute at the expected time
525 // Get the time from the first, second, and third transactions in the
526 // database (0th, 1st, and 2nd entry)
527 // Given the PRNG with default seed, buy times on time step 5, 7, and 10
528 int first_trans_time = qr.GetVal<int>("Time", 0);
529 EXPECT_EQ(5, first_trans_time);
530 int second_trans_time = qr.GetVal<int>("Time", 1);
531 EXPECT_EQ(7, second_trans_time);
532 int third_trans_time = qr.GetVal<int>("Time", 2);
533 EXPECT_EQ(11, third_trans_time);
534}
535
536// Check that randomness can be implemented in both size of request and
537// request frequency at the same time
538TEST_F(SinkTest, RandomNormalSizeUniformFreq) {
539 using cyclus::QueryResult;
540
541 std::string config =
542 " <in_commods>"
543 " <val>commods_1</val>"
544 " </in_commods>"
545 " <capacity>10</capacity>"
546 " <random_size_type>NormalReal</random_size_type>"
547 " <random_size_mean>0.8</random_size_mean>"
548 " <random_size_stddev>0.2</random_size_stddev>"
549 " <random_frequency_type>UniformInt</random_frequency_type> "
550 " <random_frequency_min>2</random_frequency_min> "
551 " <random_frequency_max>4</random_frequency_max> ";
552
553 int simdur = 6;
554 cyclus::MockSim sim(cyclus::AgentSpec
555 (":cycamore:Sink"), config, simdur);
556 sim.AddSource("commods_1").capacity(20).Finalize();
557 int id = sim.Run();
558
559 QueryResult tqr = sim.db().Query("Transactions", NULL);
560 // two transactions should have occurred
561 EXPECT_EQ(2, tqr.rows.size());
562 // check multiple cycles execute at the expected time
563 int trans_time = tqr.GetVal<int>("Time", 0);
564 EXPECT_EQ(3, trans_time);
565 int res_id = tqr.GetVal<int>("ResourceId", 0);
566 QueryResult rqr = sim.db().Query("Resources", NULL);
567 double quantity = rqr.GetVal<double>("Quantity", 0);
568 EXPECT_NEAR(6.54143, quantity, 0.00001);
569}
570// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
571cyclus::Agent* SinkConstructor(cyclus::Context* ctx) {
572 return new cycamore::Sink(ctx);
573}
574
575// required to get functionality in cyclus agent unit tests library
576#ifndef CYCLUS_AGENT_TESTS_CONNECTED
579#define CYCLUS_AGENT_TESTS_CONNECTED cyclus_agent_tests_connected
580#endif // CYCLUS_AGENT_TESTS_CONNECTED
581
582// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
583INSTANTIATE_TEST_SUITE_P(SinkFac, FacilityTests, Values(&SinkConstructor));
584INSTANTIATE_TEST_SUITE_P(SinkFac, AgentTests, Values(&SinkConstructor));
static int cyclus_agent_tests_connected
TEST_F(SinkTest, InitialState)
cyclus::Agent * SinkConstructor(cyclus::Context *ctx)
INSTANTIATE_TEST_SUITE_P(SinkFac, FacilityTests, Values(&SinkConstructor))
int ConnectAgentTests()
virtual void SetUp()
cyclus::TestContext tc_
cycamore::Sink * src_facility
TestFacility * trader
virtual void TearDown()
This facility acts as a sink of materials and products with a fixed throughput (per time step) capaci...
void SetMaxInventorySize(double size)
sets the size of the storage inventory for received material
void AddCommodity(std::string name)
add a commodity to the set of input commodities
void Capacity(double cap)
sets the capacity of a material generated at any given time step