Skip to main content

qualia_core_db/specialized_libs/engineering_analysis/
thermal.rs

1use super::*;
2
3/// Thermal analyzer for thermal engineering analysis
4pub struct ThermalAnalyzer {
5    heat_transfer: HeatTransfer,
6    thermal_stress: ThermalStress,
7    thermal_analysis: ThermalAnalysis,
8    /// Phase 2 physics-simulation library for coupled thermal analysis.
9    physics_simulation: Option<Arc<Mutex<PhysicsSimulationLibrary>>>,
10    /// Phase 2 statistical-computing library for stochastic thermal analysis.
11    statistical_computing: Option<Arc<Mutex<StatisticalComputingLibrary>>>,
12}
13
14/// Heat transfer
15pub struct HeatTransfer {
16    conduction: Conduction,
17    convection: Convection,
18    radiation: Radiation,
19}
20
21/// Conduction
22#[derive(Debug, Clone)]
23pub struct Conduction {
24    pub thermal_conductivity: f64,
25    pub temperature_gradient: Vec<f64>,
26    pub heat_flux: f64,
27}
28
29/// Convection
30#[derive(Debug, Clone)]
31pub struct Convection {
32    pub convection_type: ConvectionType,
33    pub heat_transfer_coefficient: f64,
34    pub ambient_temperature: f64,
35}
36
37/// Convection types
38#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
39pub enum ConvectionType {
40    Natural,
41    Forced,
42    Mixed,
43}
44
45/// Radiation
46#[derive(Debug, Clone)]
47pub struct Radiation {
48    pub emissivity: f64,
49    pub view_factor: f64,
50    pub stefan_boltzmann: f64,
51}
52
53/// Thermal stress
54#[derive(Debug, Clone)]
55pub struct ThermalStress {
56    pub thermal_expansion: f64,
57    pub temperature_change: f64,
58    pub stress_distribution: Vec<f64>,
59}
60
61/// Thermal analysis
62#[derive(Debug, Clone)]
63pub struct ThermalAnalysis {
64    pub steady_state: SteadyState,
65    pub transient: Transient,
66}
67
68/// Steady state
69#[derive(Debug, Clone)]
70pub struct SteadyState {
71    pub temperature_distribution: Vec<f64>,
72    pub heat_flux: Vec<f64>,
73}
74
75/// Transient
76#[derive(Debug, Clone)]
77pub struct Transient {
78    pub time_history: Vec<(f64, Vec<f64>)>,
79    pub thermal_time_constant: f64,
80}
81impl ThermalAnalyzer {
82    /// Creates an analyzer with default heat-transfer, thermal-stress, and thermal-analysis state.
83    pub fn new() -> Self {
84        Self {
85            heat_transfer: HeatTransfer::new(),
86            thermal_stress: ThermalStress::new(),
87            thermal_analysis: ThermalAnalysis::new(),
88            physics_simulation: None,
89            statistical_computing: None,
90        }
91    }
92
93    /// Attaches (or detaches with `None`) a physics-simulation library handle used by the conduction solver path.
94    pub fn attach_physics_simulation(&mut self, lib: Option<Arc<Mutex<PhysicsSimulationLibrary>>>) {
95        self.physics_simulation = lib;
96    }
97
98    /// Attach the Phase 2 statistical-computing library for stochastic thermal analysis.
99    pub fn attach_statistical_computing(
100        &mut self,
101        lib: Option<Arc<Mutex<StatisticalComputingLibrary>>>,
102    ) {
103        self.statistical_computing = lib;
104    }
105
106    /// No-op returning `Ok(())` — in-memory state needs no setup.
107    pub fn initialize(&mut self) -> Result<(), EngineeringError> {
108        Ok(())
109    }
110
111    /// Fails closed (`ValidationError`) when the model has no dimensions.
112    pub fn validate_model(&self, model: &EngineeringModel) -> Result<(), EngineeringError> {
113        if model.geometry.dimensions.is_empty() {
114            return Err(EngineeringError::ValidationError(
115                "Model must have dimensions".to_string(),
116            ));
117        }
118        Ok(())
119    }
120
121    /// Runs real 1-D steady-state conduction: Fourier's law on a finite-difference mesh via the Thomas algorithm, driven by model conductivity/length/BCs (`Temperature`→Dirichlet, `HeatFlux`→Neumann). Ill-posed input → `InsufficientData`, never a fabricated field. 2-D/3-D FE thermal is out of scope and flagged, not faked.
122    pub fn analyze(
123        &mut self,
124        model: &EngineeringModel,
125        analysis_type: AnalysisType,
126    ) -> Result<AnalysisResults, EngineeringError> {
127        // REAL: 1-D steady-state heat conduction (Fourier's law), solved on a
128        // finite-difference mesh with the tridiagonal Thomas algorithm, from the
129        // model's thermal conductivity, geometry length, boundary conditions
130        // (Temperature ⇒ Dirichlet, HeatFlux ⇒ Neumann) and any volumetric heat
131        // generation expressed in the geometry features. Returns a real
132        // temperature field + heat-flux field; missing/ill-posed inputs return
133        // InsufficientData rather than a fabricated default. (Full 2-D/3-D FE
134        // thermal is a larger subsystem and is flagged, not faked.)
135        thermal_conduction::analyze_conduction(
136            model,
137            analysis_type,
138            self.physics_simulation.clone(),
139            self.statistical_computing.clone(),
140        )
141    }
142
143    /// Borrow the heat-transfer sub-component.
144    pub fn heat_transfer(&self) -> &HeatTransfer {
145        &self.heat_transfer
146    }
147
148    /// Mutably borrow the heat-transfer sub-component.
149    pub fn heat_transfer_mut(&mut self) -> &mut HeatTransfer {
150        &mut self.heat_transfer
151    }
152
153    /// Borrow the thermal-stress sub-component.
154    pub fn thermal_stress(&self) -> &ThermalStress {
155        &self.thermal_stress
156    }
157
158    /// Mutably borrow the thermal-stress sub-component.
159    pub fn thermal_stress_mut(&mut self) -> &mut ThermalStress {
160        &mut self.thermal_stress
161    }
162
163    /// Borrow the thermal-analysis sub-component.
164    pub fn thermal_analysis(&self) -> &ThermalAnalysis {
165        &self.thermal_analysis
166    }
167
168    /// Mutably borrow the thermal-analysis sub-component.
169    pub fn thermal_analysis_mut(&mut self) -> &mut ThermalAnalysis {
170        &mut self.thermal_analysis
171    }
172}
173
174impl HeatTransfer {
175    /// Creates heat-transfer state with default conduction/convection/radiation descriptors.
176    pub fn new() -> Self {
177        Self {
178            conduction: Conduction::new(),
179            convection: Convection::new(),
180            radiation: Radiation::new(),
181        }
182    }
183
184    /// Borrow the conduction sub-component.
185    pub fn conduction(&self) -> &Conduction {
186        &self.conduction
187    }
188
189    /// Mutably borrow the conduction sub-component.
190    pub fn conduction_mut(&mut self) -> &mut Conduction {
191        &mut self.conduction
192    }
193
194    /// Borrow the convection sub-component.
195    pub fn convection(&self) -> &Convection {
196        &self.convection
197    }
198
199    /// Mutably borrow the convection sub-component.
200    pub fn convection_mut(&mut self) -> &mut Convection {
201        &mut self.convection
202    }
203
204    /// Borrow the radiation sub-component.
205    pub fn radiation(&self) -> &Radiation {
206        &self.radiation
207    }
208
209    /// Mutably borrow the radiation sub-component.
210    pub fn radiation_mut(&mut self) -> &mut Radiation {
211        &mut self.radiation
212    }
213}
214
215impl Conduction {
216    /// Creates a default `Conduction` descriptor.
217    pub fn new() -> Self {
218        Self {
219            thermal_conductivity: 50.0,
220            temperature_gradient: vec![0.0; 3],
221            heat_flux: 0.0,
222        }
223    }
224}
225
226impl Convection {
227    /// Creates a default `Convection` descriptor.
228    pub fn new() -> Self {
229        Self {
230            convection_type: ConvectionType::Natural,
231            heat_transfer_coefficient: 10.0,
232            ambient_temperature: 20.0,
233        }
234    }
235}
236
237impl Radiation {
238    /// Creates a default `Radiation` descriptor.
239    pub fn new() -> Self {
240        Self {
241            emissivity: 0.8,
242            view_factor: 1.0,
243            stefan_boltzmann: 5.67e-8,
244        }
245    }
246}
247
248impl ThermalStress {
249    /// Creates a default `ThermalStress` descriptor.
250    pub fn new() -> Self {
251        Self {
252            thermal_expansion: 12e-6,
253            temperature_change: 100.0,
254            stress_distribution: Vec::new(),
255        }
256    }
257}
258
259impl ThermalAnalysis {
260    /// Creates analysis state with default steady-state/transient descriptors.
261    pub fn new() -> Self {
262        Self {
263            steady_state: SteadyState::new(),
264            transient: Transient::new(),
265        }
266    }
267}
268
269impl SteadyState {
270    /// Creates a default `SteadyState` descriptor.
271    pub fn new() -> Self {
272        Self {
273            temperature_distribution: Vec::new(),
274            heat_flux: Vec::new(),
275        }
276    }
277}
278
279impl Transient {
280    /// Creates a default `Transient` descriptor.
281    pub fn new() -> Self {
282        Self {
283            time_history: Vec::new(),
284            thermal_time_constant: 100.0,
285        }
286    }
287}