magpylib.core package
magpylib.core package#
The core sub-package gives direct access to our field implementations.
- magpylib.core.current_line_field(field: str, observers: numpy.ndarray, current: numpy.ndarray, segment_start: numpy.ndarray, segment_end: numpy.ndarray) numpy.ndarray #
Magnetic field of line current segments.
The current flows from start to end positions. The field is set to (0,0,0) on a line segment.
- Parameters
field (str, default=`'B'`) – If field=’B’ return B-field in units of [mT], if field=’H’ return H-field in units of [kA/m].
observers (ndarray, shape (n,3)) – Observer positions (x,y,z) in Cartesian coordinates in units of [mm].
current (ndarray, shape (n,)) – Electrical current in units of [A].
start (ndarray, shape (n,3)) – Line start positions (x,y,z) in Cartesian coordinates in units of [mm].
end (ndarray, shape (n,3)) – Line end positions (x,y,z) in Cartesian coordinates in units of [mm].
- Returns
B-field or H-field – B/H-field of current in Cartesian coordinates (Bx, By, Bz) in units of [mT]/[kA/m].
- Return type
ndarray, shape (n,3)
Examples
Compute the field of two segments. The 2nd observer lies on the segment so that [0 0 0] is returned.
>>> import numpy as np >>> import magpylib as magpy >>> curr = np.array([1,2]) >>> start = np.array([(-1,0,0), (-1,0,0)]) >>> end = np.array([( 1,0,0), ( 2,0,0)]) >>> obs = np.array([( 0,0,1), ( 0,0,0)]) >>> B = magpy.core.current_line_field('B', obs, curr, start, end) >>> print(B) [[ 0. -0.14142136 0. ] [ 0. 0. 0. ]]
Notes
Field computation via law of Biot Savart. See also countless online ressources. eg. http://www.phys.uri.edu/gerhard/PHY204/tsl216.pdf
- magpylib.core.current_loop_field(field: str, observers: numpy.ndarray, current: numpy.ndarray, diameter: numpy.ndarray) numpy.ndarray #
Magnetic field of a circular (line) current loop.
The loop lies in the z=0 plane with the coordinate origin at its center.
- Parameters
field (str, default=`'B'`) – If field=’B’ return B-field in units of [mT], if field=’H’ return H-field in units of [kA/m].
observers (ndarray, shape (n,3)) – Observer positions (x,y,z) in Cartesian coordinates in units of [mm].
current (ndarray, shape (n,)) – Electrical current in units of [A].
diameter (ndarray, shape (n,)) – Diameter of loop in units of [mm].
- Returns
B-field or H-field – B/H-field of current in Cartesian coordinates (Bx, By, Bz) in units of [mT]/[kA/m].
- Return type
ndarray, shape (n,3)
Examples
Compute the field of three different loops at three different positions.
>>> import numpy as np >>> import magpylib as magpy >>> cur = np.array([1,1,2]) >>> dia = np.array([2,4,6]) >>> obs = np.array([(1,1,1), (2,2,2), (3,3,3)]) >>> B = magpy.core.current_loop_field('B', obs, cur, dia) >>> print(B) [[0.06235974 0.06235974 0.02669778] [0.03117987 0.03117987 0.01334889] [0.04157316 0.04157316 0.01779852]]
Notes
This field can be obtained by direct application of the Biot-Savardt law. Several sources in the literature provides these formulas:
Smythe, “Static and dynamic electricity” McGraw-Hill New York, 1950, vol. 3.
Simpson, “Simple analytic expressions for the magnetic field of a circular current loop,” 2001.
Ortner, “Feedback of Eddy Currents in Layered Materials for Magnetic Speed Sensing”, IEEE Transactions on Magnetics ( Volume: 53, Issue: 8, Aug. 2017).
New numerically stable implementation based on [Ortner/Leitner wip]. Based thereon the field is computed with >12 digits precision everywhere. On the loop the result is set to (0,0,0).
- magpylib.core.dipole_field(field: str, observers: numpy.ndarray, moment: numpy.ndarray) numpy.ndarray #
Magnetic field of a dipole moment.
The dipole moment lies in the origin of the coordinate system.
- Parameters
field (str, default=`'B'`) – If field=’B’ return B-field in units of [mT], if field=’H’ return H-field
observers (ndarray, shape (n,3)) – Observer positions (x,y,z) in Cartesian coordinates in units of [mm]. in units of [kA/m].
moment (ndarray, shape (n,3)) – Dipole moment vector in units of [mT*mm^3].
- Returns
B-field or H-field – B/H-field of dipole in Cartesian coordinates (Bx, By, Bz) in units of [mT]/[kA/m].
- Return type
ndarray, shape (n,3)
Examples
>>> import numpy as np >>> import magpylib as magpy >>> mom = np.array([(1,2,3), (0,0,1)]) >>> obs = np.array([(1,1,1), (0,0,2)]) >>> B = magpy.core.dipole_field('B', obs, mom) >>> print(B) [[0.07657346 0.06125877 0.04594407] [0. 0. 0.01989437]]
Notes
The field is similar to the outside-field of a spherical magnet with Volume = 1 [mm^3].
- magpylib.core.magnet_cuboid_field(field: str, observers: numpy.ndarray, magnetization: numpy.ndarray, dimension: numpy.ndarray) numpy.ndarray #
Magnetic field of a homogeneously magnetized cuboid.
The cuboid sides are parallel to the coordinate axes. The geometric center of the cuboid lies in the origin.
- Parameters
field (str, default=`'B'`) – If field=’B’ return B-field in units of [mT], if field=’H’ return H-field in units of [kA/m].
observers (ndarray, shape (n,3)) – Observer positions (x,y,z) in Cartesian coordinates in units of [mm].
magnetization (ndarray, shape (n,3)) – Homogeneous magnetization vector in units of [mT].
dimension (ndarray, shape (n,3)) – Cuboid side lengths in units of [mm].
- Returns
B-field or H-field – B/H-field of magnet in Cartesian coordinates (Bx, By, Bz) in units of [mT]/[kA/m].
- Return type
ndarray, shape (n,3)
Examples
Compute the field of three different instances.
>>> import numpy as np >>> import magpylib as magpy >>> mag = np.array([(222,333,555), (33,44,55), (0,0,100)]) >>> dim = np.array([(1,1,1), (2,3,4), (1,2,3)]) >>> obs = np.array([(1,2,3), (2,3,4), (0,0,0)]) >>> B = magpy.core.magnet_cuboid_field('B', obs, mag, dim) >>> print(B) [[ 0.49343022 1.15608356 1.65109312] [ 0.82221622 1.18511282 1.46945423] [ 0. 0. 88.77487579]]
Notes
Field computations via magnetic surface charge density. Published several times with similar expressions:
Yang: Superconductor Science and Technology 3(12):591 (1999)
Engel-Herbert: Journal of Applied Physics 97(7):074504 - 074504-4 (2005)
Camacho: Revista Mexicana de Fisica E 59 (2013) 8–17
Avoiding indeterminate forms:
In the above implementations there are several indeterminate forms where the limit must be taken. These forms appear at positions that are extensions of the edges in all xyz-octants except bottQ4. In the vicinity of these indeterminate forms the formula becomes numerically instable.
Chosen solution: use symmetries of the problem to change all positions to their bottQ4 counterparts. see also
Cichon: IEEE Sensors Journal, vol. 19, no. 7, April 1, 2019, p.2509
- magpylib.core.magnet_cylinder_field(field: str, observers: numpy.ndarray, magnetization: numpy.ndarray, dimension: numpy.ndarray) numpy.ndarray #
Magnetic field of a homogeneously magnetized cylinder.
The cylinder axis coincides with the z-axis and the geometric center of the cylinder lies in the origin.
- Parameters
field (str, default=`'B'`) – If field=’B’ return B-field in units of [mT], if field=’H’ return H-field in units of [kA/m].
observers (ndarray, shape (n,3)) – Observer positions (x,y,z) in Cartesian coordinates in units of [mm].
magnetization (ndarray, shape (n,3)) – Homogeneous magnetization vector in units of [mT].
dimension (ndarray, shape (n,2)) – Cylinder dimension (d,h) with diameter d and height h in units of [mm].
observer (ndarray, shape (n,3)) – Observer positions (x,y,z) in Cartesian coordinates in units of [mm].
field – If field=’B’ return B-field in units of [mT], if field=’H’ return H-field in units of [kA/m].
- Returns
B-field or H-field – B/H-field of magnet in Cartesian coordinates (Bx, By, Bz) in units of [mT]/[kA/m].
- Return type
ndarray, shape (n,3)
Examples
Compute the B-field of two different cylinder magnets at position (1,2,3).
>>> import numpy as np >>> import magpylib as magpy >>> mag = np.array([(0,0,1000), (100,0,100)]) >>> dim = np.array([(1,1), (2,3)]) >>> obs = np.array([(1,2,3), (1,2,3)]) >>> B = magpy.core.magnet_cylinder_field('B', obs, mag, dim) >>> print(B) [[ 0.77141782 1.54283565 1.10384481] [-0.15185713 2.90352915 2.23601722]]
Notes
Axial implementation based on
Derby: American Journal of Physics 78.3 (2010): 229-235.
Diametral implementation based on
Caciagli: Journal of Magnetism and Magnetic Materials 456 (2018): 423-432.
Leitner/Rauber/Orter: WIP
- magpylib.core.magnet_cylinder_segment_field(field: str, observers: numpy.ndarray, magnetization: numpy.ndarray, dimension: numpy.ndarray) numpy.ndarray #
Magnetic field of a homogeneously magnetized cylinder segment.
The full cylinder axis coincides with the z-axis of the coordinate system. The geometric center of the full cylinder lies in the origin.
- Parameters
field (str, default=`'B'`) – If field=’B’ return B-field in units of [mT], if field=’H’ return H-field in units of [kA/m].
observers (ndarray, shape (n,3)) – Observer positions (x,y,z) in Cartesian coordinates in units of [mm].
magnetization (ndarray, shape (n,3)) – Homogeneous magnetization vector in units of [mT].
dimension (ndarray, shape (n,5)) – Cylinder segment dimensions (r1,r2,h,phi1,phi2) with inner radius r1, outer radius r2, height h in units of [mm] and the two segment angles phi1 and phi2 in units of [deg].
- Returns
B-field or H-field – B/H-field of magnet in Cartesian coordinates (Bx, By, Bz) in units of [mT]/[kA/m].
- Return type
ndarray, shape (n,3)
Examples
Compute the field of two different cylinder segment magnets at position (1,1,1).
>>> import numpy as np >>> import magpylib as magpy >>> mag = np.array([(0,0,100), (50,50,0)]) >>> dim = np.array([(0,1,2,0,90), (1,2,4,35,125)]) >>> obs = np.array([(1,1,1), (1,1,1)]) >>> B = magpy.core.magnet_cylinder_segment_field('B', obs, mag, dim) >>> print(B) [[ 6.27410168 6.27410168 -1.20044166] [29.84602335 20.75731598 0.34961733]]
Notes
Implementation based on
Slanovc: Journal of Magnetism and Magnetic Materials, 2022 (in review)
- magpylib.core.magnet_sphere_field(field: str, observers: numpy.ndarray, magnetization: numpy.ndarray, diameter: numpy.ndarray) numpy.ndarray #
Magnetic field of a homogeneously magnetized sphere.
The center of the sphere lies in the origin of the coordinate system.
- Parameters
field (str, default=`'B'`) – If field=’B’ return B-field in units of [mT], if field=’H’ return H-field in units of [kA/m].
observers (ndarray, shape (n,3)) – Observer positions (x,y,z) in Cartesian coordinates in units of [mm].
magnetization (ndarray, shape (n,3)) – Homogeneous magnetization vector in units of [mT].
diameter (ndarray, shape (n,3)) – Sphere diameter in units of [mm].
- Returns
B-field or H-field – B/H-field of magnet in Cartesian coordinates (Bx, By, Bz) in units of [mT]/[kA/m].
- Return type
ndarray, shape (n,3)
Examples
Compute the field of two different spherical magnets at position (1,1,1), inside and outside.
>>> import numpy as np >>> import magpylib as magpy >>> dia = np.array([1,5]) >>> obs = np.array([(1,1,1), (1,1,1)]) >>> mag = np.array([(1,2,3), (0,0,3)]) >>> B = magpy.core.magnet_sphere_field('B', obs, mag, dia) >>> print(B) [[0.04009377 0.03207501 0.02405626] [0. 0. 2. ]]
Notes
The field corresponds to a dipole field on the outside and is 2/3*mag in the inside (see e.g. “Theoretical Physics, Bertelmann”).