oxigrid 0.1.2

Pure Rust Energy Systems Simulation & Optimization Library
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
//! Smart-grid communication protocol simulator.
//!
//! Models the network-level behaviour of major substation and IoT protocols:
//!
//! | Protocol | Standard | Typical use |
//! |----------|----------|-------------|
//! | `Iec61850Goose` | IEC 61850-8-1 | Fast protection tripping (< 4 ms) |
//! | `Iec61850Sampled` | IEC 61850-9-2 | Merging-unit voltage/current streams |
//! | `Dnp3` | IEEE 1815 | SCADA telemetry, outstation control |
//! | `Modbus` | Modicon 1979 | Legacy RTU/TCP sensor polling |
//! | `Mqtt` | OASIS 5.0 | IoT device telemetry |
//! | `CimXml` | IEC 61968/61970 | CIM data exchange |
//!
//! The simulator is **purely deterministic given an LCG seed**; no external PRNG
//! crates are used.  Latency jitter is generated with a Box–Muller-like
//! approximation from uniform LCG samples.
//!
//! # GOOSE delivery guarantee
//! IEC 61850 GOOSE uses rapid retransmission (T0 → T0/2 → … down to a minimum
//! period) to achieve > 99.9 % delivery despite UDP multicast.  Use
//! [`ProtocolSimulator::generate_goose_sequence`] to build realistic sequences.

use serde::{Deserialize, Serialize};
use std::fmt;

// ─────────────────────────────────────────────────────────────────────────────
// Error
// ─────────────────────────────────────────────────────────────────────────────

/// Errors from the protocol simulator.
#[derive(Debug, Clone, PartialEq)]
pub enum SimError {
    /// The requested link does not exist.
    LinkNotFound(usize),
    /// The simulation time or message list is invalid.
    InvalidInput(String),
}

impl fmt::Display for SimError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::LinkNotFound(id) => write!(f, "link {id} not found"),
            Self::InvalidInput(m) => write!(f, "invalid input: {m}"),
        }
    }
}

impl std::error::Error for SimError {}

// ─────────────────────────────────────────────────────────────────────────────
// Protocol and message types
// ─────────────────────────────────────────────────────────────────────────────

/// Communication protocol flavour.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ProtocolType {
    /// IEC 61850 GOOSE — Generic Object Oriented Substation Events.
    Iec61850Goose,
    /// IEC 61850 Sampled Values (SV/9-2LE).
    Iec61850Sampled,
    /// Distributed Network Protocol 3 (IEEE 1815).
    Dnp3,
    /// Modbus RTU or TCP.
    Modbus,
    /// MQTT 5.0 for IoT telemetry.
    Mqtt,
    /// CIM-based XML data exchange (IEC 61968/61970).
    CimXml,
}

/// Semantic classification of a message.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MessageType {
    /// Protection trip, close, or setpoint command.
    ControlCommand,
    /// Breaker status, measurement report.
    StatusReport,
    /// High-speed voltage/current samples (SV).
    SampledValues,
    /// Alarm or event notification.
    Alarm,
    /// Periodic keep-alive.
    Heartbeat,
    /// Time synchronisation (e.g. PPS, 1588 PTP).
    TimeSync,
}

/// A single protocol message.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
    /// Unique message identifier.
    pub id: u64,
    /// Protocol type.
    pub protocol: ProtocolType,
    /// Source address / APPID.
    pub source: u32,
    /// Destination address.
    pub destination: u32,
    /// Semantic type of the message.
    pub message_type: MessageType,
    /// Simulated raw payload bytes.
    pub payload: Vec<u8>,
    /// Origination timestamp `\[µs\]`.
    pub timestamp_us: u64,
    /// IEEE 802.1p priority (0–7).
    pub priority: u8,
    /// Total message size on the wire `\[bytes\]`.
    pub size_bytes: usize,
}

// ─────────────────────────────────────────────────────────────────────────────
// Network link
// ─────────────────────────────────────────────────────────────────────────────

