oxictl 0.1.0

Pure Rust Real-Time Control Systems Framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//! Voltage Source Inverter (VSI) with LCL filter model.
//!
//! Implements a three-phase VSI plant (LCL filter dynamics) and a d/q current
//! controller with cross-coupling feed-forward compensation.
//!
//! ## LCL Filter State-Space
//!
//! States: x = [iL1_α, iL1_β, vc_α, vc_β, iL2_α, iL2_β]
//!
//! Converter-side inductance L1 (with parasitic resistance Rf):
//!   L1·diL1/dt = v_inv − Rf·iL1 − vc
//!
//! Filter capacitor C:
//!   C·dvc/dt = iL1 − iL2
//!
//! Grid-side inductance L2:
//!   L2·diL2/dt = vc − v_grid
//!
//! The αβ axes are decoupled in the stationary frame, so α and β evolve
//! identically under their respective excitation signals.

use crate::core::scalar::ControlScalar;

/// Configuration for the VSI with LCL filter.
#[derive(Debug, Clone, Copy)]
pub struct VsiConfig<S: ControlScalar> {
    /// Converter-side inductance (H).
    pub l1: S,
    /// Filter capacitance (F).
    pub c: S,
    /// Grid-side inductance (H).
    pub l2: S,
    /// Filter resistance (Ω) — models winding and damping losses of L1.
    pub rf: S,
    /// Grid voltage amplitude (V).
    pub v_grid: S,
    /// Grid angular frequency (rad/s).
    pub omega_grid: S,
}

impl<S: ControlScalar> VsiConfig<S> {
    /// Construct a VsiConfig with all parameters validated.
    ///
    /// Returns `None` if any inductance or capacitance is non-positive.
    pub fn new(l1: S, c: S, l2: S, rf: S, v_grid: S, omega_grid: S) -> Option<Self> {
        if l1 <= S::ZERO || c <= S::ZERO || l2 <= S::ZERO {
            return None;
        }
        Some(Self {
            l1,
            c,
            l2,
            rf,
            v_grid,
            omega_grid,
        })
    }
}

/// LCL-filtered VSI plant model using RK4 integration.
///
/// State vector (αβ frame, 6 components):
///   [0] iL1_α — converter-side inductor current, α axis (A)
///   [1] iL1_β — converter-side inductor current, β axis (A)
///   [2] vc_α  — filter capacitor voltage, α axis (V)
///   [3] vc_β  — filter capacitor voltage, β axis (V)
///   [4] iL2_α — grid-side inductor current, α axis (A)
///   [5] iL2_β — grid-side inductor current, β axis (A)
#[derive(Debug, Clone, Copy)]
pub struct VsiPlant<S: ControlScalar> {
    /// Filter/plant configuration.
    pub config: VsiConfig<S>,
    /// State vector: [iL1_α, iL1_β, vc_α, vc_β, iL2_α, iL2_β].
    state: [S; 6],
    /// Accumulated simulation time (s) — for grid angle computation.
    t: S,
}

impl<S: ControlScalar> VsiPlant<S> {
    /// Create a VSI plant from the given configuration.
    ///
    /// All states are initialised to zero (cold start).
    pub fn new(config: VsiConfig<S>) -> Self {
        Self {
            config,
            state: [S::ZERO; 6],
            t: S::ZERO,
        }
    }

