Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix output_variables #415

Merged
merged 5 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/actions/ngen-build/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ runs:
run: |
if [ ${{ runner.os }} == 'Linux' ]
then
sudo apt-get update
sudo apt-get install -y --fix-missing libnetcdf-dev libnetcdff-dev
elif [ ${{ runner.os }} == 'macOS' ]
then
Expand Down
2 changes: 1 addition & 1 deletion include/realizations/catchment/Bmi_Module_Formulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ namespace realization {
*/
long get_data_start_time() override
{
throw runtime_error("Bmi_Modular_Formulation does not yet implement get_data_start_time");
return this->get_bmi_model()->GetStartTime();
}

/**
Expand Down
25 changes: 18 additions & 7 deletions include/realizations/catchment/Bmi_Multi_Formulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ namespace realization {
* @return The inclusive beginning of the period of time over which this instance can provide this data.
*/

[[deprecate]]
[[deprecated]]
time_t get_forcing_output_time_begin(const std::string &forcing_name) {
std::string var_name = forcing_name;
if(var_name == "*" || var_name == ""){
Expand Down Expand Up @@ -422,6 +422,15 @@ namespace realization {
return modules[get_index_for_primary_module()]->get_model_end_time();
}

/**
* Get the end time for the primary nested BMI model in its native format and units.
*
* @return The end time for the primary nested BMI model in its native format and units.
*/
const double get_model_start_time() {
return modules[get_index_for_primary_module()]->get_data_start_time();
}

string get_output_line_for_timestep(int timestep, std::string delimiter) override;

double get_response(time_step_t t_index, time_step_t t_delta) override;
Expand Down Expand Up @@ -614,19 +623,21 @@ namespace realization {
auto data_provider_iter = availableData.find(var_name);
if (data_provider_iter == availableData.end()) {
throw external::ExternalIntegrationException(
"Multi BMI formulation can't find correct nested module for BMI variable " + var_name);
"Multi BMI formulation can't find correct nested module for BMI variable " + var_name + SOURCE_LOC);
}
// Otherwise, we have a provider, and we can cast it based on the documented assumptions
try {
std::shared_ptr <Bmi_Module_Formulation<bmi::Bmi>> nested_module =
std::dynamic_pointer_cast < Bmi_Module_Formulation <
bmi::Bmi >> (data_provider_iter->second);
return nested_module->get_var_value_as_double(index, var_name);
std::shared_ptr <data_access::GenericDataProvider> nested_module =
std::dynamic_pointer_cast<data_access::GenericDataProvider>(data_provider_iter->second);
long nested_module_time = nested_module->get_data_start_time() + ( this->get_model_current_time() - this->get_model_start_time() );
auto selector = CatchmentAggrDataSelector("",var_name,nested_module_time,this->record_duration(),"1");
//TODO: After merge PR#405, try re-adding support for index
return nested_module->get_value(selector);
}
// If there was any problem with the cast and extraction of the value, throw runtime error
catch (std::exception &e) {
throw std::runtime_error("Multi BMI formulation can't use associated data provider as a nested module"
" when attempting to get values of BMI variable " + var_name);
" when attempting to get values of BMI variable " + var_name + SOURCE_LOC);
// TODO: look at adjusting defs to move this function up in class hierarchy (or at least add TODO there)
}
}
Expand Down
48 changes: 43 additions & 5 deletions test/realizations/catchments/Bmi_Multi_Formulation_Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ class Bmi_Multi_Formulation_Test : public ::testing::Test {
std::vector<std::string> example_forcing_files;
/** Main output variables for the outter formulation in each example. */
std::vector<std::string> main_output_variables;
/** Lists for "output_variables" entries; one list per example, NOT one string per nested module */
std::vector<std::vector<std::string>> specified_output_variables;
/** Collections of lib/package files names for nested modules of each example, in the order of the nested modules. */
std::vector<std::vector<std::string>> nested_module_file_lists;
/** Collections of model types for each example, in the order of the nested modules. */
Expand Down Expand Up @@ -160,6 +162,7 @@ class Bmi_Multi_Formulation_Test : public ::testing::Test {
catchment_ids = std::vector<std::string>(EX_COUNT);
example_forcing_files = std::vector<std::string>(EX_COUNT);
main_output_variables = std::vector<std::string>(EX_COUNT);
specified_output_variables = std::vector<std::vector<std::string>>(EX_COUNT);
uses_forcing_file = std::vector<bool>(EX_COUNT);
forcing_params_examples = std::vector<std::shared_ptr<forcing_params>>(EX_COUNT);
config_prop_ptree = std::vector<boost::property_tree::ptree>(EX_COUNT);
Expand Down Expand Up @@ -311,6 +314,22 @@ class Bmi_Multi_Formulation_Test : public ::testing::Test {
}
}

inline std::string buildExampleOutputVariablesSubConfig(const int ex_index){
auto list = specified_output_variables[ex_index];
std::string s = "";
if(list.size() == 0){
return s;
}
s = ",\"output_variables\": [";
std::string comma = "";
for (auto item : list) {
s += comma + "\"" + item + "\"" ;
comma = ",";
}
s += "]";
return s;
}

inline void buildExampleConfig(const int ex_index) {
std::string config =
"{\n"
Expand All @@ -331,6 +350,7 @@ class Bmi_Multi_Formulation_Test : public ::testing::Test {
+ buildExampleNestedModuleSubConfig(ex_index, 1) + "\n"
" ],\n"
" \"uses_forcing_file\": false\n"
+ buildExampleOutputVariablesSubConfig(ex_index) + "\n"
" }\n"
" }\n"
" ],\n"
Expand Down Expand Up @@ -385,7 +405,7 @@ class Bmi_Multi_Formulation_Test : public ::testing::Test {
}

inline void initializeTestExample(const int ex_index, const std::string &cat_id,
const std::vector<std::string> &nested_types) {
const std::vector<std::string> &nested_types, const std::vector<std::string> &output_variables) {
catchment_ids[ex_index] = cat_id;
example_forcing_files[ex_index] = testUtil.getForcingFilePath(cat_id);
uses_forcing_file[ex_index] = false;
Expand All @@ -408,6 +428,7 @@ class Bmi_Multi_Formulation_Test : public ::testing::Test {
}
//main_output_variables[ex_index] = "OUTPUT_VAR_1__" + std::to_string(example_module_depth[ex_index] - 1);
main_output_variables[ex_index] = nested_module_main_output_variables[ex_index][example_module_depth[ex_index] - 1];
specified_output_variables[ex_index] = output_variables;

buildExampleConfig(ex_index);
}
Expand Down Expand Up @@ -449,7 +470,7 @@ void Bmi_Multi_Formulation_Test::SetUp() {
#endif // NGEN_BMI_FORTRAN_ACTIVE


initializeTestExample(0, "cat-27", {std::string(BMI_FORTRAN_TYPE), std::string(BMI_C_TYPE)});
initializeTestExample(0, "cat-27", {std::string(BMI_FORTRAN_TYPE), std::string(BMI_C_TYPE)}, {});

/* ********************************** Second example scenario ********************************** */

Expand All @@ -461,11 +482,11 @@ void Bmi_Multi_Formulation_Test::SetUp() {
throw std::runtime_error("Error: can't run multi BMI tests for scenario at index 1 without BMI Python functionality active" SOURCE_LOC);
#endif // ACTIVATE_PYTHON

initializeTestExample(1, "cat-27", {std::string(BMI_FORTRAN_TYPE), std::string(BMI_PYTHON_TYPE)});
initializeTestExample(1, "cat-27", {std::string(BMI_FORTRAN_TYPE), std::string(BMI_PYTHON_TYPE)}, {});

initializeTestExample(2, "cat-27", {std::string(BMI_FORTRAN_TYPE), std::string(BMI_PYTHON_TYPE)});
initializeTestExample(2, "cat-27", {std::string(BMI_FORTRAN_TYPE), std::string(BMI_PYTHON_TYPE)}, {});

initializeTestExample(3, "cat-27", {std::string(BMI_FORTRAN_TYPE), std::string(BMI_PYTHON_TYPE)});
initializeTestExample(3, "cat-27", {std::string(BMI_FORTRAN_TYPE), std::string(BMI_PYTHON_TYPE)}, {"OUTPUT_VAR_1__1", "OUTPUT_VAR_2__1", "OUTPUT_VAR_1__0", "OUTPUT_VAR_2__0", "OUTPUT_VAR_3__0", "precip_rate" });
}

/** Simple test to make sure the model config from example 0 initializes. */
Expand Down Expand Up @@ -749,6 +770,23 @@ TEST_F(Bmi_Multi_Formulation_Test, GetOutputLineForTimestep_1_b) {
ASSERT_EQ(output, "0.000001,199280.000000,543.000000");
}

/**
* Test of output for example 3 with output_variables from multiple BMI modules, picking time step when there was non-zero rain rate.
*/
TEST_F(Bmi_Multi_Formulation_Test, GetOutputLineForTimestep_3_a) {
int ex_index = 3;

Bmi_Multi_Formulation formulation(catchment_ids[ex_index], std::make_unique<CsvPerFeatureForcingProvider>(*forcing_params_examples[ex_index]), utils::StreamHandler());
formulation.create_formulation(config_prop_ptree[ex_index]);

int i = 0;
while (i < 542)
formulation.get_response(i++, 3600);
formulation.get_response(i, 3600);
std::string output = formulation.get_output_line_for_timestep(i, ",");
ASSERT_EQ(output, "0.000001112,199280.000000000,199240.000000000,199280.000000000,0.000000000,0.000001001");
}

#endif // NGEN_BMI_C_LIB_ACTIVE || NGEN_BMI_FORTRAN_ACTIVE || ACTIVATE_PYTHON

#endif // NGEN_BMI_MULTI_FORMULATION_TEST_CPP