hydr_structures/structures/operating_source/
batch.rs1use super::{
4 OperatingSourceBinding, OperatingSourceFrame, OperatingSourceProposal, OperatingSourceSupport,
5};
6use crate::structures::tensor::{
7 DerivedGateOperations, DerivedPumpOperationData, DerivedPumpOperations, TensorStructureHeads,
8 TensorStructureInput,
9 operating_source::{TensorOperatingSourceContext, TensorOperatingSourcePoint, assemble},
10};
11use crate::structures::{
12 StructureBatch, StructureElement, StructureError, StructureHeads, StructureInputErrorKind,
13 StructureOperation, StructureProposal,
14};
15use numr::dtype::DType;
16use numr::runtime::Runtime;
17use std::sync::Arc;
18
19#[derive(Clone, Debug)]
21pub struct OperatingSourceBatch<R: Runtime<DType = DType>> {
22 pub(crate) context: Arc<TensorOperatingSourceContext<R>>,
23}
24impl<R: Runtime<DType = DType>> StructureBatch<R> {
25 pub fn with_operating_sources(
27 &self,
28 sources: Vec<OperatingSourceBinding<'_, R>>,
29 ) -> Result<OperatingSourceBatch<R>, StructureError>
30 where
31 R::Client: numr::ops::BinaryOps<R>
32 + numr::ops::CompareOps<R>
33 + numr::ops::ConditionalOps<R>
34 + numr::ops::ReduceOps<R>
35 + numr::ops::ScalarOps<R>
36 + numr::ops::TwoSumOps<R>
37 + numr::ops::TypeConversionOps<R>
38 + numr::ops::UnaryOps<R>,
39 {
40 Ok(OperatingSourceBatch {
41 context: Arc::new(super::binding::build(Arc::clone(&self.context), sources)?),
42 })
43 }
44}
45impl<R: Runtime<DType = DType>> OperatingSourceBatch<R> {
46 pub fn elements(&self) -> &[StructureElement] {
48 &self.context.structures.elements
49 }
50 pub fn support(&self) -> Option<OperatingSourceSupport> {
52 self.context.support
53 }
54 pub(super) fn check_time(&self, time_s: f64) -> Result<(), StructureError> {
55 if !time_s.is_finite() {
56 return Err(StructureError::input(
57 StructureInputErrorKind::NonfiniteInput,
58 "operating source point requires finite original query time",
59 ));
60 }
61 if self
62 .support()
63 .is_some_and(|support| time_s < support.start_s || time_s > support.end_s)
64 {
65 return Err(StructureError::input(
66 StructureInputErrorKind::UnsupportedRegime,
67 "operating source query lies outside common closed support",
68 ));
69 }
70 Ok(())
71 }
72 pub fn propose_at(
77 &self,
78 time_s: f64,
79 heads: &[StructureHeads],
80 manual_operations: &[StructureOperation],
81 frames: &[OperatingSourceFrame<'_, R>],
82 ) -> Result<OperatingSourceProposal<R>, StructureError>
83 where
84 R::Client: numr::ops::CumulativeOps<R>
85 + numr::ops::BinaryOps<R>
86 + numr::ops::CompareOps<R>
87 + numr::ops::ConditionalOps<R>
88 + numr::ops::CorrectlyRoundedOps<R>
89 + numr::ops::IndexingOps<R>
90 + numr::ops::ReduceOps<R>
91 + numr::ops::ScalarOps<R>
92 + numr::ops::ShapeOps<R>
93 + numr::ops::TwoSumOps<R>
94 + numr::ops::TypeConversionOps<R>
95 + numr::ops::UnaryOps<R>
96 + solvr::interpolate::Interp1dAlgorithms<R>
97 + solvr::interpolate::LinearInterp1dDiagnostics<R>,
98 {
99 self.check_time(time_s)?;
100 let context = &self.context.structures;
101 let heads_input = TensorStructureInput::heads(context, heads)?;
102 if manual_operations.len() != self.context.manual_slots.len() {
103 return Err(StructureError::input(
104 StructureInputErrorKind::OperatingPoint,
105 "supply exactly every manual-owned operation in original complete subsequence order",
106 ));
107 }
108 let point = assemble(&self.context, time_s, frames)?;
109 let hydraulic = self.hydraulic(heads, heads_input, manual_operations, &point)?;
110 Ok(OperatingSourceProposal::new(
111 Arc::clone(&self.context),
112 time_s,
113 hydraulic,
114 point,
115 ))
116 }
117 pub(super) fn hydraulic(
119 &self,
120 heads: &[StructureHeads],
121 heads_input: TensorStructureHeads<R>,
122 manual_operations: &[StructureOperation],
123 point: &TensorOperatingSourcePoint<R>,
124 ) -> Result<StructureProposal<R>, StructureError>
125 where
126 R::Client: numr::ops::CumulativeOps<R>
127 + numr::ops::BinaryOps<R>
128 + numr::ops::CompareOps<R>
129 + numr::ops::ConditionalOps<R>
130 + numr::ops::CorrectlyRoundedOps<R>
131 + numr::ops::IndexingOps<R>
132 + numr::ops::ReduceOps<R>
133 + numr::ops::ScalarOps<R>
134 + numr::ops::ShapeOps<R>
135 + numr::ops::TwoSumOps<R>
136 + numr::ops::TypeConversionOps<R>
137 + numr::ops::UnaryOps<R>
138 + solvr::interpolate::Interp1dAlgorithms<R>,
139 {
140 let context = &self.context.structures;
141 let input = TensorStructureInput::with_derived_heads(
142 context,
143 heads_input,
144 manual_operations,
145 Some(DerivedGateOperations {
146 slots: &self.context.derived_slots,
147 mask: &self.context.derived_mask,
148 opening: &point.opening,
149 isolated: &point.closed,
150 }),
151 (!self.context.pump_slots.is_empty()).then_some(DerivedPumpOperations {
152 slots: &self.context.pump_slots,
153 mask: &self.context.pump_mask,
154 running: &point.pump_running,
155 isolated: &point.pump_isolated,
156 speed: &point.pump_speed,
157 power_of_two: &point.pump_power_of_two,
158 }),
159 )?;
160 let result = crate::structures::numerics::proposal(context, &input)?;
161 Ok(StructureProposal::from_derived(
162 Arc::clone(context),
163 heads.to_vec(),
164 manual_operations.to_vec(),
165 self.context.derived_slots.clone(),
166 point.opening.clone(),
167 if self.context.pump_slots.is_empty() {
168 None
169 } else {
170 Some(DerivedPumpOperationData {
171 slots: self.context.pump_slots.clone(),
172 running: point.pump_running.clone(),
173 speed: point.pump_speed.clone(),
174 })
175 },
176 result,
177 ))
178 }
179}