pySimBlocks.core.model

class pySimBlocks.core.model.Model(name: str = 'model', model_data: Dict[str, Any] | None = None, params_dir: Path | None = None, verbose: bool = False)[source]

Bases: object

Discrete-time block-diagram model (Simulink-like).

Stores blocks and signal connections, builds the topological execution order, and provides fast access to downstream connections.

Topological sorting is applied only to the combinational (direct- feedthrough) graph. Stateful blocks act as cycle breakers, exactly as Simulink handles algebraic loops (see Simulink PDF p.7).

name

Identifier for this model.

verbose

If True, print detailed execution-order build logs.

blocks

Registry of blocks keyed by name.

connections

List of signal connections.

add_block(block: Block) Block[source]

Add a block to the model.

Parameters:

block – Block instance to register.

Returns:

The registered block.

Raises:

ValueError – If a block with the same name already exists.

get_block_by_name(name: str) Block[source]

Return a block by its name.

Parameters:

name – Name of the block to retrieve.

Returns:

The matching Block instance.

Raises:

ValueError – If no block with that name exists.

connect(src_block: str, src_port: str, dst_block: str, dst_port: str) None[source]

Connect an output port to an input port.

Registers a connection from blocks[src_block].outputs[src_port] to blocks[dst_block].inputs[dst_port].

Parameters:
  • src_block – Name of the source block.

  • src_port – Name of the source output port.

  • dst_block – Name of the destination block.

  • dst_port – Name of the destination input port.

Raises:

ValueError – If src_block or dst_block is not registered.

build_execution_order()[source]

Build the Simulink-like output execution order.

Runs a Kahn topological sort on the direct-feedthrough dependency graph. Blocks without direct feedthrough act as cycle breakers.

Returns:

Ordered list of blocks for output_update execution.

Raises:

RuntimeError – If a direct-feedthrough cycle (algebraic loop) is detected.

downstream_of(block_name: str) List[Tuple[Tuple[str, str], Tuple[str, str]]][source]

Return all connections where block_name is the source.

Parameters:

block_name – Name of the source block.

Returns:

List of connections originating from block_name.

execution_order() List[Block][source]

Return the output execution order, building it if necessary.

Returns:

Ordered list of blocks for output_update execution.

predecessors_of(block_name)[source]

Yield the names of all blocks that feed into block_name.

Parameters:

block_name – Name of the destination block.

Yields:

Source block names connected to block_name.

resolve_sample_times(dt) None[source]

Resolve effective sample times for all blocks.

Blocks with an explicit sample_time keep it; others inherit dt.

Parameters:

dt – Global simulation time step in seconds.