    /// Compute state derivatives given current state and inverter voltage commands.
    ///
    /// # Arguments
    /// * `x`      — current state [iL1_α, iL1_β, vc_α, vc_β, iL2_α, iL2_β]
    /// * `v_inv_a`, `v_inv_b` — inverter output voltages (αβ), V
    /// * `theta`  — grid angle (rad) for grid voltage feed-forward
    fn derivatives(&self, x: &[S; 6], v_inv_a: S, v_inv_b: S, theta: S) -> [S; 6] {
        let cfg = &self.config;

        // Grid voltages (αβ frame)
        let vg_a = cfg.v_grid * theta.cos();
        let vg_b = cfg.v_grid * theta.sin();

        // α axis
        let dil1_a = (v_inv_a - cfg.rf * x[0] - x[2]) / cfg.l1;
        let dvc_a = (x[0] - x[4]) / cfg.c;
        let dil2_a = (x[2] - vg_a) / cfg.l2;

        // β axis
        let dil1_b = (v_inv_b - cfg.rf * x[1] - x[3]) / cfg.l1;
        let dvc_b = (x[1] - x[5]) / cfg.c;
        let dil2_b = (x[3] - vg_b) / cfg.l2;

        [dil1_a, dil1_b, dvc_a, dvc_b, dil2_a, dil2_b]
    }

    /// Advance the plant by `dt` seconds using 4th-order Runge-Kutta integration.
    ///
    /// # Arguments
    /// * `v_inv_alpha`, `v_inv_beta` — inverter voltage references (αβ frame, V)
    /// * `dt`                         — time step (s)
    pub fn step(&mut self, v_inv_alpha: S, v_inv_beta: S, dt: S) {
        let theta0 = self.config.omega_grid * self.t;
        let theta_half = self.config.omega_grid * (self.t + dt * S::HALF);
        let theta_end = self.config.omega_grid * (self.t + dt);

        let x = self.state;

        // k1
        let k1 = self.derivatives(&x, v_inv_alpha, v_inv_beta, theta0);

        // k2 (mid-step using k1)
        let x2 = add_scaled(&x, &k1, dt * S::HALF);
        let k2 = self.derivatives(&x2, v_inv_alpha, v_inv_beta, theta_half);

        // k3 (mid-step using k2)
        let x3 = add_scaled(&x, &k2, dt * S::HALF);
        let k3 = self.derivatives(&x3, v_inv_alpha, v_inv_beta, theta_half);

        // k4 (full-step using k3)
        let x4 = add_scaled(&x, &k3, dt);
        let k4 = self.derivatives(&x4, v_inv_alpha, v_inv_beta, theta_end);

        // Combine: x_next = x + dt/6 * (k1 + 2k2 + 2k3 + k4)
        let sixth = S::from_f64(1.0 / 6.0);
        for i in 0..6 {
            self.state[i] = x[i] + dt * sixth * (k1[i] + S::TWO * k2[i] + S::TWO * k3[i] + k4[i]);
        }

        self.t += dt;
    }

    /// Grid-side inductor current α axis (A) — the controlled output.
    pub fn i_grid_alpha(&self) -> S {
        self.state[4]
    }

    /// Grid-side inductor current β axis (A).
    pub fn i_grid_beta(&self) -> S {
        self.state[5]
    }

    /// Converter-side inductor current α axis (A).
    pub fn i_l1_alpha(&self) -> S {
        self.state[0]
    }

    /// Converter-side inductor current β axis (A).
    pub fn i_l1_beta(&self) -> S {
        self.state[1]
    }

    /// Filter capacitor voltage α axis (V).
    pub fn vc_alpha(&self) -> S {
        self.state[2]
    }

    /// Filter capacitor voltage β axis (V).
    pub fn vc_beta(&self) -> S {
        self.state[3]
    }

    /// Accumulated simulation time (s).
    pub fn time(&self) -> S {
        self.t
    }

    /// Reset all state variables and the time counter to zero.
    pub fn reset(&mut self) {
        self.state = [S::ZERO; 6];
        self.t = S::ZERO;
    }

    /// Full state vector view (read-only).
    pub fn state(&self) -> &[S; 6] {
        &self.state
    }
}

