magpylib.core package#

The core sub-package gives direct access to our field implementations.

magpylib.core.current_circle_Hfield(r0: ndarray, r: ndarray, z: ndarray, i0: ndarray) ndarray#

B-field of a circular line-current loops in Cylindrical Coordinates.

The loops lies in the z=0 plane with the coordinate origin at their centers. The output is proportional to the electrical currents i0, and independent of the length units chosen for observers and loop radii.

Parameters:
  • r0 (ndarray, shape (n,)) – Radii of loops.

  • r (ndarray, shape (n,)) – Radial positions of observers.

  • z (ndarray, shape (n,)) – Axial positions of observers.

  • i0 (ndarray, shape (n,)) – Electrical currents.

Returns:

B-field – B-field generated by Loops at observer positions.

Return type:

ndarray, shape (n,3)

Notes

Implementation based on “Numerically stable and computationally efficient expression for the magnetic field of a current loop.”, M.Ortner et al, Magnetism 2023, 3(1), 11-31.

magpylib.core.current_polyline_Hfield(observers: ndarray, segments_start: ndarray, segments_end: ndarray, currents: ndarray) ndarray#

B-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. The output is proportional to the current and independent of the length units chosen for observers and dimensions.

Parameters:
  • observers (ndarray, shape (n,3)) – Observer positions (x,y,z) in Cartesian coordinates.

  • segments_start (ndarray, shape (n,3)) – Polyline start positions (x,y,z) in Cartesian coordinates.

  • segments_end (ndarray, shape (n,3)) – Polyline end positions (x,y,z) in Cartesian coordinates.

  • currents (ndarray, shape (n,)) – Electrical currents in segments.

Returns:

B-Field – B-field generated by current segments at observer positions.

Return type:

ndarray, shape (n,3)

Notes

Field computation via law of Biot Savart. See also countless online resources. eg. http://www.phys.uri.edu/gerhard/PHY204/tsl216.pdf

magpylib.core.dipole_Hfield(observers: ndarray, moments: ndarray) ndarray#

Magnetic field of a dipole moments.

The dipole moment lies in the origin of the coordinate system. The output is proportional to the moment input, and is independent of length units used for observers (and moment) input considering that the moment is proportional to [L]**2. Returns np.inf for all non-zero moment components in the origin.

Parameters:
  • observers (ndarray, shape (n,3)) – Observer positions (x,y,z) in Cartesian coordinates.

  • moments (ndarray, shape (n,3)) – Dipole moment vector.

Returns:

H-field – H-field of Dipole in Cartesian coordinates.

Return type:

ndarray, shape (n,3)

Notes

The moment of a magnet is given by its volume*magnetization.

magpylib.core.magnet_cuboid_Bfield(observers: ndarray, dimensions: ndarray, polarizations: ndarray)#

B-field of homogeneously magnetized cuboids in Cartesian Coordinates.

The cuboids sides are parallel to the coordinate axes. The geometric centers of the cuboids lie in the origin. The output is proportional to the polarization magnitude and independent of the length units chosen for observers and dimensions.

Parameters:
  • observers (ndarray, shape (n,3)) – Observer positions (x,y,z) in Cartesian coordinates.

  • dimensions (ndarray, shape (n,3)) – Length of cuboids sides.

  • polarizations (ndarray, shape (n,3)) – Magnetic polarization vectors.

Returns:

B-Field – B-field generated by Cuboids at observer positions.

Return type:

ndarray, shape (n,3)

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_axial_Bfield(z0: ndarray, r: ndarray, z: ndarray) list#

B-field of axially magnetized cylinders in Cylinder Coordinates.

The cylinder axes coincide with the z-axis of the Cylindrical CS and the geometric center of the cylinder lies in the origin. Length inputs are made dimensionless by division over the cylinder radii. Unit polarization is assumed.

Parameters:
  • z0 (ndarray, shape (n,)) – Ratios of half cylinder heights over cylinder radii.

  • r (ndarray, shape (n,)) – Ratios of radial observer positions over cylinder radii.

  • z (Ratios of axial observer positions over cylinder radii.) –

Returns:

B-field – B-field generated by Cylinders at observer positions.

Return type:

ndarray, (Br, Bphi, Bz)