/// A simplex network link connecting two communication endpoints.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkLink {
    /// Unique link identifier (used as index in the simulator).
    pub id: usize,
    /// Available bandwidth `\[Mbps\]`.
    pub bandwidth_mbps: f64,
    /// Propagation + processing latency `\[ms\]`.
    pub latency_ms: f64,
    /// Fraction of packets lost due to congestion or interference `\[0, 1\]`.
    pub packet_loss_rate: f64,
    /// One-sigma latency jitter (Gaussian approximation) `\[ms\]`.
    pub jitter_ms: f64,
}

// ─────────────────────────────────────────────────────────────────────────────
// Statistics
// ─────────────────────────────────────────────────────────────────────────────

/// Aggregate statistics from one simulation run.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProtocolSimResult {
    /// Total messages submitted to the link.
    pub messages_sent: usize,
    /// Messages successfully received (not lost, within time window).
    pub messages_received: usize,
    /// Messages dropped due to packet loss or bandwidth overflow.
    pub messages_lost: usize,
    /// Mean end-to-end latency of delivered messages `\[ms\]`.
    pub avg_latency_ms: f64,
    /// Maximum observed latency `\[ms\]`.
    pub max_latency_ms: f64,
    /// 99th-percentile latency of delivered messages `\[ms\]`.
    pub p99_latency_ms: f64,
    /// Effective throughput `\[kbps\]`.
    pub throughput_kbps: f64,
    /// Fraction of GOOSE messages delivered `\[%\]`, 0–100.
    pub goose_delivery_pct: f64,
}

// ─────────────────────────────────────────────────────────────────────────────
// LCG PRNG
// ─────────────────────────────────────────────────────────────────────────────

struct Lcg {
    state: u64,
}

impl Lcg {
    const MULT: u64 = 6_364_136_223_846_793_005;
    const ADD: u64 = 1_442_695_040_888_963_407;

    fn new(seed: u64) -> Self {
        Self {
            state: seed.wrapping_add(1),
        }
    }

    /// Next `u64` value.
    fn next_u64(&mut self) -> u64 {
        self.state = self.state.wrapping_mul(Self::MULT).wrapping_add(Self::ADD);
        self.state
    }

    /// Uniform `f64` in `\[0, 1)`.
    fn next_f64(&mut self) -> f64 {
        (self.next_u64() >> 11) as f64 / (1u64 << 53) as f64
    }

