ParameterStudy< ParameterType, TimeType, StepType > Class Template Reference

CPP API: mio::ParameterStudy< ParameterType, TimeType, StepType > Class Template Reference
mio::ParameterStudy< ParameterType, TimeType, StepType > Class Template Reference

Class used to perform multiple simulation runs with randomly sampled parameters. More...

#include <parameter_studies.h>

Public Types

using Parameters = ParameterType
 
using Step = StepType
 
using Time = TimeType
 

Public Member Functions

Time get_dt () const
 Return the initial step sized used by simulations. More...
 
size_t get_num_runs () const
 Return the number of total runs that the study will make. More...
 
RandomNumberGenerator & get_rng ()
 Access the study's random number generator. More...
 
Time get_t0 () const
 Return the initial time point for simulations. More...
 
Time get_tmax () const
 Return the final time point for simulations. More...
 
 ParameterStudy (const Parameters &parameters, Time t0, Time tmax, Step dt, size_t num_runs)
 Create a parameter study with some parameters. More...
 
template<class CreateSimulationFunction , class ProcessSimulationResultFunction >
EnsembleResultT< CreateSimulationFunction, ProcessSimulationResultFunction > run (CreateSimulationFunction &&create_simulation, ProcessSimulationResultFunction &&process_simulation_result)
 Run all simulations distributed over multiple MPI ranks. More...
 
template<class CreateSimulationFunction , class ProcessSimulationResultFunction >
EnsembleResultT< CreateSimulationFunction, ProcessSimulationResultFunction > run_serial (CreateSimulationFunction &&create_simulation, ProcessSimulationResultFunction &&process_simulation_result)
 Run all simulations in serial. More...
 
template<class CreateSimulationFunction >
std::vector< SimulationT< CreateSimulationFunction > > run_serial (CreateSimulationFunction &&create_simulation)
 Run all simulations in serial. More...
 
const Parametersget_parameters () const
 Get the input parameters that each simulation in the study is created from. More...
 
Parametersget_parameters ()
 Get the input parameters that each simulation in the study is created from. More...
 

Private Types

template<class CreateSimulationFunction , class ProcessSimulationResultFunction >
using EnsembleResultT = std::conditional_t< std::is_void_v< ProcessedResultT< CreateSimulationFunction, ProcessSimulationResultFunction > >, void, std::vector< ProcessedResultT< CreateSimulationFunction, ProcessSimulationResultFunction > >>
 Type returned by run functions. Is void if ProcessedResultT is, otherwise a vector of ProcessedResultT. More...
 
template<class CreateSimulationFunction , class ProcessSimulationResultFunction >
using ProcessedResultT = std::decay_t< std::invoke_result_t< ProcessSimulationResultFunction, SimulationT< CreateSimulationFunction >, size_t > >
 The return type of process_simulation_result. Ensures that the function is invocable. More...
 
template<class CreateSimulationFunction >
using SimulationT = std::decay_t< std::invoke_result_t< CreateSimulationFunction, Parameters, Time, Step, size_t > >
 The return type of create_simulation. Ensures that the function is invocable. More...
 

Private Member Functions

template<class T >
auto make_ensemble_result ()
 Return the ensemble result vector, or "void" in form of a char. More...
 
template<class CreateSimulationFunction , class ProcessSimulationResultFunction >
EnsembleResultT< CreateSimulationFunction, ProcessSimulationResultFunction > run_impl (size_t start_run_idx, size_t end_run_idx, CreateSimulationFunction &&create_simulation, ProcessSimulationResultFunction &&process_simulation_result)
 Main loop creating and running simulations. More...
 

Static Private Member Functions

static std::vector< size_t > distribute_runs (size_t num_runs, int num_procs)
 Distribute a number of runs over a number of processes. More...
 

Private Attributes

Step m_dt
 Initial step size of the simulation. Some integrators may adapt their step size during simulation. More...
 
size_t m_num_runs
 Total number of runs (i.e. simulations) to do when calling "run". More...
 
ParameterType m_parameters
 Stores parameters used to create a simulation for each run. More...
 
