hydr_structures/structures/controlled_gate/
batch.rs1use super::{ControlledGateEvent, ControlledGateTrial};
4use crate::structures::tensor::controlled_gate::TensorControlledGateContext;
5use crate::structures::{
6 ControlEvent, ControlSample, GateActuatorBatch, GateActuatorEvent, OperatingControlBatch,
7 StructureError,
8};
9use numr::dtype::DType;
10use numr::runtime::Runtime;
11use std::sync::Arc;
12
13#[derive(Clone, Debug)]
15pub struct ControlledGateBatch<R: Runtime<DType = DType>> {
16 pub(super) context: Arc<TensorControlledGateContext<R>>,
17 pub(crate) controls: OperatingControlBatch<R>,
18 pub(crate) actuators: GateActuatorBatch<R>,
19}
20
21impl<R: Runtime<DType = DType>> ControlledGateBatch<R> {
22 pub fn new(
24 controls: OperatingControlBatch<R>,
25 actuators: GateActuatorBatch<R>,
26 ) -> Result<Self, StructureError> {
27 let context = Arc::new(TensorControlledGateContext::new(&controls, &actuators)?);
28 Ok(Self {
29 context,
30 controls,
31 actuators,
32 })
33 }
34
35 pub fn controls(&self) -> &OperatingControlBatch<R> {
37 &self.controls
38 }
39
40 pub fn actuators(&self) -> &GateActuatorBatch<R> {
42 &self.actuators
43 }
44
45 pub fn time(&self) -> f64 {
47 self.controls.time()
48 }
49
50 pub fn accepted_steps(&self) -> usize {
52 self.controls.accepted_steps()
53 }
54
55 pub fn next_event(&self) -> Result<Option<ControlledGateEvent>, 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 let control = self.controls.next_event();
64 let actuator = self.actuators.next_event()?;
65 Ok(Self::common(control, actuator))
66 }
67
68 #[doc(hidden)]
73 pub fn next_coordinated_event(&self) -> Result<Option<ControlledGateEvent>, StructureError>
74 where
75 R::Client: numr::ops::BinaryOps<R>
76 + numr::ops::CompareOps<R>
77 + numr::ops::ConditionalOps<R>
78 + numr::ops::ReduceOps<R>,
79 {
80 let control = self.controls.next_event();
81 let actuator = self.actuators.next_coordinated_event()?;
82 Ok(Self::common(control, actuator))
83 }
84
85 pub(crate) fn event_at(&self, end_s: f64) -> Result<Option<ControlledGateEvent>, StructureError>
88 where
89 R::Client: numr::ops::BinaryOps<R>
90 + numr::ops::CompareOps<R>
91 + numr::ops::ConditionalOps<R>
92 + numr::ops::ReduceOps<R>,
93 {
94 let control = self
95 .controls
96 .next_event()
97 .filter(|event| event.time_s == end_s);
98 let actuator = self.actuators.event_at(end_s)?;
99 Ok(Self::common(control, actuator))
100 }
101
102 fn common(
104 control: Option<ControlEvent>,
105 actuator: Option<GateActuatorEvent>,
106 ) -> Option<ControlledGateEvent> {
107 let time_s = match (control, actuator) {
108 (Some(control), Some(actuator)) => control.time_s.min(actuator.time_s),
109 (Some(control), None) => control.time_s,
110 (None, Some(actuator)) => actuator.time_s,
111 (None, None) => return None,
112 };
113 let control = control.filter(|event| event.time_s == time_s);
114 let actuator = actuator.filter(|event| event.time_s == time_s);
115 Some(ControlledGateEvent {
116 time_s,
117 decision_index: control.and_then(|event| event.decision_index),
118 arrival_lower_boundary: actuator.is_some_and(|event| event.arrival_lower_boundary),
119 arrival_upper_boundary: actuator.is_some_and(|event| event.arrival_upper_boundary),
120 terminal: control.is_some_and(|event| event.terminal)
121 || actuator.is_some_and(|event| event.terminal),
122 })
123 }
124
125 pub fn trial_to(
127 &self,
128 end_s: f64,
129 decision_samples: &[ControlSample],
130 ) -> Result<ControlledGateTrial<R>, StructureError>
131 where
132 R::Client: numr::ops::BinaryOps<R>
133 + numr::ops::CompareOps<R>
134 + numr::ops::ConditionalOps<R>
135 + numr::ops::ReduceOps<R>
136 + numr::ops::ScalarOps<R>
137 + numr::ops::TwoSumOps<R>
138 + numr::ops::TypeConversionOps<R>
139 + numr::ops::UnaryOps<R>,
140 {
141 self.complete(self.prepare_to(end_s)?, decision_samples)
142 }
143}