Skip to content

Compiling

Peter Corke edited this page Oct 28, 2026 · 6 revisions

The compilation phase checks the integrity of the block diagram and sets it up for simulation or real-time execution.

Naming

As blocks and wires are created they are added to lists withing the BlockDiagram object. In this phase we assign consecutive integers (starting at zero) to all the blocks, and to all the wires. This is the .id attribute of those objects. Block names can be assigned by the user with the name= argument, otherwise a block will be named type.i where type is the type of the block (its name in lowercase) and i is a unique integer for that type. i starts at zero and increments by one for each new block of the type.

Signatures

Compilation ends by evaluating every block once, at t = 0, in plan order, and recording the type of the value on every wire. This is the wire's signature: the Python type, plus the shape and dtype if it is an ndarray. The source type column of the summary report below is read from it. See Type inference.

Subsystem resolution

A subsystem block is effectively replaced with a copy of the blocks that define the subsystem. The blocks are all renamed to the name of the parent block followed by a slash follwed by the name of the block within the system. The same subsystem can be instantiated multiple times because the original blocks are copied on every instantiation.

The result is one large non-hierarchical wireless.

Error checking

The inputs and outputs of every block are checked.

  • unconnected outputs result in a warning
  • unnconnected inputs result in an error since it will never be possible to evaluate the outputs of the block
  • an input connected to more than one output is an error

Port values are checked against the number of input and output ports for a block and will generate an error if out of bounds.

Cycles

A block diagram is a directed graph that may contain cycles. Cycles, by definition, cannot include leaf nodes (Sink or Source subclasses). A cycle is only problematic if it comprises only Function subclass blocks since it is not possible to evaluate the values on the wires without iteration -- an algebraic loop. A stateful block (Continuous or Sampled subclasses) which is strictly proper, ie. has no direct "pass through" will break this cycle.

bdsim does not attempt iteration. It uses depth-first search from each Function subclass block to identify problematic cycles and raises an error.

Once the diagram is compiled we can display various reports that describe the model. For the example eg1.py we display a summary report

    bd.report_summary()
┌─────────┬────┬────┬──────────┬────────┬────────┬─────────────┐
│  block  │ nc │ nd │   type   │ inport │ source │ source type │
├─────────┼────┼────┼──────────┼────────┼────────┼─────────────┤
│ demand@ │ 0  │ 0  │ step     │        │        │             │
├─────────┼────┼────┼──────────┼────────┼────────┼─────────────┤
│ gain.0  │ 0  │ 0  │ gain     │ 0      │ sum.0  │ float       │
├─────────┼────┼────┼──────────┼────────┼────────┼─────────────┤
│ plant   │ 1  │ 0  │ lti_siso │ 0      │ gain.0 │ float       │
├─────────┼────┼────┼──────────┼────────┼────────┼─────────────┤
│ scope.0 │ 0  │ 0  │ scope    │ 0      │ plant  │ float       │
│         │    │    │          │ 1      │ demand │ int         │
├─────────┼────┼────┼──────────┼────────┼────────┼─────────────┤
│ sum.0   │ 0  │ 0  │ sum      │ 0      │ demand │ int         │
│         │    │    │          │ 1      │ plant  │ float       │
└─────────┴────┴────┴──────────┴────────┴────────┴─────────────┘

It lists each block (column 1) and describes which blocks its inputs are connected to.

A more detailed report, sometimes useful for debugging, is

    bd.report_lists()

(bd.report() still works but is deprecated in favour of report_lists().)

Blocks::

┌────┬─────────┬─────┬──────┬────────┬─────────┬──────────┐
│ id │    name │ nin │ nout │ nstate │ ndstate │   type   │
├────┼─────────┼─────┼──────┼────────┼─────────┼──────────┤
│  0 │  demand │   0 │    1 │      0 │       0 │ step     │
│  1 │   sum.0 │   2 │    1 │      0 │       0 │ sum      │
│  2 │  gain.0 │   1 │    1 │      0 │       0 │ gain     │
│  3 │   plant │   1 │    1 │      1 │       0 │ lti_siso │
│  4 │ scope.0 │   2 │    0 │      0 │       0 │ scope    │
├────┼─────────┼─────┼──────┼────────┼─────────┼──────────┤
│    │   total │     │      │      1 │       0 │          │
└────┴─────────┴─────┴──────┴────────┴─────────┴──────────┘


Wires::

┌────┬──────┬──────┬──────────────────────────┬───────┐
│ id │ from │  to  │       description        │ type  │
├────┼──────┼──────┼──────────────────────────┼───────┤
│  0 │ 0[0] │ 1[0] │ demand[0] --> sum.0[0]   │ int   │
│  1 │ 0[0] │ 4[1] │ demand[0] --> scope.0[1] │ int   │
│  2 │ 3[0] │ 1[1] │ plant[0] --> sum.0[1]    │ float │
│  3 │ 1[0] │ 2[0] │ sum.0[0] --> gain.0[0]   │ float │
│  4 │ 2[0] │ 3[0] │ gain.0[0] --> plant[0]   │ float │
│  5 │ 3[0] │ 4[0] │ plant[0] --> scope.0[0]  │ float │
└────┴──────┴──────┴──────────────────────────┴───────┘

Dataflow graph

When executing a block diagram, a block cannot be evaluated until its input values have been computed. To achieve this an execution plan is created, essentially a dataflow graph. All blocks in the first column are evaluated first (sources and stateful blocks), then the second column and so on.

block diagram

The dataflow graph can be displayed in a simple tabular format

bd.report_schedule()

or saved in GraphViz format

bd.schedule_dotfile('myfile.dot')

and converted to any graphical format using dot, for example to render as a PDF file

$ dot -Tpdf myfile.dot > myfile.pdf

Clone this wiki locally