Skip to main content

hydra_engine_wds/simulation/
writable.rs

1// writable — implements crate::io::WritableSimulation for Simulation.
2//
3// This bridges the simulation crate's internal state to the generic trait
4// that the output writers in crate::io require. No solver logic lives
5// here — only accessors.
6
7use super::*;
8
9use crate::io::{
10    FlowBalance, FlowBalanceSummary, HydSnapshot, MassBalance, PumpEnergy, SimWarning,
11    WritableSimulation,
12};
13use crate::{LinkKind, Network};
14
15impl WritableSimulation for Simulation {
16    fn net(&self) -> &Network {
17        self.network
18            .as_ref()
19            .expect("WritableSimulation::net called before network was loaded")
20    }
21
22    fn snapshots(&self) -> &[HydSnapshot] {
23        &self.hyd_snapshots
24    }
25
26    fn pump_energy_at(&self, link_index: usize) -> Option<&PumpEnergy> {
27        self.accounting.as_ref().map(|a| &a.pump_energy[link_index])
28    }
29
30    fn peak_demand_kw(&self) -> f64 {
31        self.accounting.as_ref().map_or(0.0, |a| a.peak_demand_kw)
32    }
33
34    fn mass_balance(&self) -> Option<&MassBalance> {
35        self.quality_state.as_ref().map(|qs| &qs.mass_balance)
36    }
37
38    fn warnings(&self) -> &[SimWarning] {
39        &self.warnings
40    }
41
42    fn pump_energy_by_id(&self, pump_id: &str) -> Option<&PumpEnergy> {
43        let network = self.network.as_ref()?;
44        let link_index = network
45            .links
46            .iter()
47            .position(|l| l.base.id == pump_id && matches!(l.kind, LinkKind::Pump(_)))?;
48        self.accounting.as_ref().map(|a| &a.pump_energy[link_index])
49    }
50
51    fn analysis_times(&self) -> (Option<std::time::SystemTime>, Option<std::time::SystemTime>) {
52        (self.analysis_begun, self.analysis_ended)
53    }
54
55    fn flow_balance(&self) -> Option<&FlowBalance> {
56        self.accounting.as_ref().map(|a| &a.flow_balance)
57    }
58
59    fn flow_balance_summary(&self) -> Option<FlowBalanceSummary> {
60        self.flow_balance_summary().ok()
61    }
62}