NGen
Loading...
Searching...
No Matches
realization::Bmi_Module_Formulation Class Referenceabstract

Abstraction of a formulation with a single backing model object that implements the BMI. More...

#include <Bmi_Module_Formulation.hpp>

+ Inheritance diagram for realization::Bmi_Module_Formulation:
+ Collaboration diagram for realization::Bmi_Module_Formulation:

Public Types

typedef long time_step_t
 
using data_type = DataType
 This class provides a generic interface to data services.
 
using selection_type = SelectionType
 

Public Member Functions

 Bmi_Module_Formulation (std::string id, std::shared_ptr< data_access::GenericDataProvider > forcing_provider, utils::StreamHandler output_stream)
 Minimal constructor for objects initialize using the Formulation_Manager and subsequent calls to create_formulation.
 
 ~Bmi_Module_Formulation () override=default
 
void create_formulation (boost::property_tree::ptree &config, geojson::PropertyMap *global=nullptr) override
 
void create_formulation (geojson::PropertyMap properties) override
 
boost::span< const std::string > get_available_variable_names () const override
 Get the collection of forcing output property names this instance can provide.
 
std::string get_output_line_for_timestep (int timestep, std::string delimiter) override
 Get a delimited string with all the output variable values for the given time step.
 
double get_response (time_step_t t_index, time_step_t t_delta) override
 Get the model response for a time step.
 
time_t get_variable_time_begin (const std::string &variable_name)
 Get the inclusive beginning of the period of time over which this instance can provide data for this forcing.
 
long get_data_start_time () const override
 Get the inclusive beginning of the period of time over which this instance can provide data for this forcing.
 
long get_data_stop_time () const override
 Return the last valid time for which data from the requested variable can be requested.
 
long record_duration () const override
 Return the stride in the time dimension.
 
const double get_model_current_time () const override
 Get the current time for the backing BMI model in its native format and units.
 
const double get_model_end_time () const override
 Get the end time for the backing BMI model in its native format and units.
 
const std::vector< std::string > & get_required_parameters () const override
 
const std::string & get_config_mapped_variable_name (const std::string &model_var_name) const override
 When possible, translate a variable name for a BMI model to an internally recognized name.
 
size_t get_ts_index_for_time (const time_t &epoch_time) const override
 Get the index of the forcing time step that contains the given point in time.
 
std::vector< double > get_values (const CatchmentAggrDataSelector &selector, data_access::ReSampleMethod m=SUM) override
 Get the 1D values of a forcing property for an arbitrary time period, converting units if needed.
 
double get_value (const CatchmentAggrDataSelector &selector, data_access::ReSampleMethod m) override
 Get the value of a forcing property for an arbitrary time period, converting units if needed.
 
bool is_bmi_input_variable (const std::string &var_name) const override
 
bool is_bmi_output_variable (const std::string &var_name) const override
 
bool is_property_sum_over_time_step (const std::string &name) const override
 Get whether a property's per-time-step values are each an aggregate sum over the entire time step.
 
const std::vector< std::string > get_bmi_input_variables () const override
 
const std::vector< std::string > get_bmi_output_variables () const override
 
virtual time_t convert_model_time (const double &model_time) const =0
 Convert a time value from the model to an epoch time in seconds.
 
const std::string & get_bmi_main_output_var () const
 
std::string get_model_type_name () const
 Get the name of the specific type of the backing model object.
 
const std::vector< std::string > & get_output_header_fields () const
 Get the values making up the header line from get_output_header_line(), but organized as a vector of strings.
 
std::string get_output_header_line (std::string delimiter) const override
 Get a header line appropriate for a file made up of entries from this type's implementation of get_output_line_for_timestep.
 
const std::vector< std::string > & get_output_variable_names () const
 Get the names of variables in formulation output.
 
void set_output_precision (int precision)
 Set the precision of output values when converted to text.
 
virtual double get_var_value_as_double (const int &index, const std::string &var_name)=0
 Get value for some BMI model variable at a specific index.
 
void finalize ()
 Release resources of the given forcing provider.
 
virtual void finalize ()
 Release any resources that should not be held as the run is shutting down.
 
virtual std::string get_formulation_type () const =0
 
std::string get_id () const
 
void set_output_stream (std::string file_path)
 
void write_output (std::string out)
 
virtual data_type get_value (const selection_type &selector, ReSampleMethod m=SUM)=0
 Get the value of a forcing property for an arbitrary time period, converting units if needed.
 
virtual std::vector< data_typeget_values (const selection_type &selector, ReSampleMethod m=SUM)=0
 Get the values of a forcing property for an arbitrary time period, converting units if needed.
 

Static Public Member Functions

static void config_pattern_substitution (geojson::PropertyMap &properties, const std::string &key, const std::string &pattern, const std::string &replacement)
 Perform in-place substitution on the given config property item, if the item and the pattern are present.
 

Protected Member Functions

void get_bmi_output_var_name (const std::string &name, std::string &bmi_var_name)
 Get correct BMI variable name, which may be the output or something mapped to this output.
 
virtual std::shared_ptr< models::bmi::Bmi_Adapterconstruct_model (const geojson::PropertyMap &properties)=0
 Construct model and its shared pointer, potentially supplying input variable values from config.
 
void determine_model_time_offset ()
 Determine and set the offset time of the model in seconds, compared to forcing data.
 
const bool & get_allow_model_exceed_end_time () const override
 Get whether a model may perform updates beyond its end_time.
 
const std::string & get_bmi_init_config () const
 
std::shared_ptr< models::bmi::Bmi_Adapterget_bmi_model () const
 Get the backing model object implementing the BMI.
 
const time_t & get_bmi_model_start_time_forcing_offset_s () const override
 
void inner_create_formulation (geojson::PropertyMap properties, bool needs_param_validation)
 Universal logic applied when creating a BMI-backed formulation from NGen config.
 
void set_initial_bmi_parameters (geojson::PropertyMap properties)
 Check configuration properties for model_params and attempt to set them in the bmi model.
 
