Skip to content
Draft
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
18 changes: 18 additions & 0 deletions include/bmi/Bmi_Py_Adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,24 @@ namespace models {
}
}

/**
* Set the value of a variable. This version of setting a variable will send an array with the `size` specified instead of checking the BMI for its current size of the variable.
* Ownership of the pointer will remain in C++, so the consuming BMI should not maintain a reference to the values beyond the scope of its `set_value` method.
*
* @param name The name of the BMI variable.
* @param src Pointer to the data that will be sent to the BMI.
* @param size The number of items represented by the pointer.
*/
template <typename T>
void set_value_unchecked(const std::string &name, T *src, size_t size) {
// declare readonly array info with the pointer and size
py::buffer_info info(src, static_cast<py::ssize_t>(size), true);
// create the array with the info and NULL handler so python doesn't take ownership
py::array_t<T> src_array(info, nullptr);
// pass the array to python to read; the BMI should not attempt to maintain a reference beyond the scope of this function to prevent trying to use freed memory
bmi_model->attr("set_value")(name, src_array);
}

/**
* Set values for a model's BMI variable at specified indices.
*
Expand Down
2 changes: 1 addition & 1 deletion include/core/Layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ namespace ngen
/***
* @brief Run one simulation timestep for each model in this layer
*/
virtual void update_models(boost::span<double> catchment_outflows,
virtual void update_models(boost::span<double> catchment_evapotranspiration,
std::unordered_map<std::string, int> &catchment_indexes,
boost::span<double> nexus_downstream_flows,
std::unordered_map<std::string, int> &nexus_indexes,
Expand Down
4 changes: 2 additions & 2 deletions include/core/NgenSimulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class NgenSimulation
/**
* Run the catchment formulations for the full configured duration of the simulation
*
* Captures calculated runoff values in `catchment_outflows_` and
* Captures calculated runoff values in `catchment_evapotranspiration_` and
* `nexus_downstream_flows_` for subsequent output and consumption
* by `run_routing()`
*/
Expand Down Expand Up @@ -71,7 +71,7 @@ class NgenSimulation

// Routing data structured for t-route
std::unordered_map<std::string, int> catchment_indexes_;
std::vector<double> catchment_outflows_;
std::vector<double> catchment_evapotranspiration_;
std::unordered_map<std::string, int> nexus_indexes_;
std::vector<double> nexus_downstream_flows_;

Expand Down
10 changes: 10 additions & 0 deletions include/realizations/catchment/Bmi_Formulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define BMI_REALIZATION_CFG_PARAM_REQ__MODEL_TYPE "model_type_name"

// Then the optional
#define BMI_REALIZATION_CFG_PARAM_OPT__EVAPOTRANSPIRATION "evapotranspiration_variable"
#define BMI_REALIZATION_CFG_PARAM_OPT__USES_FORCINGS "uses_forcing_file"
#define BMI_REALIZATION_CFG_PARAM_OPT__FORCING_FILE "forcing_file"
#define BMI_REALIZATION_CFG_PARAM_OPT__VAR_STD_NAMES "variables_names_map"
Expand Down Expand Up @@ -176,6 +177,10 @@ namespace realization {
return output_variable_names;
}

std::string get_bmi_evapotranspiration_var() const {
return this->bmi_evapotranspiration_var;
}

const std::vector<std::string> &get_required_parameters() const override {
return REQUIRED_PARAMETERS;
}
Expand Down Expand Up @@ -225,6 +230,10 @@ namespace realization {
bmi_main_output_var = main_output_var;
}

void set_bmi_evapotranspiration_var(const std::string &evapotranspiration_var) {
this->bmi_evapotranspiration_var = evapotranspiration_var;
}

/**
* Set the name of the specific type of the backing model object.
*
Expand Down Expand Up @@ -253,6 +262,7 @@ namespace realization {
private:

std::string bmi_main_output_var;
std::string bmi_evapotranspiration_var;
std::string model_type_name;
/**
* Output header field strings corresponding to the variables output by the realization, as defined in
Expand Down
11 changes: 9 additions & 2 deletions include/realizations/catchment/Bmi_Module_Formulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ namespace realization {
* @param output_stream
*/
Bmi_Module_Formulation(std::string id, std::shared_ptr<data_access::GenericDataProvider> forcing_provider, utils::StreamHandler output_stream)
: Bmi_Formulation(std::move(id), forcing_provider, output_stream) { }
: Bmi_Formulation(std::move(id), forcing_provider, output_stream)
, evapotranspiration_index{-1}
{ }

~Bmi_Module_Formulation() override = default;

Expand Down Expand Up @@ -513,6 +515,8 @@ namespace realization {

bool is_realization_legacy_format() const;

double get_current_evapotranspiration() override;

private:
models::bmi::protocols::NgenBmiProtocols bmi_protocols;
/**
Expand Down Expand Up @@ -543,6 +547,8 @@ namespace realization {

std::vector<int> output_var_indices;

int evapotranspiration_index;

std::vector<std::string> OPTIONAL_PARAMETERS = {
BMI_REALIZATION_CFG_PARAM_OPT__USES_FORCINGS
BMI_REALIZATION_CFG_PARAM_OPT__FORCING_FILE,
Expand All @@ -552,7 +558,8 @@ namespace realization {
BMI_REALIZATION_CFG_PARAM_OPT__OUTPUT_PRECISION,
BMI_REALIZATION_CFG_PARAM_OPT__ALLOW_EXCEED_END,
BMI_REALIZATION_CFG_PARAM_OPT__FIXED_TIME_STEP,
BMI_REALIZATION_CFG_PARAM_OPT__LIB_FILE
BMI_REALIZATION_CFG_PARAM_OPT__LIB_FILE,
BMI_REALIZATION_CFG_PARAM_OPT__EVAPOTRANSPIRATION
};
std::vector<std::string> REQUIRED_PARAMETERS = {
BMI_REALIZATION_CFG_PARAM_REQ__INIT_CONFIG,
Expand Down
8 changes: 7 additions & 1 deletion include/realizations/catchment/Bmi_Multi_Formulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ namespace realization {
* @param output_stream
*/
Bmi_Multi_Formulation(std::string id, std::shared_ptr<data_access::GenericDataProvider> forcing_provider, utils::StreamHandler output_stream)
: Bmi_Formulation(std::move(id), forcing_provider, output_stream) { };
: Bmi_Formulation(std::move(id), forcing_provider, output_stream)
, evapotranspiration_module_index{-1}
{ };

virtual ~Bmi_Multi_Formulation() {};

Expand Down Expand Up @@ -544,6 +546,8 @@ namespace realization {

void update(time_step_t t_index, time_step_t t_delta) override;

double get_current_evapotranspiration() override;

protected:

/**
Expand Down Expand Up @@ -770,6 +774,8 @@ namespace realization {
/** Whether the realization file follows legacy format or the new format. */
bool legacy_json_format = false;

int evapotranspiration_module_index;

friend Bmi_Multi_Formulation_Test;
friend class ::Bmi_Cpp_Multi_Array_Test;

Expand Down
2 changes: 2 additions & 0 deletions include/realizations/catchment/Catchment_Formulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ namespace realization {
*/
void finalize();

virtual double get_current_evapotranspiration() = 0;

protected:
std::string get_catchment_id() const override {
return this->cat_id;
Expand Down
6 changes: 3 additions & 3 deletions src/core/Layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "HY_Features.hpp"
#endif

void ngen::Layer::update_models(boost::span<double> catchment_outflows,
void ngen::Layer::update_models(boost::span<double> catchment_evapotranspiration,
std::unordered_map<std::string, int> &catchment_indexes,
boost::span<double> nexus_downstream_flows,
std::unordered_map<std::string, int> &nexus_indexes,
Expand Down Expand Up @@ -46,8 +46,8 @@ void ngen::Layer::update_models(boost::span<double> catchment_outflows,
throw std::runtime_error(msg);
}
#if NGEN_WITH_ROUTING
int results_index = catchment_indexes[id];
catchment_outflows[results_index] += response;
int et_index = catchment_indexes[id];
catchment_evapotranspiration[et_index] += r_c->get_current_evapotranspiration();
#endif // NGEN_WITH_ROUTING
if (r_c->get_output_header_count() > 0) {
// only write output if config specifies output values
Expand Down
Loading