hydr_structures/structures/operating_source/
proposal.rs1use super::{OperatingSourceDetail, OperatingSourceOutput, OperatingSourceReceipt};
4use crate::structures::tensor::{
5 actuator::record::TensorRecord,
6 operating_source::{
7 TensorOperatingSourceContext, TensorOperatingSourceKind, TensorOperatingSourcePoint,
8 TensorOperatingSourceValue,
9 },
10};
11use crate::structures::{
12 ControlLatch, GateActuatorSupport, StructureError, StructureOperatingPoint, StructureOperation,
13 StructureProposal,
14};
15use numr::dtype::DType;
16use numr::runtime::Runtime;
17use std::sync::Arc;
18
19#[derive(Debug)]
20struct PointData<R: Runtime<DType = DType>> {
21 context: Arc<TensorOperatingSourceContext<R>>,
22 time_s: f64,
23 hydraulic: StructureProposal<R>,
24 point: TensorOperatingSourcePoint<R>,
25}
26#[derive(Clone, Debug)]
30pub struct OperatingSourceProposal<R: Runtime<DType = DType>> {
31 inner: Arc<PointData<R>>,
32}
33impl<R: Runtime<DType = DType>> OperatingSourceProposal<R> {
34 pub(crate) fn new(
35 context: Arc<TensorOperatingSourceContext<R>>,
36 time_s: f64,
37 hydraulic: StructureProposal<R>,
38 point: TensorOperatingSourcePoint<R>,
39 ) -> Self {
40 Self {
41 inner: Arc::new(PointData {
42 context,
43 time_s,
44 hydraulic,
45 point,
46 }),
47 }
48 }
49 pub fn time_s(&self) -> f64 {
51 self.inner.time_s
52 }
53 pub fn operations(&self) -> Result<Vec<StructureOperation>, StructureError> {
55 self.inner.hydraulic.operations()
56 }
57 pub fn outputs(&self) -> Result<Vec<OperatingSourceOutput>, StructureError> {
59 let data = &self.inner;
60 let n = data.context.structures.elements.len();
61 let errors = crate::structures::projection::read(&data.point.absolute_error, n)?;
62 let mut details = vec![None; n];
63 for (group, value) in data.context.groups.iter().zip(&data.point.values) {
64 match (&group.kind, value) {
65 (TensorOperatingSourceKind::Manual, TensorOperatingSourceValue::Manual) => {
66 for &slot in &group.slots {
67 details[slot] = Some(OperatingSourceDetail::Manual);
68 }
69 }
70 (
71 TensorOperatingSourceKind::Prescribed(source),
72 TensorOperatingSourceValue::Prescribed,
73 ) => {
74 for (&slot, &lane) in group.slots.iter().zip(&source.lanes) {
75 let metadata =
76 data.point.prescribed_metadata.get(lane).ok_or_else(|| {
77 StructureError::numerical(
78 "prescribed source lacks its retained combined point metadata",
79 )
80 })?;
81 details[slot] = Some(OperatingSourceDetail::PrescribedGate {
82 absolute_opening_error_m: errors[slot],
83 segment_index: metadata.segment_index,
84 exact_knot_index: metadata.exact_knot_index,
85 closed_by_definition: metadata.closed_by_definition,
86 });
87 }
88 }
89 (
90 TensorOperatingSourceKind::Actuated(context),
91 TensorOperatingSourceValue::Actuated(value),
92 ) => {
93 let position = value.position.project()?;
94 let phase =
95 crate::structures::projection::read(&value.phase, group.slots.len())?;
96 for (lane, &slot) in group.slots.iter().enumerate() {
97 let position =
98 crate::structures::project_actuator_position(&position, lane)?;
99 let phase = crate::structures::project_actuator_phase(phase[lane])?;
100 let support = if position.upper_opening_m == 0.0 {
101 GateActuatorSupport::ExactClosed
102 } else if position.lower_opening_m > 0.0 {
103 GateActuatorSupport::CertainlyPositive
104 } else {
105 GateActuatorSupport::ClosureUnresolved
106 };
107 details[slot] = Some(OperatingSourceDetail::ActuatedGate {
108 actuator_id: context.definitions[lane].actuator_id.clone(),
109 accepted_steps: value.accepted_steps,
110 accepted_time_s: value.accepted_time_s,
111 position,
112 phase,
113 support,
114 });
115 }
116 }
117 (
118 TensorOperatingSourceKind::ControlledPump(context),
119 TensorOperatingSourceValue::ControlledPump(value),
120 ) => {
121 let count = group.slots.len();
122 let running = crate::structures::projection::read(&value.running, count)?;
123 let speed = crate::structures::projection::read(&value.speed, count)?;
124 let high = crate::structures::projection::read(&value.high, count)?;
125 for (lane, &slot) in group.slots.iter().enumerate() {
126 let operating_point = if running[lane] == 0.0 {
127 StructureOperatingPoint::PumpStoppedIsolated
128 } else {
129 StructureOperatingPoint::PumpRunning {
130 speed_ratio: speed[lane],
131 }
132 };
133 details[slot] = Some(OperatingSourceDetail::ControlledPump {
134 controller_id: context.controls.definitions[lane].controller_id.clone(),
135 accepted_steps: value.accepted_steps,
136 accepted_time_s: value.accepted_time_s,
137 latch: if high[lane] == 0.0 {
138 ControlLatch::Low
139 } else {
140 ControlLatch::High
141 },
142 response_model: context.definitions[lane].model,
143 operating_point,
144 });
145 }
146 }
147 _ => {
148 return Err(StructureError::numerical(
149 "retained operating-source point differs from its registered source kind",
150 ));
151 }
152 }
153 }
154 let hydraulic = data.hydraulic.outputs()?;
155 if hydraulic.len() != n {
156 return Err(StructureError::numerical(
157 "operating-source hydraulic projection has incompatible row count",
158 ));
159 }
160 hydraulic
161 .into_iter()
162 .enumerate()
163 .map(|(element, hydraulic)| {
164 let owner = data.context.owner.get(element).copied().ok_or_else(|| {
165 StructureError::numerical("operating-source output lacks its original owner")
166 })?;
167 let group = data.context.groups.get(owner).ok_or_else(|| {
168 StructureError::numerical(
169 "operating-source output owner lacks its registered group",
170 )
171 })?;
172 let detail = details
173 .get_mut(element)
174 .and_then(Option::take)
175 .ok_or_else(|| {
176 StructureError::numerical(
177 "operating-source output lacks its mandatory source receipt",
178 )
179 })?;
180 Ok(OperatingSourceOutput {
181 hydraulic,
182 source: OperatingSourceReceipt {
183 source_id: group.id.clone(),
184 element,
185 structure_id: data.context.structures.elements[element].id.clone(),
186 time_s: data.time_s,
187 detail,
188 },
189 })
190 })
191 .collect()
192 }
193}