bool is_bmi_model_time_step_fixed () const override
 Test whether backing model has fixed time step size.
 
bool is_model_initialized () const override
 Test whether the backing model object has been initialize using the BMI standard Initialize function.
 
void set_allow_model_exceed_end_time (bool allow_exceed_end)
 
void set_bmi_init_config (const std::string &init_config)
 
void set_bmi_model (std::shared_ptr< models::bmi::Bmi_Adapter > model)
 Set the backing model object implementing the BMI.
 
void set_bmi_model_start_time_forcing_offset_s (const time_t &offset_s)
 
void set_bmi_model_time_step_fixed (bool is_fix_time_step)
 
virtual void set_model_initialized (bool is_initialized)
 Set whether the backing model object has been initialize using the BMI standard Initialize function.
 
void set_model_inputs_prior_to_update (const double &model_init_time, time_step_t t_delta)
 Set BMI input variable values for the model appropriately prior to calling its BMIupdate()``.
 
int get_output_precision ()
 
void set_bmi_main_output_var (const std::string &main_output_var)
 
virtual void set_model_type_name (std::string type_name)
 Set the name of the specific type of the backing model object.
 
void set_output_header_fields (const std::vector< std::string > &output_headers)
 
void set_output_variable_names (const std::vector< std::string > &out_var_names)
 Set the names of variables in formulation output.
 
std::string get_catchment_id () const override
 
void set_catchment_id (std::string cat_id) override
 
geojson::PropertyMap interpret_parameters (boost::property_tree::ptree &config, geojson::PropertyMap *global=nullptr)
 
void validate_parameters (geojson::PropertyMap options)
 

Protected Attributes

time_step_t last_model_response_delta = 0
 The delta of the last model update execution (typically, this is time step size).
 
time_t last_model_response_start_time = 0
 The epoch time of the model at the beginning of its last update.
 
std::map< std::string, std::shared_ptr< data_access::GenericDataProvider > > input_forcing_providers
 
int next_time_step_index = 0
 Index value (0-based) of the time step that will be processed by the next update of the model.
 
std::shared_ptr< std::ostringstream > output_text_stream
 Object to help with converting numeric output values to text.
 
std::shared_ptr< data_access::GenericDataProviderforcing
 
std::string id
 
polygon_t bounds
 
utils::StreamHandler output
 
std::shared_ptr< HY_Catchmentrealized_catchment
 
unsigned long id_number
 

Private Attributes

bool allow_model_exceed_end_time = false
 Whether model Update calls are allowed and handled in some way by the backing model for time steps after the model's end_time.
 
std::vector< std::string > available_forcings
 The set of available "forcings" (output variables, plus their mapped aliases) that the model can provide.
 
std::string bmi_init_config
 
std::shared_ptr< models::bmi::Bmi_Adapterbmi_model
 
bool bmi_model_time_step_fixed = true
 Whether backing model has fixed time step size.
 
time_t bmi_model_start_time_forcing_offset_s
 The offset, converted to seconds, from the model's start time to the start time of the initial forcing time step.
 
std::map< std::string, std::string > bmi_var_names_map
 A configured mapping of BMI model variable names to standard names for use inside the framework.
 
bool model_initialized = false
 
std::vector< std::string > OPTIONAL_PARAMETERS
 
std::vector< std::string > REQUIRED_PARAMETERS
 
std::string bmi_main_output_var
 
std::string model_type_name
 
std::vector< std::string > output_header_fields
 Output header field strings corresponding to the variables output by the realization, as defined in output_variable_names.
 
std::vector< std::string > output_variable_names
 Names of the variables to include in the output from this formulation, which will be some ordered subset of the BMI module output variables accessible to the instance.
 
int output_precision
 The degree of precision in output values when converting to text.
 
std::string cat_id
 

Friends

class Bmi_Multi_Formulation
 
class ::Bmi_Formulation_Test
 
class ::Bmi_C_Formulation_Test
 
class ::Bmi_Multi_Formulation_Test
 
class ::Bmi_Cpp_Formulation_Test
 
class ::Bmi_Cpp_Multi_Array_Test
 

Detailed Description

Abstraction of a formulation with a single backing model object that implements the BMI.

Member Typedef Documentation

◆ data_type

template<class DataType , class SelectionType >
using data_access::DataProvider< DataType, SelectionType >::data_type = DataType
inherited

This class provides a generic interface to data services.

◆ selection_type

template<class DataType , class SelectionType >
using data_access::DataProvider< DataType, SelectionType >::selection_type = SelectionType
inherited

◆ time_step_t

Constructor & Destructor Documentation

◆ Bmi_Module_Formulation()

realization::Bmi_Module_Formulation::Bmi_Module_Formulation ( std::string id,
std::shared_ptr< data_access::GenericDataProvider > forcing_provider,
utils::StreamHandler output_stream )
inline

Minimal constructor for objects initialize using the Formulation_Manager and subsequent calls to create_formulation.

Parameters
id
forcing_provider
output_stream

◆ ~Bmi_Module_Formulation()

realization::Bmi_Module_Formulation::~Bmi_Module_Formulation ( )
overridedefault

Member Function Documentation

◆ config_pattern_substitution()

static void realization::Catchment_Formulation::config_pattern_substitution ( geojson::PropertyMap & properties,
const std::string & key,
const std::string & pattern,
const std::string & replacement )
inlinestaticinherited

Perform in-place substitution on the given config property item, if the item and the pattern are present.

Any and all instances of the substring pattern are replaced by replacement, if key maps to a present string-type property value.

Parameters
propertiesA reference to the properties config object to be altered.
keyThe key for the configuration property to potentially adjust.
patternThe pattern substring to search for that, when present, should be replaced.
replacementThe replacement substring to potentially insert.

References geojson::String.

Referenced by realization::Formulation_Manager::construct_missing_formulation(), and realization::Bmi_Multi_Formulation::init_nested_module().

◆ construct_model()

virtual std::shared_ptr< models::bmi::Bmi_Adapter > realization::Bmi_Module_Formulation::construct_model ( const geojson::PropertyMap & properties)
protectedpure virtual

