Skip to content

Commit

Permalink
check that correct params are set given choice of distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
nuclearkatie committed Jan 28, 2024
1 parent 014cc54 commit 312586e
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 30 deletions.
44 changes: 36 additions & 8 deletions src/storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,35 @@ void Storage::InitFrom(cyclus::QueryableBackend* b) {

void Storage::SetUpBuyPolicy() {
/// set up active buying distribution
if (active_buying_min > active_buying_max) {
throw cyclus::ValueError("Active min larger than max.");
}
if (dormant_buying_min > dormant_buying_max) {
throw cyclus::ValueError("Dormant min larger than max.");
}
if (buying_size_min > buying_size_max) {
throw cyclus::ValueError("Buying size min larger than max.");
}

if (active_buying_frequency_type == "Fixed") {
active_dist_ = boost::shared_ptr<cyclus::FixedIntDist>(new cyclus::FixedIntDist(active_buying_val));
}
else if (active_buying_frequency_type == "Uniform") {
if ((active_buying_min == -1) || (active_buying_max == -1)) {
throw cyclus::ValueError("Invalid active buying frequency range. Please provide both a min and max value.");
}
active_dist_ = boost::shared_ptr<cyclus::UniformIntDist>(new cyclus::UniformIntDist(active_buying_min, active_buying_max));
}
else if (active_buying_frequency_type == "Normal") {
if (active_buying_max == 0) {
active_buying_max = std::numeric_limits<int>::max();
if ((active_buying_mean == -1) || (active_buying_stddev == -1)) {
throw cyclus::ValueError("Invalid active buying frequency range. Please provide both a mean and standard deviation value.");
}
if (active_buying_min == -1) {active_buying_min = 1;}
if (active_buying_max == -1) {
active_buying_max = std::numeric_limits<int>::max();}

active_dist_ = boost::shared_ptr<cyclus::NormalIntDist>(new cyclus::NormalIntDist(active_buying_mean, active_buying_stddev,
active_buying_min, active_buying_max));
active_buying_min, active_buying_max));
}
else {
throw cyclus::ValueError("Invalid active buying frequency type");}
Expand All @@ -69,14 +86,20 @@ void Storage::SetUpBuyPolicy() {
dormant_dist_ = boost::shared_ptr<cyclus::FixedIntDist>(new cyclus::FixedIntDist(dormant_buying_val));
}
else if (dormant_buying_frequency_type == "Uniform") {
if ((dormant_buying_min == -1) || (dormant_buying_max == -1)) {
throw cyclus::ValueError("Invalid dormant buying frequency range. Please provide both a min and max value.");
}
dormant_dist_ = boost::shared_ptr<cyclus::UniformIntDist>(new cyclus::UniformIntDist(dormant_buying_min, dormant_buying_max));
}
else if (dormant_buying_frequency_type == "Normal") {
if (dormant_buying_max == 0) {
dormant_buying_max = std::numeric_limits<int>::max();
if ((dormant_buying_mean == -1) || (dormant_buying_stddev == -1)) {
throw cyclus::ValueError("Invalid dormant buying frequency range. Please provide both a mean and standard deviation value.");
}
if (dormant_buying_min == -1) {dormant_buying_min = 1;}
if (dormant_buying_max == -1) {
dormant_buying_max = std::numeric_limits<int>::max();}
dormant_dist_ = boost::shared_ptr<cyclus::NormalIntDist>(new cyclus::NormalIntDist(dormant_buying_mean, dormant_buying_stddev,
dormant_buying_min, dormant_buying_max));
dormant_buying_min, dormant_buying_max));
}
else {
throw cyclus::ValueError("Invalid dormant buying frequency type");}
Expand All @@ -86,12 +109,17 @@ void Storage::SetUpBuyPolicy() {
size_dist_ = boost::shared_ptr<cyclus::FixedDoubleDist>(new cyclus::FixedDoubleDist(buying_size_val));
}
else if (buying_size_type == "Uniform") {
if ((buying_size_min == -1) || (buying_size_max == -1)) {
throw cyclus::ValueError("Invalid buying size range. Please provide both a min and max value.");
}
size_dist_ = boost::shared_ptr<cyclus::UniformDoubleDist>(new cyclus::UniformDoubleDist(buying_size_min, buying_size_max));
}
else if (buying_size_type == "Normal") {
if (buying_size_max == 0) {
buying_size_max = std::numeric_limits<double>::max();
if ((buying_size_mean == -1) || (buying_size_stddev == -1)) {
throw cyclus::ValueError("Invalid buying size range. Please provide both a mean and standard deviation value.");
}
if (buying_size_min == -1) {buying_size_min = 0;}
if (buying_size_max == -1) {buying_size_max = 1;}
size_dist_ = boost::shared_ptr<cyclus::NormalDoubleDist>(new cyclus::NormalDoubleDist(buying_size_mean, buying_size_stddev,
buying_size_min, buying_size_max));
}
Expand Down
33 changes: 17 additions & 16 deletions src/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,15 @@ class Storage
#pragma cyclus var {"default": "Fixed",\
"tooltip": "Type of active buying frequency",\
"doc": "Options: Fixed, Uniform, Normal. Fixed requires active_buying_val. Uniform "\
"requires active_buying_max, with optional active_buying_min (default 1). Normal "\
"requires active_buying_min and active_buying_max. Normal "\
"requires active_buying_mean and active_buying_std, with optional "\
"active_buying_min and active_buying_max.",\
"uitype": "combobox",\
"categorical": ["Fixed", "Uniform", "Normal"],\
"uilabel": "Active Buying Frequency Type"}
std::string active_buying_frequency_type;

