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

Allow passing feature properties to BMI models through model_params #588

Merged
merged 13 commits into from
Aug 25, 2023
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
54 changes: 47 additions & 7 deletions extern/test_bmi_cpp/include/test_bmi_cpp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ class TestBmiCpp : public bmi::Bmi {
*
* @return Pointer to the newly created @ref test_bmi_c_model struct instance in memory.
*/
TestBmiCpp(bool input_array = false, bool output_array = false):
use_input_array(input_array), use_output_array(output_array)
TestBmiCpp(bool input_array = false, bool output_array = false, bool model_params = false):
use_input_array(input_array), use_output_array(output_array), use_model_params(model_params)
{
set_usage(input_array, output_array);
set_usage(input_array, output_array, model_params);
};

virtual void Initialize(std::string config_file);
Expand Down Expand Up @@ -102,9 +102,10 @@ class TestBmiCpp : public bmi::Bmi {

private:

inline void set_usage(bool input_array = false, bool output_array = false){
inline void set_usage(bool input_array = false, bool output_array = false, bool model_params = false){
use_input_array = input_array;
use_output_array = output_array;
use_model_params = model_params;
//NOTE use the correct array constructor here or things get weird
//make_unique<double>(3) will give a unique pointer to a single double initialized to 3
//make_unique<double[]>(3) will give a unique pointer to an array of 3 doubles, default initialized
Expand Down Expand Up @@ -134,23 +135,56 @@ class TestBmiCpp : public bmi::Bmi {
output_var_item_count.push_back(3); //an array of 3 values
output_var_grids.push_back(1);
}
if( use_model_params ){
this->output_var_4 = std::make_unique<double>(0);
this->output_var_5 = std::make_unique<double>(1);
this->model_var_1 = std::make_unique<double>(1);
this->model_var_2 = std::make_unique<double>(2);
std::cout<<"USING MODEL PARAMS\n";
output_var_names.push_back("OUTPUT_VAR_4");
output_var_types.push_back("double");
output_var_units.push_back("m");
output_var_locations.push_back("node");
output_var_item_count.push_back(1);
output_var_grids.push_back(1);

output_var_names.push_back("OUTPUT_VAR_5");
output_var_types.push_back("double");
output_var_units.push_back("m");
output_var_locations.push_back("node");
output_var_item_count.push_back(1);
output_var_grids.push_back(1);

this->model_var_names = { "MODEL_VAR_1", "MODEL_VAR_2" };
this->model_var_types = { "double", "double" };
this->model_var_units = { "m", "m" };
this->model_var_locations = { "node", "node" };
this->model_var_item_count = { 1, 1 };
this->model_var_grids = { 1, 1 };
}
}
//flags for conditional use of input/output var 3
bool use_input_array, use_output_array;
bool use_input_array, use_output_array, use_model_params;

std::vector<std::string> input_var_names = { "INPUT_VAR_1", "INPUT_VAR_2" };
std::vector<std::string> output_var_names = { "OUTPUT_VAR_1", "OUTPUT_VAR_2" };
std::vector<std::string> output_var_names = { "OUTPUT_VAR_1", "OUTPUT_VAR_2"};
std::vector<std::string> model_var_names = {};
std::vector<std::string> input_var_types = { "double", "double" };
std::vector<std::string> output_var_types = { "double", "double" };
std::vector<std::string> model_var_types = {};
std::vector<std::string> input_var_units = { "m", "m" };
std::vector<std::string> output_var_units = { "m", "m/s" };
std::vector<std::string> output_var_units = { "m", "m/s", "m", "m" };
std::vector<std::string> model_var_units = {};
std::vector<std::string> input_var_locations = { "node", "node" };
std::vector<std::string> output_var_locations = { "node", "node" };
std::vector<std::string> model_var_locations = {};

std::vector<int> input_var_item_count = { 1, 1 };
std::vector<int> output_var_item_count = { 1, 1 };
std::vector<int> model_var_item_count = {};
std::vector<int> input_var_grids = { 1, 1 };
std::vector<int> output_var_grids = { 1, 1 };
std::vector<int> model_var_grids = {};

std::map<std::string,int> type_sizes = {
{BMI_TYPE_NAME_DOUBLE, sizeof(double)},
Expand Down Expand Up @@ -183,6 +217,12 @@ class TestBmiCpp : public bmi::Bmi {
std::unique_ptr<double[]> input_var_3 = nullptr;
std::unique_ptr<double[]> output_var_3 = nullptr;

// Variables for testing model params
std::unique_ptr<double> output_var_4 = nullptr;
std::unique_ptr<double> output_var_5 = nullptr;
std::unique_ptr<double> model_var_1 = nullptr;
std::unique_ptr<double> model_var_2 = nullptr;

/**
* Read the BMI initialization config file and use its contents to set the state of the model.
*
Expand Down
46 changes: 42 additions & 4 deletions extern/test_bmi_cpp/src/test_bmi_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,21 @@ void* TestBmiCpp::GetValuePtr(std::string name){
return this->output_var_3.get();
}

if (use_model_params) {
if (name == "OUTPUT_VAR_4") {
return this->output_var_4.get();
}
if (name == "OUTPUT_VAR_5") {
return this->output_var_5.get();
}
if (name == "MODEL_VAR_1") {
return this->model_var_1.get();
}
if (name == "MODEL_VAR_2") {
return this->model_var_2.get();
}
}

throw std::runtime_error("GetValuePtr called for unknown variable: "+name);
}

Expand All @@ -171,6 +186,10 @@ std::string TestBmiCpp::GetVarLocation(std::string name){
if(iter != this->input_var_names.end()){
return this->input_var_locations[iter - this->input_var_names.begin()];
}
iter = std::find(this->model_var_names.begin(), this->model_var_names.end(), name);
if(iter != this->model_var_names.end()){
return this->model_var_locations[iter - this->model_var_names.begin()];
}
throw std::runtime_error("GetVarLocation called for non-existent variable: "+name+"" SOURCE_LOC);
}

Expand All @@ -189,6 +208,10 @@ int TestBmiCpp::GetVarNbytes(std::string name){
if(iter != this->input_var_names.end()){
item_count = this->input_var_item_count[iter - this->input_var_names.begin()];
}
iter = std::find(this->model_var_names.begin(), this->model_var_names.end(), name);
if(iter != this->model_var_names.end()){
item_count = this->model_var_item_count[iter - this->model_var_names.begin()];
}
if(item_count == -1){
// This is probably impossible to reach--the same conditions above failing will cause a throw
// in GetVarItemSize --> GetVarType (called earlier) instead.
Expand All @@ -206,6 +229,10 @@ std::string TestBmiCpp::GetVarType(std::string name){
if(iter != this->input_var_names.end()){
return this->input_var_types[iter - this->input_var_names.begin()];
}
iter = std::find(this->model_var_names.begin(), this->model_var_names.end(), name);
if(iter != this->model_var_names.end()){
return this->model_var_types[iter - this->model_var_names.begin()];
}
throw std::runtime_error("GetVarType called for non-existent variable: "+name+"" SOURCE_LOC );
}

Expand All @@ -218,6 +245,10 @@ std::string TestBmiCpp::GetVarUnits(std::string name){
if(iter != this->input_var_names.end()){
return this->input_var_units[iter - this->input_var_names.begin()];
}
iter = std::find(this->model_var_names.begin(), this->model_var_names.end(), name);
if(iter != this->model_var_names.end()){
return this->model_var_types[iter - this->model_var_names.begin()];
}
throw std::runtime_error("GetVarUnits called for non-existent variable: "+name+"" SOURCE_LOC);
}

Expand Down Expand Up @@ -399,6 +430,9 @@ void TestBmiCpp::read_init_config(std::string config_file)
if( strcmp(param_key, "use_output_array") == 0) {
this->use_output_array = true;
}
if( strcmp(param_key, "use_model_params") == 0) {
this->use_model_params = true;
}
}

if (is_epoch_start_time_set == FALSE) {
Expand All @@ -410,8 +444,8 @@ void TestBmiCpp::read_init_config(std::string config_file)
#endif

//dynamic creation/usage of optional var 3
if(use_input_array || use_output_array ){
set_usage(use_input_array, use_output_array);
if(use_input_array || use_output_array || use_model_params){
set_usage(use_input_array, use_output_array, use_model_params);
}
}

Expand Down Expand Up @@ -462,7 +496,7 @@ void TestBmiCpp::read_file_line_counts(std::string file_name, int* line_count, i


void TestBmiCpp::run(long dt)
{
{
if (dt == this->time_step_size) {
*this->output_var_1 = *this->input_var_1;
*this->output_var_2 = 2.0 * *this->input_var_2;
Expand All @@ -476,5 +510,9 @@ void TestBmiCpp::run(long dt)
this->output_var_3.get()[1] += 2;
this->output_var_3.get()[2] += 3;
}
if (this->use_model_params) {
*this->output_var_4 = *this->model_var_1;
*this->output_var_5 = *this->model_var_2 * 1.0;
}
this->current_model_time += (double)dt;
}
}
Loading