    /// Approximate Gaussian sample via 12-uniform sum (central limit theorem).
    /// Mean = 0, σ ≈ 1.
    fn next_normal(&mut self) -> f64 {
        let sum: f64 = (0..12).map(|_| self.next_f64()).sum();
        sum - 6.0
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Simulator
// ─────────────────────────────────────────────────────────────────────────────

/// Smart-grid communication protocol simulator.
pub struct ProtocolSimulator {
    links: Vec<NetworkLink>,
    /// Log of `(message, arrival_time_ms, delivered)`.
    message_log: Vec<(Message, f64, bool)>,
    lcg: Lcg,
}

impl Default for ProtocolSimulator {
    fn default() -> Self {
        Self::new()
    }
}

impl ProtocolSimulator {
    /// Create a new simulator with no links.
    pub fn new() -> Self {
        Self {
            links: Vec::new(),
            message_log: Vec::new(),
            lcg: Lcg::new(0xDEAD_BEEF_1234_5678),
        }
    }

    /// Register a network link.
    pub fn add_link(&mut self, link: NetworkLink) {
        self.links.push(link);
    }

    /// Simulate transmission of a batch of messages over link `link_id`.
    ///
    /// For each message:
    /// 1. Compute base latency = `link.latency_ms`.
    /// 2. Add Gaussian jitter bounded to ±2σ.
    /// 3. Apply packet loss: LCG uniform < `loss_rate` → drop.
    /// 4. Apply bandwidth queuing: accumulate bytes transmitted; if burst
    ///    saturates the link capacity within the simulation window the excess
    ///    messages experience additional serialisation delay.
    ///
    /// Returns aggregate statistics.
    pub fn simulate(
        &mut self,
        messages: Vec<Message>,
        link_id: usize,
        simulation_time_ms: f64,
    ) -> Result<ProtocolSimResult, SimError> {
        if simulation_time_ms <= 0.0 {
            return Err(SimError::InvalidInput(
                "simulation_time_ms must be positive".to_string(),
            ));
        }
        let link = self
            .links
            .iter()
            .find(|l| l.id == link_id)
            .ok_or(SimError::LinkNotFound(link_id))?
            .clone();

        let bandwidth_bytes_per_ms = link.bandwidth_mbps * 1_000_000.0 / 8.0 / 1_000.0;

        let mut queue_time_ms = 0.0_f64; // next available slot in the queue
        let mut latencies: Vec<f64> = Vec::new();
        let mut goose_sent = 0usize;
        let mut goose_received = 0usize;
        let mut total_bytes_delivered = 0usize;

        for msg in &messages {
            let is_goose = msg.protocol == ProtocolType::Iec61850Goose;
            if is_goose {
                goose_sent += 1;
            }

            // Packet loss check
            let loss_roll = self.lcg.next_f64();
            if loss_roll < link.packet_loss_rate {
                self.message_log.push((msg.clone(), 0.0, false));
                continue;
            }

            // Base latency + jitter (bounded to ±2σ)
            let raw_jitter = self.lcg.next_normal() * link.jitter_ms;
            let jitter = raw_jitter.clamp(-2.0 * link.jitter_ms, 2.0 * link.jitter_ms);
            let latency_base = (link.latency_ms + jitter).max(0.0);

            // Serialisation / queuing delay
            let tx_time_ms = msg.size_bytes as f64 / bandwidth_bytes_per_ms;
            let msg_dispatch_time = msg.timestamp_us as f64 / 1_000.0; // µs → ms
            let start_tx = queue_time_ms.max(msg_dispatch_time);
            queue_time_ms = start_tx + tx_time_ms;
            let arrival_ms = start_tx + tx_time_ms + latency_base;

            if arrival_ms > simulation_time_ms {
                // Arrived after window — treat as lost
                self.message_log.push((msg.clone(), arrival_ms, false));
                continue;
            }

            let total_latency = arrival_ms - msg_dispatch_time;
            latencies.push(total_latency);
            total_bytes_delivered += msg.size_bytes;
            self.message_log.push((msg.clone(), arrival_ms, true));
            if is_goose {
                goose_received += 1;
            }
        }

        let messages_sent = messages.len();
        let messages_received = latencies.len();
        let messages_lost = messages_sent - messages_received;

        let avg_latency_ms = if latencies.is_empty() {
            0.0
        } else {
            latencies.iter().sum::<f64>() / latencies.len() as f64
        };

        let max_latency_ms = latencies
            .iter()
            .cloned()
            .fold(f64::NEG_INFINITY, f64::max)
            .max(0.0);

        let p99_latency_ms = percentile_sorted(&latencies, 99.0);

        let throughput_kbps = if simulation_time_ms > 0.0 {
            total_bytes_delivered as f64 * 8.0 / simulation_time_ms // bits/ms = kbps
        } else {
            0.0
        };

        let goose_delivery_pct = if goose_sent == 0 {
            100.0
        } else {
            goose_received as f64 / goose_sent as f64 * 100.0
        };

        Ok(ProtocolSimResult {
            messages_sent,
            messages_received,
            messages_lost,
            avg_latency_ms,
            max_latency_ms,
            p99_latency_ms,
            throughput_kbps,
            goose_delivery_pct,
        })
    }

    /// Generate a realistic GOOSE retransmission sequence.
    ///
    /// After the initial event at `event_time_ms`, IEC 61850 requires the
    /// publisher to retransmit with exponentially-increasing intervals starting
    /// at ~2 ms, doubling each time up to a maximum of 1000 ms.
    ///
    /// `n_retransmit` controls how many retransmissions follow the initial event.
    pub fn generate_goose_sequence(event_time_ms: f64, n_retransmit: usize) -> Vec<Message> {
        let mut msgs: Vec<Message> = Vec::with_capacity(n_retransmit + 1);

        // Initial event message
        msgs.push(Message {
            id: 0,
            protocol: ProtocolType::Iec61850Goose,
            source: 0x0001,
            destination: 0xFFFF,
            message_type: MessageType::ControlCommand,
            payload: vec![0x01, 0x00, 0x00, 0x00], // simulated GOOSE payload
            timestamp_us: (event_time_ms * 1_000.0) as u64,
            priority: 6,     // GOOSE uses VLAN priority 6
            size_bytes: 128, // typical GOOSE PDU
        });

        // Retransmissions with doubling interval
        let mut interval_ms = 2.0_f64;
        let max_interval_ms = 1_000.0_f64;
        let mut t = event_time_ms + interval_ms;

        for i in 1..=n_retransmit {
            msgs.push(Message {
                id: i as u64,
                protocol: ProtocolType::Iec61850Goose,
                source: 0x0001,
                destination: 0xFFFF,
                message_type: MessageType::ControlCommand,
                payload: vec![0x01, 0x00, 0x00, 0x00],
                timestamp_us: (t * 1_000.0) as u64,
                priority: 6,
                size_bytes: 128,
            });
            interval_ms = (interval_ms * 2.0).min(max_interval_ms);
            t += interval_ms;
        }

        msgs
    }

    /// Aggregate statistics over all messages transmitted so far.
    pub fn message_statistics(&self) -> ProtocolSimResult {
        let messages_sent = self.message_log.len();
        let delivered: Vec<f64> = self
            .message_log
            .iter()
            .filter(|(_, _, ok)| *ok)
            .map(|(msg, arrival, _)| {
                let dispatch_ms = msg.timestamp_us as f64 / 1_000.0;
                (*arrival - dispatch_ms).max(0.0)
            })
            .collect();

        let messages_received = delivered.len();
        let messages_lost = messages_sent - messages_received;
        let avg_latency_ms = if delivered.is_empty() {
            0.0
        } else {
            delivered.iter().sum::<f64>() / delivered.len() as f64
        };
        let max_latency_ms = delivered
            .iter()
            .cloned()
            .fold(f64::NEG_INFINITY, f64::max)
            .max(0.0);
        let p99_latency_ms = percentile_sorted(&delivered, 99.0);

        let total_bytes: usize = self
            .message_log
            .iter()
            .filter(|(_, _, ok)| *ok)
            .map(|(msg, _, _)| msg.size_bytes)
            .sum();
        // Use max arrival as simulation window
        let t_max = self
            .message_log
            .iter()
            .map(|(_, t, _)| *t)
            .fold(0.0_f64, f64::max);
        let throughput_kbps = if t_max > 0.0 {
            total_bytes as f64 * 8.0 / t_max
        } else {
            0.0
        };

        let goose_sent: usize = self
            .message_log
            .iter()
            .filter(|(msg, _, _)| msg.protocol == ProtocolType::Iec61850Goose)
            .count();
        let goose_received: usize = self
            .message_log
            .iter()
            .filter(|(msg, _, ok)| msg.protocol == ProtocolType::Iec61850Goose && *ok)
            .count();
        let goose_delivery_pct = if goose_sent == 0 {
            100.0
        } else {
            goose_received as f64 / goose_sent as f64 * 100.0
        };

        ProtocolSimResult {
            messages_sent,
            messages_received,
            messages_lost,
            avg_latency_ms,
            max_latency_ms,
            p99_latency_ms,
            throughput_kbps,
            goose_delivery_pct,
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Helper
// ─────────────────────────────────────────────────────────────────────────────

/// Compute the `p`-th percentile (0–100) from an unsorted slice.
fn percentile_sorted(data: &[f64], p: f64) -> f64 {
    if data.is_empty() {
        return 0.0;
    }
    let mut sorted = data.to_vec();
    sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
    let idx = ((p / 100.0) * (sorted.len() - 1) as f64).round() as usize;
    sorted[idx.min(sorted.len() - 1)]
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    fn make_link(id: usize, loss: f64) -> NetworkLink {
        NetworkLink {
            id,
            bandwidth_mbps: 100.0,
            latency_ms: 1.0,
            packet_loss_rate: loss,
            jitter_ms: 0.1,
        }
    }

    fn make_messages(n: usize, proto: ProtocolType) -> Vec<Message> {
        (0..n)
            .map(|i| Message {
                id: i as u64,
                protocol: proto,
                source: 1,
                destination: 2,
                message_type: MessageType::StatusReport,
                payload: vec![0u8; 64],
                timestamp_us: i as u64 * 1_000, // 1 ms apart
                priority: 4,
                size_bytes: 100,
            })
            .collect()
    }

    #[test]
    fn test_zero_loss_link_all_delivered() {
        let mut sim = ProtocolSimulator::new();
        sim.add_link(make_link(0, 0.0));
        let msgs = make_messages(50, ProtocolType::Dnp3);
        let result = sim
            .simulate(msgs, 0, 10_000.0)
            .expect("zero-loss simulate must succeed");
        assert_eq!(
            result.messages_lost, 0,
            "zero-loss link must deliver all messages"
        );
        assert_eq!(result.messages_received, 50);
    }

    #[test]
    fn test_high_loss_link_reduces_delivery() {
        let mut sim = ProtocolSimulator::new();
        // 50 % loss
        sim.add_link(make_link(1, 0.5));
        let msgs = make_messages(1000, ProtocolType::Modbus);
        let result = sim
            .simulate(msgs, 1, 100_000.0)
            .expect("high-loss simulate must succeed");
        // Expect roughly 50 % lost — allow wide tolerance for LCG variance
        let loss_pct = result.messages_lost as f64 / 1000.0 * 100.0;
        assert!(
            loss_pct > 30.0 && loss_pct < 70.0,
            "50 % loss link should lose 30–70 % of messages, lost {:.1} %",
            loss_pct
        );
    }

    #[test]
    fn test_goose_sequence_retransmit_intervals() {
        let event_t = 100.0_f64;
        let msgs = ProtocolSimulator::generate_goose_sequence(event_t, 5);
        assert_eq!(msgs.len(), 6, "1 initial + 5 retransmissions");
        // First message at event time
        assert_eq!(msgs[0].timestamp_us, (event_t * 1_000.0) as u64);
        // All are GOOSE
        assert!(msgs
            .iter()
            .all(|m| m.protocol == ProtocolType::Iec61850Goose));
        // Retransmit intervals must be non-decreasing (doubling up to 1000 ms)
        let times_ms: Vec<f64> = msgs
            .iter()
            .map(|m| m.timestamp_us as f64 / 1_000.0)
            .collect();
        for i in 1..times_ms.len().saturating_sub(1) {
            let gap_i = times_ms[i] - times_ms[i - 1];
            let gap_ip1 = times_ms[i + 1] - times_ms[i];
            // Each gap should be ≥ the previous (doubling schedule)
            assert!(
                gap_ip1 >= gap_i - 1e-9,
                "GOOSE retransmit interval should be non-decreasing: gap[{i}]={gap_i:.2} ms, gap[{i}+1]={gap_ip1:.2} ms"
            );
        }
    }

    #[test]
    fn test_latency_statistics_consistency() {
        let mut sim = ProtocolSimulator::new();
        sim.add_link(make_link(2, 0.0));
        let msgs = make_messages(200, ProtocolType::Mqtt);
        let result = sim
            .simulate(msgs, 2, 100_000.0)
            .expect("latency-stats simulate must succeed");
        // avg ≤ max
        assert!(
            result.avg_latency_ms <= result.max_latency_ms + 1e-9,
            "avg ({:.3}) must be ≤ max ({:.3})",
            result.avg_latency_ms,
            result.max_latency_ms
        );
        // p99 between avg and max
        assert!(
            result.p99_latency_ms >= result.avg_latency_ms - 1e-9,
            "P99 ({:.3}) should be ≥ avg ({:.3})",
            result.p99_latency_ms,
            result.avg_latency_ms
        );
        assert!(
            result.p99_latency_ms <= result.max_latency_ms + 1e-9,
            "P99 ({:.3}) should be ≤ max ({:.3})",
            result.p99_latency_ms,
            result.max_latency_ms
        );
    }

    #[test]
    fn test_bandwidth_throughput_within_link_capacity() {
        let link_mbps = 1.0_f64;
        let mut sim = ProtocolSimulator::new();
        sim.add_link(NetworkLink {
            id: 3,
            bandwidth_mbps: link_mbps,
            latency_ms: 0.5,
            packet_loss_rate: 0.0,
            jitter_ms: 0.01,
        });
        // 100 messages × 1000 bytes = 100 kB
        let msgs: Vec<Message> = (0..100)
            .map(|i| Message {
                id: i,
                protocol: ProtocolType::Dnp3,
                source: 1,
                destination: 2,
                message_type: MessageType::StatusReport,
                payload: vec![0u8; 1000],
                timestamp_us: 0,
                priority: 4,
                size_bytes: 1000,
            })
            .collect();
        // Large simulation window so all messages fit
        let result = sim
            .simulate(msgs, 3, 10_000.0)
            .expect("bandwidth simulate must succeed");
        // Throughput must not exceed link capacity (1 Mbps = 1000 kbps)
        assert!(
            result.throughput_kbps <= link_mbps * 1_000.0 + 1.0,
            "Throughput ({:.1} kbps) must not exceed link capacity ({:.0} kbps)",
            result.throughput_kbps,
            link_mbps * 1_000.0
        );
    }

    #[test]
    fn test_goose_delivery_pct_with_zero_loss() {
        let mut sim = ProtocolSimulator::new();
        sim.add_link(make_link(4, 0.0));
        let msgs = ProtocolSimulator::generate_goose_sequence(0.0, 4);
        let result = sim
            .simulate(msgs, 4, 100_000.0)
            .expect("goose zero-loss simulate must succeed");
        assert!(
            (result.goose_delivery_pct - 100.0).abs() < 1e-9,
            "Zero-loss GOOSE delivery should be 100 %, got {:.2} %",
            result.goose_delivery_pct
        );
    }

    #[test]
    fn test_link_not_found_error() {
        let mut sim = ProtocolSimulator::new();
        let msgs = make_messages(1, ProtocolType::Dnp3);
        let err = sim.simulate(msgs, 99, 1000.0).unwrap_err();
        assert!(matches!(err, SimError::LinkNotFound(99)));
    }

    #[test]
    fn test_invalid_simulation_time() {
        let mut sim = ProtocolSimulator::new();
        sim.add_link(make_link(5, 0.0));
        let msgs = make_messages(1, ProtocolType::Dnp3);
        let err = sim.simulate(msgs, 5, -1.0).unwrap_err();
        assert!(matches!(err, SimError::InvalidInput(_)));
    }

    #[test]
    fn test_sim_error_display_formatting() {
        let link_err = SimError::LinkNotFound(42);
        assert_eq!(link_err.to_string(), "link 42 not found");

        let input_err = SimError::InvalidInput("bad time".to_string());
        assert!(
            input_err.to_string().contains("bad time"),
            "InvalidInput display must include the message text"
        );
    }

    #[test]
    fn test_empty_message_list_returns_zero_stats() {
        let mut sim = ProtocolSimulator::new();
        sim.add_link(make_link(6, 0.0));
        let result = sim
            .simulate(vec![], 6, 1_000.0)
            .expect("empty message list must succeed");
        assert_eq!(result.messages_sent, 0);
        assert_eq!(result.messages_received, 0);
        assert_eq!(result.messages_lost, 0);
        assert_eq!(result.avg_latency_ms, 0.0);
        assert_eq!(result.max_latency_ms, 0.0);
        assert_eq!(result.throughput_kbps, 0.0);
        // goose_delivery_pct defaults to 100 when no GOOSE messages present
        assert!(
            (result.goose_delivery_pct - 100.0).abs() < 1e-9,
            "empty run GOOSE delivery pct must be 100.0"
        );
    }

    #[test]
    fn test_total_loss_link_delivers_nothing() {
        let mut sim = ProtocolSimulator::new();
        sim.add_link(make_link(7, 1.0)); // 100 % loss
        let msgs = make_messages(20, ProtocolType::Mqtt);
        let result = sim
            .simulate(msgs, 7, 100_000.0)
            .expect("simulate with 100% loss must not error");
        assert_eq!(
            result.messages_received, 0,
            "100 % loss link must receive no messages"
        );
        assert_eq!(result.messages_lost, 20);
        assert_eq!(result.avg_latency_ms, 0.0);
    }

    #[test]
    fn test_short_simulation_window_drops_late_messages() {
        let mut sim = ProtocolSimulator::new();
        // Very high latency so almost nothing arrives in time
        sim.add_link(NetworkLink {
            id: 8,
            bandwidth_mbps: 100.0,
            latency_ms: 5_000.0, // 5 s latency
            packet_loss_rate: 0.0,
            jitter_ms: 0.0,
        });
        let msgs = make_messages(10, ProtocolType::Dnp3);
        // Window of only 1 ms — everything arrives late
        let result = sim
            .simulate(msgs, 8, 1.0)
            .expect("short window simulate must not error");
        assert_eq!(
            result.messages_received, 0,
            "5 s latency on a 1 ms window must drop all messages"
        );
        assert_eq!(result.messages_lost, 10);
    }

    #[test]
    fn test_message_statistics_aggregates_across_simulate_calls() {
        let mut sim = ProtocolSimulator::new();
        sim.add_link(make_link(9, 0.0));
        // Two batches
        let batch_a = make_messages(10, ProtocolType::Dnp3);
        let batch_b = make_messages(5, ProtocolType::Modbus);
        sim.simulate(batch_a, 9, 100_000.0)
            .expect("first batch must succeed");
        sim.simulate(batch_b, 9, 100_000.0)
            .expect("second batch must succeed");
        let stats = sim.message_statistics();
        assert_eq!(
            stats.messages_sent, 15,
            "message_statistics must aggregate both batches"
        );
    }

    #[test]
    fn test_goose_sequence_zero_retransmissions() {
        let msgs = ProtocolSimulator::generate_goose_sequence(50.0, 0);
        assert_eq!(
            msgs.len(),
            1,
            "zero retransmissions must produce exactly 1 message"
        );
        assert_eq!(msgs[0].protocol, ProtocolType::Iec61850Goose);
        assert_eq!(msgs[0].timestamp_us, 50_000);
        assert_eq!(msgs[0].priority, 6, "GOOSE priority must be 6");
    }

    #[test]
    fn test_default_constructor_behaves_like_new() {
        let mut sim_default = ProtocolSimulator::default();
        let mut sim_new = ProtocolSimulator::new();
        sim_default.add_link(make_link(10, 0.0));
        sim_new.add_link(make_link(10, 0.0));
        let msgs_d = make_messages(5, ProtocolType::CimXml);
        let msgs_n = make_messages(5, ProtocolType::CimXml);
        let r_default = sim_default
            .simulate(msgs_d, 10, 100_000.0)
            .expect("default simulator must work");
        let r_new = sim_new
            .simulate(msgs_n, 10, 100_000.0)
            .expect("new simulator must work");
        assert_eq!(
            r_default.messages_sent, r_new.messages_sent,
            "default() and new() must produce identical sent counts"
        );
        assert_eq!(r_default.messages_received, r_new.messages_received);
    }

    #[test]
    fn test_protocol_type_and_message_type_equality() {
        // Verify Copy + Clone + PartialEq derived impls work correctly
        let p1 = ProtocolType::Modbus;
        let p2 = p1;
        assert_eq!(p1, p2);

        let p3 = ProtocolType::Mqtt;
        assert_ne!(p1, p3);

        let m1 = MessageType::Alarm;
        let m2 = m1;
        assert_eq!(m1, m2);

        let m3 = MessageType::Heartbeat;
        assert_ne!(m1, m3);
    }
}