hydr_structures/structures/controlled_pump/
snapshot.rs1use super::{ControlledPumpBatch, PumpResponseDefinition};
4use crate::structures::{OperatingControlSnapshotData, StructureError, StructureInputErrorKind};
5use hydr_core::contract::package_identity;
6use numr::dtype::DType;
7use numr::runtime::Runtime;
8
9pub const CONTROLLED_PUMP_SNAPSHOT_SCHEMA: &str = "hydr-controlled-pump-checkpoint-1";
11pub const CONTROLLED_PUMP_CONTRACT_VERSION: &str = "hydr-controlled-pump-numr-cpu-1";
13
14#[derive(Clone, Debug, PartialEq)]
18pub struct ControlledPumpSnapshotData {
19 pub schema: String,
21 pub contract: String,
23 pub definitions: Vec<PumpResponseDefinition>,
25 pub controls: OperatingControlSnapshotData,
27}
28#[derive(Clone, Debug)]
30pub struct ControlledPumpSnapshot<R: Runtime<DType = DType>> {
31 batch: ControlledPumpBatch<R>,
32}
33impl<R: Runtime<DType = DType>> ControlledPumpSnapshot<R> {
34 pub fn time(&self) -> f64 {
36 self.batch.time()
37 }
38 pub fn data(&self) -> Result<ControlledPumpSnapshotData, StructureError> {
40 self.batch.snapshot_data()
41 }
42}
43fn require(condition: bool, message: &str) -> Result<(), StructureError> {
44 if condition {
45 Ok(())
46 } else {
47 Err(StructureError::input(
48 StructureInputErrorKind::Definition,
49 message,
50 ))
51 }
52}
53impl<R: Runtime<DType = DType>> ControlledPumpBatch<R> {
54 pub fn snapshot(&self) -> ControlledPumpSnapshot<R> {
56 ControlledPumpSnapshot {
57 batch: self.clone(),
58 }
59 }
60 pub fn snapshot_data(&self) -> Result<ControlledPumpSnapshotData, StructureError> {
62 Ok(ControlledPumpSnapshotData {
63 schema: CONTROLLED_PUMP_SNAPSHOT_SCHEMA.into(),
64 contract: CONTROLLED_PUMP_CONTRACT_VERSION.into(),
65 definitions: self.definitions().to_vec(),
66 controls: self.controls.snapshot_data()?,
67 })
68 }
69 pub fn restore(&mut self, snapshot: &ControlledPumpSnapshot<R>) -> Result<(), StructureError> {
71 require(
72 super::compatibility::same_context(&self.context, &snapshot.batch.context),
73 "controlled pump snapshot response assumptions or immutable controller model differ",
74 )?;
75 let mut controls = self.controls.clone();
76 controls.restore(&snapshot.batch.controls.snapshot())?;
77 self.controls = controls;
78 Ok(())
79 }
80 pub fn restore_data(&mut self, data: &ControlledPumpSnapshotData) -> Result<(), StructureError>
84 where
85 R::Client: numr::ops::BinaryOps<R>
86 + numr::ops::CompareOps<R>
87 + numr::ops::ConditionalOps<R>
88 + numr::ops::ReduceOps<R>
89 + numr::ops::ScalarOps<R>
90 + numr::ops::TwoSumOps<R>
91 + numr::ops::TypeConversionOps<R>
92 + numr::ops::UnaryOps<R>,
93 {
94 package_identity(
95 "controlled pump",
96 &data.schema,
97 CONTROLLED_PUMP_SNAPSHOT_SCHEMA,
98 &data.contract,
99 CONTROLLED_PUMP_CONTRACT_VERSION,
100 )
101 .map_err(StructureError::definition)?;
102 require(
103 self.definitions() == data.definitions.as_slice(),
104 "controlled pump snapshot original response assumptions differ",
105 )?;
106 let mut controls = self.controls.clone();
107 controls.restore_data(&data.controls)?;
108 self.controls = controls;
109 Ok(())
110 }
111}