Construct model and its shared pointer, potentially supplying input variable values from config.

Construct a model (and a shared pointer to it), checking whether additional input variable values are present in the configuration properties and need to be used during model construction.

Parameters
propertiesConfiguration properties for the formulation, potentially containing values for input variables
Returns
A shared pointer to a newly constructed model object

Implemented in realization::Bmi_C_Formulation, and realization::Bmi_Cpp_Formulation.

Referenced by inner_create_formulation().

◆ convert_model_time()

virtual time_t realization::Bmi_Formulation::convert_model_time ( const double & model_time) const
pure virtualinherited

Convert a time value from the model to an epoch time in seconds.

Model time values are typically (though not always) 0-based totals count upward as time progresses. The units are not necessarily seconds. This performs the necessary lookup and conversion for such units, and then shifts the value appropriately for epoch time representation.

Parameters
model_time
Returns

Implemented in realization::Bmi_C_Formulation, realization::Bmi_Cpp_Formulation, and realization::Bmi_Multi_Formulation.

Referenced by determine_model_time_offset(), and set_model_inputs_prior_to_update().

◆ create_formulation() [1/2]

void realization::Bmi_Module_Formulation::create_formulation ( boost::property_tree::ptree & config,
geojson::PropertyMap * global = nullptr )
overridevirtual

◆ create_formulation() [2/2]

void realization::Bmi_Module_Formulation::create_formulation ( geojson::PropertyMap properties)
overridevirtual

◆ determine_model_time_offset()

void realization::Bmi_Module_Formulation::determine_model_time_offset ( )
protected

Determine and set the offset time of the model in seconds, compared to forcing data.

BMI models frequently have their model start time be set to 0. As such, to know what the forcing time is compared to the model time, an offset value is needed. This becomes important in situations when the size of the time steps for forcing data versus model execution are not equal. This method will determine and set this value.

References realization::Bmi_Formulation::convert_model_time(), realization::Catchment_Formulation::forcing, get_bmi_model(), and set_bmi_model_start_time_forcing_offset_s().

Referenced by inner_create_formulation().

◆ finalize() [1/2]

template<class DataType , class SelectionType >
virtual void data_access::DataProvider< DataType, SelectionType >::finalize ( )
inlinevirtualinherited

Release any resources that should not be held as the run is shutting down.

In particular, this should be called before MPI_Finalize()

Reimplemented in data_access::WrappedDataProvider.

◆ finalize() [2/2]

void realization::Catchment_Formulation::finalize ( )
inlineinherited

Release resources of the given forcing provider.

References realization::Catchment_Formulation::forcing.

◆ get_allow_model_exceed_end_time()

const bool & realization::Bmi_Module_Formulation::get_allow_model_exceed_end_time ( ) const
overrideprotectedvirtual

Get whether a model may perform updates beyond its end_time.

Get whether model Update calls are allowed and handled in some way by the backing model for time steps after the model's end_time. Implementations of this type should use this function to safeguard against entering either an invalid or otherwise undesired state as a result of attempting to process a model beyond its available data.

As mentioned, even for models that are capable of validly handling processing beyond end time, it may be desired that they do not for some reason (e.g., the way they account for the lack of input data leads to valid but incorrect results for a specific application). Because of this, whether models are allowed to process beyond their end time is configuration-based.

Returns
Whether a model may perform updates beyond its end_time.

Implements realization::Bmi_Formulation.

References allow_model_exceed_end_time.

Referenced by get_response().

◆ get_available_variable_names()

boost::span< const std::string > realization::Bmi_Module_Formulation::get_available_variable_names ( ) const
overridevirtual

Get the collection of forcing output property names this instance can provide.

This is part of the ForcingProvider interface. This interface must be implemented for items of this type to be usable as "forcing" providers for situations when some other object needs to receive as an input (i.e., one of its forcings) a data property output from this object.

For this type, this is the collection of BMI output variables, plus any aliases included in the formulation config's output variable mapping.

Returns
The collection of forcing output property names this instance can provide.
See also
ForcingProvider

Implements data_access::DataProvider< DataType, SelectionType >.

References available_forcings.

Referenced by get_value(), and get_values().

◆ get_bmi_init_config()

const std::string & realization::Bmi_Module_Formulation::get_bmi_init_config ( ) const
protected

◆ get_bmi_input_variables()

const std::vector< std::string > realization::Bmi_Module_Formulation::get_bmi_input_variables ( ) const
overridevirtual

◆ get_bmi_main_output_var()

const std::string & realization::Bmi_Formulation::get_bmi_main_output_var ( ) const
inlineinherited

◆ get_bmi_model()

◆ get_bmi_model_start_time_forcing_offset_s()

const time_t & realization::Bmi_Module_Formulation::get_bmi_model_start_time_forcing_offset_s ( ) const
overrideprotectedvirtual

◆ get_bmi_output_var_name()

void realization::Bmi_Module_Formulation::get_bmi_output_var_name ( const std::string & name,
std::string & bmi_var_name )
protected

Get correct BMI variable name, which may be the output or something mapped to this output.

Parameters
name
bmi_var_name

References bmi_var_names_map, get_bmi_model(), and bmi::Bmi::GetOutputVarNames().

Referenced by get_value(), and get_values().

◆ get_bmi_output_variables()

const std::vector< std::string > realization::Bmi_Module_Formulation::get_bmi_output_variables ( ) const
overridevirtual

◆ get_catchment_id()

◆ get_config_mapped_variable_name()

const std::string & realization::Bmi_Module_Formulation::get_config_mapped_variable_name ( const std::string & model_var_name) const
overridevirtual

When possible, translate a variable name for a BMI model to an internally recognized name.

Translate some BMI variable name to something recognized in some internal context for use within NGen. Do this according to the map of variable names supplied in the external formulation config. If no mapping for the given variable name was configured, return the variable name itself.

For example, perhaps a BMI model has the input variable "RAIN_RATE." Configuring this variable name to map to "precip_rate" will allow the formulation to understand that this particular forcing field should be used to set the model's "RAIN_RATE" variable.

