cfsem/
lib.rs

1#![doc=include_str!("../README.md")]
2#![allow(non_snake_case)]
3#![allow(clippy::needless_range_loop)]
4#![allow(clippy::needless_late_init)]
5#![allow(non_snake_case)]
6
7#[cfg(feature = "python")]
8pub mod python;
9
10#[allow(unused_imports)] // Doesn't build without this
11#[cfg(feature = "python")]
12use python::*;
13
14use std::num::NonZeroUsize;
15
16pub mod math;
17pub mod mesh;
18pub mod physics;
19
20#[cfg(test)]
21pub(crate) mod testing;
22
23/// (H/m) vacuum magnetic permeability.
24/// Value from 2022 CODATA recommended values, [NIST SPI 961](https://physics.nist.gov/cuu/pdf/wall_2022.pdf).
25pub const MU_0: f64 = 0.999_999_999_87 * core::f64::consts::PI * 4e-7; // [H/m]
26
27/// (H/m) Recurring constant multiple of `mu_0`
28pub const MU0_OVER_4PI: f64 = MU_0 / (4.0 * core::f64::consts::PI);
29
30/// Chunk size for parallelism
31pub(crate) fn chunksize(nelem: usize) -> usize {
32    let ncores = std::thread::available_parallelism()
33        .unwrap_or(NonZeroUsize::MIN)
34        .get();
35
36    (nelem / ncores).max(1)
37}
38
39#[macro_use]
40pub(crate) mod macros {
41
42    /// Make sure the length of any number of vec/array/slice are the same.
43    macro_rules! check_length {
44        ($n:expr, $($y:expr),+) => {
45            $(  // Repeat for all y
46                if $y.len() != $n {
47                    return Err("Length mismatch");
48                }
49            )+
50        };
51    }
52
53    /// Make sure the lengths of 3 vec/array/slice in a tuple are the same
54    macro_rules! check_length_3tup {
55        ($n:expr, $x:expr) => {
56            if $x.0.len() != $n || $x.1.len() != $n || $x.2.len() != $n {
57                return Err("Length mismatch");
58            }
59        };
60    }
61
62    macro_rules! par_chunks_3tup {
63        ($x:expr, $n:expr) => {
64            (
65                $x.0.par_chunks($n),
66                $x.1.par_chunks($n),
67                $x.2.par_chunks($n),
68            )
69        };
70    }
71
72    macro_rules! mut_par_chunks_3tup {
73        ($x:expr, $n:expr) => {
74            (
75                $x.0.par_chunks_mut($n),
76                $x.1.par_chunks_mut($n),
77                $x.2.par_chunks_mut($n),
78            )
79        };
80    }
81
82    pub(crate) use mut_par_chunks_3tup;
83    pub(crate) use par_chunks_3tup;
84
85    pub(crate) use check_length;
86    pub(crate) use check_length_3tup;
87}