Notes

Implementation based on Derby, American Journal of Physics 78.3 (2010): 229-235.

magpylib.core.magnet_cylinder_diametral_Hfield(z0: ndarray, r: ndarray, z: ndarray, phi: ndarray) list#

B-field of diametrally magnetized cylinders in Cylinder Coordinates.

The cylinder axes coincide with the z-axis of the Cylindrical CS and the geometric center of the cylinder lies in the origin. Length inputs are made dimensionless by division over the cylinder radii. Unit magnetization is assumed.

Parameters:
  • z0 (ndarray, shape (n)) – Ratios of cylinder heights over cylinder radii.

  • r (ndarray, shape (n)) – Ratios of radial observer positions over cylinder radii.

  • z (ndarray, shape (n)) – Ratios of axial observer positions over cylinder radii.

  • phi (Azimuth angles between observers and magnetization directions.) –

Returns:

H-Field – H-field generated by Cylinders at observer positions.

Return type:

np.ndarray, (Hr, Hphi, Hz)

Notes

Implementation partially based on Caciagli: Journal of Magnetism and Magnetic Materials 456 (2018): 423-432, and [Ortner, Leitner, Rauber] (unpublished).

magpylib.core.magnet_cylinder_segment_Hfield(observers: ndarray, dimensions: ndarray, magnetizations: ndarray) ndarray#

Magnetic field of homogeneously magnetized cylinder ring segments in Cartesian Coordinates.

The cylinder axes coincide with the z-axis of the Cylindrical CS and the geometric center of the cylinder lies in the origin. The result is proportional to the magnetization magnitude, and independent of the length units used for dimensions and observers.

Parameters:
  • observers (ndarray, shape (n,3)) – Observer positions (r,phi,z) in Cylinder coordinates, where phi is given in rad.

  • dimensions (ndarray, shape (n,6)) – Segment dimension [(r1,r2,phi1,phi2,z1,z2), …]. r1 and r2 are inner and outer radii. phi1 and phi2 are azimuth section angles in rad. z1 and z2 are z-values of bottom and top.

  • magnetizations (ndarray, shape (n,3)) – Magnetization vectors [(M, phi, th), …] in spherical CS. M is the magnitude of magnetization, phi and th are azimuth and polar angles in rad.

Returns:

H-field – H-field generated by Cylinder Segments at observer positions.

Return type:

ndarray, shape (n,3)

Notes

Implementation based on F.Slanovc, Journal of Magnetism and Magnetic Materials, Volume 559, 1 October 2022, 169482

magpylib.core.magnet_sphere_Bfield(observers: ndarray, diameters: ndarray, polarizations: ndarray) ndarray#

Magnetic field of homogeneously magnetized spheres in Cartesian Coordinates.

The center of the spheres lie in the origin of the coordinate system. The output is proportional to the polarization magnitudes, and independent of the length units chosen for observers and diameters.

Parameters:
  • observers (ndarray, shape (n,3)) – Observer positions (x,y,z) in Cartesian coordinates.

  • diameters (ndarray, shape (n,)) – Sphere diameters.

  • polarizations (ndarray, shape (n,3)) – Magnetic polarization vectors.

Returns:

B-field – B-field generated by Spheres at observer positions.

Return type:

ndarray, shape (n,3)

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.triangle_Bfield(observers: ndarray, vertices: ndarray, polarizations: ndarray) ndarray#

Magnetic field generated by homogeneously magnetically charged triangular surfaces.

The charge is proportional to the projection of the polarization vectors onto the triangle surfaces. The order of the triangle vertices defines the sign of the surface normal vector (right-hand-rule). The output is proportional to the polarization magnitude, and independent of the length units chosen for observers and vertices.

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 surface normal vector points outwards.

Parameters:
  • observers (ndarray, shape (n,3)) – Observer positions (x,y,z) in Cartesian coordinates.

  • vertices (ndarray, shape (n,3,3)) – Triangle vertex positions ((P11,P12,P13), (P21, P22, P23), …) in Cartesian coordinates.

  • polarizations (ndarray, shape (n,3)) – Magnetic polarization vectors.

Returns:

B-field – B-field generated by Triangles at observer positions.

Return type:

ndarray, shape (n,3)

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 :(