Run Simulator

A simulator which describes certain real-world system is often subject to uncertainties. Uncertainty-related analyses require running the simulator multiple times at different variable input points. Class RunSimulator implemented in this module provides functionality to sequentially or parallelly execute simulator at a number of varaible input points.

The user needs to define their simulator, implement an interface (essentially a Python function) to call the simulator from within Python and return output of interest, and inform RunSimulator the signature of the interface.

RunSimulator Class

The RunSimulator class is imported by:

from psimpy.simulator.run_simulator import RunSimulator

Methods

class RunSimulator(simulator, var_inp_parameter, fix_inp=None, o_parameter=None, dir_out=None, save_out=False)[source]

Serial or parallel execution of a given simulator.

Parameters
  • simulator (Callable) – A Python function defining the simulator. Its parameters are defined in three parts: var_inp_parameter defines variable parameters; fix_inp defines fixed parameters (fix_inp.keys()) and their values (fix_inp.values()); o_parameter defines an additional variable which is used to name files saved onto the disk within the function body of simulator. The simulator should return outputs of interest as numpy.ndarray.

  • var_inp_parameter (list of str) – A list consists of all variable input parameters of simulator.

  • fix_inp (dict, optional) – A dictionary consists of key-value pairs of all fixed input of the simulator.

  • o_parameter (str, optional) – Keyword parameter of simulator which is used to name internally saved files (if defined). It is only relevant if the function body of simulator saves data onto the disk.

  • dir_out (str, optional) – Directory to save outputs of interest returned by simulator.

  • save_out (bool, optional) – Whether to save returned values of simulator. If True, dir_out must be given.

serial_run(var_samples, prefixes=None, append=False)[source]

Perform serial execution of simulator at a set of var_samples.

Parameters
  • var_samples (numpy array) – Samples of variable input parameters. The first axis (var_samples.shape[0]) corresponds to the number of samples. The second axis (var_samples.shape[1]) corresponds to the number of variable input parameters.

  • prefixes (list of str, optional) – Contains len(var_samples) prefixes. Each of them is used to name corresponding simulation output file.

  • append (bool, optional) – If True, append var_samples to existing samples and correspondingly append simulation outputs.

Return type

None

parallel_run(var_samples, prefixes=None, append=False, max_workers=None)[source]

Perform parallel execution of simulator at a set of var_samples.

Parameters
  • var_samples (numpy array) – Samples of variable input parameters. The first axis (var_samples.shape[0]) corresponds to the number of samples. The second axis (var_samples.shape[1]) corresponds to the number of variable input parameters.

  • prefixes (list of str, optional) – Contains len(var_samples) prefixes. Each of them is used to name corresponding simulation output file.

  • append (bool, optional) – If True, append var_samples to existing samples and correspondingly append simulation outputs.

  • max_workers (int, optional) – Controls the maximum number of tasks running in parallel. Default is the number of CPUs on the host.

Return type

None

Attributes

RunSimulator.var_samples: ndarray

Samples of variable input parameters.

RunSimulator.outputs: list

Simulation outputs.