NGen
Loading...
Searching...
No Matches
models::bmi::Bmi_Adapter Class Referenceabstract

Abstract adapter interface for C++ classes to interact with the essential aspects of external models that implement the BMI spec but that are written in some other programming language. More...

#include <Bmi_Adapter.hpp>

+ Inheritance diagram for models::bmi::Bmi_Adapter:
+ Collaboration diagram for models::bmi::Bmi_Adapter:

Public Member Functions

 Bmi_Adapter (std::string model_name, std::string bmi_init_config, bool has_fixed_time_step)
 
 Bmi_Adapter (Bmi_Adapter const &)=delete
 
 Bmi_Adapter (Bmi_Adapter &&)=delete
 
virtual ~Bmi_Adapter ()=0
 
virtual bool is_model_initialized ()=0
 Whether the backing model has been initialized yet.
 
double get_time_convert_factor ()
 Determine backing model's time units and return an appropriate conversion factor.
 
double convert_model_time_to_seconds (const double &model_time_val)
 Convert model time value to value in seconds.
 
double convert_seconds_to_model_time (const double &seconds_val)
 Convert a given number of seconds to equivalent in model time units.
 
virtual const std::string get_analogous_cxx_type (const std::string &external_type_name, const size_t item_size)=0
 Get the name string for the C++ type analogous to the described type in the backing model.
 
void Initialize ()
 Initialize the wrapped BMI model functionality using the value from the bmi_init_config member variable and the API's Initialize function.
 
void Initialize (std::string config_file) override
 Initialize the wrapped BMI model object using the given config file and the object's Initialize function.
 
bool isInitialized ()
 Get whether this instance has been initialized properly.
 
std::string get_model_name ()
 Get the model name.
 
virtual void Update ()=0
 
virtual void UpdateUntil (double time)=0
 
virtual void Finalize ()=0
 
virtual std::string GetComponentName ()=0
 
virtual int GetInputItemCount ()=0
 
virtual int GetOutputItemCount ()=0
 
virtual std::vector< std::string > GetInputVarNames ()=0
 
virtual std::vector< std::string > GetOutputVarNames ()=0
 
virtual int GetVarGrid (std::string name)=0
 
virtual std::string GetVarType (std::string name)=0
 
virtual std::string GetVarUnits (std::string name)=0
 
virtual int GetVarItemsize (std::string name)=0
 
virtual int GetVarNbytes (std::string name)=0
 
virtual std::string GetVarLocation (std::string name)=0
 
virtual double GetCurrentTime ()=0
 
virtual double GetStartTime ()=0
 
virtual double GetEndTime ()=0
 
virtual std::string GetTimeUnits ()=0
 
virtual double GetTimeStep ()=0
 
virtual void GetValue (std::string name, void *dest)=0
 
virtual void * GetValuePtr (std::string name)=0
 
virtual void GetValueAtIndices (std::string name, void *dest, int *inds, int count)=0
 
virtual void SetValue (std::string name, void *src)=0
 
virtual void SetValueAtIndices (std::string name, int *inds, int count, void *src)=0
 
virtual int GetGridRank (const int grid)=0
 
virtual int GetGridSize (const int grid)=0
 
virtual std::string GetGridType (const int grid)=0
 
virtual void GetGridShape (const int grid, int *shape)=0
 
virtual void GetGridSpacing (const int grid, double *spacing)=0
 
virtual void GetGridOrigin (const int grid, double *origin)=0
 
virtual void GetGridX (const int grid, double *x)=0
 
virtual void GetGridY (const int grid, double *y)=0
 
virtual void GetGridZ (const int grid, double *z)=0
 
virtual int GetGridNodeCount (const int grid)=0
 
virtual int GetGridEdgeCount (const int grid)=0
 
virtual int GetGridFaceCount (const int grid)=0
 
virtual void GetGridEdgeNodes (const int grid, int *edge_nodes)=0
 
virtual void GetGridFaceEdges (const int grid, int *face_edges)=0
 
