Skip to content

Runtimes and simulator state

Peter Corke edited this page Apr 12, 2023 · 5 revisions

WORK IN PROGRESS

Runtimes and block diagrams

To support various operating modes of bdsim such as numerical simulation or real-time control, the concept of a "run time" has been introduced. For instance, in a desktop simulation environment the run-time has to deal with command line options, dynamic block loading and Matplotlib display graphics. For a real-time environment it must deal with threads and clocks.

The idea was that the block diagram itself should be agnostic to the run time and blocks just perform numeric operations. Said another way, a simulation knows about the block diagram, but the block diagram doesn't know about its simulation environment. If this were C code the block diagram would be reentrant.

There are a few, but important, exceptions though:

  • for desktop simulation the scope blocks need to display graphs using the windowing system
  • for real-time operations we require blocks that perform input and output in an operating-system-specific way, and perhaps need to access timers and threads.

For those few blocks that do need to know about their operating environment we pass references to a runtime-specific object that holds run time state to the various block methods.

Runtimes

Currently there are two runtimes

import bdsim

sim = bdsim.BDSim()

which returns a BDSim instance.

import bdsim

rt = bdsim.BDRealTime()

which returns a BDRealTime instance.

A block diagram is created by the block diagram factory method of any runtime

bd = sim.blockdiagram()

or

bd = rt.blockdiagram()

which in either case returns an instance of a BlockDiagram object. This object contains a list of blocks, a list of wires, various derived data structures, and methods to evaluate the block diagram which in turn requires evaluation of various block methods.

The BlockDiagram object is meant to be independent of the operating environment which is why it does not have a run method. Rather than write

bd.run()

we write

sim.run(bd)

It would also be possible to express this as

bd.run(sim)

State object

A grab bag of state variables are kept within a simulator state object. In general blocks shouldn't need access to this, but just in case, a references is passed to methods of blocks at run-time as the named parameter simstate.

The state object is specific to the particular run-time. Key elements for the simulation runtime BDSimState are:

Attribute Purpose
.T maximum simulation time
.dt fixed time step for simulation
.options Options instance with attributes for option values

For the realtime runtime the state object is of BDRealTimeState type.