Skip to main content

Pymagnet

User friendly magnetic field calculations in Python

License: MPL 2.0 License: CC BY-SA 4.0 DOI

Tests Status Coverage Status Flake8 Status

Getting Started

Installing pymagnet can be done using

python -m pip install pymagnet 

Pymagnet is a collection of routines to calculate and plot the magnetic field due to arbitrary 2D and 3D objects, like cubes or cylinders, as well as complex non-convex structures stored in STL files. The library can also calculate the magnetic forces and torques on one magnet due to all other magnets in the system.

The approach assumes the magnets are uniformly magnetised, and fully transparent to magnetic fields. There are some drawbacks to this compared to Finite Element Methods (FEM), but with the advantage of significantly faster calculations.

The current version is written in Python with some speed up using Numpy and Numba, but the backend is being ported to Rust for improved performance.

Features

This code uses analytical expressions to calculate the magnetic field due to simple magnets:

  • 3D: cubes, prisms (cuboids), cylinders, spheres

3D Example Slide

3D Example Volume

  • 2D: rectangles, squares, circles

2D circle contour

2D circle stream

and complex compound objects:

  • 3D: Polyhedra stored as STL meshes
  • 2D: Polygons constructed from collections of line elements

There are helper functions to plot the data as line, contour, slice, and volume plots, but the underlying data is also accessible.

Documentation

Full documentation can be found here: https://pdunne.github.io/pymagnet/

Examples

Examples can be found in the repository.

Or run on Colab:

2D Examples

Getting Started First Steps

1D Simple Plots Open In Colab

2D Standard Shapes Open In Colab

2D Arbitrary Polygons Open In Colab

Misc. Examples Open In Colab

3D Examples

Analytical Shapes 3D Examples

Spheres Open In Colab

3D STL Meshes Open In Colab

3D Stanford Bunny Open In Colab

Upload Your Own Custom Magnet Open In Colab

Forces and Torques

Cubes Open In Colab

Cylinders Open In Colab

Spheres Open In Colab

STL Cubes Open In Colab

STL Pentagonal Prisms Open In Colab

Binder

The example notebooks can be run as an instance using Binder:

Binder

Usage

Forms of this library have been used in a number of projects including Liquid flow and control without solid walls, Nature 2020.

Licensing

Source code licensed under the Mozilla Public License Version 2.0

Documentation is licensed under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

This is a human-readable summary of (and not a substitute for) the license, adapted from CS50x. Official translations of this license are available in other languages.

You are free to:

  • Share — copy and redistribute the material in any medium or format.
  • Adapt — remix, transform, and build upon the material.

Under the following terms:

  • Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original
  • No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be licensed as above, without any additional terms or conditions.

Release files for pymagnet 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pymagnet 0.4.1
File Size Uploaded
pymagnet-0.4.1.tar.gz 59.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pymagnet 0.4.1
File Interpreter ABI Platform
pymagnet-0.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 132.2 kB

Release files / pymagnet-0.4.1.tar.gz

Download URL pymagnet-0.4.1.tar.gz
Size 59.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a03673bf0349800ea36a3230f2f77f530a62ab6441aede2a8fccdea360a34818
BLAKE2b-256 checksum
How to use checksums
569553b4c9aa1743e76b082b611950ea298f2d6621eafba0dafa2c8dde61cea2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.11 CPython/3.9.7 Darwin/20.6.0

Release files / pymagnet-0.4.1-py3-none-any.whl

Download URL pymagnet-0.4.1-py3-none-any.whl
Size 73.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
75a172aaed97dad9a7030df03553346128236851cdb57dad05f7486dba66f0fb
BLAKE2b-256 checksum
How to use checksums
96f4ec10ec81ca6d7f2649170a7bbf77c57ad80e4a2055500d7ad98d06f85378
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.11 CPython/3.9.7 Darwin/20.6.0

Release history Release notifications | RSS feed

This release

0.4.1 This release

2 release files

0.4.0

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page