RandomNumberGenerator m_rng
 The random number generator used by the study. More...
 
Time m_t0
 
Time m_tmax
 Start and end time for the simulations. More...
 

Detailed Description

template<class ParameterType, typename TimeType, typename StepType = TimeType>
class mio::ParameterStudy< ParameterType, TimeType, StepType >

Class used to perform multiple simulation runs with randomly sampled parameters.

Note that the type of simulation is not determined until calling one of the run functions.

Template Parameters
ParameterTypeThe parameters used to create simulations.
TimeTypeThe time type used by the simulation, e.g. double or TimePoint.
StepTypeThe time step type used by the simulation, e.g. double or TimeStep. May be the same as TimeType.

Member Typedef Documentation

◆ EnsembleResultT

template<class ParameterType , typename TimeType , typename StepType = TimeType>
template<class CreateSimulationFunction , class ProcessSimulationResultFunction >
using mio::ParameterStudy< ParameterType, TimeType, StepType >::EnsembleResultT = std::conditional_t<std::is_void_v<ProcessedResultT<CreateSimulationFunction, ProcessSimulationResultFunction> >, void, std::vector<ProcessedResultT<CreateSimulationFunction, ProcessSimulationResultFunction> >>
private

Type returned by run functions. Is void if ProcessedResultT is, otherwise a vector of ProcessedResultT.

◆ Parameters

template<class ParameterType , typename TimeType , typename StepType = TimeType>
using mio::ParameterStudy< ParameterType, TimeType, StepType >::Parameters = ParameterType

◆ ProcessedResultT

template<class ParameterType , typename TimeType , typename StepType = TimeType>
template<class CreateSimulationFunction , class ProcessSimulationResultFunction >
using mio::ParameterStudy< ParameterType, TimeType, StepType >::ProcessedResultT = std::decay_t< std::invoke_result_t<ProcessSimulationResultFunction, SimulationT<CreateSimulationFunction>, size_t> >
private

The return type of process_simulation_result. Ensures that the function is invocable.

◆ SimulationT

template<class ParameterType , typename TimeType , typename StepType = TimeType>
template<class CreateSimulationFunction >
using mio::ParameterStudy< ParameterType, TimeType, StepType >::SimulationT = std::decay_t<std::invoke_result_t<CreateSimulationFunction, Parameters, Time, Step, size_t> >
private

The return type of create_simulation. Ensures that the function is invocable.

◆ Step

template<class ParameterType , typename TimeType , typename StepType = TimeType>
using mio::ParameterStudy< ParameterType, TimeType, StepType >::Step = StepType

◆ Time

template<class ParameterType , typename TimeType , typename StepType = TimeType>
using mio::ParameterStudy< ParameterType, TimeType, StepType >::Time = TimeType

Constructor & Destructor Documentation

◆ ParameterStudy()

template<class ParameterType , typename TimeType , typename StepType = TimeType>
mio::ParameterStudy< ParameterType, TimeType, StepType >::ParameterStudy ( const Parameters parameters,
Time  t0,
Time  tmax,
Step  dt,
size_t  num_runs 
)
inline

Create a parameter study with some parameters.

The simulation type is determined when calling any "run" member function.

Parameters
parametersThe parameters used to create simulations.
t0Start time of simulations.
tmaxEnd time of simulations.
dtInitial time step of simulations.
num_runsNumber of simulations that will be created and run.

Member Function Documentation

◆ distribute_runs()

template<class ParameterType , typename TimeType , typename StepType = TimeType>
static std::vector<size_t> mio::ParameterStudy< ParameterType, TimeType, StepType >::distribute_runs ( size_t  num_runs,
int  num_procs 
)
inlinestaticprivate

Distribute a number of runs over a number of processes.

Processes with low ranks get additional runs, if the number is not evenly divisible.

Parameters
num_runsThe total number of runs.
num_procsThe total number of processes, i.e. the size of MPI_Comm.
Returns
A vector of size num_procs with the number of runs each process should make.

◆ get_dt()

template<class ParameterType , typename TimeType , typename StepType = TimeType>
Time mio::ParameterStudy< ParameterType, TimeType, StepType >::get_dt ( ) const
inline