Parameters
model_var_nameThe BMI variable name to translate so its purpose is recognized internally.
Returns
Either the internal equivalent variable name, or the provided name if there is not a mapping entry.

Implements realization::Bmi_Formulation.

References bmi_var_names_map.

Referenced by set_model_inputs_prior_to_update().

◆ get_data_start_time()

long realization::Bmi_Module_Formulation::get_data_start_time ( ) const
overridevirtual

Get the inclusive beginning of the period of time over which this instance can provide data for this forcing.

This is part of the DataProvider interface. This interface must be implemented for items of this type to be usable as "forcing" providers for situations when some other object needs to receive as an input (i.e., one of its forcings) a data property output from this object.

Returns
The inclusive beginning of the period of time over which this instance can provide this data.

Implements data_access::DataProvider< DataType, SelectionType >.

References get_bmi_model(), and bmi::Bmi::GetStartTime().

◆ get_data_stop_time()

long realization::Bmi_Module_Formulation::get_data_stop_time ( ) const
overridevirtual

Return the last valid time for which data from the requested variable can be requested.

Implements data_access::DataProvider< DataType, SelectionType >.

◆ get_formulation_type()

virtual std::string realization::Formulation::get_formulation_type ( ) const
pure virtualinherited

◆ get_id()

std::string realization::Formulation::get_id ( ) const
inlineinherited

◆ get_model_current_time()

const double realization::Bmi_Module_Formulation::get_model_current_time ( ) const
overridevirtual

Get the current time for the backing BMI model in its native format and units.

Returns
The current time for the backing BMI model in its native format and units.

Implements realization::Bmi_Formulation.

References get_bmi_model(), and bmi::Bmi::GetCurrentTime().

◆ get_model_end_time()

const double realization::Bmi_Module_Formulation::get_model_end_time ( ) const
overridevirtual

Get the end time for the backing BMI model in its native format and units.

Returns
The end time for the backing BMI model in its native format and units.

Implements realization::Bmi_Formulation.

References get_bmi_model(), and bmi::Bmi::GetEndTime().

◆ get_model_type_name()

std::string realization::Bmi_Formulation::get_model_type_name ( ) const
inlineinherited

◆ get_output_header_fields()

const std::vector< std::string > & realization::Bmi_Formulation::get_output_header_fields ( ) const
inlineinherited

Get the values making up the header line from get_output_header_line(), but organized as a vector of strings.

Returns
The values making up the header line from get_output_header_line() organized as a vector.

References realization::Bmi_Formulation::output_header_fields.

Referenced by realization::Bmi_Formulation::get_output_header_line().

◆ get_output_header_line()

std::string realization::Bmi_Formulation::get_output_header_line ( std::string delimiter) const
inlineoverridevirtualinherited

Get a header line appropriate for a file made up of entries from this type's implementation of get_output_line_for_timestep.

Note that like the output generating function, this line does not include anything for time step.

Returns
An appropriate header line for this type.

Reimplemented from realization::Catchment_Formulation.

References realization::Bmi_Formulation::get_output_header_fields().

◆ get_output_line_for_timestep()

std::string realization::Bmi_Module_Formulation::get_output_line_for_timestep ( int timestep,
std::string delimiter )
overridevirtual

Get a delimited string with all the output variable values for the given time step.

This method is useful for preparing calculated data in a representation useful for output files, such as CSV files.

The resulting string contains only the calculated output values for the time step, and not the time step index itself.

An empty string is returned if the time step value is not in the range of valid time steps for which there are calculated values for all variables.

The default delimiter is a comma.

Implementations will throw invalid_argument exceptions if data for the provided time step parameter is not accessible. Note that, for this type, only the last processed time step is accessible, because formulations do not save results from previous time steps. This also has the consequence of there being no valid set of arguments before a least one call to get_response has been made.

Parameters
timestepThe time step for which data is desired.
Returns
A delimited string with all the output variable values for the given time step.

Implements realization::Catchment_Formulation.

References realization::Bmi_Formulation::get_output_variable_names(), realization::Bmi_Formulation::get_var_value_as_double(), and next_time_step_index.

◆ get_output_precision()

int realization::Bmi_Formulation::get_output_precision ( )
inlineprotectedinherited

◆ get_output_variable_names()

const std::vector< std::string > & realization::Bmi_Formulation::get_output_variable_names ( ) const
inlineinherited

Get the names of variables in formulation output.

Get the names of the variables to include in the output from this formulation, which should be some ordered subset of the BMI module output variables accessible to this instance.

Returns

References realization::Bmi_Formulation::output_variable_names.

Referenced by realization::Bmi_Multi_Formulation::check_output_var_names(), realization::Bmi_Multi_Formulation::create_multi_formulation(), get_output_line_for_timestep(), realization::Bmi_Multi_Formulation::get_output_line_for_timestep(), and inner_create_formulation().

◆ get_required_parameters()

const std::vector< std::string > & realization::Bmi_Module_Formulation::get_required_parameters ( ) const
overridevirtual

Reimplemented from realization::Bmi_Formulation.

References REQUIRED_PARAMETERS.

◆ get_response()

double realization::Bmi_Module_Formulation::get_response ( time_step_t t_index,
time_step_t t_delta )
overridevirtual

Get the model response for a time step.

Get the model response for the provided time step, executing the backing model formulation one or more times as needed.

Function assumes the backing model has been fully initialized an that any additional input values have been applied.

The function throws an error if the index of a previously processed time step is supplied, except if it is the last processed time step. In that case, the appropriate value is returned as described below, but without executing any model update.

Assuming updating to the implied time is valid for the model, the function executes one or more model updates to process future time steps for the necessary indexes. Multiple time steps updates occur when the given future time step index is not the next time step index to be processed. Regardless, all processed time steps have the size supplied in t_delta.

However, it is possible to provide t_index and t_delta values that would result in the aggregate updates taking the model's time beyond its end_time value. In such cases, if the formulation config indicates this model is not allow to exceed its set end_time, the function does not update the model and throws an error.

