qualia_core_db/domains/physical/
thermodynamics.rs1#[derive(Clone)]
6pub struct EnsembleState {
7 pub temperature: f64,
8 pub particles: usize,
9 pub total_energy: f64,
10}
11
12pub struct ThermodynamicSampler {
14 pub current_state: EnsembleState,
15}
16
17impl ThermodynamicSampler {
18 pub fn new(initial_temp: f64, particles: usize) -> Self {
20 Self {
21 current_state: EnsembleState {
22 temperature: initial_temp,
23 particles,
24 total_energy: 0.0,
25 },
26 }
27 }
28
29 pub fn metropolis_step(&mut self, proposed_energy: f64, random_uniform: f64) -> bool {
31 let delta_e = proposed_energy - self.current_state.total_energy;
32
33 let k_b = 8.617333262145e-5; let beta = 1.0 / (k_b * self.current_state.temperature);
36
37 let acceptance_probability = if delta_e < 0.0 {
38 1.0
39 } else {
40 (-beta * delta_e).exp()
41 };
42
43 if random_uniform < acceptance_probability {
44 self.current_state.total_energy = proposed_energy;
45 true } else {
47 false }
49 }
50
51 pub fn calculate_gibbs_free_energy(&self, enthalpy: f64, entropy: f64) -> f64 {
53 enthalpy - (self.current_state.temperature * entropy)
55 }
56}
57
58#[derive(Debug, Clone, Copy)]
71pub struct LithiumPack {
72 pub cells_series: u32,
73 pub cells_parallel: u32,
74 pub cell_internal_resistance_ohm: f64,
75 pub cell_capacity_ah: f64,
76}
77
78impl LithiumPack {
79 pub fn cell_ocv(&self, soc: f64) -> f64 {
82 3.0 + (4.2 - 3.0) * soc.clamp(0.0, 1.0)
83 }
84
85 pub fn pack_ocv(&self, soc: f64) -> f64 {
87 self.cells_series as f64 * self.cell_ocv(soc)
88 }
89
90 pub fn pack_resistance(&self) -> f64 {
92 self.cell_internal_resistance_ohm * self.cells_series as f64
93 / self.cells_parallel.max(1) as f64
94 }
95
96 pub fn terminal_voltage(&self, soc: f64, load_current_a: f64) -> f64 {
98 self.pack_ocv(soc) - load_current_a * self.pack_resistance()
99 }
100
101 pub fn deliverable_power(&self, soc: f64, load_current_a: f64) -> f64 {
103 self.terminal_voltage(soc, load_current_a).max(0.0) * load_current_a
104 }
105
106 pub fn pack_capacity_ah(&self) -> f64 {
108 self.cell_capacity_ah * self.cells_parallel as f64
109 }
110}
111
112#[derive(Debug, Clone, Copy)]
115pub struct SolarPanel {
116 pub short_circuit_current_a: f64,
117 pub open_circuit_voltage_v: f64,
118 pub fill_factor: f64,
119}
120
121impl SolarPanel {
122 pub fn current_at(&self, v: f64) -> f64 {
125 if v <= 0.0 {
126 return self.short_circuit_current_a;
127 }
128 if v >= self.open_circuit_voltage_v {
129 return 0.0;
130 }
131 let p = (1.0 / (1.0 - self.fill_factor.clamp(0.05, 0.95))).max(1.0);
132 self.short_circuit_current_a * (1.0 - (v / self.open_circuit_voltage_v).powf(p))
133 }
134
135 pub fn max_power_point(&self, scan_steps: u32) -> (f64, f64, f64) {
139 let mut best = (0.0f64, 0.0f64, 0.0f64);
140 let n = scan_steps.max(2);
141 for k in 1..n {
142 let v = self.open_circuit_voltage_v * k as f64 / n as f64;
143 let i = self.current_at(v);
144 let p = v * i;
145 if p > best.2 {
146 best = (v, i, p);
147 }
148 }
149 best
150 }
151}
152
153pub fn array_mppt_power(panels: &[SolarPanel], scan_steps: u32) -> f64 {
156 panels.iter().map(|p| p.max_power_point(scan_steps).2).sum()
157}
158
159pub fn heat_loss_rate(u_value_w_m2k: f64, area_m2: f64, delta_t_k: f64) -> f64 {
161 u_value_w_m2k * area_m2 * delta_t_k
162}
163
164pub fn phase_change_energy(mass_kg: f64, latent_heat_j_kg: f64) -> f64 {
168 mass_kg * latent_heat_j_kg
169}
170
171pub fn thermal_efficiency(
176 useful_power_w: f64,
177 u_value_w_m2k: f64,
178 area_m2: f64,
179 delta_t_k: f64,
180) -> f64 {
181 let loss = heat_loss_rate(u_value_w_m2k, area_m2, delta_t_k).abs();
182 let useful = useful_power_w.max(0.0);
183 if useful + loss <= 0.0 {
184 return 0.0;
185 }
186 useful / (useful + loss)
187}
188
189#[cfg(test)]
190mod offgrid_energy_tests {
191 use super::*;
192
193 #[test]
194 fn lithium_pack_sags_under_load() {
195 let pack = LithiumPack {
197 cells_series: 4,
198 cells_parallel: 2,
199 cell_internal_resistance_ohm: 0.005,
200 cell_capacity_ah: 100.0,
201 };
202 assert!((pack.pack_ocv(1.0) - 16.8).abs() < 1e-9); assert!((pack.pack_capacity_ah() - 200.0).abs() < 1e-9); let rest = pack.terminal_voltage(0.5, 0.0);
205 let loaded = pack.terminal_voltage(0.5, 50.0);
206 assert!(loaded < rest, "terminal voltage must sag under load");
207 assert!((rest - loaded - 0.5).abs() < 1e-9);
209 assert!(pack.deliverable_power(0.5, 50.0) > 0.0);
210 }
211
212 #[test]
213 fn solar_mpp_is_between_zero_and_isc_voc() {
214 let panel = SolarPanel {
215 short_circuit_current_a: 8.0,
216 open_circuit_voltage_v: 40.0,
217 fill_factor: 0.75,
218 };
219 let (v_mp, i_mp, p_mp) = panel.max_power_point(256);
220 assert!(p_mp > 0.0);
221 assert!(v_mp > 0.0 && v_mp < panel.open_circuit_voltage_v);
222 assert!(i_mp > 0.0 && i_mp < panel.short_circuit_current_a);
223 assert!(p_mp < panel.short_circuit_current_a * panel.open_circuit_voltage_v);
225 let arr = array_mppt_power(&[panel, panel], 256);
227 assert!((arr - 2.0 * p_mp).abs() < 1e-6);
228 }
229
230 #[test]
231 fn thermal_efficiency_drops_with_worse_insulation() {
232 let good = thermal_efficiency(1000.0, 0.5, 10.0, 20.0); let bad = thermal_efficiency(1000.0, 3.0, 10.0, 20.0); assert!(
236 good > bad,
237 "lower U (better insulation) ⇒ higher efficiency"
238 );
239 assert!(good > 0.0 && good < 1.0 && bad > 0.0 && bad < 1.0);
240 assert!((phase_change_energy(2.0, 334_000.0) - 668_000.0).abs() < 1.0); }
243}