zmatrix 0.4.2

Compile-time dimensional analysis & physics-aware linear algebra for aerospace and robotics
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use std::any::Any;
use std::ops::{Add, Div, Mul, Neg, Sub};
use crate::physics::basic::{
    Coef, Distance, HeatFlux, PhysicalQuantity, TemperatureDifference, ThermalConductivity,
    ThermalConductivityType,
};

/// `1 mW/(cm·K)` 等于多少 `W/(m·K)`。
///
/// 与热流密度的 `mW/cm²` 方向相反,容易记混,故单独命名:
/// `1 mW / (1 cm · 1 K) = 1e-3 W / (1e-2 m · K) = 0.1 W/(m·K)`
///
/// 对比:`1 mW/cm² = 10 W/m²`(分子分母都差一个 cm)。
const MW_PER_CM_K_TO_W_PER_M_K: f64 = 0.1;

impl Default for ThermalConductivity {
    fn default() -> Self {
        Self::from_w_per_m_k(0.0)
    }
}

impl PhysicalQuantity for ThermalConductivity {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn is_zero(&self) -> bool {
        self.v == 0.0
    }

    fn default_unit_value(&self) -> f64 {
        self.as_w_per_m_k()
    }

    fn set_value(&mut self, value: f64) {
        self.v = value;
    }

    fn unit_display_name(&self) -> String {
        "ThermalConductivity (W/(m·K))".to_string()
    }
}

impl ThermalConductivity {
    pub fn from_w_per_m_k(v: f64) -> Self {
        Self {
            default_type: ThermalConductivityType::WattPerMeterKelvin,
            v,
        }
    }

    pub fn from_kw_per_m_k(v: f64) -> Self {
        Self {
            default_type: ThermalConductivityType::KiloWattPerMeterKelvin,
            v,
        }
    }

    pub fn from_mw_per_cm_k(v: f64) -> Self {
        Self {
            default_type: ThermalConductivityType::MilliWattPerCentiMeterKelvin,
            v,
        }
    }

    pub fn as_w_per_m_k(&self) -> f64 {
        match self.default_type {
            ThermalConductivityType::WattPerMeterKelvin => self.v,
            ThermalConductivityType::KiloWattPerMeterKelvin => self.v * 1e3,
            ThermalConductivityType::MilliWattPerCentiMeterKelvin => {
                self.v * MW_PER_CM_K_TO_W_PER_M_K
            }
        }
    }

    pub fn as_kw_per_m_k(&self) -> f64 {
        match self.default_type {
            ThermalConductivityType::WattPerMeterKelvin => self.v * 1e-3,
            ThermalConductivityType::KiloWattPerMeterKelvin => self.v,
            ThermalConductivityType::MilliWattPerCentiMeterKelvin => {
                self.v * MW_PER_CM_K_TO_W_PER_M_K * 1e-3
            }
        }
    }

    pub fn as_mw_per_cm_k(&self) -> f64 {
        match self.default_type {
            ThermalConductivityType::WattPerMeterKelvin => self.v / MW_PER_CM_K_TO_W_PER_M_K,
            ThermalConductivityType::KiloWattPerMeterKelvin => {
                self.v * 1e3 / MW_PER_CM_K_TO_W_PER_M_K
            }
            ThermalConductivityType::MilliWattPerCentiMeterKelvin => self.v,
        }
    }

    /// 傅里叶定律的一维形式:`q = k · ΔT / L`。
    ///
    /// - `delta_t` 必须是 [`TemperatureDifference`] 而非 [`Temperature`](crate::physics::basic::Temperature):
    ///   传绝对温度会得到毫无意义的巨大热流,而类型系统在这里正好能挡住。
    /// - `thickness` 为 0 时结果为无穷大(无厚度的理想导体),不 panic ——
    ///   与浮点语义一致,调用方自行判断物理合理性。
    ///
    /// ```
    /// use zmatrix::physics::basic::{Distance, TemperatureDifference, ThermalConductivity};
    ///
    /// let k = ThermalConductivity::from_w_per_m_k(237.0); // 铝
    /// let q = k.heat_flux(
    ///     TemperatureDifference::from_celsius(20.0),
    ///     Distance::from_m(0.01),
    /// );
    /// assert!((q.as_w_per_m2() - 474_000.0).abs() < 1e-6);
    /// ```
    pub fn heat_flux(&self, delta_t: TemperatureDifference, thickness: Distance) -> HeatFlux {
        HeatFlux::from_w_per_m2(self.as_w_per_m_k() * delta_t.as_kelvin() / thickness.as_m())
    }

