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

Implementation based on “Numerically stable and computationally efficinet expression for the magnetic field of a current loop.”, M.Ortner et al, Submitted to MDPI Magnetism, 2022

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 in units of [kA/m].

  • observers (ndarray, shape (n,3)) – Observer positions (x,y,z) in Cartesian coordinates in units of [mm].

  • 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

Compute the B-field of two different dipole-observer instances.

>>> 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”).

magpylib.core.magnet_tetrahedron_field(field: str, observers: numpy.ndarray, magnetization: numpy.ndarray, vertices: numpy.ndarray) numpy.ndarray#

Magnetic field generated by a homogeneously magnetized tetrahedron.

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].

  • vertices (ndarray, shape (n,4,3)) – Vertices of the individual tetrahedrons [(pos1a, pos1b, pos1c, pos1d), (pos2a, pos2b, pos2c, pos2d), …].

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 tetrahedron-observer instances

>>> import numpy as np
>>> import magpylib as magpy
>>>
>>> obs = np.array([(1,2,3), (2,3,4)])
>>> mag = np.array([(222,333,444), (111,112,113)])
>>> vert = np.array([((-1,0,0), (1,-1,0), (1,1,0), (0,0,1))]*2)
>>> B = magpy.core.magnet_tetrahedron_field('B', obs, mag, vert)
>>> print(B)
[[0.19075398 0.8240532  1.18170862]
 [0.03125701 0.08445416 0.1178967 ]]

Notes

The tetrahedron is built up via 4 faces applying the Triangle class, making sure that all normal vectors point outwards, and providing inside-outside evaluation to distinguish between B- and H-field.

magpylib.core.triangle_field(field: str, observers: numpy.ndarray, magnetization: numpy.ndarray, vertices: numpy.ndarray) numpy.ndarray#

Magnetic field generated by homogeneous (magnetic) charges on triangular surfaces.

Can be used to compute the field of a homogeneously magnetized bodies with triangular surface mesh. In this case each Triangle must be defined so that the normal vector points outwards.

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]. The triangle surface charge is the projection of the magnetization vector onto the surface normal vector. The order of the vertices defines the sign of the normal vector (right-hand-rule).

  • vertices (ndarray, shape (n,3,3)) – Vertices of triangular faces of the format [(pos1a, pos1b, pos1c), (pos2a, pos2b, pos2c), …]. The vertex order defines the normal vector orientation of the face (right-hand-rule).

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 triangle-observer instances.

>>> import numpy as np
>>> import magpylib as magpy
>>> mag = np.array([(22,33,44), (33,44,55)])
>>> vert = np.array([((-1,0,0), (1,-1,0), (1,1,0))]*2)
>>> obs = np.array([(1,2,3), (2,3,4)])
>>> B = magpy.core.triangle_field('B', obs, mag, vert)
>>> print(B)
[[0.08836711 0.27062047 0.42198603]
 [0.09721743 0.17609525 0.23937461]]

Notes

Field computations implemented from Guptasarma, Geophysics, 1999, 64:1, 70-74. Corners give (nan, nan, nan). Edges and in-plane perp components are set to 0. Loss of precision when approaching a triangle as (x-edge)**2 :( Loss of precision with distance from the triangle as distance**3 :(