pySimBlocks.blocks.sources.function_source

class pySimBlocks.blocks.sources.function_source.FunctionSource(name: str, function: Callable, output_keys: List[str] | None = None, sample_time: float | None = None)[source]

Bases: BlockSource

User-defined source block driven by a callable.

Computes outputs at each step by calling a user-provided function:

y = f(t, dt)

The function must accept exactly (t, dt) as positional arguments and return a dict mapping each key in output_keys to a scalar, 1D, or 2D array-like value. Output shapes are frozen after the first call and must remain constant throughout the simulation.

output_keys

List of output port names produced by the function.

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

Load a callable from file_path and function_name YAML keys.

If function is already present in params, it is returned as-is. Otherwise, the callable is loaded dynamically from the specified file.

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 with function set to the loaded callable and file_path/function_name keys removed.

Raises:
  • ValueError – If only one of file_path or function_name is provided.

  • 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 at t0.

Parameters:

t0 – Initial simulation time in seconds.

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

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

Parameters:
  • t – Current simulation time in seconds.

  • dt – Current time step in seconds.