The function will return the value of the primary output variable (see get_bmi_main_output_var()) for the given time step after the model has been updated to that point. The type returned will always be a double, with other numeric types being cast if necessary.

The BMI spec requires for variable values to be passed to/from models via as arrays. This function essentially treats the variable array reference as if it were just a raw pointer and returns the 0-th array value.

Parameters
t_indexThe index of the time step for which to run model calculations.
d_delta_sThe duration, in seconds, of the time step for which to run model calculations.
Returns
The total discharge of the model for the given time step.

Implements realization::Catchment_Formulation.

References models::bmi::Bmi_Adapter::convert_seconds_to_model_time(), get_allow_model_exceed_end_time(), realization::Bmi_Formulation::get_bmi_main_output_var(), get_bmi_model(), realization::Formulation::get_formulation_type(), realization::Bmi_Formulation::get_var_value_as_double(), bmi::Bmi::GetCurrentTime(), next_time_step_index, set_model_inputs_prior_to_update(), bmi::Bmi::Update(), and bmi::Bmi::UpdateUntil().

◆ get_ts_index_for_time()

size_t realization::Bmi_Module_Formulation::get_ts_index_for_time ( const time_t & epoch_time) const
overridevirtual

Get the index of the forcing time step that contains the given point in time.

This is part of the ForcingProvider interface. This interface must be implemented for items of this type to be usable as "forcing" providers for situations when some other object needs to receive as an input (i.e., one of its forcings) a data property output from this object.

An std::out_of_range exception should be thrown if the time is not in any time step.

Parameters
epoch_timeThe point in time, as a seconds-based epoch time.
Returns
The index of the forcing time step that contains the given point in time.
Exceptions
std::out_of_rangeIf the given point is not in any time step.

Implements data_access::DataProvider< DataType, SelectionType >.

◆ get_value() [1/2]

template<class DataType , class SelectionType >
virtual data_type data_access::DataProvider< DataType, SelectionType >::get_value ( const selection_type & selector,
ReSampleMethod m = SUM )
pure virtualinherited

Get the value of a forcing property for an arbitrary time period, converting units if needed.

An std::out_of_range exception should be thrown if the data for the time period is not available.

Parameters
selectorData required to establish what subset of the stored data should be accessed
mHow data is to be resampled if there is a mismatch in data alignment or repeat rate
Returns
The value of the forcing property for the described time period, with units converted if needed.
Exceptions
std::out_of_rangeIf data for the time period is not available.

Referenced by data_access::WrappedDataProvider::get_value(), and set_model_inputs_prior_to_update().

◆ get_value() [2/2]

double realization::Bmi_Module_Formulation::get_value ( const CatchmentAggrDataSelector & selector,
data_access::ReSampleMethod m )
override

Get the value of a forcing property for an arbitrary time period, converting units if needed.

This is part of the ForcingProvider interface. This interface must be implemented for items of this type to be usable as "forcing" providers for situations when some other object needs to receive as an input (i.e., one of its forcings) a data property output from this object.

An std::out_of_range exception should be thrown if the data for the time period is not available.

Parameters
output_nameThe name of the forcing property of interest.
init_time_epochThe epoch time (in seconds) of the start of the time period.
duration_secondsThe length of the time period, in seconds.
output_unitsThe expected units of the desired output value.
Returns
The value of the forcing property for the described time period, with units converted if needed.
Exceptions
std::out_of_rangeIf data for the time period is not available.
See also
ForcingProvider::get_value

References get_available_variable_names(), get_bmi_model(), get_bmi_output_var_name(), UnitsHelper::get_converted_value(), CatchmentAggrDataSelector::get_duration_secs(), realization::Formulation::get_formulation_type(), CatchmentAggrDataSelector::get_init_time(), CatchmentAggrDataSelector::get_output_units(), realization::Bmi_Formulation::get_var_value_as_double(), CatchmentAggrDataSelector::get_variable_name(), and bmi::Bmi::GetVarUnits().

◆ get_values() [1/2]

template<class DataType , class SelectionType >
virtual std::vector< data_type > data_access::DataProvider< DataType, SelectionType >::get_values ( const selection_type & selector,
ReSampleMethod m = SUM )
pure virtualinherited

Get the values of a forcing property for an arbitrary time period, converting units if needed.

An std::out_of_range exception should be thrown if the data for the time period is not available.

If a provider doesn't implement this function, then by default, get_values will be a simple proxy to get_value with the result wrapped in a std::vector<double>

Parameters
output_nameThe name of the forcing property of interest.
init_time_epochThe epoch time (in seconds) of the start of the time period.
duration_secondsThe length of the time period, in seconds.
output_unitsThe expected units of the desired output value.
Returns
std::vector<double> The vector of values of the forcing property for the described time period, with units converted if needed.
Exceptions
std::out_of_rangeIf data for the time period is not available.

Referenced by data_access::WrappedDataProvider::get_values(), and set_model_inputs_prior_to_update().

◆ get_values() [2/2]

std::vector< double > realization::Bmi_Module_Formulation::get_values ( const CatchmentAggrDataSelector & selector,
data_access::ReSampleMethod m = SUM )
override

Get the 1D values of a forcing property for an arbitrary time period, converting units if needed.

Parameters
output_nameThe name of the forcing property of interest.
init_timeThe epoch time (in seconds) of the start of the time period.
duration_sThe length of the time period, in seconds.
output_unitsThe expected units of the desired output value.
Returns
std::vector<double> The 1D values of the forcing property for the described time period, with units converted if needed.
Exceptions
std::out_of_rangeIf data for the time period is not available.
std::runtime_erroroutput_name is not one of the available outputs of this provider instance.

References UnitsHelper::convert_values(), get_available_variable_names(), get_bmi_model(), get_bmi_output_var_name(), CatchmentAggrDataSelector::get_duration_secs(), realization::Formulation::get_formulation_type(), CatchmentAggrDataSelector::get_init_time(), CatchmentAggrDataSelector::get_output_units(), CatchmentAggrDataSelector::get_variable_name(), models::bmi::GetValue(), bmi::Bmi::GetVarUnits(), and logging::warning().