Return the initial step sized used by simulations.

◆ get_num_runs()

template<class ParameterType , typename TimeType , typename StepType = TimeType>
size_t mio::ParameterStudy< ParameterType, TimeType, StepType >::get_num_runs ( ) const
inline

Return the number of total runs that the study will make.

◆ get_parameters() [1/2]

template<class ParameterType , typename TimeType , typename StepType = TimeType>
Parameters& mio::ParameterStudy< ParameterType, TimeType, StepType >::get_parameters ( )
inline

Get the input parameters that each simulation in the study is created from.

◆ get_parameters() [2/2]

template<class ParameterType , typename TimeType , typename StepType = TimeType>
const Parameters& mio::ParameterStudy< ParameterType, TimeType, StepType >::get_parameters ( ) const
inline

Get the input parameters that each simulation in the study is created from.

◆ get_rng()

template<class ParameterType , typename TimeType , typename StepType = TimeType>
RandomNumberGenerator& mio::ParameterStudy< ParameterType, TimeType, StepType >::get_rng ( )
inline

Access the study's random number generator.

◆ get_t0()

template<class ParameterType , typename TimeType , typename StepType = TimeType>
Time mio::ParameterStudy< ParameterType, TimeType, StepType >::get_t0 ( ) const
inline

Return the initial time point for simulations.

◆ get_tmax()

template<class ParameterType , typename TimeType , typename StepType = TimeType>
Time mio::ParameterStudy< ParameterType, TimeType, StepType >::get_tmax ( ) const
inline

Return the final time point for simulations.

◆ make_ensemble_result()

template<class ParameterType , typename TimeType , typename StepType = TimeType>
template<class T >
auto mio::ParameterStudy< ParameterType, TimeType, StepType >::make_ensemble_result ( )
inlineprivate

Return the ensemble result vector, or "void" in form of a char.

◆ run()

template<class ParameterType , typename TimeType , typename StepType = TimeType>
template<class CreateSimulationFunction , class ProcessSimulationResultFunction >
EnsembleResultT<CreateSimulationFunction, ProcessSimulationResultFunction> mio::ParameterStudy< ParameterType, TimeType, StepType >::run ( CreateSimulationFunction &&  create_simulation,
ProcessSimulationResultFunction &&  process_simulation_result 
)
inline

Run all simulations distributed over multiple MPI ranks.

Parameters
[in]create_simulationA callable sampling the study's parameters and returning a simulation.
[in]process_simulation_resultA callable that takes the simulation and processes its result.
Returns
A vector that contains processed simulation results for each run, or void if processing returns nothing.

Important: Do not forget to use mio::mpi::init and finalize when using this function!

Important side effect: Calling this function overwrites seed and counter of thread_local_rng(). Use this RNG when sampling parameters in create_simulation.

The function signature for create_simulation is SimulationT(const Parameters& study_parameters, Time t0, Step dt, size_t run_idx), where SimulationT is some kind of simulation. The function signature for process_simulation_result is ProcessedResultT(SimulationT&&, size_t run_index), where ProcessedResultT is a (de)serializable result, or void. A void function can be useful if the results should be fully handled during the study, for example, when memory is limited and the results have to be written to a disk.

If MPI is enabled and the results are non-void, all results are gathered on the root rank 0. Other ranks will return an empty vector.

◆ run_impl()

template<class ParameterType , typename TimeType , typename StepType = TimeType>
template<class CreateSimulationFunction , class ProcessSimulationResultFunction >
EnsembleResultT<CreateSimulationFunction, ProcessSimulationResultFunction> mio::ParameterStudy< ParameterType, TimeType, StepType >::run_impl ( size_t  start_run_idx,
size_t  end_run_idx,
CreateSimulationFunction &&  create_simulation,
ProcessSimulationResultFunction &&  process_simulation_result 
)
inlineprivate

Main loop creating and running simulations.

Parameters
[in]start_run_idx,end_run_idxRange of indices. Performs one run for each index.
[in]create_simulationA callable sampling the study's parameters and return a simulation.
[in]process_simulation_resultA callable that takes the simulation and processes its result.
Returns
A vector that contains processed simulation results for each run.

