1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use super::{Domain, ModelError};
/// Depth and depth-integrated horizontal momentum for one Cartesian cell.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct CellState {
/// Water depth in metres.
pub h: f64,
/// Depth times x velocity in square metres per second.
pub qx: f64,
/// Depth times y velocity in square metres per second.
pub qy: f64,
}
/// Checked, owned row-major flow state.
#[derive(Clone, Debug, PartialEq)]
pub struct FlowState {
cells: Vec<CellState>,
}
impl FlowState {
/// Creates flow state compatible with the supplied domain.
///
/// Positive films remain positive. Dry and inactive cells require zero momentum.
///
/// # Errors
///
/// Rejects mismatched shape, invalid depths or momenta, unsupported-cell water, and unrepresentable derived quantities.
pub fn new(domain: &Domain, cells: Vec<CellState>) -> Result<Self, ModelError> {
let count = domain.grid().cell_count();
if cells.len() != count {
return Err(ModelError::invalid(
"cells",
None,
format!(
"received {} cells: supply exactly {count} row-major states",
cells.len()
),
));
}
let area = domain.grid().cell_area();
let mut total_volume = 0.0;
for (index, ((cell, active), bed)) in cells
.iter()
.zip(domain.active())
.zip(domain.bed())
.enumerate()
{
if !cell.h.is_finite() || cell.h < 0.0 {
return Err(ModelError::invalid(
"h",
Some(index),
format!(
"depth {} is invalid: supply finite nonnegative metres",
cell.h
),
));
}
for (field, momentum) in [("qx", cell.qx), ("qy", cell.qy)] {
if !momentum.is_finite() {
return Err(ModelError::invalid(
field,
Some(index),
format!(
"momentum {momentum} is nonfinite: supply finite square metres per second"
),
));
}
if cell.h == 0.0 && momentum != 0.0 {
return Err(ModelError::invalid(
field,
Some(index),
format!("dry cell carries momentum {momentum}: supply zero momentum"),
));
}
if cell.h > 0.0 && !(momentum / cell.h).is_finite() {
return Err(ModelError::invalid(
field,
Some(index),
format!(
"momentum {momentum} divided by depth {} overflows: reduce velocity",
cell.h
),
));
}
}
if !active && *cell != CellState::default() {
return Err(ModelError::invalid(
"cells",
Some(index),
format!("inactive cell contains {cell:?}: supply zero depth and momentum"),
));
}
if !(bed + cell.h).is_finite() {
return Err(ModelError::invalid(
"h",
Some(index),
format!(
"bed {bed} plus depth {} overflows: reduce elevation or depth",
cell.h
),
));
}
let volume = cell.h * area;
if !volume.is_finite() || (cell.h > 0.0 && volume == 0.0) {
return Err(ModelError::invalid(
"h",
Some(index),
format!(
"depth {} times cell area {area} is unrepresentable: rescale the domain or depth",
cell.h
),
));
}
total_volume += volume;
if !total_volume.is_finite() {
return Err(ModelError::invalid(
"cells",
Some(index),
"total water volume overflows: reduce the domain or initial storage",
));
}
}
Ok(Self { cells })
}
/// Returns row-major cell states in SI units.
pub fn cells(&self) -> &[CellState] {
&self.cells
}
}