hydr-structures 0.3.0

Hydraulic structures for the hydr simulation crates
Documentation
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//! Configured persistent concrete-law masks and sum-of-logs coefficient caches.
use numr::ops::BinaryOps;
use numr::ops::CompareOps;
use numr::ops::CorrectlyRoundedOps;
use numr::ops::IndexingOps;
use numr::ops::ReduceOps;
use numr::ops::ScalarOps;
use numr::ops::TypeConversionOps;
use numr::ops::UnaryOps;
use numr::{
    dtype::DType,
    runtime::{Runtime, RuntimeClient},
};

use crate::structures::blockage::effective;
use crate::structures::{
    DirectionPolicy, StructureConditionKind, StructureElement, StructureError,
    StructureInputErrorKind, StructureLaw,
};
use numr::tensor::Tensor;

use std::collections::BTreeSet;

pub(crate) struct TensorStructureContext<R: Runtime<DType = DType>> {
    pub culvert: Option<Box<dyn super::culvert::CulvertLaw<R>>>,
    pub gate: Option<super::gate::TensorGateContext<R>>,
    pub pump: Option<super::pump::TensorPumpContext<R>>,
    pub elements: Vec<StructureElement>,
    pub client: R::Client,
    pub device: R::Device,
    pub zero: Tensor<R>,
    pub one: Tensor<R>,
    pub orifice: Tensor<R>,
    pub weir: Tensor<R>,
    pub crown: Tensor<R>,
    pub crest: Tensor<R>,
    pub allow_a: Tensor<R>,
    pub allow_b: Tensor<R>,
    pub log_orifice_scale: Tensor<R>,
    pub log_weir_scale: Tensor<R>,
    pub log_gravity: Tensor<R>,
    pub log_two: Tensor<R>,
    /// Closed-row mask, or `None` when no element is closed.
    pub closed: Option<Tensor<R>>,
}
impl<R: Runtime<DType = DType>> TensorStructureContext<R> {
    /// Checks every law except the culvert, which rejects.
    pub(crate) fn new(
        client: R::Client,
        elements: Vec<StructureElement>,
    ) -> Result<Self, StructureError>
    where
        R::Client: numr::ops::BinaryOps<R>
            + numr::ops::CompareOps<R>
            + numr::ops::CorrectlyRoundedOps<R>
            + numr::ops::ReduceOps<R>
            + numr::ops::ScalarOps<R>
            + numr::ops::TypeConversionOps<R>
            + numr::ops::UnaryOps<R>,
    {
        Self::build(client, elements, false)
    }

    /// Checks every law and uploads the culvert rows.
    pub(crate) fn with_culverts(
        client: R::Client,
        elements: Vec<StructureElement>,
    ) -> Result<Self, StructureError>
    where
        R::Client: crate::structures::CulvertClient<R>,
    {
        let mut context = Self::build(client, elements, true)?;
        context.culvert = super::culvert::TensorCulvertLaw::new(&context)?
            .map(|law| Box::new(law) as Box<dyn super::culvert::CulvertLaw<R>>);
        Ok(context)
    }

