Skip to content

Commit

Permalink
Merge pull request #34 from KG32/fix/ox-tox-otu
Browse files Browse the repository at this point in the history
Fix/ox tox otu
  • Loading branch information
KG32 authored Sep 28, 2024
2 parents edf852a + f844cb3 commit bf86465
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dive-deco"
version = "4.1.0"
version = "4.1.1"
edition = "2021"
license = "MIT"
description = "A dive decompression models library (Buehlmann ZH-L 16C)"
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ The Bühlmann decompression set of parameters is an Haldanian mathematical model
### Features

- step-by-step decompression model (ZH-L16C params version) calculations using depth, time and used gas (incl. helium mixes)
- NDL (no-decompression limit)
- GF (gradient factors) ascent profile conservatism
- current deco runtime / deco stop planner
- decompression stages as a runtime based on current model state
- TTS (current time to surface including ascent and all decompression stops)
- TTS @+5 (TTS after 5 mins given constant depth and breathing mix)
- TTS Δ+5 (absolute change in TTS after 5 mins given current depth and gas mix)
- NDL (no-decompression limit)
- decompression ceiling
- ceiling
- supersaturation
- GF99 (the raw percentage of the Bühlmann supersaturation at the current depth, i.e. super-saturation percent gradient)
- GFsurf(the surfacing gradient factor, i.e. super-saturation percentage gradient relative to the surface)
- CNS (central nervous system toxicity)
- oxygen toxicity
- CNS (central nervous system toxicity)
- OTU (pulmonary oxygen toxicity)
- configurable model settings
- gradient factors
- surface pressure
Expand Down
10 changes: 7 additions & 3 deletions src/buehlmann/buehlmann_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ impl DecoModel for BuehlmannModel {
self.state.ox_tox.cns()
}

fn otu(&self) -> Cns {
self.state.ox_tox.otu()
}

// deprecated

fn step(&mut self, depth: Depth, time: Seconds, gas: &Gas) {
Expand Down Expand Up @@ -267,7 +271,7 @@ impl BuehlmannModel {
fn recalculate(&mut self, record: RecordData) {
self.recalculate_compartments(&record);
// todo skip on sim
self.recalculate_cns(&record);
self.recalculate_ox_tox(&record);
}

fn recalculate_compartments(&mut self, record: &RecordData) {
Expand All @@ -285,8 +289,8 @@ impl BuehlmannModel {
leading.recalculate(&recalc_record, max_gf, surface_pressure);
}

fn recalculate_cns(&mut self, record: &RecordData) {
self.state.ox_tox.recalculate_cns(record, self.config().surface_pressure);
fn recalculate_ox_tox(&mut self, record: &RecordData) {
self.state.ox_tox.recalculate(record, self.config().surface_pressure);
}

fn max_gf(&mut self, gf: GradientFactors, depth: Depth) -> GradientFactor {
Expand Down
5 changes: 4 additions & 1 deletion src/common/deco_model.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::{AscentRatePerMinute, Cns, Depth, Gas, Minutes, Seconds};
use crate::common::{AscentRatePerMinute, Cns, Otu, Depth, Gas, Minutes, Seconds};
use crate::common::global_types::{CeilingType, MbarPressure};
use crate::common::deco::{DecoRuntime, DecoCalculationError};
use crate::common::ox_tox::OxTox;
Expand Down Expand Up @@ -61,6 +61,9 @@ pub trait DecoModel {
/// central nervous system oxygen toxicity
fn cns(&self) -> Cns;

/// pulmonary oxygen toxicity
fn otu(&self) -> Otu;

/// is in deco check
fn in_deco(&self) -> bool {
let ceiling_type = self.config().ceiling_type();
Expand Down
20 changes: 19 additions & 1 deletion src/common/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct Gas {
he_pp: Pressure,
}

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, PartialOrd)]
pub struct PartialPressures {
pub o2: Pressure,
pub n2: Pressure,
Expand All @@ -22,6 +22,12 @@ pub enum InertGas {
Nitrogen
}

impl ToString for Gas {
fn to_string(&self) -> String {
format!("{}/{}", self.o2_pp * 100., self.he_pp * 100.)
}
}

impl Gas {
/// init new gas with partial pressures (eg. 0.21, 0. for air)
pub fn new(o2_pp: Pressure, he_pp: Pressure) -> Self {
Expand All @@ -42,6 +48,10 @@ impl Gas {
}
}

pub fn id(&self) -> String {
self.to_string()
}

/// gas partial pressures
pub fn partial_pressures(&self, depth: Depth, surface_pressure: MbarPressure) -> PartialPressures {
let gas_pressure = (surface_pressure as f64 / 1000.) + (depth / 10.);
Expand Down Expand Up @@ -175,4 +185,12 @@ mod tests {
assert_eq!(calculated_end, expected_end);
}
}

#[test]
fn test_id() {
let ean32 = Gas::new(0.32, 0.);
assert_eq!(ean32.id(), "32/0");
let tmx2135 = Gas::new(0.21, 0.35);
assert_eq!(tmx2135.id(), "21/35");
}
}
1 change: 1 addition & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub use global_types::{
MbarPressure,
AscentRatePerMinute,
Cns,
Otu,
NDLType,
CeilingType,
};
Expand Down
11 changes: 7 additions & 4 deletions src/common/ox_tox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ impl OxTox {
self.otu
}

pub fn recalculate_cns(&mut self, record: &RecordData, surface_pressure: MbarPressure) {
pub fn recalculate(&mut self, record: &RecordData, surface_pressure: MbarPressure) {
self.recalculate_cns(record, surface_pressure);
self.recalculate_otu(record, surface_pressure);
}

fn recalculate_cns(&mut self, record: &RecordData, surface_pressure: MbarPressure) {
let RecordData { depth, time, gas } = *record;

let pp_o2 = gas
Expand All @@ -61,7 +66,7 @@ impl OxTox {
}
}

pub fn recalculate_otu(&mut self, record: &RecordData, surface_pressure: MbarPressure) {
fn recalculate_otu(&mut self, record: &RecordData, surface_pressure: MbarPressure) {
let RecordData { depth, time, gas } = *record;
let pp_o2 = gas
.inspired_partial_pressures(depth, surface_pressure)
Expand Down Expand Up @@ -194,13 +199,11 @@ mod tests {
fn test_otu_segment() {
let mut ox_tox = OxTox::default();
let ean32 = Gas::new(0.32, 0.);

let record = RecordData {
depth: 36.,
time: 22 * 60,
gas: &ean32
};

ox_tox.recalculate_otu(&record, 1013);
assert_eq!(ox_tox.otu(), 37.75920807052313);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/buehlmann_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,13 @@ fn test_gradual_ascent_with_deco() {
model.deco(vec![air, ean50]).unwrap();
}
}


#[test]
fn test_cns_otu() {
let mut model = BuehlmannModel::default();
model.record(40., 10 * 60, &Gas::air());
model.record_travel_with_rate(0., 10., &Gas::air());
assert_close_to_abs!(model.cns(), 4., 1.);
assert_close_to_abs!(model.otu(), 13., 1.);
}

0 comments on commit bf86465

Please sign in to comment.