Skip to content
Peter Corke edited this page May 25, 2026 · 9 revisions

Summary

This Python package enables modelling and simulation of continuous-time, discrete-time or hybrid dynamic systems. Systems are conceptualized in block diagram form, but represented in terms of Python class and method calls. Unlike Simulink or LabView we write Python code rather than drawing boxes and wires. Wires can communicate any Python type such as scalars, lists, dicts, NumPy arrays, objects, and functions.

Getting started

We first sketch the dynamic system we want to simulate as a block diagram, for example this simple first-order system

block diagram

which we can express concisely with bdsim as (see bdsim/examples/eg1.py

     1	#!/usr/bin/env python3
     2	
     3	import bdsim
     4	
     5	sim = sim.BDSim()  # setup run-time environment
     6	bd = sim.blockdiagram() # create a new block diagram
     7	
     8	# define the blocks
     9	demand = bd.STEP(T=1, pos=(0,0), name='demand')
    10	sum = bd.SUM('+-', pos=(1,0))
    11	gain = bd.GAIN(10, pos=(1.5,0))
    12	plant = bd.LTI_SISO(0.5, [2, 1], name='plant', pos=(3,0))
    13	scope = bd.SCOPE(styles=['k', 'r--'], pos=(4,0))
    14	
    15	# connect the blocks
    16	bd.connect(demand, sum[0], scope[1])
    17	bd.connect(plant, sum[1])
    18	bd.connect(sum, gain)
    19	bd.connect(gain, plant)
    20	bd.connect(plant, scope[0])
    21	
    22	bd.compile()   # check the diagram
    23	bd.report()    # list all blocks and wires
    24	
    25	out = sim.run(bd, 5)   # simulate for 5s

which is just 16 executable lines of code.

The red block annotations in the diagram are the names of blocks, and have become names of instances of object that represent those blocks. The blocks can also have names which are used in diagnostics and as labels in plots.

In bdsim all wires are point to point, a one-to-many connection is implemented by many wires.

Ports are designated using Python indexing and slicing notation, for example sum[0]. Whether it is an input or output port depends on context. Blocks are connected by connect(from, to_1, to_2, ...) so an index on the first argument refers to an output port, while on the second (or subsequent) arguments refers to an input port. If a port has only a single port then no index is required.

The simulation results are return in a struct-like container object

>>> out
t      = ndarray:float64 (220,)
x      = ndarray:float64 (220, 1)
xnames = ['plant:x_0'] (list)          

which contains an array of time values, an array of state values, and a list of the names of the state variables.

More details on this Wiki about:

Clone this wiki locally