RESPOND 2.5.1
Researching Effective Strategies to Prevent Opioid Death
Loading...
Searching...
No Matches
respond::Simulation Class Reference

Manages and executes multiple models in a coordinated simulation. A Simulation aggregates Model instances and coordinates their execution, maintaining history records and providing access to simulation results.

#include <simulation.hpp>

Classes

class  ModelSlotProxy
 Proxy for mutable model slot access with clone-based assignment. More...
 

Public Member Functions

 Simulation ()
 Default constructor for a Simulation instance. Initializes the simulation with the default logger.
 
 Simulation (const std::string &log_name)
 Constructs a Simulation with a specified logger.
 
 Simulation (const std::string &log_name, const std::string &log_filepath)
 Constructs a Simulation with a specified logger and log file.
 
 Simulation (const std::string &log_name, const std::string &log_filepath, const ExecutionConfig &execution_config)
 Constructs a Simulation with logger and execution settings.
 
 Simulation (const RuntimeConfig &runtime_config)
 Constructs a Simulation with shared runtime settings.
 
 ~Simulation ()=default
 Virtual destructor for polymorphic cleanup.
 
 Simulation (const Simulation &other)
 Copy constructor creating an independent deep copy of the simulation. All models are cloned; modifications to the copy do not affect the original.
 
Simulation & operator= (const Simulation &other)
 Copy assignment operator for deep copying simulation state.
 
 Simulation (Simulation &&other) noexcept
 Move constructor for transferring simulation ownership.
 
Simulation & operator= (Simulation &&other) noexcept
 Move assignment operator for transferring simulation ownership.
 
std::unique_ptr< Model > CreateNewModel (const std::string &model_name)
 Creates a new model instance and adds it to the simulation.
 
void ClearModels ()
 Removes all models from the simulation.
 
void AddModel (const std::unique_ptr< Model > &model)
 Adds a model to the simulation. The model is cloned and managed by the simulation.
 
void Run (int duration=-1)
 Executes the simulation for all registered models. Models run sequentially by default. When model concurrency is enabled, independent models may run in parallel using the configured worker limit.
 
std::vector< std::unique_ptr< Model > > GetModels () const
 Retrieves all models in the simulation.
 
ModelSlotProxy operator[] (size_t idx)
 Mutable index-based model access.
 
const Model & operator[] (size_t idx) const
 Const index-based model access.
 
std::map< size_t, std::string > GetModelIndexNameMap () const
 Provide a mapping of model indices to their names for all models in the simulation.
 
std::unique_ptr< Model > GetModel (int idx) const
 Retrieves a specific model by index in the simulation.
 
std::vector< std::string > GetModelNames () const
 Retrieves the names of all models in the simulation.
 
const std::map< std::string, History > & GetModelHistory (size_t idx) const
 Retrieves the complete state histories for the model at the index.
 
const std::vector< std::string > GetModelHistoryNames (size_t idx) const
 Retrieves history names for the model at the specified index.
 
void SetDuration (int duration)
 Sets the default duration used by Run().
 
const ExecutionConfig & GetExecutionConfig () const
 Retrieves the simulation execution settings.
 
void SetExecutionConfig (const ExecutionConfig &execution_config)
 Sets the simulation execution settings.
 
const RuntimeConfig & GetRuntimeConfig () const
 
void SetRuntimeConfig (const RuntimeConfig &runtime_config)
 

Constructor & Destructor Documentation

◆ Simulation() [1/5]

respond::Simulation::Simulation ( const std::string &  log_name)
inline
Parameters
log_nameThe name of the logger to use for simulation output.

◆ Simulation() [2/5]

respond::Simulation::Simulation ( const std::string &  log_name,
const std::string &  log_filepath 
)
inline
Parameters
log_nameThe name of the logger to use for simulation output.
log_filepathThe file path for the logger output.

◆ Simulation() [3/5]

respond::Simulation::Simulation ( const std::string &  log_name,
const std::string &  log_filepath,
const ExecutionConfig &  execution_config 
)
inline
Parameters
log_nameThe name of the logger to use for simulation output.
log_filepathThe file path for the logger output.
execution_configResource settings for simulation execution.

◆ Simulation() [4/5]