Important side effect: Calling this function overwrites seed and counter of thread_local_rng(). Use this RNG when sampling parameters in create_simulation.

◆ run_serial() [1/2]

template<class ParameterType , typename TimeType , typename StepType = TimeType>
template<class CreateSimulationFunction >
std::vector<SimulationT<CreateSimulationFunction> > mio::ParameterStudy< ParameterType, TimeType, StepType >::run_serial ( CreateSimulationFunction &&  create_simulation)
inline

Run all simulations in serial.

Parameters
[in]create_simulationA callable sampling the study's parameters and returning a simulation.
[in]process_simulation_result(Optional) A callable that takes the simulation and processes its result.
Returns
A vector containing (processed) simulation results for each run, or void if processing returns nothing.

Important side effect: Calling this function overwrites seed and counter of thread_local_rng(). Use this RNG when sampling parameters in create_simulation.

The function signature for create_simulation is SimulationT(const Parameters& study_parameters, Time t0, Step dt, size_t run_idx), where SimulationT is some kind of simulation. The function signature for process_simulation_result is ProcessedResultT(SimulationT&&, size_t run_index), where ProcessedResultT is a (de)serializable result, or void. A void function can be useful if the results should be fully handled during the study, for example, when memory is limited and the results have to be written to a disk.

◆ run_serial() [2/2]

template<class ParameterType , typename TimeType , typename StepType = TimeType>
template<class CreateSimulationFunction , class ProcessSimulationResultFunction >
EnsembleResultT<CreateSimulationFunction, ProcessSimulationResultFunction> mio::ParameterStudy< ParameterType, TimeType, StepType >::run_serial ( CreateSimulationFunction &&  create_simulation,
ProcessSimulationResultFunction &&  process_simulation_result 
)
inline

Run all simulations in serial.

Parameters
[in]create_simulationA callable sampling the study's parameters and returning a simulation.
[in]process_simulation_result(Optional) A callable that takes the simulation and processes its result.
Returns
A vector containing (processed) simulation results for each run, or void if processing returns nothing.

Important side effect: Calling this function overwrites seed and counter of thread_local_rng(). Use this RNG when sampling parameters in create_simulation.

The function signature for create_simulation is SimulationT(const Parameters& study_parameters, Time t0, Step dt, size_t run_idx), where SimulationT is some kind of simulation. The function signature for process_simulation_result is ProcessedResultT(SimulationT&&, size_t run_index), where ProcessedResultT is a (de)serializable result, or void. A void function can be useful if the results should be fully handled during the study, for example, when memory is limited and the results have to be written to a disk.

Member Data Documentation

◆ m_dt

template<class ParameterType , typename TimeType , typename StepType = TimeType>
Step mio::ParameterStudy< ParameterType, TimeType, StepType >::m_dt
private

Initial step size of the simulation. Some integrators may adapt their step size during simulation.

◆ m_num_runs

template<class ParameterType , typename TimeType , typename StepType = TimeType>
size_t mio::ParameterStudy< ParameterType, TimeType, StepType >::m_num_runs
private

Total number of runs (i.e. simulations) to do when calling "run".

◆ m_parameters

template<class ParameterType , typename TimeType , typename StepType = TimeType>
ParameterType mio::ParameterStudy< ParameterType, TimeType, StepType >::m_parameters
private

Stores parameters used to create a simulation for each run.

◆ m_rng

template<class ParameterType , typename TimeType , typename StepType = TimeType>
RandomNumberGenerator mio::ParameterStudy< ParameterType, TimeType, StepType >::m_rng
private

The random number generator used by the study.

◆ m_t0

template<class ParameterType , typename TimeType , typename StepType = TimeType>
Time mio::ParameterStudy< ParameterType, TimeType, StepType >::m_t0
private

◆ m_tmax

template<class ParameterType , typename TimeType , typename StepType = TimeType>
Time mio::ParameterStudy< ParameterType, TimeType, StepType >::m_tmax
private

Start and end time for the simulations.