zmatrix 0.4.12

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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
use std::any::Any;
use std::ops::{Add, Div, Mul, Neg, Sub};
use crate::physics::basic::{
    Coef, Density, MassFlowRate, MassFlux, MassFluxType, PhysicalQuantity, Pressure, Velocity,
};

/// `1 g/(m²·s)` 等于多少 `kg/(m²·s)`。
const G_PER_M2_S_TO_KG: f64 = 1e-3;

/// `1 g/(cm²·s)` 等于多少 `kg/(m²·s)`。
///
/// 10,不是 1e-3 —— 分母是 cm² 而非法 m²:
/// `1 g/(cm²·s) = 1e-3 kg / 1e-4 m² / s = 10 kg/(m²·s)`。
///
/// 与上一层那个 1e-3 相差 10000 倍,和 `HeatFlux` 里 `mW/cm²` 是同一个坑。
const G_PER_CM2_S_TO_KG: f64 = 10.0;

impl Default for MassFlux {
    fn default() -> Self {
        Self::from_kg_per_m2_s(0.0)
    }
}

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

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

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

    #[cfg(feature = "tmr")]
    fn raw_value(&self) -> f64 {
        self.v
    }

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

    fn unit_display_name(&self) -> String {
        "MassFlux (kg/(m²·s))".to_string()
    }
}

impl MassFlux {
    pub fn from_kg_per_m2_s(v: f64) -> Self {
        Self {
            default_type: MassFluxType::KilogramPerSquareMeterSecond,
            v,
        }
    }

    pub fn from_g_per_m2_s(v: f64) -> Self {
        Self {
            default_type: MassFluxType::GramPerSquareMeterSecond,
            v,
        }
    }

    pub fn from_g_per_cm2_s(v: f64) -> Self {
        Self {
            default_type: MassFluxType::GramPerSquareCentiMeterSecond,
            v,
        }
    }

    pub fn as_kg_per_m2_s(&self) -> f64 {
        match self.default_type {
            MassFluxType::KilogramPerSquareMeterSecond => self.v,
            MassFluxType::GramPerSquareMeterSecond => self.v * G_PER_M2_S_TO_KG,
            MassFluxType::GramPerSquareCentiMeterSecond => self.v * G_PER_CM2_S_TO_KG,
        }
    }

    pub fn as_g_per_m2_s(&self) -> f64 {
        match self.default_type {
            MassFluxType::KilogramPerSquareMeterSecond => self.v / G_PER_M2_S_TO_KG,
            MassFluxType::GramPerSquareMeterSecond => self.v,
            MassFluxType::GramPerSquareCentiMeterSecond => {
                self.v * G_PER_CM2_S_TO_KG / G_PER_M2_S_TO_KG
            }
        }
    }

    pub fn as_g_per_cm2_s(&self) -> f64 {
        match self.default_type {
            MassFluxType::KilogramPerSquareMeterSecond => self.v / G_PER_CM2_S_TO_KG,
            MassFluxType::GramPerSquareMeterSecond => {
                self.v * G_PER_M2_S_TO_KG / G_PER_CM2_S_TO_KG
            }
            MassFluxType::GramPerSquareCentiMeterSecond => self.v,
        }
    }

    /// 动压:`q = ρv · v = ρv²`。
    ///
    /// 这是质量通量存在的理由——把 `ρv²` 拆成两步,每步都有物理名。
    /// 完整动压公式是 `q = ½ρv²`,**本方法不含 ½**,系数请自行处理;
    /// 需要带 ½ 的版本用 [`Density::dynamic_pressure`](crate::physics::basic::Density::dynamic_pressure)。
    pub fn dynamic_pressure(&self, velocity: Velocity) -> Pressure {
        Pressure::from_pa(self.as_kg_per_m2_s() * velocity.as_m_per_sec())
    }
}

impl Add for MassFlux {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        Self::from_kg_per_m2_s(self.as_kg_per_m2_s() + rhs.as_kg_per_m2_s())
    }
}

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

impl Sub for MassFlux {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self::Output {
        Self::from_kg_per_m2_s(self.as_kg_per_m2_s() - rhs.as_kg_per_m2_s())
    }
}

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

