use-magnetism 0.0.1

Magnetism-specific scalar helpers for RustUse
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
#![forbid(unsafe_code)]
#![doc = include_str!("../README.md")]

//! Magnetism-specific scalar helpers.

use core::f64::consts::TAU;

pub mod prelude;

const TRIG_EPSILON: f64 = 1.0e-12;

/// Vacuum permeability in newtons per ampere squared.
///
/// This crate keeps the value locally as a convenience for magnetism-specific
/// helpers. Broader physical constants belong in the top-level `use-constants`
/// set.
pub const VACUUM_PERMEABILITY: f64 = 1.256_637_062_12e-6;

fn all_finite(values: &[f64]) -> bool {
    values.iter().all(|value| value.is_finite())
}

fn finite_result(value: f64) -> Option<f64> {
    value.is_finite().then_some(value)
}

/// Computes magnetic flux through an area.
///
/// Formula: `Φ = B * A * cos(theta)`
///
/// Returns `None` when `area` is negative, when any input is not finite, or
/// when the computed result is not finite.
///
/// # Examples
///
/// ```
/// use use_magnetism::magnetic_flux;
///
/// assert_eq!(magnetic_flux(2.0, 3.0, 0.0), Some(6.0));
/// ```
#[must_use]
pub fn magnetic_flux(magnetic_flux_density: f64, area: f64, angle_radians: f64) -> Option<f64> {
    if !all_finite(&[magnetic_flux_density, area, angle_radians]) || area < 0.0 {
        return None;
    }

    finite_result(magnetic_flux_density * area * angle_radians.cos())
}

/// Computes magnetic flux through an area using an angle in degrees.
#[must_use]
pub fn magnetic_flux_degrees(
    magnetic_flux_density: f64,
    area: f64,
    angle_degrees: f64,
) -> Option<f64> {
    magnetic_flux(magnetic_flux_density, area, angle_degrees.to_radians())
}

/// Computes magnetic flux density from magnetic flux, area, and orientation.
///
/// Formula: `B = Φ / (A * cos(theta))`
///
/// Returns `None` when `area` is less than or equal to zero, when
/// `cos(theta)` is zero or effectively zero, when any input is not finite, or
/// when the computed result is not finite.
#[must_use]
pub fn magnetic_flux_density_from_flux(flux: f64, area: f64, angle_radians: f64) -> Option<f64> {
    if !all_finite(&[flux, area, angle_radians]) || area <= 0.0 {
        return None;
    }

    let angle_factor = angle_radians.cos();
    if !angle_factor.is_finite() || angle_factor.abs() <= TRIG_EPSILON {
        return None;
    }

    finite_result(flux / (area * angle_factor))
}

/// Computes magnetic force on a moving charge.
///
/// Formula: `F = q * v * B * sin(theta)`
///
/// The sign is preserved from the scalar inputs and angle convention.
///
/// # Examples
///
/// ```
/// use std::f64::consts::FRAC_PI_2;
///
/// use use_magnetism::magnetic_force_on_charge;
///
/// assert_eq!(magnetic_force_on_charge(1.0, 2.0, 3.0, FRAC_PI_2), Some(6.0));
/// ```
#[must_use]
pub fn magnetic_force_on_charge(
    charge: f64,
    velocity: f64,
    magnetic_flux_density: f64,
    angle_radians: f64,
) -> Option<f64> {
    if !all_finite(&[charge, velocity, magnetic_flux_density, angle_radians]) {
        return None;
    }

    finite_result(charge * velocity * magnetic_flux_density * angle_radians.sin())
}

/// Computes magnetic force on a moving charge using an angle in degrees.
#[must_use]
pub fn magnetic_force_on_charge_degrees(
    charge: f64,
    velocity: f64,
    magnetic_flux_density: f64,
    angle_degrees: f64,
) -> Option<f64> {
    magnetic_force_on_charge(
        charge,
        velocity,
        magnetic_flux_density,
        angle_degrees.to_radians(),
    )
}

/// Computes the magnitude of magnetic force on a moving charge.
///
/// Formula: `|F| = |q| * speed * |B| * sin(theta)`
#[must_use]
pub fn magnetic_force_magnitude_on_charge(
    charge: f64,
    speed: f64,
    magnetic_flux_density: f64,
    angle_radians: f64,
) -> Option<f64> {
    if !all_finite(&[charge, speed, magnetic_flux_density, angle_radians]) || speed < 0.0 {
        return None;
    }

    finite_result(charge.abs() * speed * magnetic_flux_density.abs() * angle_radians.sin().abs())
}

/// Computes magnetic force on a current-carrying wire.
///
/// Formula: `F = I * L * B * sin(theta)`
///
/// The sign is preserved from the scalar inputs and angle convention.
///
/// # Examples
///
/// ```
/// use std::f64::consts::FRAC_PI_2;
///
/// use use_magnetism::magnetic_force_on_wire;
///
/// assert_eq!(magnetic_force_on_wire(2.0, 3.0, 4.0, FRAC_PI_2), Some(24.0));
/// ```
#[must_use]
pub fn magnetic_force_on_wire(
    current: f64,
    length: f64,
    magnetic_flux_density: f64,
    angle_radians: f64,
) -> Option<f64> {
    if !all_finite(&[current, length, magnetic_flux_density, angle_radians]) || length < 0.0 {
        return None;
    }

    finite_result(current * length * magnetic_flux_density * angle_radians.sin())
}