#pragma cyclus var {"default": 1,\
#pragma cyclus var {"default": -1,\
"tooltip": "Fixed active buying frequency",\
"doc": "The length in time steps of the active buying period. Required for fixed "\
"active_buying_frequency_type. Must be greater than or equal to 1 (i.e., agent "\
Expand All @@ -249,16 +249,17 @@ class Storage
"uilabel": "Active Buying Frequency Value"}
int active_buying_val;

#pragma cyclus var {"default": 1,\
#pragma cyclus var {"default": -1,\
"tooltip": "Active buying distribution minimum",\
"doc": "The minimum length in time steps of the active buying period. Optional for "\
"Uniform and Normal active_buying_frequency_type. Must be greater than or equal to 1 ",\
"doc": "The minimum length in time steps of the active buying period. Required for "\
"Uniform and optional for Normal active_buying_frequency_type. Must be greater than "\
"or equal to 1 ",\
"uitype": "range", \
"range": [1, 1e299], \
"uilabel": "Active Buying Frequency Minimum"}
int active_buying_min;

#pragma cyclus var {"default": 1e299,\
#pragma cyclus var {"default": -1,\
"tooltip": "Active buying distribution maximum",\
"doc": "The maximum length in time steps of the active buying period. Required for "\
"Uniform active_buying_frequency_type, optional for Normal. Must be greater than or equal to active_buying_min ",\
Expand All @@ -267,7 +268,7 @@ class Storage
"uilabel": "Active Buying Frequency Maximum"}
int active_buying_max;

#pragma cyclus var {"default": 1,\
#pragma cyclus var {"default": -1,\
"tooltip": "Active buying distribution mean",\
"doc": "The mean length in time steps of the active buying period. Required for "\
"Normal active_buying_frequency_type. Must be greater than or equal to 1 ",\
Expand All @@ -276,7 +277,7 @@ class Storage
"uilabel": "Active Buying Frequency Mean"}
double active_buying_mean;

#pragma cyclus var {"default": 1,\
#pragma cyclus var {"default": -1,\
"tooltip": "Active buying distribution standard deviation",\
"doc": "The standard deviation of the length in time steps of the active buying period. "\
"Required for Normal active_buying_frequency_type. Must be greater than or equal to 0 ",\
Expand Down Expand Up @@ -305,7 +306,7 @@ class Storage
"uilabel": "Dormant Buying Frequency Value"}
int dormant_buying_val;

#pragma cyclus var {"default": 0,\
#pragma cyclus var {"default": -1,\
"tooltip": "Dormant buying distribution minimum",\
"doc": "The minimum length in time steps of the dormant buying period. Optional for "\
"Uniform and Normal dormant_buying_frequency_type.",\
Expand All @@ -314,7 +315,7 @@ class Storage
"uilabel": "Dormant Buying Frequency Minimum"}
int dormant_buying_min;

#pragma cyclus var {"default": 0,\
#pragma cyclus var {"default": -1,\
"tooltip": "Dormant buying distribution maximum",\
"doc": "The maximum length in time steps of the dormant buying period. Required for "\
"Uniform dormant_buying_frequency_type, optional for Normal. Must be greater than or equal to dormant_buying_min ",\
Expand All @@ -323,7 +324,7 @@ class Storage
"uilabel": "Dormant Buying Frequency Maximum"}
int dormant_buying_max;

#pragma cyclus var {"default": 0,\
#pragma cyclus var {"default": -1,\
"tooltip": "Dormant buying distribution mean",\
"doc": "The mean length in time steps of the dormant buying period. Required for "\
"Normal dormant_buying_frequency_type. Must be greater than or equal to 0 ",\
Expand All @@ -332,7 +333,7 @@ class Storage
"uilabel": "Dormant Buying Frequency Mean"}
double dormant_buying_mean;

#pragma cyclus var {"default": 0,\
#pragma cyclus var {"default": -1,\
"tooltip": "Dormant buying distribution standard deviation",\
"doc": "The standard deviation of the length in time steps of the dormant buying period. "\
"Required for Normal dormant_buying_frequency_type. Must be greater than or equal to 0 ",\
Expand Down Expand Up @@ -363,7 +364,7 @@ class Storage
"uilabel": "Buying Size Value"}
double buying_size_val;

#pragma cyclus var {"default": 0.0,\
#pragma cyclus var {"default": -1.0,\
"tooltip": "Buying size distribution minimum",\
"doc": "The minimum size of the buy request as a fraction of maximum capacity. "\
"Optional for Uniform and Normal buying_size_type.",\
Expand All @@ -372,7 +373,7 @@ class Storage
"uilabel": "Buying Size Minimum"}
double buying_size_min;