impl Neg for MassFlux {
    type Output = Self;
    fn neg(self) -> Self::Output {
        Self::from_kg_per_m2_s(-self.as_kg_per_m2_s())
    }
}

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

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

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

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

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

// 质量通量 × 速度 = 压力:ρv · v = ρv²(动压,不含 1/2)
impl Mul<Velocity> for MassFlux {
    type Output = Pressure;
    fn mul(self, rhs: Velocity) -> Self::Output {
        Pressure::from_pa(self.as_kg_per_m2_s() * rhs.as_m_per_sec())
    }
}

impl Mul<Velocity> for &MassFlux {
    type Output = Pressure;
    fn mul(self, rhs: Velocity) -> Self::Output {
        Pressure::from_pa(self.as_kg_per_m2_s() * rhs.as_m_per_sec())
    }
}

impl<'a> Mul<&'a Velocity> for MassFlux {
    type Output = Pressure;
    fn mul(self, rhs: &'a Velocity) -> Self::Output {
        Pressure::from_pa(self.as_kg_per_m2_s() * rhs.as_m_per_sec())
    }
}

impl<'a, 'b> Mul<&'b Velocity> for &'a MassFlux {
    type Output = Pressure;
    fn mul(self, rhs: &'b Velocity) -> Self::Output {
        Pressure::from_pa(self.as_kg_per_m2_s() * rhs.as_m_per_sec())
    }
}

// 质量通量 ÷ 速度 = 密度:ρ = (ρv) / v
impl Div<Velocity> for MassFlux {
    type Output = Density;
    fn div(self, rhs: Velocity) -> Self::Output {
        Density::from_kg_per_m3(self.as_kg_per_m2_s() / rhs.as_m_per_sec())
    }
}

impl Div<Velocity> for &MassFlux {
    type Output = Density;
    fn div(self, rhs: Velocity) -> Self::Output {
        Density::from_kg_per_m3(self.as_kg_per_m2_s() / rhs.as_m_per_sec())
    }
}

impl<'a> Div<&'a Velocity> for MassFlux {
    type Output = Density;
    fn div(self, rhs: &'a Velocity) -> Self::Output {
        Density::from_kg_per_m3(self.as_kg_per_m2_s() / rhs.as_m_per_sec())
    }
}

impl<'a, 'b> Div<&'b Velocity> for &'a MassFlux {
    type Output = Density;
    fn div(self, rhs: &'b Velocity) -> Self::Output {
        Density::from_kg_per_m3(self.as_kg_per_m2_s() / rhs.as_m_per_sec())
    }
}

// 压力 ÷ 速度 = 质量通量:ρv = ρv²/v
impl Div<Velocity> for Pressure {
    type Output = MassFlux;
    fn div(self, rhs: Velocity) -> Self::Output {
        MassFlux::from_kg_per_m2_s(self.as_pa() / rhs.as_m_per_sec())
    }
}

impl Div<Velocity> for &Pressure {
    type Output = MassFlux;
    fn div(self, rhs: Velocity) -> Self::Output {
        MassFlux::from_kg_per_m2_s(self.as_pa() / rhs.as_m_per_sec())
    }
}

impl<'a> Div<&'a Velocity> for Pressure {
    type Output = MassFlux;
    fn div(self, rhs: &'a Velocity) -> Self::Output {
        MassFlux::from_kg_per_m2_s(self.as_pa() / rhs.as_m_per_sec())
    }
}

impl<'a, 'b> Div<&'b Velocity> for &'a Pressure {
    type Output = MassFlux;
    fn div(self, rhs: &'b Velocity) -> Self::Output {
        MassFlux::from_kg_per_m2_s(self.as_pa() / rhs.as_m_per_sec())
    }
}

// 质量通量 × 面积 = 质量流量:ṁ = q·A
//
// 与热学里 `HeatFlux × Area = Power` 完全同构:
// 通量是「每平方米每秒」,乘面积得到「整个截面每秒」。
impl Mul<crate::physics::basic::Area> for MassFlux {
    type Output = MassFlowRate;
    fn mul(self, rhs: crate::physics::basic::Area) -> Self::Output {
        MassFlowRate::from_kg_per_s(self.as_kg_per_m2_s() * rhs.as_m2())
    }
}

