Skip to content

Commit

Permalink
Merge pull request #39 from KG32/feat/update-config
Browse files Browse the repository at this point in the history
update buehlmann model config
  • Loading branch information
KG32 authored Oct 26, 2024
2 parents e2174fc + 8d2bce9 commit 3647a56
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 41 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.3.2"
version = "4.3.3"
edition = "2021"
license = "MIT"
description = "A dive decompression models library (Buehlmann ZH-L 16C)"
Expand Down
52 changes: 20 additions & 32 deletions src/buehlmann/buehlmann_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,11 @@ impl BuehlmannConfig {
let gf_range = 1..=100;

if !gf_range.contains(gf_low) || !gf_range.contains(gf_high) {
return Err(ConfigValidationErr {
field: "gf",
reason: GF_RANGE_ERR_MSG,
});
return Err(ConfigValidationErr::new("gf", GF_RANGE_ERR_MSG));
}

if gf_low > gf_high {
return Err(ConfigValidationErr {
field: "gf",
reason: GF_ORDER_ERR_MSG,
});
return Err(ConfigValidationErr::new("gf", GF_ORDER_ERR_MSG));
}

Ok(())
Expand All @@ -130,10 +124,10 @@ impl BuehlmannConfig {
) -> Result<(), ConfigValidationErr> {
let mbar_pressure_range = 500..=1500;
if !mbar_pressure_range.contains(surface_pressure) {
return Err(ConfigValidationErr {
field: "surface_pressure",
reason: SURFACE_PRESSURE_ERR_MSG,
});
return Err(ConfigValidationErr::new(
"surface_pressure",
SURFACE_PRESSURE_ERR_MSG,
));
}

Ok(())
Expand All @@ -145,10 +139,10 @@ impl BuehlmannConfig {
) -> Result<(), ConfigValidationErr> {
let ascent_rate_range = 1.0..=30.0;
if !ascent_rate_range.contains(deco_ascent_rate) {
return Err(ConfigValidationErr {
field: "deco_ascent_rate",
reason: DECO_ASCENT_RATE_ERR_MSG,
});
return Err(ConfigValidationErr::new(
"deco_ascent_rate",
DECO_ASCENT_RATE_ERR_MSG,
));
}

Ok(())
Expand Down Expand Up @@ -184,10 +178,7 @@ mod tests {
let config = BuehlmannConfig::new().with_gradient_factors(gf_low, gf_high);
assert_eq!(
config.validate(),
Err(ConfigValidationErr {
field: "gf",
reason: GF_RANGE_ERR_MSG
})
Err(ConfigValidationErr::new("gf", GF_RANGE_ERR_MSG))
);
}
}
Expand All @@ -197,10 +188,7 @@ mod tests {
let config = BuehlmannConfig::new().with_gradient_factors(90, 80);
assert_eq!(
config.validate(),
Err(ConfigValidationErr {
field: "gf",
reason: GF_ORDER_ERR_MSG
})
Err(ConfigValidationErr::new("gf", GF_ORDER_ERR_MSG))
);
}

Expand All @@ -218,10 +206,10 @@ mod tests {
let config = BuehlmannConfig::new().with_surface_pressure(invalid_case);
assert_eq!(
config.validate(),
Err(ConfigValidationErr {
field: "surface_pressure",
reason: SURFACE_PRESSURE_ERR_MSG
})
Err(ConfigValidationErr::new(
"surface_pressure",
SURFACE_PRESSURE_ERR_MSG
))
);
}
}
Expand All @@ -240,10 +228,10 @@ mod tests {
let config = BuehlmannConfig::new().with_deco_ascent_rate(invalid_case);
assert_eq!(
config.validate(),
Err(ConfigValidationErr {
field: "deco_ascent_rate",
reason: DECO_ASCENT_RATE_ERR_MSG
})
Err(ConfigValidationErr::new(
"deco_ascent_rate",
DECO_ASCENT_RATE_ERR_MSG
))
);
}
}
Expand Down
43 changes: 39 additions & 4 deletions src/buehlmann/buehlmann_model.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::cmp::Ordering;

use crate::buehlmann::buehlmann_config::BuehlmannConfig;
use crate::buehlmann::compartment::{Compartment, Supersaturation};
use crate::buehlmann::zhl_values::{ZHLParams, ZHL_16C_N2_16A_HE_VALUES};
use crate::common::{
AscentRatePerMinute, Cns, Deco, DecoModel, DecoModelConfig, Depth, DiveState, Gas,
GradientFactor, Minutes, OxTox, RecordData, Seconds,
AscentRatePerMinute, Cns, ConfigValidationErr, Deco, DecoModel, DecoModelConfig, Depth,
DiveState, Gas, GradientFactor, Minutes, OxTox, RecordData, Seconds,
};
use crate::{CeilingType, DecoCalculationError, DecoRuntime, GradientFactors, Sim};
use std::cmp::Ordering;

const NDL_CUT_OFF_MINS: Minutes = 99;

Expand Down Expand Up @@ -264,6 +263,15 @@ impl BuehlmannModel {
self.compartments.clone()
}

pub fn update_config(
&mut self,
new_config: BuehlmannConfig,
) -> Result<(), ConfigValidationErr> {
new_config.validate()?;
self.config = new_config;
Ok(())
}

fn leading_comp(&self) -> &Compartment {
let mut leading_comp: &Compartment = &self.compartments[0];
for compartment in &self.compartments[1..] {
Expand Down Expand Up @@ -503,4 +511,31 @@ mod tests {

assert_eq!(initial_gfs, surface_interval_gfs);
}

#[test]
fn test_updating_config() {
let mut model = BuehlmannModel::default();
let initial_config = model.config();
let new_config = BuehlmannConfig::new()
.with_gradient_factors(30, 70)
.with_round_ceiling(true)
.with_ceiling_type(CeilingType::Adaptive)
.with_round_ceiling(true);
assert_ne!(initial_config, new_config, "given configs aren't identical");

model.update_config(new_config).unwrap();
let updated_config = model.config();
assert_eq!(updated_config, new_config, "new config saved");

let invalid_config = new_config.with_gradient_factors(0, 150);
let update_res = model.update_config(invalid_config);
assert_eq!(
update_res,
Err(ConfigValidationErr {
field: "gf".to_string(),
reason: "GF values have to be in 1-100 range".to_string(),
}),
"invalid config update results in Err"
);
}
}
15 changes: 12 additions & 3 deletions src/common/deco_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@ use crate::common::ox_tox::OxTox;
use crate::common::{AscentRatePerMinute, Cns, Depth, Gas, Minutes, Otu, Seconds};

#[derive(Debug, PartialEq)]
pub struct ConfigValidationErr<'a> {
pub field: &'a str,
pub reason: &'a str,
pub struct ConfigValidationErr {
pub field: String,
pub reason: String,
}

impl ConfigValidationErr {
pub fn new(field: &str, reason: &str) -> Self {
Self {
field: field.to_string(),
reason: reason.to_string(),
}
}
}

pub trait DecoModelConfig {
Expand Down

0 comments on commit 3647a56

Please sign in to comment.