virtual void GetGridFaceNodes (const int grid, int *face_nodes)=0
 
virtual void GetGridNodesPerFace (const int grid, int *nodes_per_face)=0
 

Protected Member Functions

virtual void construct_and_init_backing_model ()=0
 Construct the backing BMI model object, then call its BMI-native Initialize() function.
 

Protected Attributes

std::string bmi_init_config
 Path (as a string) to the BMI config file for initializing the backing model (empty if none).
 
bool bmi_model_has_fixed_time_step = true
 Whether this particular model has a time step size that cannot be changed internally or externally.
 
double bmi_model_time_convert_factor
 Conversion factor for converting values for model time in model's unit type to equivalent in seconds.
 
std::shared_ptr< double > bmi_model_time_step_size = nullptr
 Pointer to stored time step size value of backing model, if it is fixed and has been retrieved.
 
std::string init_exception_msg
 Message from an exception (if encountered) on the first attempt to initialize the backing model.
 
std::shared_ptr< std::vector< std::string > > input_var_names
 Pointer to collection of input variable names for backing model, used by GetInputVarNames().
 
bool model_initialized = false
 Whether the backing model has been initialized yet, which is always initially false.
 
std::string model_name
 
std::shared_ptr< std::vector< std::string > > output_var_names
 Pointer to collection of output variable names for backing model, used by GetOutputVarNames().
 

Detailed Description

Abstract adapter interface for C++ classes to interact with the essential aspects of external models that implement the BMI spec but that are written in some other programming language.

Constructor & Destructor Documentation

◆ Bmi_Adapter() [1/3]

models::bmi::Bmi_Adapter::Bmi_Adapter ( std::string model_name,
std::string bmi_init_config,
bool has_fixed_time_step )

◆ Bmi_Adapter() [2/3]

models::bmi::Bmi_Adapter::Bmi_Adapter ( Bmi_Adapter const & )
delete

◆ Bmi_Adapter() [3/3]

models::bmi::Bmi_Adapter::Bmi_Adapter ( Bmi_Adapter && )
delete

◆ ~Bmi_Adapter()

models::bmi::Bmi_Adapter::~Bmi_Adapter ( )
pure virtualdefault

Member Function Documentation

◆ construct_and_init_backing_model()

virtual void models::bmi::Bmi_Adapter::construct_and_init_backing_model ( )
protectedpure virtual

Construct the backing BMI model object, then call its BMI-native Initialize() function.

Implementations should return immediately without taking any further action if model_initialized is already true.

The call to the BMI native Initialize(string) should pass the value stored in bmi_init_config.

Implemented in models::bmi::Bmi_C_Adapter, and models::bmi::Bmi_Cpp_Adapter.

Referenced by Initialize().

◆ convert_model_time_to_seconds()

double models::bmi::Bmi_Adapter::convert_model_time_to_seconds ( const double & model_time_val)

Convert model time value to value in seconds.

Performs necessary lookup and conversion of some given model time value - as from GetCurrentTime() or GetStartTime() - and returns the equivalent value when represented in seconds.

Parameters
model_time_valArbitrary model time value in units provided by GetTimeUnits()
Returns
Equivalent model time value to value in seconds.

References bmi_model_time_convert_factor.

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

◆ convert_seconds_to_model_time()

double models::bmi::Bmi_Adapter::convert_seconds_to_model_time ( const double & seconds_val)

Convert a given number of seconds to equivalent in model time units.

Parameters
seconds_val
Returns

References bmi_model_time_convert_factor.

Referenced by realization::Bmi_Module_Formulation::get_response().

◆ Finalize()

virtual void bmi::Bmi::Finalize ( )
pure virtualinherited

◆ get_analogous_cxx_type()

virtual const std::string models::bmi::Bmi_Adapter::get_analogous_cxx_type ( const std::string & external_type_name,
const size_t item_size )
pure virtual

Get the name string for the C++ type analogous to the described type in the backing model.

