oxiphysics-fem 0.1.2

Finite element method for the OxiPhysics engine
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
// Copyright 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! HomogenizationCell: effective thermal and mechanical property computation.

// ---------------------------------------------------------------------------
// HomogenizationCell: effective thermal and mechanical properties
// ---------------------------------------------------------------------------

/// A periodic homogenization cell for computing effective material properties.
///
/// Contains multiple phases with known volume fractions, thermal conductivities,
/// and elastic moduli. Used to compute effective properties of composite materials.
pub struct HomogenizationCell {
    /// Young's moduli of each phase (Pa).
    pub youngs_moduli: Vec<f64>,
    /// Poisson's ratios of each phase.
    pub poisson_ratios: Vec<f64>,
    /// Volume fractions of each phase (must sum to 1).
    pub volume_fractions: Vec<f64>,
    /// Thermal conductivities of each phase (W/m·K).
    pub thermal_conductivities: Vec<f64>,
}

impl HomogenizationCell {
    /// Create a new `HomogenizationCell`.
    ///
    /// # Panics
    /// Panics if the input vectors have different lengths.
    pub fn new(
        youngs_moduli: Vec<f64>,
        poisson_ratios: Vec<f64>,
        volume_fractions: Vec<f64>,
        thermal_conductivities: Vec<f64>,
    ) -> Self {
        assert_eq!(youngs_moduli.len(), poisson_ratios.len());
        assert_eq!(youngs_moduli.len(), volume_fractions.len());
        assert_eq!(youngs_moduli.len(), thermal_conductivities.len());
        Self {
            youngs_moduli,
            poisson_ratios,
            volume_fractions,
            thermal_conductivities,
        }
    }

    /// Compute effective thermal conductivity using the Voigt (upper) and
    /// Reuss (lower) bounds, returning the Hill average.
    ///
    /// Voigt bound (parallel): k_V = Σ f_i * k_i
    /// Reuss bound (series):   k_R = 1 / (Σ f_i / k_i)
    /// Hill average:           k_H = (k_V + k_R) / 2
    ///
    /// Returns `(k_voigt, k_reuss, k_hill)`.
    pub fn compute_effective_thermal_conductivity(&self) -> (f64, f64, f64) {
        let k_voigt: f64 = self
            .volume_fractions
            .iter()
            .zip(self.thermal_conductivities.iter())
            .map(|(&f, &k)| f * k)
            .sum();

        let reuss_sum: f64 = self
            .volume_fractions
            .iter()
            .zip(self.thermal_conductivities.iter())
            .map(|(&f, &k)| if k > 1e-30 { f / k } else { f64::INFINITY })
            .sum();

        let k_reuss = if reuss_sum > 1e-30 {
            1.0 / reuss_sum
        } else {
            0.0
        };
        let k_hill = 0.5 * (k_voigt + k_reuss);

        (k_voigt, k_reuss, k_hill)
    }

    /// Compute effective bulk modulus using Voigt-Reuss-Hill bounds.
    ///
    /// Bulk modulus for each phase: K_i = E_i / (3 * (1 - 2 * nu_i))
    ///
    /// Voigt bound: K_V = Σ f_i * K_i
    /// Reuss bound: K_R = 1 / (Σ f_i / K_i)
    /// Hill average: K_H = (K_V + K_R) / 2
    ///
    /// Returns `(k_voigt, k_reuss, k_hill)`.
    pub fn compute_effective_bulk_modulus(&self) -> (f64, f64, f64) {
        let bulk_moduli: Vec<f64> = self
            .youngs_moduli
            .iter()
            .zip(self.poisson_ratios.iter())
            .map(|(&e, &nu)| {
                let denom = 3.0 * (1.0 - 2.0 * nu);
                if denom.abs() > 1e-30 { e / denom } else { 0.0 }
            })
            .collect();

        let k_voigt: f64 = self
            .volume_fractions
            .iter()
            .zip(bulk_moduli.iter())
            .map(|(&f, &k)| f * k)
            .sum();

        let reuss_sum: f64 = self
            .volume_fractions
            .iter()
            .zip(bulk_moduli.iter())
            .map(|(&f, &k)| if k > 1e-30 { f / k } else { f64::INFINITY })
            .sum();

        let k_reuss = if reuss_sum > 1e-30 {
            1.0 / reuss_sum
        } else {
            0.0
        };
        let k_hill = 0.5 * (k_voigt + k_reuss);

        (k_voigt, k_reuss, k_hill)
    }

