From 96d1e8393e1cb334e1ebeef9bacce10a42714f8e Mon Sep 17 00:00:00 2001 From: Shengting Cui Date: Thu, 7 Sep 2023 19:19:12 +0000 Subject: [PATCH 1/2] Remove custom time unit conversion in favor of UDUNITS --- .../realizations/catchment/Bmi_Adapter.hpp | 32 +++++++------------ .../catchment/Bmi_Fortran_Adapter.hpp | 2 +- src/realizations/catchment/Bmi_C_Adapter.cpp | 2 +- .../catchment/Bmi_Cpp_Adapter.cpp | 2 +- src/realizations/catchment/Bmi_Py_Adapter.cpp | 2 +- 5 files changed, 15 insertions(+), 25 deletions(-) diff --git a/include/realizations/catchment/Bmi_Adapter.hpp b/include/realizations/catchment/Bmi_Adapter.hpp index ea06a729f8..d146f9d4b3 100644 --- a/include/realizations/catchment/Bmi_Adapter.hpp +++ b/include/realizations/catchment/Bmi_Adapter.hpp @@ -8,6 +8,7 @@ #include "JSONProperty.hpp" #include "State_Exception.hpp" #include "StreamHandler.hpp" +#include namespace models { namespace bmi { @@ -81,29 +82,18 @@ namespace models { /** - * Determine backing model's time units and set the reference parameter to an appropriate conversion factor. + * Determine backing model's time units and return an appropriate conversion factor. * * A backing BMI model may use arbitrary units for time, but it will expose what those units are via the - * BMI ``GetTimeUnits`` function. This function retrieves (and interprets) its model's units and - * sets the given reference parameter to an appropriate factor for converting its internal time values to - * equivalent representations within the model, and vice versa. - * - * @param time_convert_factor A reference to set to the determined conversion factor. + * BMI ``GetTimeUnits`` function. This function retrieves (and interprets) its model's units and + * return an appropriate factor for converting its internal time values to equivalent representations + * within the model, and vice versa. This function coomplies with the BMI get_time_units standard */ - void acquire_time_conversion_factor(double &time_convert_factor) { - std::string time_units = GetTimeUnits(); - if (time_units == "s" || time_units == "sec" || time_units == "second" || time_units == "seconds") - time_convert_factor = 1.0; - else if (time_units == "m" || time_units == "min" || time_units == "minute" || - time_units == "minutes") - time_convert_factor = 60.0; - else if (time_units == "h" || time_units == "hr" || time_units == "hour" || time_units == "hours") - time_convert_factor = 3600.0; - else if (time_units == "d" || time_units == "day" || time_units == "days") - time_convert_factor = 86400.0; - else - throw std::runtime_error( - "Invalid model time step units ('" + time_units + "') in " + model_name + "."); + double get_time_convert_factor() { + double value = 1.0; + std::string input_units = GetTimeUnits(); + std::string output_units = "s"; + return UnitsHelper::get_converted_value(input_units, value, output_units); } /** @@ -193,7 +183,7 @@ namespace models { construct_and_init_backing_model(); // Make sure this is set to 'true' after this function call finishes model_initialized = true; - acquire_time_conversion_factor(bmi_model_time_convert_factor); + bmi_model_time_convert_factor = get_time_convert_factor(); } // Record the exception message before re-throwing to handle subsequent function calls properly catch (std::exception& e) { diff --git a/include/realizations/catchment/Bmi_Fortran_Adapter.hpp b/include/realizations/catchment/Bmi_Fortran_Adapter.hpp index 626ee76e7b..41a6fa6269 100644 --- a/include/realizations/catchment/Bmi_Fortran_Adapter.hpp +++ b/include/realizations/catchment/Bmi_Fortran_Adapter.hpp @@ -48,7 +48,7 @@ namespace models { construct_and_init_backing_model_for_fortran(); // Make sure this is set to 'true' after this function call finishes model_initialized = true; - acquire_time_conversion_factor(bmi_model_time_convert_factor); + bmi_model_time_convert_factor = get_time_convert_factor(); } // Record the exception message before re-throwing to handle subsequent function calls properly catch( models::external::State_Exception& e) diff --git a/src/realizations/catchment/Bmi_C_Adapter.cpp b/src/realizations/catchment/Bmi_C_Adapter.cpp index 60f63c5b4f..f177073c6b 100755 --- a/src/realizations/catchment/Bmi_C_Adapter.cpp +++ b/src/realizations/catchment/Bmi_C_Adapter.cpp @@ -78,7 +78,7 @@ Bmi_C_Adapter::Bmi_C_Adapter(const std::string &type_name, std::string library_f construct_and_init_backing_model_for_type(); // Make sure this is set to 'true' after this function call finishes model_initialized = true; - acquire_time_conversion_factor(bmi_model_time_convert_factor); + bmi_model_time_convert_factor = get_time_convert_factor(); } // Record the exception message before re-throwing to handle subsequent function calls properly catch( models::external::State_Exception& e) diff --git a/src/realizations/catchment/Bmi_Cpp_Adapter.cpp b/src/realizations/catchment/Bmi_Cpp_Adapter.cpp index 50eead33ce..7445036a5d 100644 --- a/src/realizations/catchment/Bmi_Cpp_Adapter.cpp +++ b/src/realizations/catchment/Bmi_Cpp_Adapter.cpp @@ -39,7 +39,7 @@ Bmi_Cpp_Adapter::Bmi_Cpp_Adapter(const std::string& type_name, std::string libra construct_and_init_backing_model_for_type(); // Make sure this is set to 'true' after this function call finishes model_initialized = true; - acquire_time_conversion_factor(bmi_model_time_convert_factor); + bmi_model_time_convert_factor = get_time_convert_factor(); } // Record the exception message before re-throwing to handle subsequent function calls properly catch (const std::exception &e) { diff --git a/src/realizations/catchment/Bmi_Py_Adapter.cpp b/src/realizations/catchment/Bmi_Py_Adapter.cpp index e43d37bd18..1947b4f407 100644 --- a/src/realizations/catchment/Bmi_Py_Adapter.cpp +++ b/src/realizations/catchment/Bmi_Py_Adapter.cpp @@ -26,7 +26,7 @@ Bmi_Py_Adapter::Bmi_Py_Adapter(const std::string &type_name, std::string bmi_ini construct_and_init_backing_model_for_py_adapter(); // Make sure this is set to 'true' after this function call finishes model_initialized = true; - acquire_time_conversion_factor(bmi_model_time_convert_factor); + bmi_model_time_convert_factor = get_time_convert_factor(); } catch (std::runtime_error& e){ //Catch specific exception and re-throw so type/message isn't erased model_initialized = true; From a35e7b584e8078f6be1c9e53f33d11eb4a74bb42 Mon Sep 17 00:00:00 2001 From: Shengting Cui Date: Tue, 12 Sep 2023 13:24:30 +0000 Subject: [PATCH 2/2] Fix a typo in Bmi_Adapter.hpp. --- include/realizations/catchment/Bmi_Adapter.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/realizations/catchment/Bmi_Adapter.hpp b/include/realizations/catchment/Bmi_Adapter.hpp index d146f9d4b3..640ef34d5f 100644 --- a/include/realizations/catchment/Bmi_Adapter.hpp +++ b/include/realizations/catchment/Bmi_Adapter.hpp @@ -87,7 +87,7 @@ namespace models { * A backing BMI model may use arbitrary units for time, but it will expose what those units are via the * BMI ``GetTimeUnits`` function. This function retrieves (and interprets) its model's units and * return an appropriate factor for converting its internal time values to equivalent representations - * within the model, and vice versa. This function coomplies with the BMI get_time_units standard + * within the model, and vice versa. This function complies with the BMI get_time_units standard */ double get_time_convert_factor() { double value = 1.0;