### Import and Setup Source: https://docs.pathsim.org/fmi/v0.1.1/examples/fmu-model-exchange-bouncing-ball Imports necessary libraries and modules for the bouncing ball example. ```python import numpy as np import matplotlib.pyplot as plt # Apply PathSim docs matplotlib style for consistent, theme-friendly figures # plt.style.use removed for browser from pathlib import Path from pathsim import Simulation, Connection from pathsim.blocks import Scope from pathsim_fmi import ModelExchangeFMU from pathsim.solvers import RKBS32 ``` -------------------------------- ### Import and Setup for Kalman Filter Example Source: https://docs.pathsim.org/pathsim/v0.25.0/notebooks/kalman_filter.ipynb Imports required libraries and PathSim blocks, and applies the PathSim docs matplotlib style. ```python import numpy as np import matplotlib.pyplot as plt # Apply PathSim docs matplotlib style plt.style.use('../pathsim_docs.mplstyle') from pathsim import Simulation, Connection from pathsim.blocks import ( Constant, Integrator, Adder, WhiteNoise, KalmanFilter, Scope ) ``` -------------------------------- ### Set up simulation parameters Source: https://docs.pathsim.org/fmi/v0.1.1/notebooks/fmu_model_exchange_bouncing_ball.ipynb Sets the start time, stop time, and step size for the simulation. ```python start_time = 0.0 stop_time = 3.0 step_size = 0.01 ``` -------------------------------- ### Simulation Setup Source: https://docs.pathsim.org/pathsim/v0.25.0/examples/fmu-model-exchange-vanderpol Initializes a Simulation with the RKDP54 solver, dt=0.1, dt_max=0.1, and tolerances, then runs for 30 seconds. ```Python # Initialize simulation sim = Simulation( blocks, connections, dt=0.1, dt_max=0.1, Solver=RKDP54, tolerance_lte_rel=1e-6, tolerance_lte_abs=1e-9, log=True ) # Run simulation sim.run(30.0) ``` -------------------------------- ### Import and Setup Source: https://docs.pathsim.org/pathsim/v0.25.0/notebooks/dcmotor_control.ipynb Imports the necessary PathSim classes and blocks, and applies the documentation matplotlib style. ```python import numpy as np import matplotlib.pyplot as plt # Apply PathSim docs matplotlib style plt.style.use('../pathsim_docs.mplstyle') from pathsim import Simulation, Connection from pathsim.blocks import StepSource, Integrator, Amplifier, Adder, Scope, AntiWindupPID, Clip from pathsim.solvers import RKCK54 ``` -------------------------------- ### Import FastSim and print version Source: https://docs.pathsim.org/fastsim/v0.28.3/examples/c-code-generation Imports the FastSim library and its core components, then prints the library version. This is the initial setup for all subsequent examples on the page. ```Python import fastsim as fs from fastsim import Simulation, Connection from fastsim.blocks import Source, Adder, Amplifier, Integrator, ODE, Scope from fastsim.solvers import RK4 print(f"fastsim {fs.__version__}") ``` -------------------------------- ### Setup System Blocks and Connections Source: https://docs.pathsim.org/pathsim/v0.25.0/notebooks/chemical_reactor.ipynb Initializes the ODE block for the reactor and defines the connections between system components and the scope. ```python # Define system blocks CSTR = ODE(reaction_rates, np.array([Ca_0, Cb_0, T_0])) Sco = Scope(labels=['Ca', 'Cb', 'T']) blocks = [CSTR, Src_Ca, Src_T, Sco] connections = [ Connection(CSTR, Sco), # Ca output Connection(CSTR[1], Sco[1]), # Cb output Connection(CSTR[2], Sco[2]), # T output Connection(Src_Ca, CSTR[0]), # Ca_in input Connection(Src_T, CSTR[1]) # T_in input ] ``` -------------------------------- ### DC Motor Model Setup Source: https://docs.pathsim.org/fastsim/v0.28.3/examples/one-script-two-engines Initializes the environment and imports necessary libraries for the DC motor PI speed control model. ```python import time import numpy as np import matplotlib.pyplot as plt # Apply the FastSim docs matplotlib style ``` -------------------------------- ### Import FastSim and dependencies for C code generation example Source: https://docs.pathsim.org/fastsim/v0.28.3/notebooks/c_code_generation.ipynb Imports numpy, matplotlib, and FastSim modules needed for the DC motor example. Applies the FastSim docs matplotlib style and prints the FastSim version. ```python import numpy as np import matplotlib.pyplot as plt # Apply the FastSim docs matplotlib style plt.style.use('../fastsim_docs.mplstyle') import fastsim as fs from fastsim import Simulation, Connection from fastsim.blocks import Source, Adder, Amplifier, Integrator, ODE, Scope from fastsim.solvers import RK4 print(f"fastsim {fs.__version__}") ``` -------------------------------- ### Import and Setup Source: https://docs.pathsim.org/pathsim/v0.25.0/examples/fmu-model-exchange-vanderpol Imports numpy, matplotlib, Path, Simulation, Connection, ModelExchangeFMU, Scope, and solvers RKDP54 and ESDIRK43. The matplotlib style is removed for browser compatibility. ```Python import numpy as np import matplotlib.pyplot as plt # Apply PathSim docs matplotlib style for consistent, theme-friendly figures # plt.style.use removed for browser from pathlib import Path from pathsim import Simulation, Connection from pathsim.blocks import ModelExchangeFMU, Scope from pathsim.solvers import RKDP54, ESDIRK43 ``` -------------------------------- ### Import PathSim classes Source: https://docs.pathsim.org/pathsim/v0.25.0/examples/fmcw-radar Initial setup to import the necessary libraries and PathSim components for the radar simulation. ```python import numpy as np import matplotlib.pyplot as plt # Apply PathSim docs matplotlib style for consistent, theme-friendly figures # plt.style.use removed for browser from pathsim import Simulation, Connection ``` -------------------------------- ### Prompt supercritical simulation setup Source: https://docs.pathsim.org/chem/v0.2.8/notebooks/point_kinetics.ipynb Sets up a simulation with a step reactivity of 0.008 (prompt supercritical) using the PointKinetics block, GEAR52A solver, and a relaxed tolerance. Runs for 0.5 seconds. ```python reactor = PointKinetics(n0=1.0) rho_prompt = 0.008 src_rho = Source(lambda t: rho_prompt if t > 0 else 0.0) src_s = Source(lambda t: 0.0) sco = Scope(labels=["n"]) sim = Simulation( [src_rho, src_s, reactor, sco], [ Connection(src_rho, reactor), Connection(src_s, reactor[1]), Connection(reactor, sco), ], dt=0.001, Solver=GEAR52A, tolerance_fpi=1e-6, log=True, ) sim.run(0.5) ``` -------------------------------- ### Flash Distillation Simulation Setup and Execution Source: https://docs.pathsim.org/chem/v0.2.8/examples/flash-distillation Defines the feed sources, scope, and simulation parameters for a flash distillation process, followed by the execution of the simulation. ```python # Feed: 10 mol/s, equimolar (z1 = 0.5), 1 atm src_f = Source(lambda t: 10.0) src_z = Source(lambda t: 0.5) src_t = Source(lambda t: 340.0 + t * 0.5) # ramp 340 -> 400 K src_p = Source(lambda t: 101325.0) sco = Scope(labels=["V_rate", "L_rate", "y_1 (benzene)", "x_1 (benzene)"]) sim = Simulation( [src_f, src_z, src_t, src_p, flash, sco], [ Connection(src_f, flash), # F Connection(src_z, flash[1]), # z_1 Connection(src_t, flash[2]), # T Connection(src_p, flash[3]), # P Connection(flash, sco), # V_rate Connection(flash[1], sco[1]), # L_rate Connection(flash[2], sco[2]), # y_1 Connection(flash[3], sco[3]), # x_1 ], dt=0.5, log=True, ) sim.run(120) ``` -------------------------------- ### Connect blocks and initialize simulation with RKCK54 Source: https://docs.pathsim.org/pathsim/v0.25.0/examples/spectrum-analysis Wire the blocks: the source feeds the filter, scope, and spectrum; the filtered output goes to the scope and spectrum second channels. Initializes the simulation with dt=1e-2, logging enabled, and the RKCK54 solver with tolerances 1e-5 (relative) and 1e-7 (absolute). The shown log output confirms setup with 4 blocks and 5 edges. ```python # The connections between the blocks connections = [ Connection(Src, FLT, Sco, Spc), Connection(FLT, Sco[1], Spc[1]) ] ``` ```python # Initialize simulation Sim = Simulation( blocks, connections, dt=1e-2, log=True, Solver=RKCK54, tolerance_lte_rel=1e-5, tolerance_lte_abs=1e-7 ) ``` ```text 18:38:08 - INFO - LOGGING (log: True) 18:38:08 - INFO - BLOCKS (total: 4, dynamic: 2, static: 2, eventful: 0) 18:38:08 - INFO - GRAPH (nodes: 4, edges: 5, alg. depth: 1, loop depth: 0, runtime: 0.072ms) ``` -------------------------------- ### Initialize simulation with RKCK54 solver Source: https://docs.pathsim.org/pathsim/v0.25.0/examples/transfer-function Initializes the PathSim Simulation with the created blocks and connections, using the RKCK54 solver (Runge-Kutta-Cash-Karp 5(4) method) for accurate integration. The log output shows the simulation setup details. ```Python # Initialize simulation Sim = Simulation(blocks, connections, dt=dt, log=True, Solver=RKCK54) ``` ```Log 18:38:16 - INFO - LOGGING (log: True) 18:38:16 - INFO - BLOCKS (total: 3, dynamic: 1, static: 2, eventful: 0) 18:38:16 - INFO - GRAPH (nodes: 3, edges: 3, alg. depth: 1, loop depth: 0, runtime: 0.056ms) ``` -------------------------------- ### Set up sources, scope, and run simulation Source: https://docs.pathsim.org/chem/v0.2.8/examples/heat-exchanger Creates Source blocks for hot (370 K) and cold (290 K) inlets, a Scope to record outlet temperatures, and a Simulation with connections. Runs the simulation for 200 seconds with dt=0.02 and logging enabled. ```python src_th = Source(lambda t: 370.0) # hot inlet [K] src_tc = Source(lambda t: 290.0) # cold inlet [K] sco = Scope(labels=["T_h_out [K]", "T_c_out [K]"]) sim = Simulation( [src_th, src_tc, hx, sco], [ Connection(src_th, hx), # T_h_in Connection(src_tc, hx[1]), # T_c_in Connection(hx, sco), # T_h_out Connection(hx[1], sco[1]), # T_c_out ], dt=0.02, log=True, ) sim.run(200) ``` ```text 15:05:58 - INFO - LOGGING (log: True) 15:05:58 - INFO - BLOCKS (total: 4, dynamic: 1, static: 3, eventful: 0) 15:05:58 - INFO - GRAPH (nodes: 4, edges: 4, alg. depth: 1, loop depth: 0, runtime: 0.185ms) 15:05:58 - INFO - STARTING -> TRANSIENT (Duration: 200.00s) 15:05:58 - INFO - -------------------- 1% | 0.1s<2.2s | 4479.6 it/s 15:05:58 - INFO - ####---------------- 20% | 0.4s<1.0s | 8005.6 it/s 15:05:58 - INFO - ########------------ 40% | 0.8s<1.1s | 5336.6 it/s 15:05:59 - INFO - ############-------- 60% | 1.1s<0.8s | 5293.4 it/s 15:05:59 - INFO - ################---- 80% | 1.5s<0.4s | 5531.5 it/s 15:05:59 - INFO - #################### 100% | 1.8s<--:-- | 6519.9 it/s 15:05:59 - INFO - FINISHED -> TRANSIENT (total steps: 10000, successful: 10000, runtime: 1765.92 ms) ``` -------------------------------- ### Import libraries and PathSim components Source: https://docs.pathsim.org/rf/v0.1.2/examples/rf-amplifier-compression Imports numpy, matplotlib, PathSim simulation components, and the RFAmplifier block. This setup is required for the subsequent simulation examples. ```python import numpy as np import matplotlib.pyplot as plt # Apply PathSim docs matplotlib style # plt.style.use removed for browser from pathsim import Simulation, Connection from pathsim.blocks import Source, Scope from pathsim.solvers import RKCK54 from pathsim_rf import RFAmplifier ``` -------------------------------- ### Initialize and Run Simulation Source: https://docs.pathsim.org/fmi/v0.1.1/examples/fmu-model-exchange-bouncing-ball Sets up the simulation with the RKBS32 solver and runs it for 4 seconds. ```python # Initialize simulation sim = Simulation( blocks, connections, dt=0.01, dt_max=0.01, Solver=RKBS32, tolerance_lte_rel=1e-6, tolerance_lte_abs=1e-9, log=True ) # Run simulation sim.run(4.0) ``` -------------------------------- ### Simulation setup with CellElectrothermal Source: https://docs.pathsim.org/batt/v0.2.2/examples/01-spme-discharge Imports required libraries and blocks for the simulation. The code is part of a Python example that requires external dependencies and cannot be run in the browser. ```python import matplotlib.pyplot as plt from pathsim import Simulation, Connection from pathsim.blocks import Constant, Scope from pathsim.solvers import ESDIRK43 from pathsim_batt import CellElectrothermal ``` -------------------------------- ### Install PathSim FMI Source: https://docs.pathsim.org/fmi Install the required package via pip. ```bash pip install pathsim-fmi==0.1.1 ``` -------------------------------- ### Initialize and run simulation with RKBS32 solver Source: https://docs.pathsim.org/pathsim/v0.25.0/examples/bouncing-ball Sets up the Simulation with blocks, connections, events, dt=0.01, dt_max=0.04, logging enabled, and RKBS32 solver with LTE tolerances. Runs for 10 seconds and prints progress logs. ```python from pathsim.solvers import RKBS32 # Initialize simulation Sim = Simulation( blocks, connections, events, dt=0.01, dt_max=0.04, log=True, Solver=RKBS32, tolerance_lte_rel=1e-5, tolerance_lte_abs=1e-7 ) # Run the simulation Sim.run(10) ``` -------------------------------- ### Configure and Run Simulation Source: https://docs.pathsim.org/pathsim/v0.25.0/examples/lorenz-attractor Initializes the simulation with an adaptive RKBS32 solver and executes it for 50 seconds. ```python Sim = Simulation( blocks, connections, Solver=RKBS32, tolerance_lte_rel=1e-4, tolerance_lte_abs=1e-6, tolerance_fpi=1e-6 ) # Run the simulation Sim.run(50) ``` ```text 18:37:48 - INFO - LOGGING (log: True) 18:37:48 - INFO - BLOCKS (total: 13, dynamic: 3, static: 10, eventful: 0) 18:37:48 - INFO - GRAPH (nodes: 13, edges: 20, alg. depth: 4, loop depth: 0, runtime: 0.145ms) 18:37:48 - INFO - STARTING -> TRANSIENT (Duration: 50.00s) 18:37:49 - INFO - -------------------- 1% | 0.1s<6.5s | 896.1 it/s 18:37:49 - INFO - ####---------------- 20% | 0.6s<2.1s | 1044.7 it/s 18:37:50 - INFO - ########------------ 40% | 1.5s<1.8s | 1047.2 it/s 18:37:51 - INFO - ############-------- 60% | 2.5s<2.3s | 1046.4 it/s 18:37:52 - INFO - ###############----- 78% | 3.5s<2.7s | 1064.5 it/s 18:37:52 - INFO - ################---- 80% | 3.6s<0.8s | 1051.6 it/s 18:37:53 - INFO - #################### 100% | 4.5s<--:-- | 1054.1 it/s 18:37:53 - INFO - FINISHED -> TRANSIENT (total steps: 4669, successful: 4057, runtime: 4539.55 ms) ``` -------------------------------- ### Install FMPy Source: https://docs.pathsim.org/fmi/v0.1.1/examples/fmu-model-exchange-bouncing-ball Install the FMPy package, required for using FMU blocks in PathSim. ```bash pip install fmpy ``` -------------------------------- ### Install PathSim via conda Source: https://docs.pathsim.org/pathsim Install the specific version 0.25.0 of the PathSim package from the conda-forge channel. ```bash conda install -c conda-forge pathsim=0.25.0 ``` -------------------------------- ### Set up Simulation with RKCK54 solver Source: https://docs.pathsim.org/pathsim/v0.25.0/examples/bouncing-pendulum Initializes a Simulation instance with the blocks, connections, events, a time step of 0.1, logging enabled, and the RKCK54 solver with absolute and relative tolerances. The output shows the logging information about blocks, graph, and runtime. ```Python # Simulation instance from the blocks and connections Sim = Simulation( blocks, connections, events, dt=0.1, log=True, Solver=RKCK54, tolerance_lte_abs=1e-8, tolerance_lte_rel=1e-6 ) ``` ```Output 18:37:15 - INFO - LOGGING (log: True) 18:37:15 - INFO - BLOCKS (total: 5, dynamic: 2, static: 3, eventful: 0) 18:37:15 - INFO - GRAPH (nodes: 5, edges: 6, alg. depth: 3, loop depth: 0, runtime: 0.078ms) ``` -------------------------------- ### Install PathSim via pip Source: https://docs.pathsim.org/pathsim Install the specific version 0.25.0 of the PathSim package using pip. ```bash pip install pathsim==0.25.0 ``` -------------------------------- ### Reject unknown start value name Source: https://docs.pathsim.org/fastsim/v0.28.3/examples/fmi-import Attempts to create a ModelExchangeFMU with an unknown start value name and catches the resulting ValueError, printing the error message. ```python try: ModelExchangeFMU(fmu("Dahlquist"), start_values={"not_a_variable": 1.0}) except ValueError as e: print(f"ValueError: {e}") ``` -------------------------------- ### Initialize and run simulation with RKBS32 solver Source: https://docs.pathsim.org/pathsim/v0.25.0/notebooks/bouncing_ball.ipynb Sets up the Simulation with the blocks, connections, and events, using the RKBS32 adaptive solver. The simulation runs for 10 seconds with specified tolerances and logging enabled. ```python from pathsim.solvers import RKBS32 # Initialize simulation Sim = Simulation( blocks, connections, events, dt=0.01, dt_max=0.04, log=True, Solver=RKBS32, tolerance_lte_rel=1e-5, tolerance_lte_abs=1e-7 ) # Run the simulation Sim.run(10) ``` -------------------------------- ### Import NumPy and Matplotlib for standalone JIT example Source: https://docs.pathsim.org/fastsim/v0.28.3/examples/standalone-jit Imports required for the standalone JIT example, which compiles a hand-written NumPy function to native Rust and computes its exact Jacobian. ```python import numpy as np import matplotlib.pyplot as plt ``` -------------------------------- ### Simulate with RKBS32 Solver Source: https://docs.pathsim.org/pathsim/v0.25.0/examples/delta-sigma-adc Sets up the simulation with the blocks and connections, using the RKBS32 adaptive solver with a maximum timestep equal to 10% of the sampling period. Runs for 2 seconds and shows logging with throughput and step counts. ```python # Simulation with adaptive solver Sim = Simulation( blocks, connections, dt_max=T_clk*0.1, Solver=RKBS32 ) # Run simulation for 2 seconds Sim.run(2) ``` -------------------------------- ### Override start value for Dahlquist FMU Source: https://docs.pathsim.org/fastsim/v0.28.3/examples/fmi-import Creates a ModelExchangeFMU with a start value for the decay rate k set to 3.0, runs the simulation, and prints the worst error against exp(-3t). ```python f3 = ModelExchangeFMU(fmu("Dahlquist"), start_values={"k": 3.0}) sco3 = Scope(labels=["x"], sampling_period=1e-2) sim3 = Simulation([f3, sco3], [Connection(f3, sco3)], Solver=RKCK54, dt=1e-2, log=False) sim3.run(5.0) t3, [x3] = sco3.read() print(f"k=3: worst |x - exp(-3t)| = {np.max(np.abs(x3 - np.exp(-3 * t3))):.3e}") ``` -------------------------------- ### Initialize and Run Simulation Source: https://docs.pathsim.org/pathsim/v0.25.0/examples/nested-subsystems Configures the simulation with a stiff solver and executes it. ```python # Initialize simulation Sim = Simulation( blocks, connections, dt=dt, log=True, Solver=ESDIRK43, tolerance_lte_abs=1e-6, tolerance_lte_rel=1e-4, tolerance_fpi=1e-7 ) # Run simulation (note: 2*mu for long-term dynamics) Sim.run(2*mu) ``` ```text 18:37:49 - INFO - LOGGING (log: True) 18:37:49 - INFO - BLOCKS (total: 2, dynamic: 1, static: 1, eventful: 0) 18:37:49 - INFO - GRAPH (nodes: 2, edges: 1, alg. depth: 1, loop depth: 0, runtime: 0.024ms) 18:37:49 - INFO - STARTING -> TRANSIENT (Duration: 2000.00s) 18:37:49 - INFO - -------------------- 1% | 0.8s<20.1s | 24.3 it/s 18:37:50 - INFO - #####--------------- 27% | 1.2s<1.8s | 15.6 it/s 18:37:51 - INFO - ########------------ 40% | 2.0s<8.5s | 28.4 it/s 18:37:53 - INFO - ########------------ 41% | 4.8s<19.8s | 32.1 it/s 18:37:54 - INFO - #############------- 67% | 5.5s<1.0s | 14.1 it/s 18:37:55 - INFO - ################---- 80% | 6.3s<3.4s | 15.9 it/s 18:37:57 - INFO - ################---- 81% | 9.0s<9.7s | 47.2 it/s 18:37:58 - INFO - #################### 100% | 9.4s<--:-- | 18.6 it/s 18:37:58 - INFO - FINISHED -> TRANSIENT (total steps: 341, successful: 234, runtime: 9390.20 ms) ``` -------------------------------- ### Inspect generated C header interface Source: https://docs.pathsim.org/fastsim/v0.28.3/examples/c-code-generation Prints the beginning of the generated `motor.h` header file starting from the `typedef struct` definition, and lists all function declarations that start with `void motor_`, `double motor_`, or `int motor_`. ```Python header = files["motor.h"] start = header.index("typedef struct") print(header[start:start + 900]) ``` ```Python for line in files["motor.h"].splitlines(): if line.startswith(("void motor_", "double motor_", "int motor_")): print(line) ``` -------------------------------- ### Instantiate the simulation Source: https://docs.pathsim.org/pathsim/v0.25.0/examples/thermostat Configure the simulation using the defined blocks, connections, events, and an adaptive RKBS32 integrator. ```python sim = Simulation( blocks=blocks, connections=connections, events=events, integrator=RKBS32(dt=0.01) ) sim.run(t_end=10.0) ``` -------------------------------- ### Run simulation with sources and scope Source: https://docs.pathsim.org/chem/v0.2.8/notebooks/heat_exchanger.ipynb Sets up the simulation with constant temperature sources for inlets and runs the transient analysis for 200 time steps. ```python src_th = Source(lambda t: 370.0) # hot inlet [K] src_tc = Source(lambda t: 290.0) # cold inlet [K] sco = Scope(labels=["T_h_out [K]", "T_c_out [K]"]) sim = Simulation( [src_th, src_tc, hx, sco], [ Connection(src_th, hx), # T_h_in Connection(src_tc, hx[1]), # T_c_in Connection(hx, sco), # T_h_out Connection(hx[1], sco[1]), # T_c_out ], dt=0.02, log=True, ) sim.run(200) ``` -------------------------------- ### Initialize and Run Simulation Source: https://docs.pathsim.org/pathsim/v0.25.0/notebooks/nested_subsystems.ipynb Configures the simulation using the ESDIRK43 stiff solver and executes it for the specified duration. ```python # Initialize simulation Sim = Simulation( blocks, connections, dt=dt, log=True, Solver=ESDIRK43, tolerance_lte_abs=1e-6, tolerance_lte_rel=1e-4, tolerance_fpi=1e-7 ) # Run simulation (note: 2*mu for long-term dynamics) Sim.run(2*mu) ``` -------------------------------- ### Set up simulation with temperature ramp Source: https://docs.pathsim.org/chem/v0.2.8/notebooks/vapor_pressure_curves.ipynb Creates a Source block generating a linear temperature ramp from 300 K to 500 K over 200 seconds, connects it to all three correlation blocks in parallel, and uses a Scope to record the three pressure outputs. Runs the simulation for 200 seconds. ```python # Temperature ramp: 300 K to 500 K over 200 seconds src_t = Source(lambda t: 300 + t) # Scope to record all three vapor pressures sco = Scope(labels=["Antoine", "Kirchhoff", "Wagner"]) sim = Simulation( [src_t, antoine, kirchhoff, wagner, sco], [ Connection(src_t, antoine, kirchhoff, wagner), Connection(antoine, sco), Connection(kirchhoff, sco[1]), Connection(wagner, sco[2]), ], dt=1.0, log=True, ) sim.run(200) ``` -------------------------------- ### Verify Orbit Periodicity Source: https://docs.pathsim.org/fastsim/v0.28.3/examples/periodic-steady-state Checks the closure residual to verify that the state at the end of the period matches the start. ```python start = np.array([x_lc[0], v_lc[0]]) end = np.array([x_lc[-1], v_lc[-1]]) print(f"x(0), v(0) = {start}") print(f"x(T), v(T) = {end}") print(f"closure residual ||x(T) - x(0)|| = {np.linalg.norm(end - start):.3e}") ``` -------------------------------- ### Import required modules Source: https://docs.pathsim.org/pathsim/v0.25.0/examples/cascade-controller Imports numpy, matplotlib, and PathSim classes needed for the cascade controller example. ```python import numpy as np import matplotlib.pyplot as plt # Apply PathSim docs matplotlib style # plt.style.use removed for browser from pathsim import Simulation, Connection, Subsystem, Interface from pathsim.blocks import Source, TransferFunctionZPG, Adder, Scope, PID, WhiteNoise from pathsim.solvers import RKCK54 ``` -------------------------------- ### Create simulation with connections and run Source: https://docs.pathsim.org/pathsim/v0.25.0/examples/rf-network-oneport Creates the PathSim simulation with the source, RF network, and spectrum blocks, and defines connections. Uses RKBS32 solver with tight tolerances due to super tiny states. Runs the simulation for 1e-9 seconds. ```python # create the system connections and simulation setup sim = Simulation( blocks=[src, rfntwk, spc], connections=[ Connection(src, rfntwk, spc[0]), Connection(rfntwk, spc[1]) ], tolerance_lte_abs=1e-16, # this is due to the super tiny states tolerance_lte_rel=1e-5, # so error control is dominated by the relative truncation error Solver=RKBS32, ) sim.run(1e-9) ``` -------------------------------- ### Create and run Simulation with RKBS32 solver Source: https://docs.pathsim.org/pathsim/v0.25.0/examples/stick-slip Instantiate a Simulation with blocks, connections, events, and solver parameters (dt=0.01, dt_max=0.1, log=True, Solver=RKBS32, tolerance_lte_abs=1e-6, tolerance_lte_rel=1e-4), then run for 2*T. The log output shows transient progress with total steps and runtime. ```python # Create a simulation instance from the blocks and connections Sim = Simulation( blocks, connections, events, dt=0.01, dt_max=0.1, log=True, Solver=RKBS32, tolerance_lte_abs=1e-6, tolerance_lte_rel=1e-4 ) # Run the simulation for some time Sim.run(2*T) ``` ```text 18:38:10 - INFO - LOGGING (log: True) 18:38:10 - INFO - BLOCKS (total: 11, dynamic: 2, static: 9, eventful: 0) 18:38:10 - INFO - GRAPH (nodes: 11, edges: 17, alg. depth: 5, loop depth: 0, runtime: 0.118ms) 18:38:10 - INFO - STARTING -> TRANSIENT (Duration: 100.00s) 18:38:10 - INFO - -------------------- 1% | 0.0s<0.5s | 1981.6 it/s 18:38:11 - INFO - ####---------------- 20% | 0.2s<0.5s | 1497.0 it/s 18:38:11 - INFO - ########------------ 40% | 0.5s<0.5s | 1572.4 it/s 18:38:11 - INFO - ############-------- 60% | 0.8s<0.3s | 1514.2 it/s 18:38:12 - INFO - ###############----- 75% | 1.8s<31:53:33 | 1478.4 it/s 18:38:13 - INFO - ###############----- 76% | 2.9s<0.1s | 1839.2 it/s 18:38:13 - INFO - ################---- 80% | 2.9s<0.1s | 1828.5 it/s 18:38:13 - INFO - #################### 100% | 3.1s<--:-- | 1405.7 it/s 18:38:13 - INFO - FINISHED -> TRANSIENT (total steps: 5055, successful: 2409, runtime: 3116.29 ms) ``` -------------------------------- ### Run Simulation Source: https://docs.pathsim.org/pathsim/v0.25.0/notebooks/sar_adc.ipynb Initialize the simulation using an adaptive solver. ```python # Simulation with adaptive solver Sim = Simulation( blocks, connections, ``` -------------------------------- ### Run Model Exchange FMU in PathSim Source: https://docs.pathsim.org/fmi Example of integrating a Model Exchange FMU into a PathSim simulation with a Scope block. ```python from pathsim import Simulation, Connection from pathsim.blocks import Scope from pathsim_fmi import ModelExchangeFMU fmu = ModelExchangeFMU("VanDerPol.fmu") sco = Scope(labels=["x0", "x1"], sampling_period=0.02) sim = Simulation( blocks=[fmu, sco], connections=[ Connection(fmu[0], sco[0]), Connection(fmu[1], sco[1]), ], dt=0.01, ) sim.run(5.0) sco.plot() ``` -------------------------------- ### Configure CSTR parameters Source: https://docs.pathsim.org/chem/v0.2.8/examples/cstr-reaction Initializes the CSTR reactor with specific physical and kinetic parameters, starting from an empty state at ambient temperature. ```python reactor = CSTR( V=1.0, # reactor volume [m³] F=0.05, # volumetric flow rate [m³/s] -> tau = 20 s k0=1e6, # pre-exponential factor [1/s] Ea=40000.0, # activation energy [J/mol] n=1.0, # first-order reaction dH_rxn=-40000.0, # exothermic [J/mol] rho=1000.0, # density [kg/m³] Cp=4184.0, # heat capacity [J/(kg·K)] UA=800.0, # cooling jacket [W/K] C_A0=0.0, # start empty T0=300.0, # initial temperature [K] ) ``` -------------------------------- ### Initialize Kalman Filter and Simulation Blocks Source: https://docs.pathsim.org/pathsim/v0.25.0/notebooks/kalman_filter.ipynb Sets up the WhiteNoise, KalmanFilter, and Scope blocks required for the simulation. ```python # Noisy measurement (spectral_density must be scaled by dt for discrete-time white noise) noise = WhiteNoise(spectral_density=measurement_std**2 * dt) measured_pos = Adder() # Kalman filter kf = KalmanFilter(F, H, Q, R, x0=x0_kf, P0=P0_kf) # Scopes for recording sc_true = Scope(labels=["true position", "true velocity"]) sc_meas = Scope(labels=["measured position"]) sc_est = Scope(labels=["estimated position", "estimated velocity"]) blocks = [vel, pos, noise, measured_pos, kf, sc_true, sc_meas, sc_est] ``` -------------------------------- ### Import PathSim and plotting libraries Source: https://docs.pathsim.org/pathsim/v0.25.0/examples/algebraic-loop Imports numpy, matplotlib, and PathSim modules needed for the example. The matplotlib style is not applied in the browser. ```Python import numpy as np import matplotlib.pyplot as plt # Apply PathSim docs matplotlib style for consistent, theme-friendly figures # plt.style.use removed for browser from pathsim import Simulation, Connection from pathsim.blocks import Source, Amplifier, Adder, Scope ``` -------------------------------- ### Instantiate system blocks Source: https://docs.pathsim.org/pathsim/v0.25.0/notebooks/harmonic_oscillator.ipynb Creates the required Integrator, Amplifier, Adder, and Scope blocks with specified parameters and collects them into a list. ```python # Blocks that define the system I1 = Integrator(v0) # integrator for velocity I2 = Integrator(x0) # integrator for position A1 = Amplifier(c) A2 = Amplifier(k) A3 = Amplifier(-1/m) P1 = Adder() Sc = Scope(labels=["velocity", "position"]) blocks = [I1, I2, A1, A2, A3, P1, Sc] ``` -------------------------------- ### Set up PathSim simulation with temperature ramp and scope Source: https://docs.pathsim.org/chem/v0.2.8/examples/vapor-pressure-curves Creates a Source block generating a linear temperature ramp from 300 K to 500 K over 200 seconds, a Scope to record the three pressure outputs, and a Simulation connecting the source to all correlations in parallel. Runs the simulation for 200 seconds with a time step of 1.0 second. ```python # Temperature ramp: 300 K to 500 K over 200 seconds src_t = Source(lambda t: 300 + t) # Scope to record all three vapor pressures sco = Scope(labels=["Antoine", "Kirchhoff", "Wagner"]) sim = Simulation( [src_t, antoine, kirchhoff, wagner, sco], [ Connection(src_t, antoine, kirchhoff, wagner), Connection(antoine, sco), Connection(kirchhoff, sco[1]), Connection(wagner, sco[2]), ], dt=1.0, log=True, ) sim.run(200) ``` -------------------------------- ### Importing dependencies for the Duffing oscillator simulation Source: https://docs.pathsim.org/fastsim/v0.28.3/examples/periodic-steady-state Initial imports required for the Duffing oscillator example, including time, numpy, and matplotlib. ```python import time import numpy as np import matplotlib.pyplot as plt ``` -------------------------------- ### Run Simulation Source: https://docs.pathsim.org/pathsim/v0.25.0/notebooks/kalman_filter.ipynb Initializes the simulation with the defined blocks and connections, then executes it for 20 seconds. ```python # Initialize simulation Sim = Simulation( blocks, connections, dt=dt, ) # Run the simulation for 20 seconds Sim.run(duration=20) ``` -------------------------------- ### Import PathSim and RFNetwork libraries Source: https://docs.pathsim.org/rf/v0.1.2/examples/rf-network-oneport Import the required libraries for the RF network simulation. The RFNetwork block requires the scikit-rf package to be installed. ```python import matplotlib.pyplot as plt # Apply PathSim docs matplotlib style for consistent, theme-friendly figures # plt.style.use removed for browser from pathsim import Simulation, Connection from pathsim.blocks import Spectrum, GaussianPulseSource from pathsim.solvers import RKBS32 from pathsim_rf import RFNetwork # requires the scikit-rf package to be installed ``` -------------------------------- ### Configure System Components and Connections Source: https://docs.pathsim.org/pathsim/v0.25.0/notebooks/noisy_amplifier.ipynb Initializes the sinusoidal source, scope, and spectrum analyzer, then defines the system blocks and connections. ```python Src = SinusoidalSource(frequency=2e5, amplitude=1) Sco = Scope(labels=["Src", "AMP"]) Spc = Spectrum(freq=np.logspace(4.1, 7, 250), labels=["Src", "AMP"]) blocks = [AMP, Src, Sco, Spc] connections = [ Connection(Src, AMP, Sco[0], Spc[0]), Connection(AMP, Sco[1], Spc[1]) ] ``` -------------------------------- ### Steady-state summary output Source: https://docs.pathsim.org/chem/v0.2.8/examples/process-flowsheet Example output from the steady-state summary print script, showing the unit flows and temperatures, plus the reactor concentration and conversion. ```text Process Steady State ============================================= Unit Flow [m³/s] T [K] --------------------------------------------- Fresh feed 0.05 300.0 Second stream 0.05 310.0 Mixer outlet 0.1000 305.00 Heater outlet 0.1000 305.48 CSTR outlet — 305.46 Reactor: C_A = 1.9452 mol/m³ (feed = 2.0 mol/m³) Conversion = 2.7% ``` -------------------------------- ### Instantiate and run the Simulation Source: https://docs.pathsim.org/pathsim/v0.25.0/notebooks/stick_slip.ipynb Creates a Simulation instance using the RKBS32 adaptive timestep ODE solver and runs it for a specified duration. ```python # Create a simulation instance from the blocks and connections Sim = Simulation( blocks, connections, events, dt=0.01, dt_max=0.1, log=True, Solver=RKBS32, tolerance_lte_abs=1e-6, tolerance_lte_rel=1e-4 ) # Run the simulation for some time Sim.run(2*T) ``` -------------------------------- ### Create FMU Block and Scope Source: https://docs.pathsim.org/fmi/v0.1.1/examples/fmu-model-exchange-bouncing-ball Creates the ModelExchangeFMU block with start values and tolerance, and a Scope to record height and velocity. Connects FMU outputs to the scope. ```python # Create the Model Exchange FMU block fmu = ModelExchangeFMU( fmu_path=str(fmu_path), instance_name="bouncing_ball", start_values={"e": 0.7}, # coefficient of restitution tolerance=1e-10, verbose=False ) # Scope to record height and velocity sco = Scope(labels=["h [m]", "v [m/s]"]) blocks = [fmu, sco] # Connect FMU outputs to scope connections = [ Connection(fmu[0], sco[0]), # height Connection(fmu[1], sco[1]), # velocity ] ``` -------------------------------- ### Configure and run the stiff system simulation Source: https://docs.pathsim.org/fmi/v0.1.1/notebooks/fmu_model_exchange_vanderpol.ipynb Sets up a simulation for a stiff system using the ESDIRK43 solver with specific tolerance settings and runs it for 1500 seconds. ```python verbose=False ) sco_stiff = Scope(labels=[r"$x_0$", r"$x_1$"]) sim_stiff = Simulation( [fmu_stiff, sco_stiff], [Connection(fmu_stiff[0], sco_stiff[0]), Connection(fmu_stiff[1], sco_stiff[1])], dt=0.1, Solver=ESDIRK43, # implicit solver for stiff systems tolerance_lte_rel=1e-4, tolerance_lte_abs=1e-6, tolerance_fpi=1e-8, log=True ) sim_stiff.run(1500.0) ``` -------------------------------- ### Import numpy and matplotlib for chemical reactor simulation Source: https://docs.pathsim.org/pathsim/v0.25.0/examples/chemical-reactor Initializes the Python environment for the chemical reactor example by importing numpy for numerical operations and matplotlib.pyplot for plotting. ```python import numpy as np import matplotlib.pyplot as plt ``` -------------------------------- ### Import and Setup for PID Controller Simulation Source: https://docs.pathsim.org/pathsim/v0.25.0/notebooks/pid_controller.ipynb Imports required libraries and PathSim classes for the PID controller simulation. Applies the PathSim documentation matplotlib style. ```python import numpy as np import matplotlib.pyplot as plt # Apply PathSim docs matplotlib style plt.style.use('../pathsim_docs.mplstyle') from pathsim import Simulation, Connection from pathsim.blocks import Source, Integrator, Amplifier, Adder, Scope, PID from pathsim.solvers import RKCK54 ``` -------------------------------- ### Configure and run the flash drum simulation Source: https://docs.pathsim.org/chem/v0.2.8/notebooks/multicomponent_flash.ipynb Sets up the MultiComponentFlash block with BTX defaults, defines feed sources, connects the simulation components, and runs the simulation for 80 steps. ```python # Create the flash drum (3 components, BTX defaults) flash = MultiComponentFlash(N_comp=3, holdup=100.0) # Feed sources src_f = Source(lambda t: 10.0) # 10 mol/s src_z1 = Source(lambda t: 0.5) # 50% benzene src_z2 = Source(lambda t: 0.1) # 10% toluene (p-xylene = 40% inferred) src_t = Source(lambda t: 340.0 + t) # ramp 340 -> 420 K src_p = Source(lambda t: 101325.0) # 1 atm # Record all outputs: V_rate, L_rate, y_benzene, y_toluene, x_benzene, x_toluene sco = Scope(labels=["V_rate", "L_rate", "y_benz", "y_tol", "x_benz", "x_tol"]) sim = Simulation( [src_f, src_z1, src_z2, src_t, src_p, flash, sco], [ Connection(src_f, flash), # F Connection(src_z1, flash[1]), # z_1 (benzene) Connection(src_z2, flash[2]), # z_2 (toluene) Connection(src_t, flash[3]), # T Connection(src_p, flash[4]), # P Connection(flash, sco), # V_rate Connection(flash[1], sco[1]), # L_rate Connection(flash[2], sco[2]), # y_benzene Connection(flash[3], sco[3]), # y_toluene Connection(flash[4], sco[4]), # x_benzene Connection(flash[5], sco[5]), # x_toluene ], dt=0.5, log=True, ) sim.run(80) ``` -------------------------------- ### Create Simulation Instance with RKBS32 Solver Source: https://docs.pathsim.org/pathsim/v0.25.0/examples/elastic-pendulum Initializes a Simulation object with the provided blocks and connections, using the RKBS32 solver. The log output shows 5 blocks (2 dynamic, 3 static, 1 eventful) and a graph with 5 nodes and 9 edges. ```Python # Simulation instance sim = Simulation( blocks, connections, Solver=RKBS32 ) ``` ```Output 18:37:34 - INFO - LOGGING (log: True) 18:37:34 - INFO - BLOCKS (total: 5, dynamic: 2, static: 3, eventful: 1) 18:37:34 - INFO - GRAPH (nodes: 5, edges: 9, alg. depth: 2, loop depth: 0, runtime: 0.072ms) ``` -------------------------------- ### Set up feed, coolant, and simulation Source: https://docs.pathsim.org/chem/v0.2.8/notebooks/cstr_reaction.ipynb Defines constant feed and coolant conditions, creates a Scope to record outlet concentration and temperature, and runs the simulation for 200 seconds with a time step of 0.1. ```python # Constant feed and coolant conditions src_c = Source(lambda t: 1000.0) # feed concentration [mol/m³] src_t = Source(lambda t: 320.0) # feed temperature [K] src_tc = Source(lambda t: 290.0) # coolant temperature [K] sco = Scope(labels=["C_A [mol/m³]", "T [K]"]) sim = Simulation( [src_c, src_t, src_tc, reactor, sco], [ Connection(src_c, reactor), # C_in Connection(src_t, reactor[1]), # T_in Connection(src_tc, reactor[2]), # T_c Connection(reactor, sco), # C_out Connection(reactor[1], sco[1]), # T_out ], dt=0.1, log=True, ) sim.run(200) ``` -------------------------------- ### Configure CSTR reactor parameters Source: https://docs.pathsim.org/chem/v0.2.8/notebooks/cstr_reaction.ipynb Creates a CSTR block with parameters for an exothermic liquid-phase reaction. The reactor starts empty at ambient temperature and is fed with a concentrated stream. ```python reactor = CSTR( V=1.0, # reactor volume [m³] F=0.05, # volumetric flow rate [m³/s] -> tau = 20 s k0=1e6, # pre-exponential factor [1/s] Ea=40000.0, # activation energy [J/mol] n=1.0, # first-order reaction dH_rxn=-40000.0, # exothermic [J/mol] rho=1000.0, # density [kg/m³] Cp=4184.0, # heat capacity [J/(kg·K)] UA=800.0, # cooling jacket [W/K] C_A0=0.0, # start empty T0=300.0, # initial temperature [K] ) ```