hydr_structures/structures/operating_source/
events.rs1use super::{
4 OperatingSourceBatch, OperatingSourceEvent, OperatingSourceEventKind,
5 OperatingSourceEventOrigin, OperatingSourceFrame,
6};
7use crate::structures::StructureError;
8use crate::structures::tensor::operating_source::{TensorOperatingSourceKind, frames};
9use numr::dtype::DType;
10use numr::runtime::Runtime;
11
12impl<R: Runtime<DType = DType>> OperatingSourceBatch<R> {
13 pub fn next_event_after(
17 &self,
18 time_s: f64,
19 source_frames: &[OperatingSourceFrame<'_, R>],
20 ) -> Result<Option<OperatingSourceEvent>, StructureError>
21 where
22 R::Client: numr::ops::BinaryOps<R>
23 + numr::ops::CompareOps<R>
24 + numr::ops::ConditionalOps<R>
25 + numr::ops::ReduceOps<R>,
26 {
27 self.next_event_from(time_s, source_frames, false)
28 }
29
30 #[doc(hidden)]
35 pub fn next_coordinated_event_after(
36 &self,
37 time_s: f64,
38 source_frames: &[OperatingSourceFrame<'_, R>],
39 ) -> Result<Option<OperatingSourceEvent>, StructureError>
40 where
41 R::Client: numr::ops::BinaryOps<R>
42 + numr::ops::CompareOps<R>
43 + numr::ops::ConditionalOps<R>
44 + numr::ops::ReduceOps<R>,
45 {
46 self.next_event_from(time_s, source_frames, true)
47 }
48
49 fn next_event_from(
52 &self,
53 time_s: f64,
54 source_frames: &[OperatingSourceFrame<'_, R>],
55 coordinated: bool,
56 ) -> Result<Option<OperatingSourceEvent>, StructureError>
57 where
58 R::Client: numr::ops::BinaryOps<R>
59 + numr::ops::CompareOps<R>
60 + numr::ops::ConditionalOps<R>
61 + numr::ops::ReduceOps<R>,
62 {
63 self.check_time(time_s)?;
64 let frames = frames(&self.context, time_s, source_frames)?;
65 let Some(support) = self.support() else {
66 return Ok(None);
67 };
68 if time_s == support.end_s {
69 return Ok(None);
70 }
71 let mut next_time = support.end_s;
72 let mut origins = Vec::new();
73 let mut next_frame = frames.into_iter();
74 for group in &self.context.groups {
75 let candidate = match &group.kind {
76 TensorOperatingSourceKind::Manual => None,
77 TensorOperatingSourceKind::Prescribed(source) => {
78 if time_s >= source.support.end_s {
79 None
80 } else {
81 let index = source.event_times.partition_point(|event| *event <= time_s);
82 source
83 .event_times
84 .get(index)
85 .map(|&time_s| (time_s, OperatingSourceEventKind::PrescribedKnot))
86 }
87 }
88 TensorOperatingSourceKind::Actuated(_) => {
89 let Some(OperatingSourceFrame::ActuatedGate { actuator, .. }) =
90 next_frame.next()
91 else {
92 return Err(StructureError::numerical(
93 "checked actuator event frame is incomplete or has another kind",
94 ));
95 };
96 let event = if coordinated {
97 actuator.next_coordinated_event()?
98 } else {
99 actuator.next_event()?
100 };
101 event.map(|event| {
102 (
103 event.time_s,
104 OperatingSourceEventKind::Actuator {
105 target_index: event.target_index,
106 arrival_lower_boundary: event.arrival_lower_boundary,
107 arrival_upper_boundary: event.arrival_upper_boundary,
108 terminal: event.terminal,
109 },
110 )
111 })
112 }
113 TensorOperatingSourceKind::ControlledPump(_) => {
114 let Some(OperatingSourceFrame::ControlledPump { pump, .. }) = next_frame.next()
115 else {
116 return Err(StructureError::numerical(
117 "checked pump event frame is incomplete or has another kind",
118 ));
119 };
120 pump.next_event().map(|event| {
121 (
122 event.time_s,
123 OperatingSourceEventKind::PumpControl {
124 decision_index: event.decision_index,
125 terminal: event.terminal,
126 },
127 )
128 })
129 }
130 };
131 if let Some((candidate, kind)) = candidate {
132 if candidate <= time_s || candidate > support.end_s {
133 continue;
134 }
135 if candidate < next_time {
136 next_time = candidate;
137 origins.clear();
138 }
139 if candidate == next_time {
140 origins.push(OperatingSourceEventOrigin {
141 source_id: group.id.clone(),
142 kind,
143 });
144 }
145 }
146 }
147 Ok(Some(OperatingSourceEvent {
148 time_s: next_time,
149 terminal: next_time == support.end_s,
150 origins,
151 }))
152 }
153}