impl Mul<crate::physics::basic::Area> for &MassFlux {
    type Output = MassFlowRate;
    fn mul(self, rhs: crate::physics::basic::Area) -> Self::Output {
        MassFlowRate::from_kg_per_s(self.as_kg_per_m2_s() * rhs.as_m2())
    }
}

impl<'a> Mul<&'a crate::physics::basic::Area> for MassFlux {
    type Output = MassFlowRate;
    fn mul(self, rhs: &'a crate::physics::basic::Area) -> Self::Output {
        MassFlowRate::from_kg_per_s(self.as_kg_per_m2_s() * rhs.as_m2())
    }
}

impl<'a, 'b> Mul<&'b crate::physics::basic::Area> for &'a MassFlux {
    type Output = MassFlowRate;
    fn mul(self, rhs: &'b crate::physics::basic::Area) -> Self::Output {
        MassFlowRate::from_kg_per_s(self.as_kg_per_m2_s() * rhs.as_m2())
    }
}

// 面积 × 质量通量 = 质量流量(交换律)
impl Mul<MassFlux> for crate::physics::basic::Area {
    type Output = MassFlowRate;
    fn mul(self, rhs: MassFlux) -> Self::Output {
        MassFlowRate::from_kg_per_s(self.as_m2() * rhs.as_kg_per_m2_s())
    }
}

impl<'a> Mul<MassFlux> for &'a crate::physics::basic::Area {
    type Output = MassFlowRate;
    fn mul(self, rhs: MassFlux) -> Self::Output {
        MassFlowRate::from_kg_per_s(self.as_m2() * rhs.as_kg_per_m2_s())
    }
}

impl<'a> Mul<&'a MassFlux> for crate::physics::basic::Area {
    type Output = MassFlowRate;
    fn mul(self, rhs: &'a MassFlux) -> Self::Output {
        MassFlowRate::from_kg_per_s(self.as_m2() * rhs.as_kg_per_m2_s())
    }
}

