qsim_core/
state.rs

1//! Central state storage for simulation
2
3/// Central storage for simulation state.
4///
5/// Uses contiguous `Vec<f64>` arrays for cache-friendly access
6/// and efficient parallel computation.
7#[derive(Debug, Clone)]
8pub struct StateStore {
9    /// Voltage magnitudes (per-unit)
10    pub voltage_magnitude: Vec<f64>,
11    /// Voltage angles (radians)
12    pub voltage_angle: Vec<f64>,
13    /// Active power injection (MW)
14    pub active_power: Vec<f64>,
15    /// Reactive power injection (MVAr)
16    pub reactive_power: Vec<f64>,
17}
18
19impl StateStore {
20    /// Create a new state store with given capacity
21    pub fn new(bus_count: usize) -> Self {
22        Self {
23            voltage_magnitude: vec![1.0; bus_count],
24            voltage_angle: vec![0.0; bus_count],
25            active_power: vec![0.0; bus_count],
26            reactive_power: vec![0.0; bus_count],
27        }
28    }
29
30    /// Number of buses in the state
31    pub fn bus_count(&self) -> usize {
32        self.voltage_magnitude.len()
33    }
34
35    /// Reset all values to defaults
36    pub fn reset(&mut self) {
37        self.voltage_magnitude.fill(1.0);
38        self.voltage_angle.fill(0.0);
39        self.active_power.fill(0.0);
40        self.reactive_power.fill(0.0);
41    }
42}
43
44impl Default for StateStore {
45    fn default() -> Self {
46        Self::new(0)
47    }
48}