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
/*
* Magba is licensed under The 3-Clause BSD, see LICENSE.
* Copyright 2025 Sira Pornsiriprasert <code@psira.me>
*/
//! Analytical B-field computation for magnet dipole moment.
use ;
use replace_float_literals;
use crate::;
/// Computes B-field of a magnetic dipole moment at point (x, y, z) in local frame.
///
/// # Arguments
///
/// - `point`: Observer position (m)
/// - `moment`: Magnetic dipole moment vector (A·m²)
///
/// # Returns
///
/// - B-field vector (T) at point (x, y, z)
///
/// # References
///
/// - Ortner, Michael, and Lucas Gabriel Coliado Bandeira. “Magpylib: A Free Python Package for Magnetic Field Computation.” SoftwareX 11 (January 1, 2020): 100466. <https://doi.org/10.1016/j.softx.2020.100466>.
/// Computes B-field of a magnetic dipole moment at point (x, y, z).
///
/// # Arguments
///
/// - `points`: Observer positions (m)
/// - `position`: Magnet position (m)
/// - `orientation`: Magnet orientation in unit quaternion
/// - `moment`: Magnetic dipole moment vector (A·m²)
/// - `out`: Mutable slice to store the B-field vectors at each observer (T)
///
/// # Examples
///
/// ```
/// # use approx::assert_relative_eq;
/// # use magba::fields::dipole_B;
/// # use nalgebra::*;
/// let b_field = dipole_B(
/// point![5.0, 6.0, 7.0],
/// point![1.0, 2.0, 3.0],
/// UnitQuaternion::from_scaled_axis(
/// [1.0471975511965976, 0.6283185307179586, 0.4487989505128276].into(),
/// ),
/// vector![0.45, 0.3, 0.15],
/// );
/// let expected = vector![1.5509430032394472e-10, 1.8780091679184128e-10, 2.1982579999135383e-10];
/// assert_relative_eq!(b_field, expected, epsilon = 2e-10);
/// ```
///
/// # References
///
/// - Ortner, Michael, and Lucas Gabriel Coliado Bandeira. “Magpylib: A Free Python Package for Magnetic Field Computation.” SoftwareX 11 (January 1, 2020): 100466. <https://doi.org/10.1016/j.softx.2020.100466>.
/// Computes B-field at points in global frame for a magnetic dipole moment.
///
/// # Arguments
///
/// - `points`: Observer positions (m)
/// - `position`: Magnet position (m)
/// - `orientation`: Magnet orientation in unit quaternion
/// - `moment`: Magnetic dipole moment vector (A·m²)
/// - `out`: Mutable slice to store the B-field vectors at each observer (T)
///
/// # Examples
///
/// ```
/// # use approx::assert_relative_eq;
/// # use magba::fields::dipole_B_batch;
/// # use nalgebra::*;
/// let mut out = [Vector3::zeros(); 3];
/// dipole_B_batch(
/// &[
/// point![5.0, 6.0, 7.0],
/// point![4.0, 3.0, 2.0],
/// point![0.5, 0.25, 0.125],
/// ],
/// point![1.0, 2.0, 3.0],
/// UnitQuaternion::from_scaled_axis(
/// [1.0471975511965976, 0.6283185307179586, 0.4487989505128276].into(),
/// ),
/// vector![0.45, 0.3, 0.15],
/// &mut out,
/// );
///
/// let expected_fields = [
/// vector![
/// 1.5509430032394472e-10,
/// 1.8780091679184128e-10,
/// 2.1982579999135383e-10,
/// ],
/// vector![
/// 1.912964350397969e-9,
/// 1.6848048132752178e-10,
/// -1.5822176176441132e-9,
/// ],
/// vector![
/// -6.242720907089988e-10,
/// 7.557286976901354e-10,
/// 1.912964350397969e-9,
/// ],
/// ];
///
/// out.iter()
/// .zip(expected_fields.iter())
/// .for_each(|(actual, expected)| assert_relative_eq!(actual, expected, epsilon = 2e-10));
/// ```
///
/// # References
///
/// - Ortner, Michael, and Lucas Gabriel Coliado Bandeira. “Magpylib: A Free Python Package for Magnetic Field Computation.” SoftwareX 11 (January 1, 2020): 100466. <https://doi.org/10.1016/j.softx.2020.100466>.
/// Computes B-field at each given points in global frame for multiple magnetic dipole moments.
///
/// # Arguments
///
/// - `points`: Observer positions (m)
/// - `positions`: Magnet positions (m)
/// - `orientations`: Magnet orientations in unit quaternion
/// - `moments`: Magnetic dipole moment vectors (A·m²)
/// - `out`: Mutable slice to store the net B-field vectors at each observer (T)
///
/// # References
///
/// - Ortner, Michael, and Lucas Gabriel Coliado Bandeira. “Magpylib: A Free Python Package for Magnetic Field Computation.” SoftwareX 11 (January 1, 2020): 100466. <https://doi.org/10.1016/j.softx.2020.100466>.