/// PI current controller for a VSI operating in the synchronous d/q frame.
///
/// The controller tracks d/q current references and applies cross-coupling
/// feed-forward compensation to decouple the d and q axes:
///
///   v_d* = kp·e_d + ki·∫e_d dt − ω·L2·iq + v_gd
///   v_q* = kp·e_q + ki·∫e_q dt + ω·L2·id + v_gq
///
/// The output is transformed back to αβ for the plant.
#[derive(Debug, Clone, Copy)]
pub struct VsiCurrentController<S: ControlScalar> {
    /// Proportional gain.
    pub kp: S,
    /// Integral gain.
    pub ki: S,
    /// Grid angular frequency (rad/s) — used for cross-coupling feed-forward.
    pub omega: S,
    /// Grid-side inductance L2 (H) — used for cross-coupling feed-forward.
    pub l2: S,
    /// Integrator state — d axis.
    int_d: S,
    /// Integrator state — q axis.
    int_q: S,
    /// Output voltage limit (V) — prevents integrator windup via clamping.
    pub v_limit: S,
}

impl<S: ControlScalar> VsiCurrentController<S> {
    /// Construct a VsiCurrentController.
    ///
    /// * `kp`      — proportional gain
    /// * `ki`      — integral gain
    /// * `omega`   — grid frequency (rad/s)
    /// * `l2`      — grid inductance (H)
    /// * `v_limit` — maximum output voltage magnitude (V)
    pub fn new(kp: S, ki: S, omega: S, l2: S, v_limit: S) -> Self {
        Self {
            kp,
            ki,
            omega,
            l2,
            int_d: S::ZERO,
            int_q: S::ZERO,
            v_limit,
        }
    }

    /// Update the current controller.
    ///
    /// # Arguments
    /// * `id_ref`, `iq_ref`   — d/q current references (A)
    /// * `id`, `iq`           — measured d/q currents (A)
    /// * `v_gd`, `v_gq`       — grid d/q voltages for feed-forward (V)
    /// * `theta`              — grid angle (rad)
    /// * `dt`                 — time step (s)
    ///
    /// Returns `(v_alpha, v_beta)` — voltage commands in the αβ frame (V).
    #[allow(clippy::too_many_arguments)]
    pub fn update(
        &mut self,
        id_ref: S,
        iq_ref: S,
        id: S,
        iq: S,
        v_gd: S,
        v_gq: S,
        theta: S,
        dt: S,
    ) -> (S, S) {
        let e_d = id_ref - id;
        let e_q = iq_ref - iq;

        // Integrate errors (pre-windup accumulation)
        let int_d_new = self.int_d + e_d * dt;
        let int_q_new = self.int_q + e_q * dt;

        // Cross-coupling feed-forward: decouple d/q axes
        let cross_d = -self.omega * self.l2 * iq;
        let cross_q = self.omega * self.l2 * id;

        // PI + cross-coupling + grid feed-forward
        let vd = self.kp * e_d + self.ki * int_d_new + cross_d + v_gd;
        let vq = self.kp * e_q + self.ki * int_q_new + cross_q + v_gq;

        // Clamp output voltage to limit
        let vd_clamped = vd.clamp_val(-self.v_limit, self.v_limit);
        let vq_clamped = vq.clamp_val(-self.v_limit, self.v_limit);

        // Anti-windup: only update integrators if output is not saturated
        self.int_d = if vd_clamped == vd {
            int_d_new
        } else {
            self.int_d
        };
        self.int_q = if vq_clamped == vq {
            int_q_new
        } else {
            self.int_q
        };

        // Inverse Park transform: dq → αβ
        let cos_t = theta.cos();
        let sin_t = theta.sin();
        let v_alpha = vd_clamped * cos_t - vq_clamped * sin_t;
        let v_beta = vd_clamped * sin_t + vq_clamped * cos_t;

        (v_alpha, v_beta)
    }

    /// Reset integrator states.
    pub fn reset(&mut self) {
        self.int_d = S::ZERO;
        self.int_q = S::ZERO;
    }

    /// Integrator state for diagnostic purposes.
    pub fn integrator_d(&self) -> S {
        self.int_d
    }

    /// Integrator state for diagnostic purposes.
    pub fn integrator_q(&self) -> S {
        self.int_q
    }
}

// --- Internal helper ------------------------------------------------------------