The supported languages for BMI modules support different types than C++ in general, but it is necessary to map "external" types to the appropriate C++ type for certain interactions; e.g., setting a variable value. This function encapsulates the translation specific to the particular language.

Note that the size of an individual item is also required, as this can vary in certain situations depending on the implementation language of a backing model.

Implementations should all throw the same type of exception (currently, std::runtime_error) if/when an unrecognized external type name parameter is provided.

Parameters
external_type_nameThe string name of some type in backing model's implementation language.
item_sizeThe particular size in bytes for items of the involved analogous types.
Returns
The name string for the C++ type analogous to the described type in the backing model.
Exceptions
std::runtime_errorIf an unrecognized external type name parameter is provided.

Implemented in models::bmi::Bmi_C_Adapter, and models::bmi::Bmi_Cpp_Adapter.

Referenced by models::bmi::GetValue(), realization::Bmi_Module_Formulation::set_initial_bmi_parameters(), and realization::Bmi_Module_Formulation::set_model_inputs_prior_to_update().

◆ get_model_name()

std::string models::bmi::Bmi_Adapter::get_model_name ( )

Get the model name.

Returns
The name of the model connected to the adapter.

References model_name.

Referenced by models::bmi::GetValue().

◆ get_time_convert_factor()

double models::bmi::Bmi_Adapter::get_time_convert_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 return an appropriate factor for converting its internal time values to equivalent representations within the model, and vice versa. This function complies with the BMI get_time_units standard

Exceptions
runtime_errorIf the delegated BMI functions to query time throw an exception, the exception is caught and wrapped in a runtime_error

References UnitsHelper::get_converted_value(), and bmi::Bmi::GetTimeUnits().

Referenced by models::bmi::Bmi_C_Adapter::Bmi_C_Adapter(), models::bmi::Bmi_Cpp_Adapter::Bmi_Cpp_Adapter(), and Initialize().

◆ GetComponentName()

virtual std::string bmi::Bmi::GetComponentName ( )
pure virtualinherited

◆ GetCurrentTime()

virtual double bmi::Bmi::GetCurrentTime ( )
pure virtualinherited

◆ GetEndTime()

virtual double bmi::Bmi::GetEndTime ( )
pure virtualinherited

◆ GetGridEdgeCount()

virtual int bmi::Bmi::GetGridEdgeCount ( const int grid)
pure virtualinherited

◆ GetGridEdgeNodes()

virtual void bmi::Bmi::GetGridEdgeNodes ( const int grid,
int * edge_nodes )
pure virtualinherited

◆ GetGridFaceCount()

virtual int bmi::Bmi::GetGridFaceCount ( const int grid)
pure virtualinherited

◆ GetGridFaceEdges()

virtual void bmi::Bmi::GetGridFaceEdges ( const int grid,
int * face_edges )
pure virtualinherited

◆ GetGridFaceNodes()

virtual void bmi::Bmi::GetGridFaceNodes ( const int grid,
int * face_nodes )
pure virtualinherited

◆ GetGridNodeCount()

virtual int bmi::Bmi::GetGridNodeCount ( const int grid)
pure virtualinherited

◆ GetGridNodesPerFace()

virtual void bmi::Bmi::GetGridNodesPerFace ( const int grid,
int * nodes_per_face )
pure virtualinherited

◆ GetGridOrigin()

virtual void bmi::Bmi::GetGridOrigin ( const int grid,
double * origin )
pure virtualinherited

◆ GetGridRank()

virtual int bmi::Bmi::GetGridRank ( const int grid)
pure virtualinherited

◆ GetGridShape()

virtual void bmi::Bmi::GetGridShape ( const int grid,
int * shape )
pure virtualinherited

◆ GetGridSize()

virtual int bmi::Bmi::GetGridSize ( const int grid)
pure virtualinherited

◆ GetGridSpacing()

virtual void bmi::Bmi::GetGridSpacing ( const int grid,
double * spacing )
pure virtualinherited

