use super::EngineError;
use crate::model::Domain;
use std::collections::BTreeSet;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Axis {
X,
Y,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct BoundaryFace {
pub axis: Axis,
pub column: usize,
pub row: usize,
}
#[derive(Clone, Debug, PartialEq)]
pub struct FluxPort {
pub face: BoundaryFace,
pub water_rate: f64,
pub momentum_rate: [f64; 2],
pub signal_speed: f64,
pub provenance: String,
}
#[derive(Clone, Debug, PartialEq)]
pub struct StageBoundary {
pub face: BoundaryFace,
pub elevation: f64,
pub inflow_tangential_velocity: Option<f64>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct DischargeBoundary {
pub face: BoundaryFace,
pub outward_rate: f64,
pub inflow_tangential_velocity: Option<f64>,
}
#[derive(Clone, Debug, PartialEq)]
pub enum ExteriorState {
Dry,
Wet {
elevation: f64,
outward_velocity: f64,
tangential_velocity: f64,
},
}
#[derive(Clone, Debug, PartialEq)]
pub struct ExteriorBoundary {
pub face: BoundaryFace,
pub state: ExteriorState,
}
impl ExteriorBoundary {
#[doc(hidden)]
pub fn conserved(&self, domain: &Domain) -> Result<[f64; 3], EngineError> {
let cell = exterior_cell(domain, self.face)?;
let ExteriorState::Wet {
elevation,
outward_velocity,
tangential_velocity,
} = self.state
else {
return Ok([0.0; 3]);
};
let h = elevation - domain.bed()[cell];
let normal = h * outward_velocity;
let tangent = h * tangential_velocity;
if ![
elevation,
outward_velocity,
tangential_velocity,
h,
normal,
tangent,
]
.iter()
.all(|value| value.is_finite())
|| h <= 0.0
|| (outward_velocity != 0.0 && normal == 0.0)
|| (tangential_velocity != 0.0 && tangent == 0.0)
{
return Err(EngineError::rejected(
"exterior boundary",
format!(
"state={self:?}, bed={}: require finite positive wet depth and representable velocity products",
domain.bed()[cell]
),
));
}
Ok([h, normal, tangent])
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum BoundaryForcing {
Flux(FluxPort),
Stage(StageBoundary),
Discharge(DischargeBoundary),
Exterior(ExteriorBoundary),
Transmissive(BoundaryFace),
}
impl BoundaryForcing {
pub fn face(&self) -> BoundaryFace {
match self {
Self::Flux(value) => value.face,
Self::Stage(value) => value.face,
Self::Discharge(value) => value.face,
Self::Exterior(value) => value.face,
Self::Transmissive(face) => *face,
}
}
}
impl From<FluxPort> for BoundaryForcing {
fn from(value: FluxPort) -> Self {
Self::Flux(value)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct BoundaryConditions {
open_faces: Vec<BoundaryFace>,
}
impl BoundaryConditions {
pub fn impermeable_walls() -> Self {
Self {
open_faces: Vec::new(),
}
}
pub fn with_open_faces(
domain: &Domain,
open_faces: Vec<BoundaryFace>,
) -> Result<Self, EngineError> {
let mut seen = BTreeSet::new();
for face in &open_faces {
exterior_cell(domain, *face)?;
if !seen.insert(*face) {
return Err(EngineError::rejected(
"boundary",
format!("duplicate face {face:?}: supply each exterior face once"),
));
}
}
Ok(Self { open_faces })
}
pub fn open_faces(&self) -> &[BoundaryFace] {
&self.open_faces
}
}
#[inline]
pub fn exterior_cell(domain: &Domain, face: BoundaryFace) -> Result<usize, EngineError> {
let (nx, ny) = domain.grid().dimensions();
let cell = match face.axis {
Axis::X if face.row < ny && (face.column == 0 || face.column == nx) => {
Some(face.row * nx + if face.column == 0 { 0 } else { nx - 1 })
}
Axis::Y if face.column < nx && (face.row == 0 || face.row == ny) => {
Some(if face.row == 0 {
face.column
} else {
(ny - 1) * nx + face.column
})
}
_ => None,
};
match cell {
Some(cell) if domain.active()[cell] => Ok(cell),
_ => Err(EngineError::rejected(
"boundary",
format!("face {face:?} is not an active exterior face: correct its support"),
)),
}
}