b_asic.simulation¶
B-ASIC Simulation Module.
Contains a class for simulating the result of an SFG given a set of input values.
- class b_asic.simulation.Simulation(sfg: SFG, input_providers: Sequence[int | float | complex | DTypeLike | APyFixed | APyCFixed | APyFloat | APyCFloat | Sequence[int | float | complex | DTypeLike | APyFixed | APyCFixed | APyFloat | APyCFloat] | Callable[[int], int | float | complex | DTypeLike | APyFixed | APyCFixed | APyFloat | APyCFloat] | None] | None = None, data_type: DataType | None = None)¶
Bases:
objectSimulation of an SFG.
Build a topological evaluation order once, including nested SFGs, and reuse it for each iteration. Delay states are updated after all outputs are computed.
Use FastSimulation (from the C++ extension module) for a more effective simulation when running many iterations.
Graphs containing Expansion or Compression are normalized on an internal copy. One iteration processes one common period, consuming
input_samples_per_iterationsamples per input and producingoutput_samples_per_iterationsamples per output. Input callables receive their local sample index. Saved traces contain consecutive samples at each signal’s own rate. Run methods return the last sample of each output in the last period. Finite input arrays must contain whole periods of equal duration.- Parameters:
- sfg
SFG The signal flow graph to simulate.
- input_providerslist, optional
Input values, one list item per input. Each list item can be an array of values, a callable taking a time index and returning the value, or a number (constant input). If a value is not provided for an input, it will be 0.
- data_type
DataType, optional If provided, this finite arithmetic data type will be used for all operations.
- sfg
- clear_state() None¶
Clear all current state of the simulation, except for the results and iteration.
- property input_samples_per_iteration: tuple[int, ...]¶
Get the number of samples consumed per original input in one period.
- property output_samples_per_iteration: tuple[int, ...]¶
Get the number of samples produced per original output in one period.
- property results: Mapping[ResultKey, Sequence[int | float | complex | DTypeLike | APyFixed | APyCFixed | APyFloat | APyCFloat]]¶
A mapping from result keys to numpy arrays containing all results.
This includes intermediate values, calculated for each iteration up until now that was run with save_results enabled. The mapping is indexed using the
key()method of Operation with the appropriate output index.- Example result after 3 iterations::
{“c1”: [3, 6, 7], “c2”: [4, 5, 5], “bfly1.0”: [7, 0, 0], “bfly1.1”: [-1, 0, 2], “out0”: [7, -2, -1]}
- run(save_results: bool = True) Sequence[int | float | complex | DTypeLike | APyFixed | APyCFixed | APyFloat | APyCFloat]¶
Run the simulation until the end of its input arrays.
Return the output values of the last iteration.
- Parameters:
- save_resultsbool, default: True
Whether the results should be saved.
- Returns:
- list[
Num] Output values of the last iteration, in SFG output order.
- list[
- run_for(iterations: int, save_results: bool = True) Sequence[int | float | complex | DTypeLike | APyFixed | APyCFixed | APyFloat | APyCFloat]¶
Run a given number of iterations of the simulation.
Return the output values of the last iteration.
- Parameters:
- iterationsint
Number of iterations to simulate.
- save_resultsbool, default: True
Whether the results should be saved.
- Returns:
- list[
Num] Output values of the last iteration, in SFG output order.
- list[
- run_until(iteration: int, save_results: bool = True) Sequence[int | float | complex | DTypeLike | APyFixed | APyCFixed | APyFloat | APyCFloat]¶
Run the simulation until the iteration number.
Will run until the number of iterations is greater than or equal to the given iteration and return the output values of the last iteration. The number of iterations actually simulated depends on the current state of the Simulation.
- Parameters:
- iterationint
Iteration number to stop the simulation at.
- save_resultsbool, default: True
Whether the results should be saved.
- Returns:
- list[
Num] Output values of the last iteration, in SFG output order.
- list[
- set_input(index: int, input_provider: int | float | complex | DTypeLike | APyFixed | APyCFixed | APyFloat | APyCFloat | Sequence[int | float | complex | DTypeLike | APyFixed | APyCFixed | APyFloat | APyCFloat] | Callable[[int], int | float | complex | DTypeLike | APyFixed | APyCFixed | APyFloat | APyCFloat]) None¶
Set the input used to for a specific input index.
- Parameters:
- indexint
The input index.
- input_providerlist, callable, or number
Can be an array of values, a callable taking a time index and returning the value, or a number (constant input).
- set_inputs(input_providers: Sequence[int | float | complex | DTypeLike | APyFixed | APyCFixed | APyFloat | APyCFloat | Sequence[int | float | complex | DTypeLike | APyFixed | APyCFixed | APyFloat | APyCFloat] | Callable[[int], int | float | complex | DTypeLike | APyFixed | APyCFixed | APyFloat | APyCFloat] | None]) None¶
Set the input functions used to get values for the inputs to the internal SFG.
- Parameters:
- input_providerssequence of list, callable, or number
The input functions to use.
- step(save_results: bool = True) Sequence[int | float | complex | DTypeLike | APyFixed | APyCFixed | APyFloat | APyCFloat]¶
Run one iteration of the simulation and return the resulting output values.
- Parameters:
- save_resultsbool, default: True
Whether the results should be saved.
- Returns:
- list[
Num] Output values for the simulated iteration, in SFG output order.
- list[