dess_examples/
components.rs

1use crate::imports::*;
2
3/// ThermalMass component with capacitance, state, and history
4#[derive(HistoryMethods, BareClone, Default)]
5#[common_derives]
6#[pyo3_api(
7    #[new]
8    /// New thermal mass with capacitance `c` and initial temperature `t0`
9    pub fn __new__(c: f64, temp0: f64) -> Self {
10        Self {
11            c,
12            state: ThermalMassState {
13                temp: temp0,
14                dtemp: Default::default(),
15            },
16            history: Default::default(),
17        }
18    }
19)]
20pub struct ThermalMass {
21    /// thermal capacitance \[J/K\]
22    pub c: f64,
23    pub state: ThermalMassState,
24    pub history: ThermalMassStateHistoryVec,
25}
26
27impl HasState for ThermalMass {
28    fn set_state(&mut self, val: f64) {
29        self.state.temp = val;
30    }
31    fn state(&self) -> f64 {
32        self.state.temp
33    }
34    fn deriv(&self) -> f64 {
35        self.state.dtemp
36    }
37    fn set_deriv(&mut self, val: f64) {
38        self.state.dtemp = val;
39    }
40    fn step_deriv(&mut self, val: f64) {
41        self.state.dtemp += val;
42    }
43    fn storage(&self) -> f64 {
44        self.c
45    }
46}
47
48/// ThermalReservoir component with capacitance, state, and history
49#[derive(HistoryMethods, BareClone, Default)]
50#[common_derives]
51#[pyo3_api(
52    #[new]
53    /// New thermal reservoir with initial temperature `t0`
54    pub fn __new__(temp0: f64) -> Self {
55        Self {
56            state: ThermalMassState {
57                temp: temp0,
58                dtemp: Default::default(),
59            },
60            history: Default::default(),
61        }
62    }
63)]
64pub struct ThermalReservoir {
65    pub state: ThermalMassState,
66    pub history: ThermalMassStateHistoryVec,
67}
68
69impl HasState for ThermalReservoir {
70    fn set_state(&mut self, val: f64) {
71        self.state.temp = val;
72    }
73    fn state(&self) -> f64 {
74        self.state.temp
75    }
76    fn deriv(&self) -> f64 {
77        self.state.dtemp
78    }
79    fn set_deriv(&mut self, _val: f64) {
80        self.state.dtemp = 0.0;
81    }
82    fn step_deriv(&mut self, val: f64) {
83        self.state.dtemp += val;
84    }
85    fn storage(&self) -> f64 {
86        f64::INFINITY
87    }
88}
89
90/// State for tracking temperature of [ThermalMass]
91#[derive(Copy, HistoryVec, Default)]
92#[common_derives]
93#[pyo3_api]
94pub struct ThermalMassState {
95    /// temperature \[°C\]
96    pub temp: f64,
97    /// derivative of temperature w.r.t. time \[°C/s\]
98    pub dtemp: f64,
99}
100
101/// Conductance component
102#[derive(HistoryMethods, BareClone)]
103#[pyo3_api(
104    #[new]
105    fn __new__(h: f64) -> Self {
106        Self {
107            h,
108            state: ConductanceState {
109                q: Default::default(),
110            },
111            history: ConductanceStateHistoryVec {
112                q: Default::default(),
113            },
114        }
115    }
116)]
117#[common_derives]
118#[derive(Default)]
119pub struct Conductance {
120    /// Thermal conductance \[W/K\] between two temperatures
121    pub h: f64,
122    pub state: ConductanceState,
123    pub history: ConductanceStateHistoryVec,
124}
125
126impl Flow for Conductance {
127    fn flow(&self) -> f64 {
128        self.state.q
129    }
130    fn set_flow(&mut self, p0: &dyn HasState, p1: &dyn HasState) {
131        self.state.q = self.h * (p0.state() - p1.state());
132    }
133}
134
135/// Struct for tracking flow variables in Conductance
136#[derive(Copy, HistoryVec, Default)]
137#[common_derives]
138#[pyo3_api]
139pub struct ConductanceState {
140    /// Heat transfer rate \[W\]
141    pub q: f64,
142}