    /// Compute Hill (Voigt-Reuss-Hill) bounds for effective Young's modulus,
    /// shear modulus, and bulk modulus.
    ///
    /// For a multi-phase composite, this returns:
    /// - Voigt upper bounds for E, G, K
    /// - Reuss lower bounds for E, G, K
    /// - Hill average (E_V + E_R)/2, (G_V + G_R)/2, (K_V + K_R)/2
    ///
    /// Returns `(e_voigt, e_reuss, e_hill, g_voigt, g_reuss, g_hill, k_voigt, k_reuss, k_hill)`.
    pub fn compute_hill_bounds(&self) -> (f64, f64, f64, f64, f64, f64, f64, f64, f64) {
        let shear_moduli: Vec<f64> = self
            .youngs_moduli
            .iter()
            .zip(self.poisson_ratios.iter())
            .map(|(&e, &nu)| {
                let denom = 2.0 * (1.0 + nu);
                if denom.abs() > 1e-30 { e / denom } else { 0.0 }
            })
            .collect();

        let bulk_moduli: Vec<f64> = self
            .youngs_moduli
            .iter()
            .zip(self.poisson_ratios.iter())
            .map(|(&e, &nu)| {
                let denom = 3.0 * (1.0 - 2.0 * nu);
                if denom.abs() > 1e-30 { e / denom } else { 0.0 }
            })
            .collect();

        // Voigt bounds
        let e_voigt: f64 = self
            .volume_fractions
            .iter()
            .zip(self.youngs_moduli.iter())
            .map(|(&f, &e)| f * e)
            .sum();
        let g_voigt: f64 = self
            .volume_fractions
            .iter()
            .zip(shear_moduli.iter())
            .map(|(&f, &g)| f * g)
            .sum();
        let k_voigt: f64 = self
            .volume_fractions
            .iter()
            .zip(bulk_moduli.iter())
            .map(|(&f, &k)| f * k)
            .sum();

        // Reuss bounds
        let e_reuss_sum: f64 = self
            .volume_fractions
            .iter()
            .zip(self.youngs_moduli.iter())
            .map(|(&f, &e)| if e > 1e-30 { f / e } else { f64::INFINITY })
            .sum();
        let g_reuss_sum: f64 = self
            .volume_fractions
            .iter()
            .zip(shear_moduli.iter())
            .map(|(&f, &g)| if g > 1e-30 { f / g } else { f64::INFINITY })
            .sum();
        let k_reuss_sum: f64 = self
            .volume_fractions
            .iter()
            .zip(bulk_moduli.iter())
            .map(|(&f, &k)| if k > 1e-30 { f / k } else { f64::INFINITY })
            .sum();

        let e_reuss = if e_reuss_sum > 1e-30 {
            1.0 / e_reuss_sum
        } else {
            0.0
        };
        let g_reuss = if g_reuss_sum > 1e-30 {
            1.0 / g_reuss_sum
        } else {
            0.0
        };
        let k_reuss = if k_reuss_sum > 1e-30 {
            1.0 / k_reuss_sum
        } else {
            0.0
        };

        let e_hill = 0.5 * (e_voigt + e_reuss);
        let g_hill = 0.5 * (g_voigt + g_reuss);
        let k_hill = 0.5 * (k_voigt + k_reuss);

        (
            e_voigt, e_reuss, e_hill, g_voigt, g_reuss, g_hill, k_voigt, k_reuss, k_hill,
        )
    }

    /// Compute effective thermal expansion coefficient using Turner's model.
    ///
    /// alpha_eff = Σ (f_i * K_i * alpha_i) / Σ (f_i * K_i)
    ///
    /// where K_i is the bulk modulus of phase i.
    pub fn compute_effective_cte(&self, alphas: &[f64]) -> f64 {
        assert_eq!(alphas.len(), self.youngs_moduli.len());
        let bulk_moduli: Vec<f64> = self
            .youngs_moduli
            .iter()
            .zip(self.poisson_ratios.iter())
            .map(|(&e, &nu)| {
                let denom = 3.0 * (1.0 - 2.0 * nu);
                if denom.abs() > 1e-30 { e / denom } else { 0.0 }
            })
            .collect();

        let numerator: f64 = self
            .volume_fractions
            .iter()
            .zip(bulk_moduli.iter())
            .zip(alphas.iter())
            .map(|((&f, &k), &a)| f * k * a)
            .sum();
        let denominator: f64 = self
            .volume_fractions
            .iter()
            .zip(bulk_moduli.iter())
            .map(|(&f, &k)| f * k)
            .sum();

        if denominator.abs() < 1e-30 {
            0.0
        } else {
            numerator / denominator
        }
    }

    /// Number of phases in this homogenization cell.
    pub fn num_phases(&self) -> usize {
        self.youngs_moduli.len()
    }
}

#[cfg(test)]
mod tests_homogenization_cell {
    use super::*;

    fn two_phase_cell() -> HomogenizationCell {
        // Steel (60%) + Aluminum (40%)
        HomogenizationCell::new(
            vec![200e9, 70e9],
            vec![0.3, 0.33],
            vec![0.6, 0.4],
            vec![50.0, 200.0], // W/m·K
        )
    }

    // ── Thermal conductivity tests ───────────────────────────────────────

    #[test]
    fn test_thermal_conductivity_voigt_ge_reuss() {
        let cell = two_phase_cell();
        let (k_v, k_r, _k_h) = cell.compute_effective_thermal_conductivity();
        assert!(
            k_v >= k_r,
            "Voigt bound must be >= Reuss bound: k_V={}, k_R={}",
            k_v,
            k_r
        );
    }

