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-23 15:26:26,728 - INFO - LOGGING (log: True)
2025-10-23 15:26:26,729 - INFO - BLOCK (type: Source, dynamic: False, events: 0)
2025-10-23 15:26:26,730 - INFO - BLOCK (type: Integrator, dynamic: True, events: 0)
2025-10-23 15:26:26,730 - INFO - BLOCK (type: Scope, dynamic: False, events: 0)
2025-10-23 15:26:26,731 - INFO - GRAPH (nodes: 3, edges: 3, alg. depth: 1, loop depth: 0, runtime: 0.030ms)
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-23 15:26:26,736 - INFO - STARTING -> TRANSIENT (Duration: 10.00s)
2025-10-23 15:26:26,737 - INFO - TRANSIENT: 0% | elapsed: 00:00:00 (eta: --:--:--) | 0 steps (N/A steps/s)
2025-10-23 15:26:26,745 - INFO - TRANSIENT: 20% | elapsed: 00:00:00 (eta: --:--:--) | 200 steps (24514.4 steps/s)
2025-10-23 15:26:26,753 - INFO - TRANSIENT: 40% | elapsed: 00:00:00 (eta: --:--:--) | 401 steps (24752.9 steps/s)
2025-10-23 15:26:26,761 - INFO - TRANSIENT: 60% | elapsed: 00:00:00 (eta: --:--:--) | 601 steps (23487.4 steps/s)
2025-10-23 15:26:26,770 - INFO - TRANSIENT: 80% | elapsed: 00:00:00 (eta: --:--:--) | 801 steps (24384.3 steps/s)
2025-10-23 15:26:26,778 - INFO - TRANSIENT: 100% | elapsed: 00:00:00 (eta: --:--:--) | 1001 steps (24770.8 steps/s)
2025-10-23 15:26:26,778 - INFO - TRANSIENT: 100% | elapsed: 00:00:00 (eta: --:--:--) | 1001 steps (23542.1 avg steps/s)
2025-10-23 15:26:26,778 - INFO - FINISHED -> TRANSIENT (total steps: 1001, successful: 1001, runtime: 42.52 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.