    /// 由实测热流密度、温差、厚度反推导热系数。
    ///
    /// 与 [`heat_flux`](Self::heat_flux) 互逆,用于从实验数据标定材料参数。
    ///
    /// ```
    /// use zmatrix::physics::basic::{
    ///     Distance, HeatFlux, TemperatureDifference, ThermalConductivity,
    /// };
    ///
    /// let k = ThermalConductivity::from_flux(
    ///     HeatFlux::from_w_per_m2(474_000.0),
    ///     TemperatureDifference::from_celsius(20.0),
    ///     Distance::from_m(0.01),
    /// );
    /// assert!((k.as_w_per_m_k() - 237.0).abs() < 1e-9);
    /// ```
    pub fn from_flux(
        flux: HeatFlux,
        delta_t: TemperatureDifference,
        thickness: Distance,
    ) -> Self {
        Self::from_w_per_m_k(
            flux.as_w_per_m2() * thickness.as_m() / delta_t.as_kelvin(),
        )
    }
}

impl Add for ThermalConductivity {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        Self::from_w_per_m_k(self.as_w_per_m_k() + rhs.as_w_per_m_k())
    }
}

impl<'a, 'b> Add<&'b ThermalConductivity> for &'a ThermalConductivity {
    type Output = ThermalConductivity;
    fn add(self, rhs: &'b ThermalConductivity) -> Self::Output {
        ThermalConductivity::from_w_per_m_k(self.as_w_per_m_k() + rhs.as_w_per_m_k())
    }
}
impl<'a> Add<&'a ThermalConductivity> for ThermalConductivity {
    type Output = ThermalConductivity;
    fn add(self, rhs: &'a ThermalConductivity) -> Self::Output {
        ThermalConductivity::from_w_per_m_k(self.as_w_per_m_k() + rhs.as_w_per_m_k())
    }
}
impl<'a> Add<ThermalConductivity> for &'a ThermalConductivity {
    type Output = ThermalConductivity;
    fn add(self, rhs: ThermalConductivity) -> Self::Output {
        ThermalConductivity::from_w_per_m_k(self.as_w_per_m_k() + rhs.as_w_per_m_k())
    }
}

impl Sub for ThermalConductivity {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self::Output {
        Self::from_w_per_m_k(self.as_w_per_m_k() - rhs.as_w_per_m_k())
    }
}

impl<'a, 'b> Sub<&'b ThermalConductivity> for &'a ThermalConductivity {
    type Output = ThermalConductivity;
    fn sub(self, rhs: &'b ThermalConductivity) -> Self::Output {
        ThermalConductivity::from_w_per_m_k(self.as_w_per_m_k() - rhs.as_w_per_m_k())
    }
}
impl<'a> Sub<&'a ThermalConductivity> for ThermalConductivity {
    type Output = ThermalConductivity;
    fn sub(self, rhs: &'a ThermalConductivity) -> Self::Output {
        ThermalConductivity::from_w_per_m_k(self.as_w_per_m_k() - rhs.as_w_per_m_k())
    }
}
impl<'a> Sub<ThermalConductivity> for &'a ThermalConductivity {
    type Output = ThermalConductivity;
    fn sub(self, rhs: ThermalConductivity) -> Self::Output {
        ThermalConductivity::from_w_per_m_k(self.as_w_per_m_k() - rhs.as_w_per_m_k())
    }
}

impl Neg for ThermalConductivity {
    type Output = Self;
    fn neg(self) -> Self::Output {
        Self::from_w_per_m_k(-self.as_w_per_m_k())
    }
}

impl Mul<f64> for ThermalConductivity {
    type Output = Self;
    fn mul(self, rhs: f64) -> Self::Output {
        Self::from_w_per_m_k(self.as_w_per_m_k() * rhs)
    }
}

impl Div<f64> for ThermalConductivity {
    type Output = Self;
    fn div(self, rhs: f64) -> Self::Output {
        Self::from_w_per_m_k(self.as_w_per_m_k() / rhs)
    }
}

impl Mul<ThermalConductivity> for f64 {
    type Output = ThermalConductivity;
    fn mul(self, rhs: ThermalConductivity) -> Self::Output {
        ThermalConductivity::from_w_per_m_k(self * rhs.as_w_per_m_k())
    }
}

impl Mul<Coef> for ThermalConductivity {
    type Output = Self;
    fn mul(self, rhs: Coef) -> Self::Output {
        Self::from_w_per_m_k(self.as_w_per_m_k() * rhs.get_value())
    }
}

