pySimBlocks.core.block

class pySimBlocks.core.block.Block(name: str, sample_time: float | None = None)[source]

Bases: ABC

Base class for all discrete-time blocks (Simulink-like).

A block follows two-phase execution per timestep: output_update computes y[k] from x[k] and u[k], then state_update computes x[k+1] from x[k] and u[k].

name

Unique identifier for this block instance.

sample_time

Sampling period in seconds, or None to use the global dt.

inputs

Input port values, set by the simulator each step.

Type:

Dict[str, numpy.ndarray]

outputs

Output port values, written by output_update.

Type:

Dict[str, numpy.ndarray]

state

Committed state x[k].

Type:

Dict[str, numpy.ndarray]

next_state

Pending state x[k+1], written by state_update.

Type:

Dict[str, numpy.ndarray]

direct_feedthrough = True

True if outputs depend directly on inputs.

is_source = False

True if the block produces signals with no inputs.

inputs: Dict[str, ndarray]
outputs: Dict[str, ndarray]
state: Dict[str, ndarray]
next_state: Dict[str, ndarray]
classmethod adapt_params(params: Dict[str, Any], params_dir: Path | None = None) Dict[str, Any][source]

Adapt parameters from YAML format to constructor format.

Parameters:
  • params – Raw parameter dict loaded from the YAML project file.

  • params_dir – Directory of the project file, for resolving relative paths. None if not applicable.

Returns:

Parameter dict ready to be passed to the block constructor.

property has_state: bool

True if the block carries internal state.

abstractmethod initialize(t0: float)[source]

Initialize state x[0] and outputs y[0].

Must populate self.state and self.outputs before the first step.

Parameters:

t0 – Initial simulation time in seconds.

abstractmethod output_update(t: float, dt: float)[source]

Compute outputs y[k] from x[k] and inputs u[k].

Called before state_update each timestep. Must write to self.outputs.

Parameters:
  • t – Current simulation time in seconds.

  • dt – Current time step in seconds.

abstractmethod state_update(t: float, dt: float)[source]

Compute next state x[k+1] from x[k] and inputs u[k].

Must write to self.next_state.

Parameters:
  • t – Current simulation time in seconds.

  • dt – Current time step in seconds.

commit_state()[source]

Copy x[k+1] into x[k] to finalize the timestep.

Called by the simulator after all blocks have completed state_update.

finalize()[source]

Clean up resources at the end of the simulation.