◆ get_var_value_as_double()

virtual double realization::Bmi_Formulation::get_var_value_as_double ( const int & index,
const std::string & var_name )
pure virtualinherited

Get value for some BMI model variable at a specific index.

Function gets the value for a provided variable, returned from the backing model as an array, and returns the specific value at the desired index cast as a double type.

The function makes several assumptions:

1. `index` is within array bounds
2. `var_name` is in the set of valid variable names for the model
3. the type for output variable allows the value to be cast to a `double` appropriately

It falls to user (functions) of this function to ensure these assumptions hold before invoking.

Parameters
index
var_name
Returns

Implemented in realization::Bmi_C_Formulation, realization::Bmi_Cpp_Formulation, and realization::Bmi_Multi_Formulation.

Referenced by get_output_line_for_timestep(), get_response(), and get_value().

◆ get_variable_time_begin()

time_t realization::Bmi_Module_Formulation::get_variable_time_begin ( const std::string & variable_name)

Get the inclusive beginning of the period of time over which this instance can provide data for this forcing.

This is part of the ForcingProvider interface. This interface must be implemented for items of this type to be usable as "forcing" providers for situations when some other object needs to receive as an input (i.e., one of its forcings) a data property output from this object.

Returns
The inclusive beginning of the period of time over which this instance can provide this data.

◆ inner_create_formulation()

void realization::Bmi_Module_Formulation::inner_create_formulation ( geojson::PropertyMap properties,
bool needs_param_validation )
protected

Universal logic applied when creating a BMI-backed formulation from NGen config.

This performs all the necessary steps to initialize this formulation from provided configuration properties. It is written in such a way that it can be used in appropriately crafted nested calls from both public create_formulation implementations, thus allowing the primary formulation initialization logic to be centralized and not duplicated.

Parameters
properties
needs_param_validation

References available_forcings, bmi_var_names_map, construct_model(), determine_model_time_offset(), get_bmi_model(), realization::Bmi_Formulation::get_output_variable_names(), models::bmi::Bmi_Adapter::is_model_initialized(), model_initialized, set_allow_model_exceed_end_time(), set_bmi_init_config(), realization::Bmi_Formulation::set_bmi_main_output_var(), set_bmi_model(), set_bmi_model_time_step_fixed(), set_initial_bmi_parameters(), realization::Bmi_Formulation::set_model_type_name(), realization::Bmi_Formulation::set_output_header_fields(), realization::Bmi_Formulation::set_output_precision(), realization::Bmi_Formulation::set_output_variable_names(), and realization::Formulation::validate_parameters().

Referenced by create_formulation(), and create_formulation().

◆ interpret_parameters()

geojson::PropertyMap realization::Formulation::interpret_parameters ( boost::property_tree::ptree & config,
geojson::PropertyMap * global = nullptr )
inlineprotectedinherited

◆ is_bmi_input_variable()

bool realization::Bmi_Module_Formulation::is_bmi_input_variable ( const std::string & var_name) const
overridevirtual

◆ is_bmi_model_time_step_fixed()

bool realization::Bmi_Module_Formulation::is_bmi_model_time_step_fixed ( ) const
overrideprotectedvirtual

Test whether backing model has fixed time step size.

Returns
Whether backing model has fixed time step size.

Implements realization::Bmi_Formulation.

References bmi_model_time_step_fixed.

Referenced by realization::Bmi_C_Formulation::construct_model(), and realization::Bmi_Cpp_Formulation::construct_model().

◆ is_bmi_output_variable()

bool realization::Bmi_Module_Formulation::is_bmi_output_variable ( const std::string & var_name) const
overridevirtual

◆ is_model_initialized()

bool realization::Bmi_Module_Formulation::is_model_initialized ( ) const
overrideprotectedvirtual

Test whether the backing model object has been initialize using the BMI standard Initialize function.

Returns
Whether backing model object has been initialize using the BMI standard Initialize function.

Implements realization::Bmi_Formulation.

References model_initialized.

◆ is_property_sum_over_time_step()

bool realization::Bmi_Module_Formulation::is_property_sum_over_time_step ( const std::string & name) const
overridevirtual

Get whether a property's per-time-step values are each an aggregate sum over the entire time step.

This is part of the ForcingProvider interface. This interface must be implemented for items of this type to be usable as "forcing" providers for situations when some other object needs to receive as an input (i.e., one of its forcings) a data property output from this object.

Certain properties, like rain fall, are aggregated sums over an entire time step. Others, such as pressure, are not such sums and instead something else like an instantaneous reading or an average value.

It may be the case that forcing data is needed for some discretization different than the forcing time step. This aspect must be known in such cases to perform the appropriate value interpolation.

For instances of this type, all output forcings fall under this category.

Parameters
nameThe name of the forcing property for which the current value is desired.
Returns
Whether the property's value is an aggregate sum, which is always true for this type.

Reimplemented from data_access::DataProvider< DataType, SelectionType >.

◆ record_duration()

long realization::Bmi_Module_Formulation::record_duration ( ) const
overridevirtual

Return the stride in the time dimension.

Implements data_access::DataProvider< DataType, SelectionType >.

◆ set_allow_model_exceed_end_time()

void realization::Bmi_Module_Formulation::set_allow_model_exceed_end_time ( bool allow_exceed_end)
protected

◆ set_bmi_init_config()

void realization::Bmi_Module_Formulation::set_bmi_init_config ( const std::string & init_config)
protected

References bmi_init_config.

Referenced by inner_create_formulation().

◆ set_bmi_main_output_var()

void realization::Bmi_Formulation::set_bmi_main_output_var ( const std::string & main_output_var)
inlineprotectedinherited

◆ set_bmi_model()

void realization::Bmi_Module_Formulation::set_bmi_model ( std::shared_ptr< models::bmi::Bmi_Adapter > model)
protected

Set the backing model object implementing the BMI.

Parameters
modelShared pointer to the BMI model.

References bmi_model.