◆ GetGridType()

virtual std::string bmi::Bmi::GetGridType ( const int grid)
pure virtualinherited

◆ GetGridX()

virtual void bmi::Bmi::GetGridX ( const int grid,
double * x )
pure virtualinherited

◆ GetGridY()

virtual void bmi::Bmi::GetGridY ( const int grid,
double * y )
pure virtualinherited

◆ GetGridZ()

virtual void bmi::Bmi::GetGridZ ( const int grid,
double * z )
pure virtualinherited

◆ GetInputItemCount()

virtual int bmi::Bmi::GetInputItemCount ( )
pure virtualinherited

◆ GetInputVarNames()

◆ GetOutputItemCount()

virtual int bmi::Bmi::GetOutputItemCount ( )
pure virtualinherited

◆ GetOutputVarNames()

◆ GetStartTime()

virtual double bmi::Bmi::GetStartTime ( )
pure virtualinherited

◆ GetTimeStep()

virtual double bmi::Bmi::GetTimeStep ( )
pure virtualinherited

◆ GetTimeUnits()

virtual std::string bmi::Bmi::GetTimeUnits ( )
pure virtualinherited

◆ GetValue()

virtual void bmi::Bmi::GetValue ( std::string name,
void * dest )
pure virtualinherited

◆ GetValueAtIndices()

virtual void bmi::Bmi::GetValueAtIndices ( std::string name,
void * dest,
int * inds,
int count )
pure virtualinherited

◆ GetValuePtr()

virtual void * bmi::Bmi::GetValuePtr ( std::string name)
pure virtualinherited

◆ GetVarGrid()

virtual int bmi::Bmi::GetVarGrid ( std::string name)
pure virtualinherited

◆ GetVarItemsize()

◆ GetVarLocation()

virtual std::string bmi::Bmi::GetVarLocation ( std::string name)
pure virtualinherited

◆ GetVarNbytes()

◆ GetVarType()

virtual std::string bmi::Bmi::GetVarType ( std::string name)
pure virtualinherited

◆ GetVarUnits()

virtual std::string bmi::Bmi::GetVarUnits ( std::string name)
pure virtualinherited

◆ Initialize() [1/2]

void models::bmi::Bmi_Adapter::Initialize ( )

Initialize the wrapped BMI model functionality using the value from the bmi_init_config member variable and the API's Initialize function.

If no attempt to initialize the model has yet been made (i.e., model_initialized is false when this function is called), then an initialization is attempted for the model. This is handled by a nested call to the construct_and_init_backing_model function. If initialization fails, an exception will be raised, with it's message saved as part of this object's state. However, regardless of the success of initialization, model_initialized is set to true after the attempt.

Additionally, if a successful model initialization is performed, bmi_model_time_convert_factor is immediately thereafter set by passing it by reference in a call to acquire_time_conversion_factor.

If an attempt to initialize the model has already been made (i.e., model_initialized is true), this function will either simply return or will throw a runtime_error, with its message including the message of the exception from the earlier attempt.

Exceptions
runtime_errorIf called again after earlier call that resulted in an exception, or if BMI config file could not be read.
models::external::State_ExceptionIf initialize() in nested model does not return successful.

References bmi_init_config, bmi_model_time_convert_factor, construct_and_init_backing_model(), utils::FileChecker::file_is_readable(), get_time_convert_factor(), init_exception_msg, model_initialized, and model_name.

Referenced by Initialize().

◆ Initialize() [2/2]

void models::bmi::Bmi_Adapter::Initialize ( std::string config_file)
overridevirtual

Initialize the wrapped BMI model object using the given config file and the object's Initialize function.

If the given file is not the same as what is in bmi_init_configand the model object has not already been initialized, this function will produce a warning message about the difference, then subsequently updatebmi_init_config`` to the given file. It will then proceed with initialization.

