simulator

class sysdynpy.simulator.Simulator(simulation_steps=10, time_unit='days', dt=0.05)[source]

Bases: object

Can be used to run simulations for a given system.

__init__(simulation_steps=10, time_unit='days', dt=0.05)[source]

Constructor method.

Parameters
  • simulation_steps (int) – The number of steps to simulate when run_simulation() is called, defaults to 10

  • time_unit (str) – Defines the time period of one simulation step. The value must be one of the values defines in VALID_TIME_UNITS , defaults to “days”

  • dt (float) –

    The interval between calculations. Must be a number between zero and one. The reciprocal of this argument is the number of calculation steps per simulation step. One means that the system element values are calculated once per simulation step. Smaller numbers result in higher accuracy but longer calculation times while greater numbers lead to faster calculations but lower accuracy. Defaults to 0.05.

    Example:
    0.05 means the reciprocal is 1 / 0.05 = 20. So by default 20 calculations are done per simulation step.

_calculate_stock_change(stock)[source]

Calculates the change for a given stock based on the calculation rule.

This method only checks the input- and output flows of the stock. It presumes that values for these flows have already been calculated for the current simulation step.

Parameters

stock (Stock) – The stock to calculate the change for.

Returns

The calculated value

Return type

float

_calculate_value(element)[source]

Calculates the value for a given element based on the calculation rule.

This is a recursive function to calculate the value of a flow or dynamic variable. The values of flows or dynamic variables can always be deduced from parameters or stocks. But it can not be assumed that a dynamic variable or flow directly depends on only parameters or stocks.

Parameters

element (Flow or DynamicVariable) – The element to calculate a value for.

Returns

The calculated value

Return type

float

get_simulation_results()[source]

Provides the trajectories of all system elements during the simulation. Must be called after the simulation.

Returns

A dictionary where each key corresponds to the name of a system element. The values are lists. For each simulation step a list contains the value of the system element (key) at that simulation step.

Example:

{
    someStockName: [10, 7.5, 5, 3.5, 2.5, 3, 3],
    someFlowName: [0, -2.5, -2, -1.5, -1, 0, 0],
    someParameterName: [3, 3, 3, 3, 3, 3, 3]
}

Return type

None or dict

run_simulation(system)[source]

Runs a simulation for the given system.

Once the simulation is finished, results are available by calling get_simulation_results()

Parameters

system (System) – The system to run a simulation for.

VALID_TIME_UNITS = ('milliseconds', 'seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years')

Possible values for the property time_unit.

_system_states

Stores the system state after each simulation step.

Type

list

property dt

see __init__()

property simulation_steps

see __init__()

property time_unit

see __init__()