Referenced by inner_create_formulation().

◆ set_bmi_model_start_time_forcing_offset_s()

void realization::Bmi_Module_Formulation::set_bmi_model_start_time_forcing_offset_s ( const time_t & offset_s)
protected

◆ set_bmi_model_time_step_fixed()

void realization::Bmi_Module_Formulation::set_bmi_model_time_step_fixed ( bool is_fix_time_step)
protected

◆ set_catchment_id()

void realization::Catchment_Formulation::set_catchment_id ( std::string cat_id)
inlineoverrideprotectedvirtualinherited

◆ set_initial_bmi_parameters()

void realization::Bmi_Module_Formulation::set_initial_bmi_parameters ( geojson::PropertyMap properties)
protected

Check configuration properties for model_params and attempt to set them in the bmi model.

This checks for a key named model_params in the parsed properties, and for each property it will attempt to call SetValue using the property's key as the BMI variable and the property's value as the value to set.

This function should only be called once bmi_model is properly constructed. If bmi_model is a nullptr, this function becomes a no-op.

References models::bmi::Bmi_Adapter::get_analogous_cxx_type(), get_bmi_model(), geojson::get_propertytype_name(), realization::get_values_as_type(), bmi::Bmi::GetVarItemsize(), bmi::Bmi::GetVarNbytes(), geojson::List, geojson::Natural, geojson::Real, bmi::Bmi::SetValue(), and logging::warning().

Referenced by inner_create_formulation().

◆ set_model_initialized()

void realization::Bmi_Module_Formulation::set_model_initialized ( bool is_initialized)
protectedvirtual

Set whether the backing model object has been initialize using the BMI standard Initialize function.

Parameters
is_initializedWhether model object has been initialize using the BMI standard Initialize.

References model_initialized.

◆ set_model_inputs_prior_to_update()

void realization::Bmi_Module_Formulation::set_model_inputs_prior_to_update ( const double & model_init_time,
time_step_t t_delta )
protected

Set BMI input variable values for the model appropriately prior to calling its BMIupdate()``.

Parameters
model_initial_timeThe model's time prior to the update, in its internal units and representation.
t_deltaThe size of the time step over which the formulation is going to update the model, which might be different than the model's internal time step.

References realization::Bmi_Formulation::convert_model_time(), realization::Catchment_Formulation::forcing, models::bmi::Bmi_Adapter::get_analogous_cxx_type(), get_bmi_model(), get_bmi_model_start_time_forcing_offset_s(), realization::Catchment_Formulation::get_catchment_id(), get_config_mapped_variable_name(), data_access::DataProvider< DataType, SelectionType >::get_value(), realization::get_value_as_type(), data_access::DataProvider< DataType, SelectionType >::get_values(), realization::get_values_as_type(), bmi::Bmi::GetInputVarNames(), bmi::Bmi::GetVarItemsize(), bmi::Bmi::GetVarNbytes(), input_forcing_providers, and bmi::Bmi::SetValue().

Referenced by get_response().

◆ set_model_type_name()

virtual void realization::Bmi_Formulation::set_model_type_name ( std::string type_name)
inlineprotectedvirtualinherited

Set the name of the specific type of the backing model object.

Parameters
type_nameThe name of the backing model object's type.

References realization::Bmi_Formulation::model_type_name.

Referenced by realization::Bmi_Multi_Formulation::create_multi_formulation(), and inner_create_formulation().

◆ set_output_header_fields()

void realization::Bmi_Formulation::set_output_header_fields ( const std::vector< std::string > & output_headers)
inlineprotectedinherited

◆ set_output_precision()

void realization::Bmi_Formulation::set_output_precision ( int precision)
inlineinherited

Set the precision of output values when converted to text.

Parameters
precisionThe desired precision, as a number of decimal places.

References realization::Bmi_Formulation::output_precision, and realization::Bmi_Formulation::output_text_stream.

Referenced by realization::Bmi_Formulation::Bmi_Formulation(), realization::Bmi_Multi_Formulation::create_multi_formulation(), and inner_create_formulation().

◆ set_output_stream()

void HY_CatchmentArea::set_output_stream ( std::string file_path)
inlineinherited

◆ set_output_variable_names()

void realization::Bmi_Formulation::set_output_variable_names ( const std::vector< std::string > & out_var_names)
inlineprotectedinherited

Set the names of variables in formulation output.

Set the names of the variables to include in the output from this formulation, which should be some ordered subset of the output variables from the model.

Parameters
out_var_namesthe names of variables in formulation output, in the order they should appear.

References realization::Bmi_Formulation::output_variable_names.

Referenced by realization::Bmi_Multi_Formulation::create_multi_formulation(), and inner_create_formulation().

◆ validate_parameters()

◆ write_output()

void HY_CatchmentArea::write_output ( std::string out)
inlineinherited

Friends And Related Symbol Documentation

◆ ::Bmi_C_Formulation_Test

friend class ::Bmi_C_Formulation_Test
friend

◆ ::Bmi_Cpp_Formulation_Test

friend class ::Bmi_Cpp_Formulation_Test
friend

◆ ::Bmi_Cpp_Multi_Array_Test

friend class ::Bmi_Cpp_Multi_Array_Test
friend

◆ ::Bmi_Formulation_Test

friend class ::Bmi_Formulation_Test
friend

◆ ::Bmi_Multi_Formulation_Test

friend class ::Bmi_Multi_Formulation_Test
friend

◆ Bmi_Multi_Formulation

friend class Bmi_Multi_Formulation
friend

Member Data Documentation

◆ allow_model_exceed_end_time

bool realization::Bmi_Module_Formulation::allow_model_exceed_end_time = false
private

Whether model Update calls are allowed and handled in some way by the backing model for time steps after the model's end_time.

Referenced by get_allow_model_exceed_end_time(), and set_allow_model_exceed_end_time().

◆ available_forcings

std::vector<std::string> realization::Bmi_Module_Formulation::available_forcings
private

The set of available "forcings" (output variables, plus their mapped aliases) that the model can provide.

