What is Magpylib ?#

Magpylib is a Python package for calculating 3D static magnetic fields of permanent magnets, currents and other sources. The computation is based on analytical expressions and therefore extremely fast. A user friendly API combined with graphic output enables convenient positioning of sources and observers.

source fundamentals


Magpylib is on PyPI and conda-forge. Install using pip (pip install magpylib) or conda (conda install magpylib) package managers.

The following Example code outlines the core functionality:

import magpylib as magpy
source = magpy.magnet.Cylinder(magnetization=(0,0,350), dimension=(4,5), position=(1,2,3))
observer = (4,4,4)
B = source.getB(observer)

# out: [ 10.30092924   6.86728616 -20.96623472]

Here, a cylinder shaped permanent magnet with (diameter, height) of (4, 5) millimeters is created in a global coordinate system at position (1,2,3). The magnetization is homogeneous and points in z-direction with an amplitude of 350 millitesla (=\(\mu_0\times M\)). The B-field is computed at the observer position (4,4,4) and returned in units of millitesla.