Simulator¶
The Simulator class is the central orchestrator of OpenSMC. It manages the interaction between the plant, the controller, and the optional estimator to perform closed-loop simulations using fixed-step solvers like RK4 or Euler.
Usage Example¶
from opensmc import Simulator
from opensmc.plants import DoubleIntegrator
from opensmc.controllers import ClassicalSMC
# Setup
plant = DoubleIntegrator()
controller = ClassicalSMC() # Using default surface and reaching law
# Create simulator with time parameters
sim = Simulator(dt=0.001, T=5.0, method='rk4')
# Run simulation — returns dict with 't', 'x', 'u', 's', 'ref'
ref_fn = lambda t: [0.0, 0.0]
result = sim.run(controller, plant, ref_fn=ref_fn)
API Reference¶
simulator
¶
OpenSMC Simulator — Fixed-step RK4/Euler closed-loop integration engine.
Classes¶
Simulator
¶
Fixed-step closed-loop simulator.
Parameters¶
dt : float — integration time step (seconds) T : float — total simulation time (seconds) method : str — 'rk4' or 'euler'
Usage¶
sim = Simulator(dt=1e-4, T=10.0) result = sim.run(controller, plant, ref_fn, dist_fn)
Source code in opensmc/simulator.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
Functions¶
run(controller, plant, ref_fn=None, dist_fn=None, x0=None)
¶
Run closed-loop simulation.
Parameters¶
controller : Controller — computes u from (t, x, xref, plant) plant : Plant — dynamics ref_fn : callable(t) -> ndarray or None — reference signal dist_fn : callable(t) -> ndarray or None — disturbance signal x0 : ndarray or None — initial state (uses plant default if None)
Returns¶
result : dict with keys: t : ndarray (N,) — time array x : ndarray (N, n_states) — state trajectory u : ndarray (N, n_inputs) — control history xref : ndarray (N, n_states) — reference history e : ndarray (N, n_states) — error history s : ndarray (N,) or (N, n_s) — sliding variable history
Source code in opensmc/simulator.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
Functions¶
euler_step(f, t, x, dt, *args)
¶
rk4_step(f, t, x, dt, *args)
¶
Single RK4 integration step for dx/dt = f(t, x, *args).