respond::Simulation::Simulation ( const Simulation &  other)
inline
Parameters
otherThe Simulation instance to copy from.

◆ Simulation() [5/5]

respond::Simulation::Simulation ( Simulation &&  other)
inlinenoexcept
Parameters
otherThe simulation to move from.

Member Function Documentation

◆ AddModel()

void respond::Simulation::AddModel ( const std::unique_ptr< Model > &  model)
inline
Parameters
modelA unique_ptr to a Model instance to add.

◆ CreateNewModel()

std::unique_ptr< Model > respond::Simulation::CreateNewModel ( const std::string &  model_name)
inline
Parameters
model_nameThe name identifier for the model to create. This name is used to identify the model type and initialize it accordingly.
Returns
An editable deep copy of the newly created model. Changes to this copy do not affect the model managed by the simulation until it is assigned through the mutable model slot, for example simulation[0] = *model.

◆ GetExecutionConfig()

const ExecutionConfig & respond::Simulation::GetExecutionConfig ( ) const
inline
Returns
The current execution configuration.

◆ GetModel()

std::unique_ptr< Model > respond::Simulation::GetModel ( int  idx) const
inline
Parameters
idxThe index of the model to retrieve.
Returns
A deep-copied Model at the specified index.

If idx is -1, the last model is returned.

Exceptions
std::out_of_rangeif no models exist or idx is out of range.

◆ GetModelHistory()

const std::map< std::string, History > & respond::Simulation::GetModelHistory ( size_t  idx) const
inline
Parameters
idxThe index of the model to retrieve histories for.
Returns
Map of history names to history records for the selected model.

◆ GetModelHistoryNames()

const std::vector< std::string > respond::Simulation::GetModelHistoryNames ( size_t  idx) const
inline
Parameters
idxThe index of the model to retrieve history names for.
Returns
Vector of history names for the selected model.

◆ GetModelIndexNameMap()

std::map< size_t, std::string > respond::Simulation::GetModelIndexNameMap ( ) const
inline

This method returns a map where the keys are the indices of the models in the simulation, and the values are the corresponding model names. This allows for easy identification of models by their index in the simulation.

Returns
The map of model indices to model names.

◆ GetModelNames()

std::vector< std::string > respond::Simulation::GetModelNames ( ) const
inline
Returns
Vector of model names in the order they were added.

◆ GetModels()

std::vector< std::unique_ptr< Model > > respond::Simulation::GetModels ( ) const
inline
Returns
A deep-copied vector of Model unique_ptrs.

◆ operator=() [1/2]

Simulation & respond::Simulation::operator= ( const Simulation &  other)
inline
Parameters
otherThe simulation to copy from.
Returns
Reference to this simulation after assignment.

◆ operator=() [2/2]

Simulation & respond::Simulation::operator= ( Simulation &&  other)
inlinenoexcept
Parameters
otherThe simulation to move from.
Returns
Reference to this simulation after assignment.

◆ operator[]() [1/2]

ModelSlotProxy respond::Simulation::operator[] ( size_t  idx)
inline

Returns a proxy that supports both model member access and clone-based replacement assignment.

Parameters
idxThe index of the model to access.
Returns
A mutable proxy for the model slot.
Exceptions
std::out_of_rangeif idx is out of range.

◆ operator[]() [2/2]

const Model & respond::Simulation::operator[] ( size_t  idx) const
inline
Parameters
idxThe index of the model to access.
Returns
Const reference to the model at the index.
Exceptions
std::out_of_rangeif idx is out of range.

◆ Run()

void respond::Simulation::Run ( int  duration = -1)
inline
Exceptions
std::invalid_argumentif multiple models would execute in parallel while more than one Eigen worker thread is configured.
std::invalid_argumentif no models have been added.
std::invalid_argumentif duration is zero or less than -1.
Anyexception raised by a model after all workers have joined.

◆ SetDuration()

void respond::Simulation::SetDuration ( int  duration)
inline
Parameters
durationA positive number of timesteps.
Exceptions
std::invalid_argumentif duration is not positive.

◆ SetExecutionConfig()

void respond::Simulation::SetExecutionConfig ( const ExecutionConfig &  execution_config)
inline
Parameters
execution_configResource settings for simulation execution.

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