    fn build(
        client: R::Client,
        elements: Vec<StructureElement>,
        culverts: bool,
    ) -> Result<Self, StructureError>
    where
        R::Client: numr::ops::BinaryOps<R>
            + numr::ops::CompareOps<R>
            + numr::ops::CorrectlyRoundedOps<R>
            + numr::ops::ReduceOps<R>
            + numr::ops::ScalarOps<R>
            + numr::ops::TypeConversionOps<R>
            + numr::ops::UnaryOps<R>,
    {
        if elements.is_empty() {
            return Err(StructureError::input(
                StructureInputErrorKind::Definition,
                "structure batch requires at least one explicit law",
            ));
        }
        let mut ids = BTreeSet::new();
        let n = elements.len();
        let mut closed = Vec::with_capacity(n);
        let (
            mut orifice,
            mut weir,
            mut crown,
            mut crest,
            mut allow_a,
            mut allow_b,
            mut cd,
            mut area,
            mut cw,
            mut length,
            mut gravity,
        ) = (
            Vec::with_capacity(n),
            Vec::with_capacity(n),
            Vec::with_capacity(n),
            Vec::with_capacity(n),
            Vec::with_capacity(n),
            Vec::with_capacity(n),
            Vec::with_capacity(n),
            Vec::with_capacity(n),
            Vec::with_capacity(n),
            Vec::with_capacity(n),
            Vec::with_capacity(n),
        );
        for (index, e) in elements.iter().enumerate() {
            for (value, field) in [
                (&e.id, "structure.id"),
                (&e.endpoint_a, "endpoint_a"),
                (&e.endpoint_b, "endpoint_b"),
            ] {
                if value.trim().is_empty() || value.chars().any(char::is_control) {
                    return Err(StructureError::input(StructureInputErrorKind::Identity,format!("{field}={value:?}: supply explicit nonempty identity without control characters")).at(index));
                }
            }
            if !ids.insert(&e.id) || e.endpoint_a == e.endpoint_b {
                return Err(StructureError::input(StructureInputErrorKind::Identity,format!("structure={:?},endpoints={:?}/{:?}: require unique structure IDs and distinct endpoints",e.id,e.endpoint_a,e.endpoint_b)).at(index));
            }
            if e.datum.trim().is_empty() || e.datum.chars().any(char::is_control) {
                return Err(StructureError::input(
                    StructureInputErrorKind::Datum,
                    "supply explicit common vertical datum",
                )
                .at(index));
            }
            effective::check(e).map_err(|error| error.at(index))?;
            closed.push(if e.condition.kind() == StructureConditionKind::Closed {
                1.0
            } else {
                0.0
            });
            let symmetric = match &e.law {
                StructureLaw::Culvert(parameters) => {
                    if !culverts {
                        return Err(StructureError::input(
                            StructureInputErrorKind::Definition,
                            "a culvert law needs certified roots and quadrature on the runtime. Build the batch with StructureBatch::with_culverts",
                        )
                        .at(index));
                    }
                    orifice.push(0.0);
                    weir.push(0.0);
                    crown.push(0.0);
                    crest.push(0.0);
                    cd.push(1.0);
                    area.push(1.0);
                    cw.push(1.0);
                    length.push(1.0);
                    gravity.push(1.0);
                    culvert_ends(parameters, e.direction).map_err(|error| error.at(index))?;
                    true
                }
                StructureLaw::HydrostaticRectangularGate(parameters) => {
                    orifice.push(0.0);
                    weir.push(0.0);
                    crown.push(0.0);
                    crest.push(0.0);
                    cd.push(1.0);
                    area.push(1.0);
                    cw.push(1.0);
                    length.push(1.0);
                    gravity.push(1.0);
                    parameters.applicability().symmetric_reversal
                }
                StructureLaw::Pump(_) => {
                    orifice.push(0.0);
                    weir.push(0.0);
                    crown.push(0.0);
                    crest.push(0.0);
                    cd.push(1.0);
                    area.push(1.0);
                    cw.push(1.0);
                    length.push(1.0);
                    gravity.push(1.0);
                    if e.direction == DirectionPolicy::Bidirectional {
                        return Err(StructureError::input(
                            StructureInputErrorKind::Definition,
                            "pump requires fixed A-to-B or B-to-A orientation",
                        )
                        .at(index));
                    }
                    false
                }
                StructureLaw::SubmergedOrifice(p) => {
                    orifice.push(1.0);
                    weir.push(0.0);
                    crown.push(p.crown_m());
                    crest.push(0.0);
                    cd.push(p.discharge_coefficient());
                    area.push(effective::orifice_area(&e.condition, p));
                    cw.push(1.0);
                    length.push(1.0);
                    gravity.push(p.gravity_m_per_s2());
                    p.applicability().symmetric_reversal
                }
                StructureLaw::SuppressedRectangularWeir(p) => {
                    orifice.push(0.0);
                    weir.push(1.0);
                    crown.push(0.0);
                    crest.push(p.crest_m());
                    cd.push(1.0);
                    area.push(1.0);
                    cw.push(p.discharge_coefficient());
                    length.push(effective::weir_crest_length(&e.condition, p));
                    gravity.push(1.0);
                    p.applicability().symmetric_reversal
                }
            };
            if e.direction == DirectionPolicy::Bidirectional && !symmetric {
                return Err(StructureError::input(
                    StructureInputErrorKind::Definition,
                    "bidirectional flow requires explicit symmetric coefficient applicability",
                )
                .at(index));
            }
            allow_a.push(if e.direction == DirectionPolicy::BtoAOnly {
                0.0
            } else {
                1.0
            });
            allow_b.push(if e.direction == DirectionPolicy::AtoBOnly {
                0.0
            } else {
                1.0
            });
        }
        drop(ids);
        let device = client.device().clone();
        if let Some(error) = crate::tensor::probe_f64::<R, _>(&client)? {
            return Err(StructureError::input(
                StructureInputErrorKind::Definition,
                error.message("structures"),
            ));
        }
        if let Some(error) = crate::tensor::probe_correctly_rounded::<R>() {
            return Err(StructureError::input(
                StructureInputErrorKind::Definition,
                error.message("structures"),
            ));
        }
        let tensor = |v: &[f64]| Tensor::<R>::from_slice(v, &[v.len()], &device);
        let one = tensor(&vec![1.0; n])?;
        let log_orifice_scale = client.add(
            &client.cr_log(&tensor(&cd)?)?,
            &client.cr_log(&tensor(&area)?)?,
        )?;
        let log_weir_scale = client.add(
            &client.cr_log(&tensor(&cw)?)?,
            &client.cr_log(&tensor(&length)?)?,
        )?;
        let context = Self {
            culvert: None,
            closed: if closed.contains(&1.0) {
                Some(tensor(&closed)?)
            } else {
                None
            },
            gate: super::gate::TensorGateContext::new(&elements, &client, &device)?,
            pump: super::pump::TensorPumpContext::new(&elements, &device)?,
            zero: tensor(&vec![0.0; n])?,
            orifice: tensor(&orifice)?,
            weir: tensor(&weir)?,
            crown: tensor(&crown)?,
            crest: tensor(&crest)?,
            allow_a: tensor(&allow_a)?,
            allow_b: tensor(&allow_b)?,
            log_gravity: client.cr_log(&tensor(&gravity)?)?,
            log_two: client.cr_log(&client.mul_scalar(&one, 2.0)?)?,
            log_orifice_scale,
            log_weir_scale,
            one,
            elements,
            client,
            device,
        };
        for value in [
            &context.log_orifice_scale,
            &context.log_weir_scale,
            &context.log_gravity,
            &context.log_two,
        ] {
            context.finite(value, "cached positive coefficient/geometry logarithm")?;
        }
        if let Some(gate) = &context.gate {
            context.finite(&gate.log_scale, "cached gate coefficient/width logarithm")?;
            context.finite(&gate.log_root_gravity, "cached gate gravity logarithm")?;
        }
        Ok(context)
    }
    pub(crate) fn tensor(&self, values: &[f64]) -> Result<Tensor<R>, StructureError> {
        Ok(Tensor::<R>::from_slice(
            values,
            &[values.len()],
            &self.device,
        )?)
    }
    pub(crate) fn any(&self, mask: &Tensor<R>) -> Result<bool, StructureError>
    where
        R::Client: numr::ops::ReduceOps<R>,
    {
        Ok(self.client.any(mask, &[], false)?.item::<f64>()? != 0.0)
    }
    fn lane(&self, mask: &Tensor<R>) -> Result<Option<usize>, StructureError>
    where
        R::Client: numr::ops::ReduceOps<R> + numr::ops::IndexingOps<R>,
    {
        if !self.any(mask)? {
            return Ok(None);
        }
        let rejected = self.client.argmax(mask, 0, false)?.item::<i64>()?;
        let index = usize::try_from(rejected)
            .ok()
            .filter(|&index| index < mask.numel() && index < self.elements.len())
            .ok_or_else(|| {
                StructureError::numerical("structure rejection mask contradicts reduction")
            })?;
        Ok(Some(index))
    }
    pub(crate) fn reject(&self, mask: &Tensor<R>, quantity: &str) -> Result<(), StructureError>
    where
        R::Client: numr::ops::ReduceOps<R> + numr::ops::IndexingOps<R>,
    {
        if let Some(index) = self.lane(mask)? {
            return Err(StructureError::numerical(format!("structure={:?},{quantity}: rescale supported heads/parameters or select a representable held interval",self.elements[index].id)).at(index));
        }
        Ok(())
    }
    pub(crate) fn reject_support(
        &self,
        mask: &Tensor<R>,
        quantity: &str,
    ) -> Result<(), StructureError>
    where
        R::Client: numr::ops::ReduceOps<R> + numr::ops::IndexingOps<R>,
    {
        if let Some(index) = self.lane(mask)? {
            return Err(StructureError::input(StructureInputErrorKind::UnsupportedRegime,format!("structure={:?},{quantity}: supply complete support in this law's admitted physical regime",self.elements[index].id)).at(index));
        }
        Ok(())
    }
    pub(crate) fn reject_input(
        &self,
        mask: &Tensor<R>,
        kind: StructureInputErrorKind,
        quantity: &str,
    ) -> Result<(), StructureError>
    where
        R::Client: numr::ops::ReduceOps<R> + numr::ops::IndexingOps<R>,
    {
        if let Some(index) = self.lane(mask)? {
            return Err(StructureError::input(
                kind,
                format!(
                    "structure={:?},{quantity}: supply an admitted explicit operating point",
                    self.elements[index].id
                ),
            )
            .at(index));
        }
        Ok(())
    }
    pub(crate) fn finite(&self, value: &Tensor<R>, quantity: &str) -> Result<(), StructureError>
    where
        R::Client: numr::ops::BinaryOps<R>
            + numr::ops::ReduceOps<R>
            + numr::ops::IndexingOps<R>
            + numr::ops::TypeConversionOps<R>
            + numr::ops::UnaryOps<R>,
    {
        self.reject(
            &self.client.maximum(
                &self
                    .client
                    .cast(&self.client.isnan(value)?, value.dtype())?,
                &self
                    .client
                    .cast(&self.client.isinf(value)?, value.dtype())?,
            )?,
            quantity,
        )
    }
    pub(crate) fn positive(&self, value: &Tensor<R>, quantity: &str) -> Result<(), StructureError>
    where
        R::Client: numr::ops::BinaryOps<R>
            + numr::ops::CompareOps<R>
            + numr::ops::ReduceOps<R>
            + numr::ops::TypeConversionOps<R>
            + numr::ops::UnaryOps<R>,
    {
        self.finite(value, quantity)?;
        self.reject(&self.client.le(value, &self.zero)?, quantity)
    }
}

/// Checks that a culvert declares an end for each admitted direction and no other.
fn culvert_ends(
    parameters: &crate::structures::CulvertParameters,
    direction: DirectionPolicy,
) -> Result<(), StructureError> {
    let ends = parameters.ends();
    let (a, b) = (ends.a.is_some(), ends.b.is_some());
    let expected = match direction {
        DirectionPolicy::Bidirectional => (true, true),
        DirectionPolicy::AtoBOnly => (true, false),
        DirectionPolicy::BtoAOnly => (false, true),
    };
    if (a, b) != expected {
        return Err(StructureError::input(
            StructureInputErrorKind::Definition,
            format!(
                "direction={direction:?},end_a={a},end_b={b}: declare the culvert end of each admitted direction and no other. Bidirectional needs both ends, AtoBOnly the A end, BtoAOnly the B end"
            ),
        ));
    }
    Ok(())
}

impl<R: Runtime<DType = DType>> std::fmt::Debug for TensorStructureContext<R> {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("TensorStructureContext")
            .field("elements", &self.elements)
            .finish_non_exhaustive()
    }
}