Referenced by get_available_variable_names(), and inner_create_formulation().

◆ bmi_init_config

std::string realization::Bmi_Module_Formulation::bmi_init_config
private

◆ bmi_main_output_var

std::string realization::Bmi_Formulation::bmi_main_output_var
privateinherited

◆ bmi_model

std::shared_ptr<models::bmi::Bmi_Adapter> realization::Bmi_Module_Formulation::bmi_model
private

Referenced by get_bmi_model(), and set_bmi_model().

◆ bmi_model_start_time_forcing_offset_s

time_t realization::Bmi_Module_Formulation::bmi_model_start_time_forcing_offset_s
private

The offset, converted to seconds, from the model's start time to the start time of the initial forcing time step.

Referenced by get_bmi_model_start_time_forcing_offset_s(), and set_bmi_model_start_time_forcing_offset_s().

◆ bmi_model_time_step_fixed

bool realization::Bmi_Module_Formulation::bmi_model_time_step_fixed = true
private

Whether backing model has fixed time step size.

Referenced by is_bmi_model_time_step_fixed(), and set_bmi_model_time_step_fixed().

◆ bmi_var_names_map

std::map<std::string, std::string> realization::Bmi_Module_Formulation::bmi_var_names_map
private

A configured mapping of BMI model variable names to standard names for use inside the framework.

Referenced by get_bmi_output_var_name(), get_config_mapped_variable_name(), and inner_create_formulation().

◆ bounds

polygon_t HY_CatchmentArea::bounds
protectedinherited

◆ cat_id

std::string realization::Catchment_Formulation::cat_id
privateinherited

◆ forcing

◆ id

std::string realization::Formulation::id
protectedinherited

◆ id_number

unsigned long HY_CatchmentRealization::id_number
protectedinherited

◆ input_forcing_providers

std::map<std::string, std::shared_ptr<data_access::GenericDataProvider> > realization::Bmi_Module_Formulation::input_forcing_providers
protected

◆ last_model_response_delta

time_step_t realization::Bmi_Module_Formulation::last_model_response_delta = 0
protected

The delta of the last model update execution (typically, this is time step size).

◆ last_model_response_start_time

time_t realization::Bmi_Module_Formulation::last_model_response_start_time = 0
protected

The epoch time of the model at the beginning of its last update.

◆ model_initialized

bool realization::Bmi_Module_Formulation::model_initialized = false
private

◆ model_type_name

std::string realization::Bmi_Formulation::model_type_name
privateinherited

◆ next_time_step_index

int realization::Bmi_Module_Formulation::next_time_step_index = 0
protected

Index value (0-based) of the time step that will be processed by the next update of the model.

A formulation time step for BMI types can be thought of as the execution of a call to any of the functions of the underlying BMI model that advance the model (either update or update_until). This member stores the ordinal index of the next time step to be executed. Except in the initial formulation state, this will be one greater than the index of the last executed time step.

E.g., on initialization, before any calls to get_response, this value will be 0. After a call to get_response (assuming 0 as the passed t_index argument), time step 0 will be processed, and this member would be incremented by 1, thus making it 1.

The member serves as an implicit marker of how many time steps have been processed so far. Knowing this is required to maintain valid behavior in certain things, such as get_response (we may want to process multiple time steps forward to a particular index other than the next, but it would not be valid to receive a t_index earlier than the last processed time step) and get_output_line_for_timestep (because formulations do not save results from previous time steps, only the results from the last processed time step can be used to generate output).

Referenced by get_output_line_for_timestep(), and get_response().

◆ OPTIONAL_PARAMETERS

std::vector<std::string> realization::Bmi_Module_Formulation::OPTIONAL_PARAMETERS
private
Initial value:
= {
BMI_REALIZATION_CFG_PARAM_OPT__USES_FORCINGS
BMI_REALIZATION_CFG_PARAM_OPT__FORCING_FILE,
BMI_REALIZATION_CFG_PARAM_OPT__VAR_STD_NAMES,
BMI_REALIZATION_CFG_PARAM_OPT__OUT_VARS,
BMI_REALIZATION_CFG_PARAM_OPT__OUT_HEADER_FIELDS,
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
}

◆ output

◆ output_header_fields

std::vector<std::string> realization::Bmi_Formulation::output_header_fields
privateinherited

Output header field strings corresponding to the variables output by the realization, as defined in output_variable_names.

Referenced by realization::Bmi_Formulation::get_output_header_fields(), and realization::Bmi_Formulation::set_output_header_fields().

◆ output_precision

int realization::Bmi_Formulation::output_precision
privateinherited

The degree of precision in output values when converting to text.

Referenced by realization::Bmi_Formulation::get_output_precision(), and realization::Bmi_Formulation::set_output_precision().

◆ output_text_stream

std::shared_ptr<std::ostringstream> realization::Bmi_Formulation::output_text_stream
protectedinherited

Object to help with converting numeric output values to text.

Referenced by realization::Bmi_Multi_Formulation::get_output_line_for_timestep(), and realization::Bmi_Formulation::set_output_precision().

◆ output_variable_names

std::vector<std::string> realization::Bmi_Formulation::output_variable_names
privateinherited

Names of the variables to include in the output from this formulation, which will be some ordered subset of the BMI module output variables accessible to the instance.

Referenced by realization::Bmi_Formulation::get_output_variable_names(), and realization::Bmi_Formulation::set_output_variable_names().

◆ realized_catchment

std::shared_ptr<HY_Catchment> HY_CatchmentRealization::realized_catchment
protectedinherited

◆ REQUIRED_PARAMETERS

std::vector<std::string> realization::Bmi_Module_Formulation::REQUIRED_PARAMETERS
private
Initial value:
= {
BMI_REALIZATION_CFG_PARAM_REQ__INIT_CONFIG,
BMI_REALIZATION_CFG_PARAM_REQ__MAIN_OUT_VAR,
BMI_REALIZATION_CFG_PARAM_REQ__MODEL_TYPE,
}

Referenced by get_required_parameters().


The documentation for this class was generated from the following files: