Skip to main content

Free Python3 package to compute magnetic fields.

Project description


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

Installation

Install from PyPI using pip

pip install magpylib

Install from conda forge using conda

conda install -c conda-forge magpylib

Magpylib supports Python3.8+ and relies on common scientific computation libraries Numpy, Scipy, Matplotlib and Plotly. Optionally, Pyvista is recommended as graphical backend.

Ressources

Quickstart

Here is an example how to use Magpylib.

import magpylib as magpy

# Create a Cuboid magnet with sides 1,2 and 3 mm respectively, and magnetization
# (polarization) of 1000 mT pointing in x-direction.
cube = magpy.magnet.Cuboid(
  magnetization=(1000,0,0),
  dimension=(1,2,3),
)

# By default, the magnet position is (0,0,0) and its orientation is the unit
# rotation (given by a scipy rotation object), which corresponds to magnet sided
# parallel to global coordinate axes.
print(cube.position)                   # --> [0. 0. 0.]
print(cube.orientation.as_rotvec())    # --> [0. 0. 0.]

# Manipulate object position and orientation through the respective attributes,
# or by using the powerful `move` and `rotate` methods.
cube.move((0,0,-2))
cube.rotate_from_angax(angle=45, axis='z')
print(cube.position)                            # --> [0. 0. -2.]
print(cube.orientation.as_rotvec(degrees=True)) # --> [0. 0. 45.]

# Compute the magnetic field in units of mT at a set of observer positions. Magpylib
# makes use of vectorized computation. Hand over all field computation instances,
# e.g. different observer positions, at one funtion call. Avoid Python loops !!!
observers = [(0,0,0), (1,0,0), (2,0,0)]
B = magpy.getB(cube, observers)
print(B.round()) # --> [[-91. -91.   0.]
                 #      [  1. -38.  84.]
                 #      [ 18. -14.  26.]]

# Sensors are observer objects that can have their own position and orientation.
# Compute the H-field in units of kA/m.
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()) # --> [-95. -36. -14.]

# Position and orientation attributes of Magpylib objects can be vectors of
# multiple positions/orientations refered to as "paths". When computing the
# magnetic field of an object with a path, it is computed at every path index.
cube.position = [(0,0,-2), (1,0,-2), (2,0,-2)]
B = cube.getB(sensor)
print(B.round()) # --> [[-119.  -45.  -18.]
                 #      [   8.  -73.  -55.]
                 #      [  15.  -30.   -8.]]

# When several objects are involved and things are getting complex, make use of
# the `show` function to view your system through Matplotlib, Plotly or Pyvista backends.
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 = {4.4.1rc0},
    date = {2023-06-25},
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

magpylib-4.4.1rc0.tar.gz (222.3 kB view details)

Uploaded Source

Built Distribution

magpylib-4.4.1rc0-py3-none-any.whl (262.1 kB view details)

Uploaded Python 3

File details

Details for the file magpylib-4.4.1rc0.tar.gz.

File metadata

  • Download URL: magpylib-4.4.1rc0.tar.gz
  • Upload date:
  • Size: 222.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for magpylib-4.4.1rc0.tar.gz
Algorithm Hash digest
SHA256 9646582da72ad718ab3743ade1140ef7073fce4e7e3ebc8f42c700315ebf678a
MD5 2dd70e73a2969cedb0ca63648612dda5
BLAKE2b-256 b7883c8676edbcc1bc157131f7eac360c5fe4eb910358f42513eec12ffa749ff

See more details on using hashes here.

File details

Details for the file magpylib-4.4.1rc0-py3-none-any.whl.

File metadata

  • Download URL: magpylib-4.4.1rc0-py3-none-any.whl
  • Upload date:
  • Size: 262.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for magpylib-4.4.1rc0-py3-none-any.whl
Algorithm Hash digest
SHA256 e00b2822e5ab063c3f610d5dcaf3f0434a430507b544196647c7a0bffabef449
MD5 ce6b64bf7612a28ba07273297b557227
BLAKE2b-256 3eee4a87ba8b67419feff086b53aa31943b94dca8497813a6bf503132283faee

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page