/// Add `scale * b` to `a` element-wise, returning a new array.
#[inline]
fn add_scaled<S: ControlScalar>(a: &[S; 6], b: &[S; 6], scale: S) -> [S; 6] {
    [
        a[0] + scale * b[0],
        a[1] + scale * b[1],
        a[2] + scale * b[2],
        a[3] + scale * b[3],
        a[4] + scale * b[4],
        a[5] + scale * b[5],
    ]
}

// --- Park/Inverse Park helpers (αβ → dq) ----------------------------------------

/// Forward Park transform: αβ → dq.
///
/// id =  i_α·cos(θ) + i_β·sin(θ)
/// iq = -i_α·sin(θ) + i_β·cos(θ)
pub fn park_transform<S: ControlScalar>(i_alpha: S, i_beta: S, theta: S) -> (S, S) {
    let cos_t = theta.cos();
    let sin_t = theta.sin();
    let id = i_alpha * cos_t + i_beta * sin_t;
    let iq = -i_alpha * sin_t + i_beta * cos_t;
    (id, iq)
}

// ---- Unit Tests -----------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use core::f64::consts::PI;

    fn default_config() -> VsiConfig<f64> {
        VsiConfig::new(
            3e-3,  // L1 = 3 mH
            10e-6, // C  = 10 µF
            1e-3,  // L2 = 1 mH
            0.1,   // Rf = 0.1 Ω
            230.0, // V_grid = 230 V (peak)
            2.0 * PI * 50.0,
        )
        .expect("valid config")
    }

    /// Verify the plant stays bounded when driven at grid frequency.
    #[test]
    fn vsi_plant_bounded_under_grid_excitation() {
        let cfg = default_config();
        let mut plant = VsiPlant::new(cfg);
        let dt = 1e-5_f64;
        let omega = 2.0 * PI * 50.0;
        let v_amp = 230.0_f64;

        for k in 0..10_000 {
            let t = k as f64 * dt;
            let v_a = v_amp * (omega * t).cos();
            let v_b = v_amp * (omega * t).sin();
            plant.step(v_a, v_b, dt);
        }

        // Grid-side current must be finite and within a physically reasonable bound
        let i_a = plant.i_grid_alpha();
        let i_b = plant.i_grid_beta();
        assert!(i_a.is_finite(), "i_grid_alpha not finite: {i_a}");
        assert!(i_b.is_finite(), "i_grid_beta not finite: {i_b}");
        // Peak current should be < 100 A (400V / 3mH filter impedance)
        assert!(i_a.abs() < 200.0, "i_grid_alpha={i_a:.3} A exceeds bound");
    }

    /// Current step response: open-loop DC voltage drive increases grid-side current.
    ///
    /// Applies a constant DC voltage to the inverter inputs and verifies that the
    /// grid-side inductor current grows monotonically.  With zero grid voltage and a
    /// positive DC drive, the net volt-seconds across L2 must be positive, so iL2
    /// must increase over time.
    ///
    /// This validates the plant model independently of any controller design.
    #[test]
    fn vsi_current_tracking_step_response() {
        let cfg = VsiConfig::new(
            3e-3,  // L1 = 3 mH (standard LCL values)
            10e-6, // C  = 10 µF
            1e-3,  // L2 = 1 mH
            0.1,   // Rf = 0.1 Ω (low, to allow good current buildup)
            0.0,   // V_grid = 0 (isolated test)
            2.0 * PI * 50.0,
        )
        .expect("valid config");

        let mut plant = VsiPlant::new(cfg);
        let dt = 1e-6_f64; // 1 MHz — accurate integration at LCL resonant freq

        // Apply a constant positive DC voltage to α axis only
        let v_dc = 100.0_f64;

        // Run for 1 ms (1000 steps)
        for _ in 0..1000 {
            plant.step(v_dc, 0.0, dt);
        }

        let i_final = plant.i_grid_alpha();

        // With a positive DC drive, the grid-side current must be positive
        // (energy is being delivered to the grid side)
        assert!(
            i_final > 0.0,
            "i_grid_alpha={i_final:.4} A should be positive with positive DC drive"
        );

        // Sanity: approximate steady increment ≈ v_dc * t / (L1+L2) ≈ 100*1e-3/4e-3 = 25 A
        // (upper bound ignoring capacitor and resistance)
        assert!(
            i_final < 200.0,
            "i_grid_alpha={i_final:.4} A exceeds physical bound"
        );
    }

    /// Reactive power sign: β-axis voltage drive produces β-axis grid current.
    ///
    /// A purely β-axis sinusoidal inverter voltage (90° ahead of the α cosine)
    /// produces a β-axis grid current.  In d/q convention, iq (q-axis current)
    /// carries reactive power: Q = (3/2)·V_d·I_q.
    ///
    /// This test verifies the sign convention: driving the β axis with a positive
    /// sinusoid must produce a positive average β-axis current at grid frequency.
    ///
    /// The controller is bypassed; we drive the plant open-loop and measure
    /// the fundamental-frequency current component.
    #[test]
    fn vsi_reactive_power_sign() {
        let cfg = VsiConfig::new(
            3e-3,
            10e-6,
            1e-3,
            10.0, // Rf = 10 Ω — heavy damping for clean open-loop response
            0.0,  // V_grid = 0
            2.0 * PI * 50.0,
        )
        .expect("valid config");

        let mut plant = VsiPlant::new(cfg);
        let dt = 1e-5_f64;
        let omega = 2.0 * PI * 50.0;
        let v_amp = 50.0_f64;

        // Prime for 5 periods to reach quasi-steady-state
        let steps_prime = (5.0 / (50.0 * dt)) as usize;
        for k in 0..steps_prime {
            let t = k as f64 * dt;
            plant.step(0.0, v_amp * (omega * t).sin(), dt);
        }

        // Accumulate β-axis current over 2 full periods
        let steps_measure = (2.0 / (50.0 * dt)) as usize;
        let mut i_beta_sum = 0.0_f64;
        let mut count = 0usize;

        for k in 0..steps_measure {
            let t = (steps_prime + k) as f64 * dt;
            plant.step(0.0, v_amp * (omega * t).sin(), dt);
            // Measure the in-phase component of i_β (correlate with sin(ωt))
            i_beta_sum += plant.i_grid_beta() * (omega * t).sin();
            count += 1;
        }

        // Average fundamental component of β current (should be non-negative for inductive load)
        let i_beta_fund = i_beta_sum / count as f64;

        // The β-axis voltage excites the β-axis current; the fundamental component
        // must be non-trivially non-zero (either leading or lagging but present)
        assert!(
            i_beta_fund.abs() > 0.001,
            "fundamental β current = {i_beta_fund:.4} A should be non-zero under β drive"
        );
    }

    /// VsiConfig rejects zero or negative inductance/capacitance.
    #[test]
    fn vsi_config_rejects_invalid() {
        assert!(VsiConfig::<f64>::new(0.0, 10e-6, 1e-3, 0.1, 230.0, 314.16).is_none());
        assert!(VsiConfig::<f64>::new(3e-3, 0.0, 1e-3, 0.1, 230.0, 314.16).is_none());
        assert!(VsiConfig::<f64>::new(3e-3, 10e-6, 0.0, 0.1, 230.0, 314.16).is_none());
    }

    /// Controller reset clears integrators.
    #[test]
    fn vsi_controller_reset() {
        let mut ctrl = VsiCurrentController::<f64>::new(10.0, 500.0, 314.16, 1e-3, 400.0);
        let theta = 0.5_f64;
        ctrl.update(10.0, 0.0, 0.0, 0.0, 230.0, 0.0, theta, 1e-4);
        ctrl.reset();
        assert_eq!(ctrl.integrator_d(), 0.0);
        assert_eq!(ctrl.integrator_q(), 0.0);
    }
}