Skip to main content

qualia_core_db/render/physics/response/
thermal.rs

1//! Driven thermal reservoirs using the canonical fixed-state ODE kernel.
2use crate::solvers::calculus::{ODEFunction, RungeKutta4Static};
3use crate::solvers::{SolverConfig, SolverResult, SolversError};
4#[derive(Clone, Copy, Debug)]
5pub struct ThermalTargets {
6    pub temperatures: [f64; 2],
7    pub rates: [f64; 2],
8}
9impl ODEFunction for ThermalTargets {
10    fn derivatives(&self, _t: f64, y: &[f64; 4]) -> [f64; 4] {
11        [
12            (self.temperatures[0] - y[0]) * self.rates[0],
13            (self.temperatures[1] - y[1]) * self.rates[1],
14            0.0,
15            0.0,
16        ]
17    }
18}
19/// Rates are inverse seconds. Selecting/calibrating heat-balance targets is a
20/// caller/domain-model responsibility; integration itself is engine-owned.
21pub fn step_thermal(
22    temperatures: [f64; 2],
23    targets: ThermalTargets,
24    dt: f64,
25) -> SolverResult<[f64; 2]> {
26    if temperatures
27        .iter()
28        .chain(targets.temperatures.iter())
29        .chain(targets.rates.iter())
30        .any(|n| !n.is_finite())
31        || !dt.is_finite()
32        || !(0.0..=0.05).contains(&dt)
33        || targets.rates.iter().any(|n| !(0.0..=10.0).contains(n))
34    {
35        return Err(SolversError::InvalidParameters);
36    }
37    if dt == 0.0 {
38        return Ok(temperatures);
39    }
40    let mut solver = RungeKutta4Static::new(
41        dt.min(1.0 / 240.0),
42        SolverConfig {
43            max_iterations: 16,
44            ..SolverConfig::default()
45        },
46    );
47    let state = solver.integrate(
48        &targets,
49        0.0,
50        [temperatures[0], temperatures[1], 0.0, 0.0],
51        dt,
52    )?;
53    Ok([state.y[0], state.y[1]])
54}