qsim_core/
traits.rs

1//! Core traits for qsim
2
3use crate::{Result, StateStore, Topology};
4
5/// Trait for grid elements (buses, branches, generators, loads)
6pub trait GridElement: Send + Sync {
7    /// Element type name for debugging
8    fn element_type(&self) -> &'static str;
9
10    /// Apply element's contribution to the state
11    fn apply(&self, state: &mut StateStore);
12}
13
14/// Trait for power flow solvers
15pub trait Solver: Send + Sync {
16    /// Solver name for debugging
17    fn name(&self) -> &'static str;
18
19    /// Solve power flow for the given topology and state
20    fn solve(&self, topology: &Topology, state: &mut StateStore) -> Result<SolverResult>;
21}
22
23/// Result from a solver execution
24#[derive(Debug, Clone)]
25pub struct SolverResult {
26    /// Number of iterations (for iterative solvers)
27    pub iterations: usize,
28    /// Final convergence error
29    pub convergence_error: f64,
30    /// Whether solver converged
31    pub converged: bool,
32}
33
34impl SolverResult {
35    /// Create a successful result
36    pub fn converged(iterations: usize, error: f64) -> Self {
37        Self {
38            iterations,
39            convergence_error: error,
40            converged: true,
41        }
42    }
43
44    /// Create a failed result
45    pub fn failed(iterations: usize, error: f64) -> Self {
46        Self {
47            iterations,
48            convergence_error: error,
49            converged: false,
50        }
51    }
52}
53
54/// Trait for time stepping in simulations
55pub trait TimeStepper: Send + Sync {
56    /// Advance simulation by one step
57    fn step(&mut self, state: &mut StateStore, dt: f64) -> Result<()>;
58}
59
60/// Trait for output/observation handlers
61pub trait OutputHandler: Send + Sync {
62    /// Called after each simulation step
63    fn on_step(&mut self, step: usize, time: f64, state: &StateStore);
64
65    /// Called when simulation completes
66    fn on_complete(&mut self, state: &StateStore);
67}