Skip to content

Commit

Permalink
fix sink default pref (#442)
Browse files Browse the repository at this point in the history
* fix sink default pref

* change default pref from 1 to cyclus::kDefaultPref

* this should fix the unit test segfault

* add test checking default prefs

* add pref tests
  • Loading branch information
Baaaaam authored and gonuke committed Nov 18, 2016
1 parent cf840fb commit a0dfc66
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 5 deletions.
22 changes: 19 additions & 3 deletions src/sink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ Sink::~Sink() {}

#pragma cyclus def initfromcopy cycamore::Sink

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Sink::EnterNotify() {
cyclus::Facility::EnterNotify();

if (in_commod_prefs.size() == 0) {
for (int i = 0; i < in_commods.size(); ++i) {
in_commod_prefs.push_back(cyclus::kDefaultPref);
}
} else if (in_commod_prefs.size() != in_commods.size()) {
std::stringstream ss;
ss << "in_commod_prefs has " << in_commod_prefs.size()
<< " values, expected " << in_commods.size();
throw cyclus::ValueError(ss.str());
}

}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
std::string Sink::str() {
using std::string;
Expand Down Expand Up @@ -77,10 +94,9 @@ Sink::GetMatlRequests() {
}

if (amt > cyclus::eps()) {
std::vector<std::string>::const_iterator it;
std::vector<Request<Material>*> mutuals;
for (it = in_commods.begin(); it != in_commods.end(); ++it) {
mutuals.push_back(port->AddRequest(mat, this, *it));
for (int i = 0; i < in_commods.size(); i++) {
mutuals.push_back(port->AddRequest(mat, this, in_commods[i], in_commod_prefs[i]));
}
port->AddMutualReqs(mutuals);
ports.insert(port);
Expand Down
14 changes: 14 additions & 0 deletions src/sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class Sink : public cyclus::Facility {

virtual std::string str();

virtual void EnterNotify();

virtual void Tick();

virtual void Tock();
Expand Down Expand Up @@ -98,6 +100,10 @@ class Sink : public cyclus::Facility {
inline const std::vector<std::string>&
input_commodities() const { return in_commods; }

/// @return the input commodities preferences
inline const std::vector<double>&
input_commodity_preferences() const { return in_commod_prefs; }

private:
/// all facilities must have at least one input commodity
#pragma cyclus var {"tooltip": "input commodities", \
Expand All @@ -106,6 +112,14 @@ class Sink : public cyclus::Facility {
"uitype": ["oneormore", "incommodity"]}
std::vector<std::string> in_commods;

#pragma cyclus var {"default": [],\
"doc":"preferences for each of the given commodities, in the same order."\
"Defauts to 1 if unspecified",\
"uilabel":"In Commody Preferences", \
"range": [None, [1e-299, 1e299]], \
"uitype":["oneormore", "range"]}
std::vector<double> in_commod_prefs;

#pragma cyclus var {"default": "", \
"tooltip": "requested composition", \
"doc": "name of recipe to use for material requests, " \
Expand Down
55 changes: 54 additions & 1 deletion src/sink_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ TEST_F(SinkTest, InitialState) {
std::string arr[] = {commod1_, commod2_};
std::vector<std::string> vexp (arr, arr + sizeof(arr) / sizeof(arr[0]) );
EXPECT_EQ(vexp, src_facility->input_commodities());

src_facility->EnterNotify();
double pref[] = {cyclus::kDefaultPref, cyclus::kDefaultPref};
std::vector<double> vpref (pref, pref + sizeof(pref) / sizeof(pref[0]) );
EXPECT_EQ(vpref, src_facility->input_commodity_preferences());
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down Expand Up @@ -113,6 +118,7 @@ TEST_F(SinkTest, Requests) {
std::string arr[] = {commod1_, commod2_};
std::vector<std::string> commods (arr, arr + sizeof(arr) / sizeof(arr[0]) );

src_facility->EnterNotify();
std::set<RequestPortfolio<Material>::Ptr> ports =
src_facility->GetMatlRequests();

Expand Down Expand Up @@ -197,7 +203,8 @@ TEST_F(SinkTest, InRecipe){
// create a sink facility to interact with the DRE
cycamore::Sink* snk = new cycamore::Sink(&ctx);
snk->AddCommodity("some_u");

snk->EnterNotify();

std::set<RequestPortfolio<Material>::Ptr> ports =
snk->GetMatlRequests();
ASSERT_EQ(ports.size(), 1);
Expand All @@ -211,6 +218,52 @@ TEST_F(SinkTest, InRecipe){
EXPECT_EQ(req->commodity(),"some_u");
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
TEST_F(SinkTest, BidPrefs) {
using cyclus::QueryResult;
using cyclus::Cond;

std::string config =
" <in_commods>"
" <val>commods_1</val>"
" <val>commods_2</val>"
" </in_commods>"
" <in_commod_prefs>"
" <val>10</val> "
" <val>1</val> "
" </in_commod_prefs>"
" <capacity>1</capacity>"
" <input_capacity>1.0</input_capacity> ";

int simdur = 1;
cyclus::MockSim sim(cyclus::AgentSpec
(":cycamore:Sink"), config, simdur);

sim.AddSource("commods_1")
.capacity(1)
.Finalize();

sim.AddSource("commods_2")
.capacity(1)
.Finalize();

int id = sim.Run();

std::vector<Cond> conds;
conds.push_back(Cond("Commodity", "==", std::string("commods_1")));
QueryResult qr = sim.db().Query("Transactions", &conds);

// should trade only with #1 since it has highier priority
EXPECT_EQ(1, qr.rows.size());

std::vector<Cond> conds2;
conds2.push_back(Cond("Commodity", "==", std::string("commods_2")));
QueryResult qr2 = sim.db().Query("Transactions", &conds2);

// should trade only with #1 since it has highier priority
EXPECT_EQ(0, qr2.rows.size());

}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
TEST_F(SinkTest, Print) {
EXPECT_NO_THROW(std::string s = src_facility->str());
Expand Down
2 changes: 1 addition & 1 deletion src/storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void Storage::EnterNotify() {

if (in_commod_prefs.size() == 0) {
for (int i = 0; i < in_commods.size(); ++i) {
in_commod_prefs.push_back(1);
in_commod_prefs.push_back(cyclus::kDefaultPref);
}
} else if (in_commod_prefs.size() != in_commods.size()) {
std::stringstream ss;
Expand Down

0 comments on commit a0dfc66

Please sign in to comment.