Skip to main content

simulate_closed_loop

Function simulate_closed_loop 

Source
pub fn simulate_closed_loop(
    source: &str,
    dialect: Dialect,
    diodes: &BTreeMap<String, IdealDiode>,
    ideal_switches: &BTreeMap<String, IdealSwitch>,
    controller: &StateSpace,
    reference: impl Fn(f64) -> f64,
    measure: impl Fn(&OperatingPoint) -> f64,
    pwm: impl Fn(f64, f64) -> BTreeMap<String, GateState>,
    output_clamp: (f64, f64),
    shared_r_on: f64,
    x_initial: Option<&[f64]>,
    t_final: f64,
    dt: f64,
) -> Result<Vec<(f64, OperatingPoint, f64)>, DaeError>
Expand description

Runs a closed-loop transient: at each timestep, measure reads a scalar from the previous step’s OperatingPoint (e.g. an output node voltage), controller (its own state carried forward internally) is stepped with reference(t) - measured as its input via RK4, and pwm(controller_output, t) decides every ideal switch’s gate state for this step from that controller output. Everything else matches crate::simulate_transient_with_ideal_switches (system rebuilt per step, trapezoidal with backward-Euler fallback on any diode-segment or gate-state change).

reference is a function of time rather than a fixed value so a reference-step robustness test (settle at one setpoint, then step to another mid-run) is just move |t| if t < t_step { v1 } else { v2 } — no separate code path needed. Pass move |_| v for a fixed setpoint.

output_clamp = (lo, hi) is the controller output’s valid (unsaturated) range — typically (0.0, 1.0) for a duty command. Two-sided conditional-integration anti-windup is applied against it: if a tentative controller step would push the output further past lo or hi while the current error is still driving it that direction, the controller’s internal state is not advanced that step (frozen at its previous value) rather than integrating further into a saturation it may struggle to recover from. This directly addresses a documented real failure mode this project validated against: an internal Xyce/ngspice closed-loop boost-PI benchmark (its own gotcha note on nonlinear integrator-windup failure) used one-sided anti-windup and, per that benchmark’s own conclusion, never actually achieved working closed-loop regulation — the integrator wound down past the point of recovery during startup overshoot, the PWM output floored at zero, and the converter stopped switching for the rest of the run, with the misleadingly-plausible final voltage reading being nothing more than the output capacitor passively discharging through the load. Symmetric (two-sided) anti-windup is the standard, well-known fix for exactly this failure mode.

Returns, per step: (t, OperatingPoint, controller_output).

Examples found in repository?
examples/boost_pid_closed_loop.rs (lines 47-61)
16fn main() {
17    let netlist = "V1 vin 0 12\nD1 vx 0 idealswitchmodel\nD2 vx vout dmodel\nL1 vin vx 100u\nC1 vout 0 100u\nR1 vout 0 50";
18    let switch = IdealSwitch::new(0.01, IdealDiode::new(0.0, -1e6, 0.0, 1e6, 0.0));
19    let mut ideal_switches = BTreeMap::new();
20    ideal_switches.insert("D1".to_string(), switch);
21    let mut diodes = BTreeMap::new();
22    diodes.insert(
23        "D2".to_string(),
24        IdealDiode::new(0.0, -100.0, 0.0, 0.6, 100.0),
25    );
26
27    let reference = 24.0;
28    let pid = Pid::new(0.01, 20.0, 0.0, 1000.0).unwrap();
29    let controller = pid.to_state_space();
30
31    let switching_freq = 100_000.0;
32    let measure = |point: &dae_runtime::OperatingPoint| point.value("V(vout)").unwrap();
33    let pwm = move |duty: f64, t: f64| {
34        let duty = duty.clamp(0.0, 1.0);
35        let carrier = sawtooth_carrier(t, switching_freq);
36        let state = if carrier < duty {
37            GateState::On
38        } else {
39            GateState::Off
40        };
41        BTreeMap::from([("D1".to_string(), state)])
42    };
43
44    let dt = 1e-7;
45    let t_final = 0.010;
46
47    let trace = simulate_closed_loop(
48        netlist,
49        Dialect::Ngspice,
50        &diodes,
51        &ideal_switches,
52        &controller,
53        move |_| reference,
54        measure,
55        pwm,
56        (0.0, 1.0),
57        0.01,
58        None,
59        t_final,
60        dt,
61    )
62    .unwrap();
63
64    let out_path = "boost_pid_elspice_pwl_out.csv";
65    let mut f = std::fs::File::create(out_path).unwrap();
66    writeln!(f, "t,V(vout),duty").unwrap();
67    for (t, point, duty) in &trace {
68        writeln!(f, "{t},{},{}", point.value("V(vout)").unwrap(), duty).unwrap();
69    }
70    println!("wrote {out_path}");
71}