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
)
09:33:25 - INFO - LOGGING (log: True)
09:33:25 - INFO - BLOCKS (total: 3, dynamic: 1, static: 2, eventful: 0)
09:33:25 - INFO - GRAPH (nodes: 3, edges: 3, alg. depth: 1, loop depth: 0, runtime: 0.026ms)
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()
09:33:25 - INFO - STARTING -> TRANSIENT (Duration: 10.00s)
09:33:25 - INFO - -------------------- 1% | 0.0s<0.0s | 24755.5 it/s
09:33:25 - INFO - ####---------------- 20% | 0.0s<0.0s | 27985.7 it/s
09:33:25 - INFO - ########------------ 40% | 0.0s<0.0s | 27239.9 it/s
09:33:25 - INFO - ############-------- 60% | 0.0s<0.0s | 22742.8 it/s
09:33:25 - INFO - ################---- 80% | 0.0s<0.0s | 28006.6 it/s
09:33:25 - INFO - #################### 100% | 0.0s<--:-- | 27277.1 it/s
09:33:25 - INFO - FINISHED -> TRANSIENT (total steps: 1001, successful: 1001, runtime: 45.50 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.