SimPaM/patient-monitor-simulation/src/model.rs

131 lines
3.8 KiB
Rust
Raw Normal View History

2026-11-12 09:36:13 +01:00
//! Shared configuration of the synthetic monitor signals.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum EcgRhythm {
Sinus,
AtrialFibrillation,
VentricularTachycardia,
VentricularFibrillation,
Asystole,
}
impl EcgRhythm {
/// VF and asystole have no organized QRS to trigger a mechanical pulse.
pub fn has_qrs(self) -> bool {
!matches!(self, Self::VentricularFibrillation | Self::Asystole)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PulseConduction {
OneToOne,
TwoToOne,
ThreeToOne,
None,
Random,
}
impl PulseConduction {
pub fn conducts(self, beat: u64) -> bool {
match self {
Self::OneToOne => true,
// One mechanical pulse for every two QRS complexes.
Self::TwoToOne => (beat - 1) % 2 == 0,
Self::ThreeToOne => (beat - 1) % 3 == 0,
Self::None => false,
Self::Random => {
// Deterministic pseudo-random 65% mechanical capture for reproducible lessons.
let mut x = beat.wrapping_mul(0x9E37_79B9_7F4A_7C15);
x ^= x >> 30;
x = x.wrapping_mul(0xBF58_476D_1CE4_E5B9);
x ^= x >> 27;
(x % 100) < 65
}
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ArterialShape {
Normal,
Damped,
Hyperdynamic,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CapnoShape {
Normal,
Obstructive,
}
#[derive(Clone, Copy, Debug)]
pub struct Parameters {
pub ecg_rhythm: EcgRhythm,
pub heart_rate: i32, // bpm, organized electrical rate; not mechanical pulse rate
pub pulse_conduction: PulseConduction,
pub spo2: i32, // %
pub art_systolic: i32, // mmHg, nominal pressure; display axis 0..200
pub art_diastolic: i32, // mmHg
pub arterial_shape: ArterialShape,
pub pulse_pressure_variation: i32, // PPV %, 0 disables respiratory variation
pub nibp_systolic: i32, // mmHg, historical/static simulated cuff value
pub nibp_diastolic: i32, // mmHg
pub etco2: i32, // mmHg
pub respiratory_rate: i32, // breaths/min
pub capno_shape: CapnoShape,
// These affect display only; the underlying physiological signals continue.
pub show_ecg: bool,
pub show_spo2: bool,
pub show_art: bool,
pub show_capno: bool,
pub show_nibp: bool,
}
impl Default for Parameters {
fn default() -> Self {
Self {
ecg_rhythm: EcgRhythm::Sinus,
heart_rate: 75,
pulse_conduction: PulseConduction::OneToOne,
spo2: 98,
art_systolic: 120,
art_diastolic: 75,
arterial_shape: ArterialShape::Normal,
pulse_pressure_variation: 0,
nibp_systolic: 118,
nibp_diastolic: 74,
etco2: 38,
respiratory_rate: 14,
capno_shape: CapnoShape::Normal,
show_ecg: true,
show_spo2: true,
show_art: true,
show_capno: true,
show_nibp: true,
}
}
}
pub fn mean_arterial_pressure(systolic: i32, diastolic: i32) -> i32 {
((systolic as f64 + 2.0 * diastolic as f64) / 3.0).round() as i32
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn mean_pressure_uses_one_third_pulse_pressure() {
assert_eq!(mean_arterial_pressure(120, 75), 90);
}
#[test]
fn grouped_mechanical_capture_follows_requested_patterns() {
assert_eq!((1..=8).filter(|&b| PulseConduction::TwoToOne.conducts(b)).collect::<Vec<_>>(),
vec![1, 3, 5, 7]);
assert_eq!((1..=9).filter(|&b| PulseConduction::ThreeToOne.conducts(b)).collect::<Vec<_>>(),
vec![1, 4, 7]);
assert!((1..=20).all(|b| PulseConduction::OneToOne.conducts(b)));
assert!((1..=20).all(|b| !PulseConduction::None.conducts(b)));
}
}