#pragma cyclus var {"default": 1.0,\
#pragma cyclus var {"default": -1.0,\
"tooltip": "Buying size distribution maximum",\
"doc": "The maximum size of the buy request as a fraction of maximum capacity. "\
"Required for Uniform buying_size_type, optional for Normal. Must be greater than "\
Expand All @@ -382,7 +383,7 @@ class Storage
"uilabel": "Buying Size Maximum"}
double buying_size_max;

#pragma cyclus var {"default": 1.0,\
#pragma cyclus var {"default": -1.0,\
"tooltip": "Buying size distribution mean",\
"doc": "The mean size of the buy request as a fraction of maximum capacity. "\
"Required for Normal buying_size_type.",\
Expand All @@ -391,7 +392,7 @@ class Storage
"uilabel": "Buying Size Mean"}
double buying_size_mean;

#pragma cyclus var {"default": 0.0,\
#pragma cyclus var {"default": -1.0,\
"tooltip": "Buying size distribution standard deviation",\
"doc": "The standard deviation of the size of the buy request as a fraction of "\
"maximum capacity. Required for Normal buying_size_type.",\
Expand Down
60 changes: 54 additions & 6 deletions src/storage_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -645,16 +645,64 @@ TEST_F(StorageTest, NormalActiveDormantBuyingSize){
// second cycle (rows 4 and 4) include time steps 6 and 7
EXPECT_EQ(6, qr.GetVal<int>("Time", 4));
EXPECT_EQ(7, qr.GetVal<int>("Time", 5));
// third cycle (row 6) includes time step 8
// third cycle (row 6) includes time step 8 -9
EXPECT_EQ(8, qr.GetVal<int>("Time", 6));
// fourth cycle (rows 7, 8, 9) includes time steps 12 - 14
EXPECT_EQ(12, qr.GetVal<int>("Time", 7));
EXPECT_EQ(9, qr.GetVal<int>("Time", 7));
// fourth cycle (rows 8, 9) includes time steps 13 - 14
EXPECT_EQ(13, qr.GetVal<int>("Time", 8));
EXPECT_EQ(14, qr.GetVal<int>("Time", 9));

qr = sim.db().Query("Resources", NULL);
EXPECT_NEAR(0.30861, qr.GetVal<double>("Quantity", 0), 0.00001);
EXPECT_NEAR(0.51678, qr.GetVal<double>("Quantity", 1), 0.00001);
EXPECT_NEAR(0.61256, qr.GetVal<double>("Quantity", 2), 0.00001);
EXPECT_NEAR(0.61256, qr.GetVal<double>("Quantity", 0), 0.00001);
EXPECT_NEAR(0.62217, qr.GetVal<double>("Quantity", 1), 0.00001);
EXPECT_NEAR(0.39705, qr.GetVal<double>("Quantity", 2), 0.00001);
}

TEST_F(StorageTest, IncorrectBuyPolSetupUniform) {
// uniform missing min and max
std::string config_uniform =
" <in_commods> <val>spent_fuel</val> </in_commods> "
" <out_commods> <val>dry_spent</val> </out_commods> "
" <throughput>1</throughput>"
" <active_buying_frequency_type>Uniform</active_buying_frequency_type>";

int simdur = 15;

cyclus::MockSim sim(cyclus::AgentSpec (":cycamore:Storage"), config_uniform,
simdur);
EXPECT_THROW(sim.Run(), cyclus::ValueError);
}

TEST_F(StorageTest, IncorrectBuyPolSetupNormal) {
// normal missing mean and std dev
std::string config_normal =
" <in_commods> <val>spent_fuel</val> </in_commods> "
" <out_commods> <val>dry_spent</val> </out_commods> "
" <throughput>1</throughput>"
" <active_buying_frequency_type>Normal</active_buying_frequency_type>";
int simdur = 15;

cyclus::MockSim sim(cyclus::AgentSpec (":cycamore:Storage"), config_normal,
simdur);
EXPECT_THROW(sim.Run(), cyclus::ValueError);
}

TEST_F(StorageTest, IncorrectBuyPolSetupMinMax) {
// tries to set min > max
std::string config_uniform_min_bigger_max =
" <in_commods> <val>spent_fuel</val> </in_commods> "
" <out_commods> <val>dry_spent</val> </out_commods> "
" <throughput>1</throughput>"
" <active_buying_frequency_type>Uniform</active_buying_frequency_type>"
" <active_buying_min>3</active_buying_min>"
" <active_buying_max>2</active_buying_max>";

int simdur = 15;

cyclus::MockSim sim(cyclus::AgentSpec (":cycamore:Storage"),
config_uniform_min_bigger_max, simdur);
EXPECT_THROW(sim.Run(), cyclus::ValueError);

}

TEST_F(StorageTest, PositionInitialize){
Expand Down

0 comments on commit 312586e

Please sign in to comment.