Quickstart¶
This simple example demonstrates the basics of PathSim by integrating a cosine function to produce a sine wave.
Setup¶
First, import the necessary modules:
[1]:
import numpy as np
import matplotlib.pyplot as plt
from pathsim import Simulation, Connection
from pathsim.blocks import Source, Integrator, Scope
# Apply custom plotting style
plt.style.use('pathsim_docs.mplstyle')
Build the System¶
Define the blocks that make up our system:
[2]:
# Define the blocks of our system
Sr = Source(np.cos) # Source block that outputs cos(t)
In = Integrator() # Integrator block
Sc = Scope(labels=["cos", "sin"]) # Scope to record signals
Create the Simulation¶
Connect the blocks and create a simulation instance:
[3]:
# Create simulation with blocks and connections
Sim = Simulation(
blocks=[Sr, In, Sc],
connections=[
Connection(Sr, In), # cosine → integrator
Connection(Sr, Sc[0]), # cosine → scope channel 0
Connection(In, Sc[1]), # sine → scope channel 1
],
dt=0.01
)
2025-10-13 13:30:41,966 - INFO - LOGGING (log: True)
2025-10-13 13:30:41,966 - INFO - BLOCK (type: Source, dynamic: False, events: 0)
2025-10-13 13:30:41,967 - INFO - BLOCK (type: Integrator, dynamic: True, events: 0)
2025-10-13 13:30:41,967 - INFO - BLOCK (type: Scope, dynamic: False, events: 0)
2025-10-13 13:30:41,968 - INFO - GRAPH (nodes: 3, edges: 3, alg. depth: 1, loop depth: 0, runtime: 0.022ms)
Run and Visualize¶
Execute the simulation for 10 time units and plot the results:
[4]:
# Run for 10 time units
Sim.run(10)
# Plot the scope
Sc.plot(lw=2)
plt.show()
2025-10-13 13:30:41,971 - INFO - STARTING -> TRANSIENT (Duration: 10.00s)
2025-10-13 13:30:41,972 - INFO - TRANSIENT: 0% | elapsed: 00:00:00 (eta: --:--:--) | 0 steps (N/A steps/s)
2025-10-13 13:30:41,981 - INFO - TRANSIENT: 20% | elapsed: 00:00:00 (eta: --:--:--) | 200 steps (22646.2 steps/s)
2025-10-13 13:30:41,989 - INFO - TRANSIENT: 40% | elapsed: 00:00:00 (eta: --:--:--) | 401 steps (24499.2 steps/s)
2025-10-13 13:30:41,998 - INFO - TRANSIENT: 60% | elapsed: 00:00:00 (eta: --:--:--) | 601 steps (21873.5 steps/s)
2025-10-13 13:30:42,007 - INFO - TRANSIENT: 80% | elapsed: 00:00:00 (eta: --:--:--) | 801 steps (23180.8 steps/s)
2025-10-13 13:30:42,015 - INFO - TRANSIENT: 100% | elapsed: 00:00:00 (eta: --:--:--) | 1001 steps (24649.7 steps/s)
2025-10-13 13:30:42,016 - INFO - TRANSIENT: 100% | elapsed: 00:00:00 (eta: --:--:--) | 1001 steps (22458.6 avg steps/s)
2025-10-13 13:30:42,016 - INFO - FINISHED -> TRANSIENT (total steps: 1001, successful: 1001, runtime: 44.57 ms)
The plot shows both the original cosine wave and the integrated sine wave, demonstrating how PathSim can simulate continuous-time systems through block-based modeling.