use super::{ControlledPumpOutput, ControlledPumpTrial, PumpResponseDefinition};
use crate::structures::tensor::controlled_pump::{
TensorControlledPumpContext, TensorControlledPumpSelection,
};
use crate::structures::{
ControlEvent, ControlSample, OperatingControlBatch, StructureError, StructureOperation,
};
use numr::dtype::DType;
use numr::runtime::Runtime;
use std::sync::Arc;
#[derive(Clone, Debug)]
pub struct ControlledPumpBatch<R: Runtime<DType = DType>> {
pub(crate) context: Arc<TensorControlledPumpContext<R>>,
pub(crate) controls: OperatingControlBatch<R>,
}
impl<R: Runtime<DType = DType>> ControlledPumpBatch<R> {
pub fn new(
controls: OperatingControlBatch<R>,
definitions: Vec<PumpResponseDefinition>,
) -> Result<Self, StructureError> {
let context = Arc::new(TensorControlledPumpContext::new(&controls, definitions)?);
Ok(Self { context, controls })
}
pub fn controls(&self) -> &OperatingControlBatch<R> {
&self.controls
}
pub fn definitions(&self) -> &[PumpResponseDefinition] {
&self.context.definitions
}
pub fn time(&self) -> f64 {
self.controls.time()
}
pub fn accepted_steps(&self) -> usize {
self.controls.accepted_steps()
}
pub fn next_event(&self) -> Option<ControlEvent> {
self.controls.next_event()
}
pub fn trial_to(
&self,
end_s: f64,
samples: &[ControlSample],
) -> Result<ControlledPumpTrial<R>, StructureError>
where
R::Client: numr::ops::BinaryOps<R>
+ numr::ops::CompareOps<R>
+ numr::ops::ConditionalOps<R>
+ numr::ops::ReduceOps<R>
+ numr::ops::ScalarOps<R>
+ numr::ops::TwoSumOps<R>
+ numr::ops::TypeConversionOps<R>
+ numr::ops::UnaryOps<R>,
{
self.complete(self.prepare_to(end_s)?, samples)
}
pub(crate) fn selected(&self) -> Result<TensorControlledPumpSelection<R>, StructureError>
where
R::Client: numr::ops::ConditionalOps<R>,
{
self.context.select(&self.controls.state.high)
}
pub fn outputs(&self) -> Result<Vec<ControlledPumpOutput>, StructureError>
where
R::Client: numr::ops::ConditionalOps<R>,
{
super::projection::outputs(self)
}
pub fn operations(&self) -> Result<Vec<StructureOperation>, StructureError>
where
R::Client: numr::ops::ConditionalOps<R>,
{
Ok(self
.outputs()?
.into_iter()
.map(|output| StructureOperation {
structure_id: output.structure_id,
operating_point: output.operating_point,
})
.collect())
}
}