Skip to main content

biotsavart

Magnetic fields of steady currents in free space, from the Biot–Savart law.

A current path is a polyline of straight segments. The field of each segment is evaluated in closed form (no numerical quadrature) and summed. Everything is vectorised with NumPy, so evaluating millions of observation points is fast. The method is exact for straight wires, so it suits loops made of long straight sides (rectangular loops, figure-8 loops, cable layouts). Curved wires work by splitting them into short segments.

Field lines and magnitude of a 1 m square loop in the y = 0 plane

Installation

pip install biotsavartlaw

or from source:

git clone https://github.com/mpgriff/biotsavart.git
cd biotsavart
pip install -e .

The import name is biotsavart. Requires Python ≥ 3.8, NumPy and Matplotlib.

Quick start

import numpy as np
from biotsavart import Loop, ObsPoint, calc_bfield, mu0

loop = Loop.square(1.0)                     # 1 m square in the z = 0 plane, centred on the origin

# observation points on a regular grid (x, y, z may each be a scalar or a 1D array)
obs = ObsPoint(x=np.linspace(-2, 2, 101), y=0.0, z=np.linspace(-2, 2, 101))

B = calc_bfield(loop.xyz, obs.xyz, I=10.0)  # tesla, shape (101, 101, 3)
Bx, By, Bz = B[..., 0], B[..., 1], B[..., 2]

Check: the field at the centre of a square loop of side $a$ is $2\sqrt{2} \mu_0 I/(\pi a)$.

>>> calc_bfield(loop.xyz, np.zeros(3), I=1.0)[2]
1.1313708498984761e-06
>>> 2*np.sqrt(2)*mu0/np.pi
1.1313708498984761e-06

API

Object Description
Loop(x, y, z) A current path given by its vertices, in order. The current flows from vertex i to i+1. For a closed loop, repeat the first vertex at the end.
Loop.square(side) Square in the $z=0$ plane, centred on the origin, counter-clockwise seen from $+z$.
Loop.fig8(side) Figure-8 loop made of two adjacent squares with opposite circulation.
Loop.circle(r, line=0.02) Circle approximated by segments about line metres long.
loop.xyz Vertices as an (N, 3) array. This is what calc_bfield takes.
ObsPoint(x, y, z, grid=True) Observation points. With grid=True (default) the inputs are combined with meshgrid. With grid=False they are matched point by point. Use .xyz to get an (..., 3) array.
calc_bfield(loop_xyz, xyz, I=1.) Field of the whole path at the points xyz (any shape ending in 3). Returns the same shape.
bfield_from_line(P, Q, xyz, I=1.) Field of a single segment from P to Q.
mu0 $4\pi\times10^{-7}$ H/m

Use SI units throughout: positions in metres, current in amperes, field in tesla. Any (..., 3) NumPy array works as observation points, so ObsPoint is only a convenience.

A point that lies exactly on the line of a segment has no well-defined field from that segment. That segment contributes zero there, and NumPy may warn about the division by zero.

How it works

The coordinate transformations used for one wire segment

For a straight segment, the Biot–Savart integral has a simple closed form in a particular frame: the observation point at the origin and the wire parallel to the $x$-axis, from $x_1$ to $x_2$, at height $y=d$ in the $z=0$ plane. There the field points purely along $z$:

B_z = -\mathrm{sign}(d)\,\frac{\mu_0 I}{4\pi |d|}\left(\frac{x_2}{\sqrt{x_2^2+d^2}}-\frac{x_1}{\sqrt{x_1^2+d^2}}\right)

bfield_from_line moves every (segment, observation point) pair into that frame and back:

  1. Take one segment $P\to Q$ of the loop and an observation point $\vec r_{obs}$.
  2. Translate: $\vec p = P-\vec r_{obs}$, $\vec q = Q-\vec r_{obs}$, so the observation point is the origin.
  3. Find the plane through the origin, $\vec p$ and $\vec q$. Its unit normal is $\hat n = \vec p\times\vec q/|\vec p\times\vec q|$, flipped if needed so that $n_z \ge 0$.
  4. Rotate that plane onto $z=0$ (getR). The rotation is a half-turn about the bisector of $\hat n$ and $\hat z$, $R = 2 \hat a \hat a^{\mathsf T}-\mathbb{1}$ with $\hat a = (\hat n+\hat z)/|\hat n+\hat z|$. It swaps $\hat n$ and $\hat z$, and it is symmetric and its own inverse.
  5. Rotate about $z$ (getRz) so the segment points along $+x$. The segment's $y$ coordinate is now the perpendicular distance $d$.
  6. Evaluate the closed form above and rotate back: $\vec B = R R_z^{\mathsf T} \vec B''$. The field is therefore always parallel to $\hat n$.

calc_bfield repeats this for each segment, vectorised over all observation points, and sums the contributions. The notebook examples/implementation_overview.ipynb goes through each step with figures. It also checks the result against brute-force numerical integration.

Validation

tests/test_physics.py compares against analytic results: a long straight wire ($B = \mu_0 I / 2\pi r$, along $x$ and $y$) and the on-axis field of a circular loop. The circle test also checks that a coarse polygon does not match, which confirms the test can fail.

On-axis field of a circular loop: analytic vs 62 and 5 segments

pip install -e ".[test]"
pytest

License

MIT, see LICENSE.

Metadata

Release files for biotsavartlaw 0.1.0

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

Source distribution (sdist)

Source distribution for biotsavartlaw 0.1.0
File Size Uploaded Provenance Actions
biotsavartlaw-0.1.0.tar.gz 18.4 kB Without provenance Details for biotsavartlaw-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 36.3 kB

Release files / biotsavartlaw-0.1.0.tar.gz

Download URL biotsavartlaw-0.1.0.tar.gz
Size 18.4 kB
Tags Source
SHA-256 checksum
How to use checksums
6790a59df80f53b7659db67a28e6c264f53df6de8202c5a2ff60088c5066ae42
BLAKE2b-256 checksum
How to use checksums
317cf5bec8b0c90ec0e13905e9ad80052ec9e1aa855242f9f4ca3dfdce8709a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / biotsavartlaw-0.1.0-py3-none-any.whl

Download URL biotsavartlaw-0.1.0-py3-none-any.whl
Size 17.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7287d6aa3279b497e717b9e7cee16f1a216df08beb9190661c2d7be085365009
BLAKE2b-256 checksum
How to use checksums
11e92442be8674c8dde3d3810f181cf77e57bf549de4dba53d57777ef3b8ecd7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Security for biotsavartlaw 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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