use super::EngineError;
#[derive(Clone, Debug, PartialEq)]
pub struct BoundaryTolerances {
pub discharge_absolute: f64,
pub discharge_relative: f64,
pub characteristic_relative: f64,
pub critical_relative: f64,
}
impl Default for BoundaryTolerances {
fn default() -> Self {
Self {
discharge_absolute: 1e-12,
discharge_relative: 1e-12,
characteristic_relative: 1e-12,
critical_relative: 64.0 * f64::EPSILON,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct SolverConfig {
pub gravity: f64,
pub cfl: f64,
pub max_step: f64,
pub min_step: f64,
pub max_retries: usize,
pub root_absolute_depth: f64,
pub root_relative_depth: f64,
pub root_iterations: usize,
pub volume_absolute: f64,
pub volume_relative: f64,
pub momentum_film_depth: f64,
pub max_cumulative_water_adjustment_m3: f64,
pub boundary: BoundaryTolerances,
}
impl Default for SolverConfig {
fn default() -> Self {
Self {
gravity: 9.81,
cfl: 0.45,
max_step: 1.0,
min_step: 1e-12,
max_retries: 64,
root_absolute_depth: 1e-12,
root_relative_depth: 1e-12,
root_iterations: 128,
volume_absolute: 1e-9,
volume_relative: 1e-12,
momentum_film_depth: 0.0,
max_cumulative_water_adjustment_m3: 1e-9,
boundary: BoundaryTolerances::default(),
}
}
}
impl SolverConfig {
#[doc(hidden)]
pub fn check(&self) -> Result<(), EngineError> {
for (field, value) in [
("gravity", self.gravity),
("cfl", self.cfl),
("max_step", self.max_step),
("min_step", self.min_step),
("root_absolute_depth", self.root_absolute_depth),
("root_relative_depth", self.root_relative_depth),
("volume_absolute", self.volume_absolute),
("volume_relative", self.volume_relative),
] {
if !value.is_finite() || value <= 0.0 {
return Err(EngineError::rejected(
"config",
format!("{field}={value}: supply a finite positive value"),
));
}
}
if self.cfl > 0.45
|| self.min_step > self.max_step
|| self.root_iterations == 0
|| !self.momentum_film_depth.is_finite()
|| self.momentum_film_depth < 0.0
|| !self.max_cumulative_water_adjustment_m3.is_finite()
|| self.max_cumulative_water_adjustment_m3 < 0.0
{
return Err(EngineError::rejected(
"config",
format!(
"{self:?}: require CFL <=0.45, min_step <=max_step, positive root iterations, and finite nonnegative film depth and adjustment budget"
),
));
}
for (name, value) in [
("discharge_absolute", self.boundary.discharge_absolute),
("discharge_relative", self.boundary.discharge_relative),
(
"characteristic_relative",
self.boundary.characteristic_relative,
),
("critical_relative", self.boundary.critical_relative),
] {
if !value.is_finite() || value <= 0.0 || (name != "discharge_absolute" && value >= 1.0)
{
return Err(EngineError::rejected(
"config",
format!(
"boundary {name}={value}: supply a finite positive tolerance with relative values below one"
),
));
}
}
Ok(())
}
}