impl<'a, 'b> Mul<&'b MassFlux> for &'a crate::physics::basic::Area {
    type Output = MassFlowRate;
    fn mul(self, rhs: &'b MassFlux) -> Self::Output {
        MassFlowRate::from_kg_per_s(self.as_m2() * rhs.as_kg_per_m2_s())
    }
}

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

    #[test]
    fn test_construction_and_units() {
        let mf = MassFlux::from_kg_per_m2_s(10.0);
        assert_relative_eq!(mf.as_kg_per_m2_s(), 10.0, epsilon = 1e-12);
        assert_relative_eq!(mf.as_g_per_m2_s(), 10_000.0, epsilon = 1e-6);
        assert_relative_eq!(mf.as_g_per_cm2_s(), 1.0, epsilon = 1e-12);

        assert_relative_eq!(MassFlux::from_g_per_m2_s(1000.0).as_kg_per_m2_s(), 1.0, epsilon = 1e-12);
        assert_relative_eq!(MassFlux::from_g_per_cm2_s(1.0).as_kg_per_m2_s(), 10.0, epsilon = 1e-12);
    }

    #[test]
    fn test_cm2_unit_is_10_not_1e_minus3() {
        // 本类型最易错的一处:g/(cm²·s) 的因子是 10,不是 1e-3
        let cm2 = MassFlux::from_g_per_cm2_s(1.0);
        let m2 = MassFlux::from_g_per_m2_s(1.0);
        // 两者相差 10000 倍
        assert_relative_eq!(cm2.as_kg_per_m2_s() / m2.as_kg_per_m2_s(), 10_000.0, epsilon = 1e-9);
    }

    #[test]
    fn test_all_unit_branches() {
        // 三种构造表示的同一通量
        let a = MassFlux::from_kg_per_m2_s(25.0);
        let b = MassFlux::from_g_per_m2_s(25_000.0);
        let c = MassFlux::from_g_per_cm2_s(2.5);

        for mf in [a, b, c] {
            assert_relative_eq!(mf.as_kg_per_m2_s(), 25.0, epsilon = 1e-9);
            assert_relative_eq!(mf.as_g_per_m2_s(), 25_000.0, epsilon = 1e-4);
            assert_relative_eq!(mf.as_g_per_cm2_s(), 2.5, epsilon = 1e-9);
        }
    }

    #[test]
    fn test_unit_round_trip() {
        for v in [0.0, 1e-6, 0.5, 25.0, 1e4] {
            let mf = MassFlux::from_kg_per_m2_s(v);
            assert_relative_eq!(
                MassFlux::from_g_per_m2_s(mf.as_g_per_m2_s()).as_kg_per_m2_s(),
                v,
                epsilon = 1e-9
            );
            assert_relative_eq!(
                MassFlux::from_g_per_cm2_s(mf.as_g_per_cm2_s()).as_kg_per_m2_s(),
                v,
                epsilon = 1e-9
            );
        }
    }

    #[test]
    fn test_arith() {
        let a = MassFlux::from_kg_per_m2_s(10.0);
        let b = MassFlux::from_g_per_cm2_s(1.0); // 同为 10 kg/(m²·s)

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

        assert_relative_eq!((a - b).as_kg_per_m2_s(), 0.0, epsilon = 1e-9);
        assert_relative_eq!((&a - &b).as_kg_per_m2_s(), 0.0, epsilon = 1e-9);
        assert_relative_eq!((a - &b).as_kg_per_m2_s(), 0.0, epsilon = 1e-9);
        assert_relative_eq!((&a - b).as_kg_per_m2_s(), 0.0, epsilon = 1e-9);

        assert_relative_eq!((a * 2.0).as_kg_per_m2_s(), 20.0, epsilon = 1e-9);
        assert_relative_eq!((a / 4.0).as_kg_per_m2_s(), 2.5, epsilon = 1e-9);
        assert_relative_eq!((0.5 * a).as_kg_per_m2_s(), 5.0, epsilon = 1e-9);
        assert_relative_eq!((a * Coef::new(3.0)).as_kg_per_m2_s(), 30.0, epsilon = 1e-9);
        assert_relative_eq!((a / Coef::new(2.0)).as_kg_per_m2_s(), 5.0, epsilon = 1e-9);
        assert_relative_eq!((-a).as_kg_per_m2_s(), -10.0, epsilon = 1e-9);
    }

    #[test]
    fn test_mul_velocity_gives_pressure() {
        // 海平面 1.225 kg/m³ × 200 m/s = 245 kg/(m²·s)
        // 再 × 200 = 49000 Pa = ρv²
        let mf = MassFlux::from_kg_per_m2_s(245.0);
        let v = Velocity::from_m_per_sec(200.0);

        let q = mf * v;
        assert_relative_eq!(q.as_pa(), 49_000.0, epsilon = 1e-9);

        assert_relative_eq!((&mf * v).as_pa(), 49_000.0, epsilon = 1e-9);
        assert_relative_eq!((mf * &v).as_pa(), 49_000.0, epsilon = 1e-9);
        assert_relative_eq!((&mf * &v).as_pa(), 49_000.0, epsilon = 1e-9);

        assert_relative_eq!(mf.dynamic_pressure(v).as_pa(), 49_000.0, epsilon = 1e-9);

        // 含 1/2 的完整动压:24500 Pa
        assert_relative_eq!((mf * v * Coef::new(0.5)).as_pa(), 24_500.0, epsilon = 1e-9);
    }

    #[test]
    fn test_inverse_operations() {
        let mf = MassFlux::from_kg_per_m2_s(245.0);
        let v = Velocity::from_m_per_sec(200.0);

        // 通量 ÷ 速度 = 密度
        let rho = mf / v;
        assert_relative_eq!(rho.as_kg_per_m3(), 1.225, epsilon = 1e-9);
        assert_relative_eq!((&mf / &v).as_kg_per_m3(), 1.225, epsilon = 1e-9);
        assert_relative_eq!((mf / &v).as_kg_per_m3(), 1.225, epsilon = 1e-9);
        assert_relative_eq!((&mf / v).as_kg_per_m3(), 1.225, epsilon = 1e-9);

        // 压力 ÷ 速度 = 通量
        let p = Pressure::from_pa(49_000.0);
        let back = p / v;
        assert_relative_eq!(back.as_kg_per_m2_s(), 245.0, epsilon = 1e-9);
        assert_relative_eq!((&p / &v).as_kg_per_m2_s(), 245.0, epsilon = 1e-9);
        assert_relative_eq!((p / &v).as_kg_per_m2_s(), 245.0, epsilon = 1e-9);
        assert_relative_eq!((&p / v).as_kg_per_m2_s(), 245.0, epsilon = 1e-9);
    }

    #[test]
    fn test_chain_matches_single_step_formula() {
        // 两步链 (ρ × v) × v 必须与一步 ρv² 完全一致
        use crate::physics::basic::Density;

        let rho = Density::from_kg_per_m3(1.225);
        let v = Velocity::from_m_per_sec(200.0);

        let two_step = (rho * v) * v;
        let one_step = rho.as_kg_per_m3() * v.as_m_per_sec() * v.as_m_per_sec();

        assert_relative_eq!(two_step.as_pa(), one_step, epsilon = 1e-9);
        assert_relative_eq!(two_step.as_pa(), 49_000.0, epsilon = 1e-9);
    }

    #[test]
    fn test_mul_area_gives_mass_flow_rate() {
        // 通量 245 kg/(m²·s) × 截面 0.5 m² = 122.5 kg/s
        let mf = MassFlux::from_kg_per_m2_s(245.0);
        let area = crate::physics::basic::Area::from_m2(0.5);

        assert_relative_eq!((mf * area).as_kg_per_s(), 122.5, epsilon = 1e-9);
        assert_relative_eq!((&mf * area).as_kg_per_s(), 122.5, epsilon = 1e-9);
        assert_relative_eq!((mf * &area).as_kg_per_s(), 122.5, epsilon = 1e-9);
        assert_relative_eq!((&mf * &area).as_kg_per_s(), 122.5, epsilon = 1e-9);

        // 交换律
        assert_relative_eq!((area * mf).as_kg_per_s(), 122.5, epsilon = 1e-9);
        assert_relative_eq!((&area * mf).as_kg_per_s(), 122.5, epsilon = 1e-9);
        assert_relative_eq!((area * &mf).as_kg_per_s(), 122.5, epsilon = 1e-9);
        assert_relative_eq!((&area * &mf).as_kg_per_s(), 122.5, epsilon = 1e-9);
    }

    #[test]
    fn test_full_mass_flow_chain() {
        // 完整链条:ρ → 通量 → 流量 → 质量
        // 1.225 kg/m³ × 200 m/s = 245 kg/(m²·s)
        // × 0.5 m² = 122.5 kg/s
        // × 10 s = 1225 kg
        let rho = Density::from_kg_per_m3(1.225);
        let v = Velocity::from_m_per_sec(200.0);
        let area = crate::physics::basic::Area::from_m2(0.5);

        let flux = rho * v;
        assert_relative_eq!(flux.as_kg_per_m2_s(), 245.0, epsilon = 1e-9);

        let rate = flux * area;
        assert_relative_eq!(rate.as_kg_per_s(), 122.5, epsilon = 1e-9);

        let mass = rate * chrono::TimeDelta::seconds(10);
        assert_relative_eq!(mass.as_kg(), 1225.0, epsilon = 1e-9);
    }

    #[test]
    fn test_physical_quantity_contract() {
        let mf = MassFlux::default();
        assert!(mf.is_zero());
        assert_eq!(mf.default_unit_value(), 0.0);
        assert_eq!(mf.unit_display_name(), "MassFlux (kg/(m²·s))");

        let mut mf2 = MassFlux::default();
        mf2.set_value(25.0);
        assert_relative_eq!(mf2.as_kg_per_m2_s(), 25.0, epsilon = 1e-12);
        assert!(!mf2.is_zero());

        assert!(mf2.as_any().downcast_ref::<MassFlux>().is_some());
    }
}