/// Computes magnetic force on a current-carrying wire using an angle in degrees.
#[must_use]
pub fn magnetic_force_on_wire_degrees(
    current: f64,
    length: f64,
    magnetic_flux_density: f64,
    angle_degrees: f64,
) -> Option<f64> {
    magnetic_force_on_wire(
        current,
        length,
        magnetic_flux_density,
        angle_degrees.to_radians(),
    )
}

/// Computes magnetic flux density around a long straight wire.
///
/// Formula: `B = μ0 * I / (2πr)`
///
/// # Examples
///
/// ```
/// use use_magnetism::magnetic_field_around_long_straight_wire;
///
/// let field = magnetic_field_around_long_straight_wire(10.0, 0.5).unwrap();
/// assert!(field.is_sign_positive());
/// ```
#[must_use]
pub fn magnetic_field_around_long_straight_wire(current: f64, distance: f64) -> Option<f64> {
    if !all_finite(&[current, distance]) || distance <= 0.0 {
        return None;
    }

    finite_result(VACUUM_PERMEABILITY * current / (TAU * distance))
}

/// Computes magnetic flux density inside an ideal long solenoid.
///
/// Formula: `B = μ0 * (N / L) * I`
///
/// # Examples
///
/// ```
/// use use_magnetism::magnetic_field_inside_solenoid;
///
/// let field = magnetic_field_inside_solenoid(1_000.0, 2.0, 0.5).unwrap();
/// assert!(field.is_sign_positive());
/// ```
#[must_use]
pub fn magnetic_field_inside_solenoid(turns: f64, current: f64, length: f64) -> Option<f64> {
    if !all_finite(&[turns, current, length]) || turns < 0.0 || length <= 0.0 {
        return None;
    }

    finite_result(VACUUM_PERMEABILITY * (turns / length) * current)
}

/// Computes magnetic flux density at the center of a circular current loop.
///
/// Formula: `B = μ0 * I / (2r)`
#[must_use]
pub fn magnetic_field_at_center_of_loop(current: f64, radius: f64) -> Option<f64> {
    if !all_finite(&[current, radius]) || radius <= 0.0 {
        return None;
    }

    finite_result(VACUUM_PERMEABILITY * current / (2.0 * radius))
}

/// Computes magnetic energy density.
///
/// Formula: `u = B² / (2μ0)`
///
/// # Examples
///
/// ```
/// use use_magnetism::magnetic_energy_density;
///
/// let energy_density = magnetic_energy_density(2.0).unwrap();
/// assert!(energy_density.is_sign_positive());
/// ```
#[must_use]
pub fn magnetic_energy_density(magnetic_flux_density: f64) -> Option<f64> {
    if !magnetic_flux_density.is_finite() {
        return None;
    }

    finite_result((magnetic_flux_density * magnetic_flux_density) / (2.0 * VACUUM_PERMEABILITY))
}

/// Computes magnetic pressure.
///
/// Magnetic pressure and magnetic energy density share the same numeric
/// expression in SI units.
#[must_use]
pub fn magnetic_pressure(magnetic_flux_density: f64) -> Option<f64> {
    magnetic_energy_density(magnetic_flux_density)
}

/// A simple magnetic field described by flux density.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MagneticField {
    pub flux_density: f64,
}

impl MagneticField {
    /// Creates a new magnetic field from flux density.
    #[must_use]
    pub fn new(flux_density: f64) -> Option<Self> {
        flux_density.is_finite().then_some(Self { flux_density })
    }

    /// Computes the magnetic flux through an area in this field.
    #[must_use]
    pub fn flux_through_area(&self, area: f64, angle_radians: f64) -> Option<f64> {
        magnetic_flux(self.flux_density, area, angle_radians)
    }

    /// Computes the magnetic force on a moving charge in this field.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::f64::consts::FRAC_PI_2;
    ///
    /// use use_magnetism::MagneticField;
    ///
    /// let field = MagneticField::new(3.0).unwrap();
    /// assert_eq!(field.force_on_charge(1.0, 2.0, FRAC_PI_2), Some(6.0));
    /// ```
    #[must_use]
    pub fn force_on_charge(&self, charge: f64, velocity: f64, angle_radians: f64) -> Option<f64> {
        magnetic_force_on_charge(charge, velocity, self.flux_density, angle_radians)
    }

    /// Computes the magnetic force on a current-carrying wire in this field.
    #[must_use]
    pub fn force_on_wire(&self, current: f64, length: f64, angle_radians: f64) -> Option<f64> {
        magnetic_force_on_wire(current, length, self.flux_density, angle_radians)
    }

    /// Computes the magnetic energy density for this field.
    #[must_use]
    pub fn energy_density(&self) -> Option<f64> {
        magnetic_energy_density(self.flux_density)
    }
}