    #[test]
    fn test_thermal_conductivity_hill_between_bounds() {
        let cell = two_phase_cell();
        let (k_v, k_r, k_h) = cell.compute_effective_thermal_conductivity();
        assert!(
            (k_r..=k_v).contains(&k_h),
            "Hill average must be between Reuss and Voigt: k_R={}, k_H={}, k_V={}",
            k_r,
            k_h,
            k_v
        );
    }

    #[test]
    fn test_thermal_conductivity_single_phase_exact() {
        let cell = HomogenizationCell::new(vec![200e9], vec![0.3], vec![1.0], vec![50.0]);
        let (k_v, k_r, k_h) = cell.compute_effective_thermal_conductivity();
        assert!((k_v - 50.0).abs() < 1e-10, "Voigt single phase: {}", k_v);
        assert!((k_r - 50.0).abs() < 1e-10, "Reuss single phase: {}", k_r);
        assert!((k_h - 50.0).abs() < 1e-10, "Hill single phase: {}", k_h);
    }

    #[test]
    fn test_thermal_conductivity_voigt_is_weighted_average() {
        let cell = two_phase_cell();
        let (k_v, _k_r, _k_h) = cell.compute_effective_thermal_conductivity();
        let expected = 0.6 * 50.0 + 0.4 * 200.0;
        assert!(
            (k_v - expected).abs() < 1e-9,
            "Voigt k = {}, expected {}",
            k_v,
            expected
        );
    }

    // ── Bulk modulus tests ───────────────────────────────────────────────

    #[test]
    fn test_bulk_modulus_voigt_ge_reuss() {
        let cell = two_phase_cell();
        let (k_v, k_r, _k_h) = cell.compute_effective_bulk_modulus();
        assert!(
            k_v >= k_r,
            "Voigt K must be >= Reuss K: K_V={}, K_R={}",
            k_v,
            k_r
        );
    }

    #[test]
    fn test_bulk_modulus_hill_between_bounds() {
        let cell = two_phase_cell();
        let (k_v, k_r, k_h) = cell.compute_effective_bulk_modulus();
        assert!(
            (k_r..=k_v).contains(&k_h),
            "Hill K must be between bounds: K_R={:.3e}, K_H={:.3e}, K_V={:.3e}",
            k_r,
            k_h,
            k_v
        );
    }

    #[test]
    fn test_bulk_modulus_single_phase_exact() {
        let e = 200e9;
        let nu = 0.3;
        let k_expected = e / (3.0 * (1.0 - 2.0 * nu));
        let cell = HomogenizationCell::new(vec![e], vec![nu], vec![1.0], vec![50.0]);
        let (k_v, k_r, k_h) = cell.compute_effective_bulk_modulus();
        assert!(
            (k_v - k_expected).abs() / k_expected < 1e-9,
            "Voigt K: {} vs {}",
            k_v,
            k_expected
        );
        assert!(
            (k_r - k_expected).abs() / k_expected < 1e-9,
            "Reuss K: {} vs {}",
            k_r,
            k_expected
        );
        assert!(
            (k_h - k_expected).abs() / k_expected < 1e-9,
            "Hill K: {} vs {}",
            k_h,
            k_expected
        );
    }

    // ── Hill bounds (full tensor) tests ──────────────────────────────────

    #[test]
    fn test_hill_bounds_e_voigt_ge_e_reuss() {
        let cell = two_phase_cell();
        let (e_v, e_r, _e_h, _gv, _gr, _gh, _kv, _kr, _kh) = cell.compute_hill_bounds();
        assert!(e_v >= e_r, "E_Voigt must be >= E_Reuss: {} vs {}", e_v, e_r);
    }

    #[test]
    fn test_hill_bounds_e_hill_between() {
        let cell = two_phase_cell();
        let (e_v, e_r, e_h, _gv, _gr, _gh, _kv, _kr, _kh) = cell.compute_hill_bounds();
        assert!(
            (e_r..=e_v).contains(&e_h),
            "E_Hill={} must be between E_R={} and E_V={}",
            e_h,
            e_r,
            e_v
        );
    }

    #[test]
    fn test_hill_bounds_single_phase_collapses() {
        let e = 200e9_f64;
        let nu = 0.3_f64;
        let cell = HomogenizationCell::new(vec![e], vec![nu], vec![1.0], vec![50.0]);
        let (e_v, e_r, e_h, _gv, _gr, _gh, _kv, _kr, _kh) = cell.compute_hill_bounds();
        assert!((e_v - e).abs() / e < 1e-9, "Single phase E_Voigt: {}", e_v);
        assert!((e_r - e).abs() / e < 1e-9, "Single phase E_Reuss: {}", e_r);
        assert!((e_h - e).abs() / e < 1e-9, "Single phase E_Hill: {}", e_h);
    }
}