This example is interactive. Click the play button on any cell to execute it, or run all cells in sequence.

Algebraic Loop

Demonstration of PathSim's automatic handling of algebraic loops.

You can also find this example as a single file in the GitHub repository.

simple algebraic loop diagram

What is an Algebraic Loop?

An algebraic loop occurs when the output of a block depends on its own input in the same timestep. This creates an implicit equation that must be solved:

MATHDISPLAY0ENDMATH

PathSim automatically detects algebraic loops and resolves them at each timestep using accelerated fixed-point iterations through wrapping looping connections with the ConnectionBooster class.

System Description

In this example:

  • A source generates a sinusoidal signal: MATHINLINE2ENDMATH
  • An amplifier multiplies by gain MATHINLINE3ENDMATH
  • An adder sums the source and amplifier output
  • The amplifier input comes from the adder output, creating a loop

This creates the algebraic equation: MATHDISPLAY0ENDMATH

Which has the analytical solution: MATHDISPLAY1ENDMATH

PathSim automatically detects and solves algebraic loops using accelerated fixed-point iteration trough the ConnectionBooster class. Tolerances for the loop solver are configurable via the `tolerance_fpi` parameter.

Python
Loading...

System Parameters

We define:

  • Timestep: 0.1 s
  • Algebraic feedback gain: MATHINLINE0ENDMATH
Python
Loading...

Block Diagram

Python
Loading...

Connections

Notice that the output of the Add block connects to the input of Amp, and the output of Amp connects back to the second input of Add. This creates the algebraic loop.

Python
Loading...

Simulation

PathSim automatically detects the algebraic loop and solves it using fixed-point iteration at each timestep. No special configuration is needed!

Python
Loading...
12:43:14 - INFO - LOGGING (log: True)
12:43:14 - INFO - BLOCKS (total: 4, dynamic: 0, static: 4, eventful: 0)
12:43:14 - INFO - GRAPH (nodes: 4, edges: 6, alg. depth: 1, loop depth: 2, runtime: 0.467ms)
12:43:14 - INFO - STARTING -> TRANSIENT (Duration: 5.00s)
12:43:14 - INFO - --------------------   2% | 0.0s<0.1s | 340.0 it/s
12:43:14 - INFO - ####----------------  21% | 0.0s<0.0s | 6406.3 it/s
12:43:14 - INFO - ########------------  40% | 0.0s<0.0s | 6770.9 it/s
12:43:14 - INFO - ############--------  60% | 0.0s<0.0s | 6789.0 it/s
12:43:14 - INFO - ################----  80% | 0.0s<0.0s | 7532.9 it/s
12:43:14 - INFO - #################### 100% | 0.0s<--:-- | 6844.3 it/s
12:43:14 - INFO - FINISHED -> TRANSIENT (total steps: 51, successful: 51, runtime: 28.28 ms)

Results

The plot shows:

  • src (blue): Input signal = MATHINLINE0ENDMATH
  • amp (orange): Amplifier output = MATHINLINE1ENDMATH
  • add (green): Adder output = MATHINLINE2ENDMATH

The algebraic loop is solved correctly at each timestep!

Python
Loading...
Output

Verification

Let's verify the solution is correct by checking that the adder output matches the analytical solution.

Python
Loading...
Maximum error: 2.22e-16
Output