#[cfg(test)]
#[allow(clippy::float_cmp)]
mod tests {
    use core::f64::consts::FRAC_PI_2;

    use super::{
        MagneticField, magnetic_energy_density, magnetic_field_around_long_straight_wire,
        magnetic_field_at_center_of_loop, magnetic_field_inside_solenoid, magnetic_flux,
        magnetic_flux_degrees, magnetic_flux_density_from_flux, magnetic_force_magnitude_on_charge,
        magnetic_force_on_charge, magnetic_force_on_charge_degrees, magnetic_force_on_wire,
        magnetic_force_on_wire_degrees, magnetic_pressure,
    };

    const EPSILON: f64 = 1.0e-12;

    fn assert_close(actual: f64, expected: f64) {
        let scale = actual.abs().max(expected.abs()).max(1.0);
        let delta = (actual - expected).abs();

        assert!(
            delta <= EPSILON * scale,
            "actual={actual} expected={expected} delta={delta} tolerance={}",
            EPSILON * scale
        );
    }

    fn assert_some_close(actual: Option<f64>, expected: f64) {
        assert_close(actual.expect("expected Some value"), expected);
    }

    #[test]
    fn magnetic_flux_handles_requested_cases() {
        assert_eq!(magnetic_flux(2.0, 3.0, 0.0), Some(6.0));
        assert_some_close(magnetic_flux(2.0, 3.0, FRAC_PI_2), 0.0);
        assert_eq!(magnetic_flux(2.0, -3.0, 0.0), None);
        assert_some_close(magnetic_flux_degrees(2.0, 3.0, 60.0), 3.0);
    }

    #[test]
    fn magnetic_flux_density_requires_valid_geometry() {
        assert_eq!(magnetic_flux_density_from_flux(6.0, 3.0, 0.0), Some(2.0));
        assert_eq!(magnetic_flux_density_from_flux(6.0, 0.0, 0.0), None);
        assert_eq!(magnetic_flux_density_from_flux(6.0, 3.0, FRAC_PI_2), None);
    }

    #[test]
    fn magnetic_force_on_charge_handles_sign_and_units() {
        assert_some_close(magnetic_force_on_charge(1.0, 2.0, 3.0, FRAC_PI_2), 6.0);
        assert_some_close(magnetic_force_on_charge(-1.0, 2.0, 3.0, FRAC_PI_2), -6.0);
        assert_some_close(magnetic_force_on_charge_degrees(1.0, 2.0, 3.0, 90.0), 6.0);
    }

    #[test]
    fn magnetic_force_magnitude_requires_non_negative_speed() {
        assert_some_close(
            magnetic_force_magnitude_on_charge(-1.0, 2.0, -3.0, FRAC_PI_2),
            6.0,
        );
        assert_eq!(
            magnetic_force_magnitude_on_charge(1.0, -2.0, 3.0, FRAC_PI_2),
            None
        );
    }

    #[test]
    fn magnetic_force_on_wire_handles_requested_cases() {
        assert_some_close(magnetic_force_on_wire(2.0, 3.0, 4.0, FRAC_PI_2), 24.0);
        assert_some_close(magnetic_force_on_wire_degrees(2.0, 3.0, 4.0, 90.0), 24.0);
        assert_eq!(magnetic_force_on_wire(2.0, -3.0, 4.0, FRAC_PI_2), None);
    }

    #[test]
    fn magnetic_field_helpers_require_positive_lengths() {
        let wire_field = magnetic_field_around_long_straight_wire(10.0, 0.5)
            .expect("expected finite wire field");
        assert!(wire_field.is_finite());
        assert!(wire_field > 0.0);
        assert_eq!(magnetic_field_around_long_straight_wire(10.0, 0.0), None);

        let solenoid_field =
            magnetic_field_inside_solenoid(1_000.0, 2.0, 0.5).expect("expected finite solenoid");
        assert!(solenoid_field.is_finite());
        assert!(solenoid_field > 0.0);
        assert_eq!(magnetic_field_inside_solenoid(-1_000.0, 2.0, 0.5), None);
        assert_eq!(magnetic_field_inside_solenoid(1_000.0, 2.0, 0.0), None);

        let loop_field =
            magnetic_field_at_center_of_loop(10.0, 0.5).expect("expected finite loop field");
        assert!(loop_field.is_finite());
        assert!(loop_field > 0.0);
        assert_eq!(magnetic_field_at_center_of_loop(10.0, 0.0), None);
    }

    #[test]
    fn magnetic_pressure_matches_energy_density() {
        let energy_density = magnetic_energy_density(2.0).expect("expected finite energy density");
        assert!(energy_density.is_finite());
        assert!(energy_density > 0.0);
        assert_eq!(magnetic_pressure(2.0), magnetic_energy_density(2.0));
    }

    #[test]
    fn magnetic_field_struct_delegates_to_free_functions() {
        let field = MagneticField::new(3.0).expect("valid field");

        assert_eq!(field.flux_through_area(2.0, 0.0), Some(6.0));
        assert_some_close(field.force_on_charge(1.0, 2.0, FRAC_PI_2), 6.0);
        assert_eq!(MagneticField::new(f64::NAN), None);
    }
}