However, if the given file is not the same as what is in bmi_init_config`, but the model has already been initialized, a runtime_error exception is thrown.

This otherwise operates using the logic of Initialize().

Parameters
config_file
See also
Initialize()
Exceptions
models::external::State_ExceptionIf initialize() in nested model is not successful.
runtime_errorIf already initialized but using a different file than the passed argument.

Implements bmi::Bmi.

References bmi_init_config, Initialize(), model_initialized, logging::warning(), and models::external::State_Exception::what().

◆ is_model_initialized()

virtual bool models::bmi::Bmi_Adapter::is_model_initialized ( )
pure virtual

Whether the backing model has been initialized yet.

Returns
Whether the backing model has been initialized yet.

Implemented in models::bmi::Bmi_C_Adapter, and models::bmi::Bmi_Cpp_Adapter.

Referenced by realization::Bmi_Module_Formulation::inner_create_formulation(), and realization::Bmi_Cpp_Formulation::is_model_initialized().

◆ isInitialized()

bool models::bmi::Bmi_Adapter::isInitialized ( )

Get whether this instance has been initialized properly.

Returns
Whether this instance has been initialized properly.

References model_initialized.

◆ SetValue()

virtual void bmi::Bmi::SetValue ( std::string name,
void * src )
pure virtualinherited

◆ SetValueAtIndices()

virtual void bmi::Bmi::SetValueAtIndices ( std::string name,
int * inds,
int count,
void * src )
pure virtualinherited

◆ Update()

virtual void bmi::Bmi::Update ( )
pure virtualinherited

◆ UpdateUntil()

virtual void bmi::Bmi::UpdateUntil ( double time)
pure virtualinherited

Member Data Documentation

◆ bmi_init_config

std::string models::bmi::Bmi_Adapter::bmi_init_config
protected

Path (as a string) to the BMI config file for initializing the backing model (empty if none).

Referenced by models::bmi::Bmi_C_Adapter::construct_and_init_backing_model_for_type(), models::bmi::Bmi_Cpp_Adapter::construct_and_init_backing_model_for_type(), Initialize(), and Initialize().

◆ bmi_model_has_fixed_time_step

bool models::bmi::Bmi_Adapter::bmi_model_has_fixed_time_step = true
protected

Whether this particular model has a time step size that cannot be changed internally or externally.

Referenced by models::bmi::Bmi_C_Adapter::get_bmi_model_time_step_size_ptr(), and models::bmi::Bmi_Cpp_Adapter::get_bmi_model_time_step_size_ptr().

◆ bmi_model_time_convert_factor

double models::bmi::Bmi_Adapter::bmi_model_time_convert_factor
protected

Conversion factor for converting values for model time in model's unit type to equivalent in seconds.

Referenced by models::bmi::Bmi_C_Adapter::Bmi_C_Adapter(), models::bmi::Bmi_Cpp_Adapter::Bmi_Cpp_Adapter(), convert_model_time_to_seconds(), convert_seconds_to_model_time(), and Initialize().

◆ bmi_model_time_step_size

std::shared_ptr<double> models::bmi::Bmi_Adapter::bmi_model_time_step_size = nullptr
protected

Pointer to stored time step size value of backing model, if it is fixed and has been retrieved.

Referenced by models::bmi::Bmi_C_Adapter::get_bmi_model_time_step_size_ptr(), and models::bmi::Bmi_Cpp_Adapter::get_bmi_model_time_step_size_ptr().

◆ init_exception_msg

◆ input_var_names

std::shared_ptr<std::vector<std::string> > models::bmi::Bmi_Adapter::input_var_names
protected

Pointer to collection of input variable names for backing model, used by GetInputVarNames().

Referenced by models::bmi::Bmi_C_Adapter::GetInputVarNames().

◆ model_initialized

◆ model_name

◆ output_var_names

std::shared_ptr<std::vector<std::string> > models::bmi::Bmi_Adapter::output_var_names
protected

Pointer to collection of output variable names for backing model, used by GetOutputVarNames().

Referenced by models::bmi::Bmi_C_Adapter::GetOutputVarNames().


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