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

Nested Subsystems

Demonstration of hierarchical modeling using nested subsystems for a Van der Pol oscillator.

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

Why Use Subsystems?

Subsystems allow you to:

  • Organize complex systems into logical modules
  • Reuse components across different models
  • Abstract implementation details
  • Scale to large systems with many components
  • Debug and test individual modules separately

The Van der Pol Oscillator Revisited

The stiff Van der Pol oscillator is described by:

MATHDISPLAY0ENDMATH MATHDISPLAY1ENDMATH

With MATHINLINE2ENDMATH (very stiff!)

Hierarchical Structure

This example demonstrates hierarchical modeling using Subsystem and Interface blocks for modular system design.

Python
Loading...

System Parameters

Python
Loading...

Level 1: ODE Function Subsystem

First, we create a subsystem that computes MATHINLINE0ENDMATH

This subsystem:

  • Takes two inputs: MATHINLINE1ENDMATH and MATHINLINE2ENDMATH
  • Returns one output: the computed derivative
  • Is self-contained and reusable
vanderpol function as a subsystem

The Interface block defines the subsystem's inputs and outputs and this is how it looks like in pathsim:

Python
Loading...

Level 2: Van der Pol Subsystem

Now we create a subsystem that contains:

  • Two integrators (for MATHINLINE0ENDMATH and MATHINLINE1ENDMATH)
  • The ODE function subsystem we just created
vanderpol ODE as a subsystem

This implements the complete Van der Pol ODE system:

Python
Loading...

Level 3: Top-Level System

Finally, we create the top-level system that contains:

  • The VDP subsystem
  • A Scope for visualization
top level system view

At this level, the VDP subsystem looks like a simple block with two outputs, hiding all its internal complexity

Python
Loading...

Simulation Setup

We use a stiff solver (ESDIRK43) because :math:`\mu = 1000makes this a very stiff system.

Python
Loading...
00:44:07 - INFO - LOGGING (log: True)
00:44:07 - INFO - BLOCKS (total: 2, dynamic: 1, static: 1, eventful: 0)
00:44:07 - INFO - GRAPH (nodes: 2, edges: 1, alg. depth: 1, loop depth: 0, runtime: 0.020ms)
00:44:07 - INFO - STARTING -> TRANSIENT (Duration: 2000.00s)
00:44:08 - INFO - --------------------   1% | 0.8s<16.5s | 27.8 it/s
00:44:08 - INFO - #####---------------  27% | 1.2s<1.8s | 17.1 it/s
00:44:09 - INFO - ########------------  40% | 2.1s<11.4s | 18.9 it/s
00:44:11 - INFO - ########------------  41% | 4.5s<14.5s | 41.2 it/s
00:44:12 - INFO - #############-------  67% | 5.0s<0.5s | 25.7 it/s
00:44:12 - INFO - ################----  80% | 5.7s<2.6s | 19.3 it/s
00:44:15 - INFO - ################----  81% | 8.4s<8.1s | 45.7 it/s
00:44:16 - INFO - #################### 100% | 8.7s<--:-- | 20.5 it/s
00:44:16 - INFO - FINISHED -> TRANSIENT (total steps: 341, successful: 234, runtime: 8725.58 ms)

Results: Time Series

The Van der Pol oscillator with MATHINLINE0ENDMATH exhibits relaxation oscillations - fast transitions between slow phases. This requires a stiff solver to handle efficiently.

Python
Loading...
Output

Subsystem Benefits Demonstrated

This example shows several advantages of subsystems:

  1. Modularity: The ODE function is completely separate from the integration
  2. Reusability: The Fn subsystem could be used in other models
  3. Clarity: The top level is clean - just VDP and Scope
  4. Debugging: Each subsystem can be tested independently
  5. Abstraction: Inner complexity is hidden from higher levels

Comparison with ODE Block

Compare this hierarchical approach with using a single ODE block:

# Alternative: Using ODE block (simpler but less modular)
def vdp_ode(x, u, t):
    return np.array([x[1], mu*(1 - x[0]**2)*x[1] - x[0]])

VDP = ODE(vdp_ode, np.array([x1_0, x2_0]))

Both approaches work! Use subsystems when:

  • You need modularity and reusability
  • The system is complex with many components
  • You want to visualize internal signals
  • You're building block diagram models