[!WARNING]
Version 5 introduces critical breaking changes with, among others, the move to SI units. We recommended to pin your dependencies to magpylib>=4.5<5
until you are ready to migrate to the latest version! (see details)
Magpylib is an open-source Python package for calculating static magnetic fields of magnets, currents, and other sources. It uses analytical expressions, solutions to macroscopic magnetostatic problems, implemented in vectorized form which makes the computation extremely fast and leverages the open-source Python ecosystem for spectacular visualizations!
Installation
Install from PyPI using pip
pip install magpylib
Install from conda forge using conda
conda install -c conda-forge magpylib
Magpylib supports Python3.10+ and relies on common scientific computation libraries NumPy, Scipy, Matplotlib and Plotly. Optionally, Pyvista is recommended as graphical backend.
Resources
- Check out our Documentation for detailed information about the last stable release, or the Dev Docs to see the unreleased development version features.
- Please abide by our Code of Conduct.
- Contribute through Discussions and coding by following the Contribution Guide. The Git project Issues give an up-to-date list of potential enhancements and planned milestones. Propose new ones.
- A Youtube video introduction to Magpylib v4.0.0 within the GSC network.
- An open-access paper from the year 2020 describes v2 of this library with most basic concepts still intact in later versions.
Quickstart
Here is an example on how to use Magpylib.
import magpylib as magpy
cube = magpy.magnet.Cuboid(
polarization=(1, 0, 0),
dimension=(0.01, 0.02, 0.03),
)
print(cube.position)
print(cube.orientation.as_rotvec())
cube.move((0, 0, -0.02))
cube.rotate_from_angax(angle=45, axis="z")
print(cube.position)
print(cube.orientation.as_rotvec(degrees=True))
observers = [(0, 0, 0), (0.01, 0, 0), (0.02, 0, 0)]
B = magpy.getB(cube, observers)
print(B.round(2))
sensor = magpy.Sensor(position=(0, 0, 0))
sensor.rotate_from_angax(angle=45, axis=(1, 1, 1))
H = magpy.getH(cube, sensor)
print(H.round())
cube.position = [(0, 0, -.02), (1, 0, -.02), (2, 0, -.02)]
B = cube.getB(sensor)
print(B.round(2))
magpy.show(cube, sensor, backend="pyvista")
More details and other important features are described in detail in the Documentation. Key features are:
- Collections: Group multiple objects for common manipulation
- Complex shapes: Create magnets with arbitrary shapes
- Graphics: Styling options, graphic backends, animations, and 3D models
- CustomSource: Integrate your own field implementation
- Direct interface: Bypass the object oriented interface (max speed)
How can I cite this library ?
We would be happy if you give us credit for our efforts. A valid bibtex entry for the 2020 open-access paper would be
@article{ortner2020magpylib,
title={Magpylib: A free Python package for magnetic field computation},
author={Ortner, Michael and Bandeira, Lucas Gabriel Coliado},
journal={SoftwareX},
volume={11},
pages={100466},
year={2020},
publisher={Elsevier}
}
A valid software citation could be
@software{magpylib,
author = {{Michael-Ortner et al.}},
title = {magpylib},
url = {https://magpylib.readthedocs.io/en/latest/},
version = {5.1.1},
date = {2023-06-25},
}