impl Div<Coef> for ThermalConductivity {
    type Output = Self;
    fn div(self, rhs: Coef) -> Self::Output {
        Self::from_w_per_m_k(self.as_w_per_m_k() / rhs.get_value())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_relative_eq;

    #[test]
    fn test_construction_and_units() {
        // 铝
        let k = ThermalConductivity::from_w_per_m_k(237.0);
        assert_relative_eq!(k.as_w_per_m_k(), 237.0, epsilon = 1e-9);
        assert_relative_eq!(k.as_kw_per_m_k(), 0.237, epsilon = 1e-9);
        assert_relative_eq!(k.as_mw_per_cm_k(), 2370.0, epsilon = 1e-9);

        // 隔热毡:0.04 W/(m·K) = 0.4 mW/(cm·K)
        let k = ThermalConductivity::from_mw_per_cm_k(0.4);
        assert_relative_eq!(k.as_w_per_m_k(), 0.04, epsilon = 1e-9);

        // 关键换算:1 mW/(cm·K) = 0.1 W/(m·K),与热流密度的 ×10 相反
        let k = ThermalConductivity::from_mw_per_cm_k(1.0);
        assert_relative_eq!(k.as_w_per_m_k(), 0.1, epsilon = 1e-9);

        // kW 分支
        let k = ThermalConductivity::from_kw_per_m_k(1.5);
        assert_relative_eq!(k.as_w_per_m_k(), 1500.0, epsilon = 1e-9);
        assert_relative_eq!(k.as_mw_per_cm_k(), 15000.0, epsilon = 1e-9);
    }

    #[test]
    fn test_unit_round_trip() {
        for v in [0.0, 0.04, 1.0, 237.0, 1e5] {
            let k = ThermalConductivity::from_w_per_m_k(v);
            assert_relative_eq!(
                ThermalConductivity::from_kw_per_m_k(k.as_kw_per_m_k()).as_w_per_m_k(),
                v,
                epsilon = 1e-9
            );
            assert_relative_eq!(
                ThermalConductivity::from_mw_per_cm_k(k.as_mw_per_cm_k()).as_w_per_m_k(),
                v,
                epsilon = 1e-9
            );
        }
    }

    #[test]
    fn test_unit_independence_in_arith() {
        let a = ThermalConductivity::from_w_per_m_k(100.0);
        let b = ThermalConductivity::from_mw_per_cm_k(1000.0); // = 100 W/(m·K)
        assert_relative_eq!(a.as_w_per_m_k(), b.as_w_per_m_k(), epsilon = 1e-9);

        assert_relative_eq!((a + b).as_w_per_m_k(), 200.0, epsilon = 1e-9);
        assert_relative_eq!((a - b).as_w_per_m_k(), 0.0, epsilon = 1e-9);

        // 混合单位求和:铝 + 钛
        let alu = ThermalConductivity::from_w_per_m_k(237.0);
        let ti = ThermalConductivity::from_mw_per_cm_k(220.0); // = 22 W/(m·K)
        assert_relative_eq!((alu + ti).as_w_per_m_k(), 259.0, epsilon = 1e-9);
    }

    #[test]
    fn test_scalar_arith() {
        let k = ThermalConductivity::from_w_per_m_k(100.0);
        assert_relative_eq!((k * 2.0).as_w_per_m_k(), 200.0, epsilon = 1e-9);
        assert_relative_eq!((k / 4.0).as_w_per_m_k(), 25.0, epsilon = 1e-9);
        assert_relative_eq!((0.5 * k).as_w_per_m_k(), 50.0, epsilon = 1e-9);
        assert_relative_eq!((k * Coef::new(3.0)).as_w_per_m_k(), 300.0, epsilon = 1e-9);
        assert_relative_eq!((k / Coef::new(2.0)).as_w_per_m_k(), 50.0, epsilon = 1e-9);
        assert_relative_eq!((-k).as_w_per_m_k(), -100.0, epsilon = 1e-9);
    }

    #[test]
    fn test_fourier_law() {
        // 铝板 10 mm,温差 20 K:q = 237 × 20 / 0.01 = 474000 W/m²
        let k = ThermalConductivity::from_w_per_m_k(237.0);
        let q = k.heat_flux(
            TemperatureDifference::from_celsius(20.0),
            Distance::from_m(0.01),
        );
        assert_relative_eq!(q.as_w_per_m2(), 474_000.0, epsilon = 1e-6);

        // 厚度加倍 → 热流减半
        let q2 = k.heat_flux(
            TemperatureDifference::from_celsius(20.0),
            Distance::from_m(0.02),
        );
        assert_relative_eq!(q2.as_w_per_m2(), 237_000.0, epsilon = 1e-6);

        // 温差加倍 → 热流加倍
        let q3 = k.heat_flux(
            TemperatureDifference::from_celsius(40.0),
            Distance::from_m(0.01),
        );
        assert_relative_eq!(q3.as_w_per_m2(), 948_000.0, epsilon = 1e-6);

        // 隔热毡同厚度同温差:k 小 6000 倍以上
        let insulation = ThermalConductivity::from_mw_per_cm_k(0.4); // 0.04 W/(m·K)
        let q4 = insulation.heat_flux(
            TemperatureDifference::from_celsius(20.0),
            Distance::from_m(0.01),
        );
        assert_relative_eq!(q4.as_w_per_m2(), 80.0, epsilon = 1e-9);
        assert_relative_eq!(q.as_w_per_m2() / q4.as_w_per_m2(), 5925.0, epsilon = 1e-9);
    }

    #[test]
    fn test_fourier_law_zero_thickness() {
        // 零厚度:热流发散为无穷大,不 panic(浮点语义)
        let k = ThermalConductivity::from_w_per_m_k(237.0);
        let q = k.heat_flux(
            TemperatureDifference::from_celsius(20.0),
            Distance::from_m(0.0),
        );
        assert!(q.as_w_per_m2().is_infinite());
    }

    #[test]
    fn test_from_flux_is_inverse() {
        let k = ThermalConductivity::from_w_per_m_k(237.0);
        let dt = TemperatureDifference::from_celsius(20.0);
        let l = Distance::from_m(0.01);

        let q = k.heat_flux(dt, l);
        let k_back = ThermalConductivity::from_flux(q, dt, l);
        assert_relative_eq!(k_back.as_w_per_m_k(), 237.0, epsilon = 1e-9);

        // 不同单位构造的输入也应得到同一结果
        let q_mw = HeatFlux::from_w_per_m2(q.as_w_per_m2());
        let dt_f = TemperatureDifference::from_fahrenheit(36.0); // = 20 K
        let l_mm = Distance::from_m(0.01);
        let k2 = ThermalConductivity::from_flux(q_mw, dt_f, l_mm);
        assert_relative_eq!(k2.as_w_per_m_k(), 237.0, epsilon = 1e-6);
    }

    #[test]
    fn test_physical_quantity_contract() {
        let k = ThermalConductivity::default();
        assert_relative_eq!(k.as_w_per_m_k(), 0.0, epsilon = 1e-12);
        assert!(k.is_zero());
        assert_eq!(k.default_unit_value(), 0.0);
        assert_eq!(k.unit_display_name(), "ThermalConductivity (W/(m·K))");

        let mut k2 = ThermalConductivity::default();
        k2.set_value(50.0);
        assert_relative_eq!(k2.as_w_per_m_k(), 50.0, epsilon = 1e-12);
        assert!(!k2.is_zero());

        let any = k2.as_any();
        assert!(any.downcast_ref::<ThermalConductivity>().is_some());
    }

    #[test]
    fn test_all_unit_branches() {
        // 三种构造方式表示同一个 100 W/(m·K),所有取值方法结果必须一致
        let variants = [
            ThermalConductivity::from_w_per_m_k(100.0),
            ThermalConductivity::from_kw_per_m_k(0.1),
            ThermalConductivity::from_mw_per_cm_k(1000.0),
        ];
        for k in variants {
            assert_relative_eq!(k.as_w_per_m_k(), 100.0, epsilon = 1e-9);
            assert_relative_eq!(k.as_kw_per_m_k(), 0.1, epsilon = 1e-9);
            assert_relative_eq!(k.as_mw_per_cm_k(), 1000.0, epsilon = 1e-9);
        }
    }

    #[test]
    fn test_ref_variants() {
        let a = ThermalConductivity::from_w_per_m_k(100.0);
        let b = ThermalConductivity::from_w_per_m_k(40.0);

        assert_relative_eq!((a + b).as_w_per_m_k(), 140.0, epsilon = 1e-9);
        assert_relative_eq!((&a + &b).as_w_per_m_k(), 140.0, epsilon = 1e-9);
        assert_relative_eq!((a + &b).as_w_per_m_k(), 140.0, epsilon = 1e-9);
        assert_relative_eq!((&a + b).as_w_per_m_k(), 140.0, epsilon = 1e-9);

        assert_relative_eq!((a - b).as_w_per_m_k(), 60.0, epsilon = 1e-9);
        assert_relative_eq!((&a - &b).as_w_per_m_k(), 60.0, epsilon = 1e-9);
        assert_relative_eq!((a - &b).as_w_per_m_k(), 60.0, epsilon = 1e-9);
        assert_relative_eq!((&a - b).as_w_per_m_k(), 60.0, epsilon = 1e-9);
    }
}