pySimBlocks.blocks.operators.algebraic_function

class pySimBlocks.blocks.operators.algebraic_function.AlgebraicFunction(name: str, function: Callable, input_keys: List[str], output_keys: List[str], sample_time: float | None = None)[source]

Bases: Block

User-defined algebraic function block.

Stateless block defined by a user-provided Python callable:

y = g(t, dt, u1, u2, …)

Input and output port names are declared dynamically from input_keys and output_keys. All inputs and outputs must be 2D numpy arrays. Input and output shapes are frozen per port after the first use.

input_keys

Names of the input ports.

output_keys

Names of the output ports.

direct_feedthrough = True

True if outputs depend directly on inputs.

is_source = False

True if the block produces signals with no inputs.

classmethod adapt_params(params: Dict[str, Any], params_dir: Path | None = None) Dict[str, Any][source]

Load the user function from file_path and function_name YAML keys.

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

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

Returns:

Parameter dict with function set to the loaded callable and file_path / function_name keys removed.

Raises:
  • ValueError – If params_dir is None or required keys are missing.

  • FileNotFoundError – If the function file does not exist.

  • AttributeError – If the named function is not found in the module.

  • TypeError – If the resolved attribute is not callable.

initialize(t0: float) None[source]

Validate the function signature and compute initial outputs.

Parameters:

t0 – Initial simulation time in seconds.

Raises:
  • ValueError – If the function signature does not match input_keys.

  • RuntimeError – If the function does not return the expected output keys.

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

Call the user function and write outputs to the output ports.

Parameters:
  • t – Current simulation time in seconds.

  • dt – Current time step in seconds.

Raises:
  • RuntimeError – If any input is not set or the function fails.

  • TypeError – If any input or output is not a numpy array.

  • ValueError – If any input or output is not 2D, or if shapes changed.

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

No-op: AlgebraicFunction is a stateless block.

Parameters:
  • t – Current simulation time in seconds.

  • dt – Current time step in seconds.