FieldForge is a Python package for magnetic field simulations based on the Biot–Savart law. The package uses a fully vectorized NumPy implementation to efficiently compute magnetic field intensity (H) and magnetic flux density (B) generated by current-carrying conductors.
- Fast vectorized magnetic field calculations
- Solenoid and spiral solenoid generators
- Translation and rotation of current sources
- 3D visualization utilities
- Pure Python implementation using NumPy and Matplotlib
Repository: https://github.com/dev-FilipeBCS/Magnetic-Field
Python package:
import fieldforge as ff
Clone the repository and install the package:
git clone https://github.com/dev-FilipeBCS/Magnetic-Field.git
cd Magnetic-Field
pip install .For editable installs during development:
pip install -e .- Python 3.10+
- NumPy
- Matplotlib
These dependencies are installed automatically.
Magnetic-Field/
├── pyproject.toml
├── README.md
└── src/
└── fieldforge/
├── source.py
├── simulation.py
├── plot.py
└── __init__.py
import numpy as np
import fieldforge as ffcoil = ff.solenoid(
radius=0.05,
height=0.10,
current=1.0,
turns=20,
n_elements=144
)field_points = np.array([
[0.0, 0.0, 0.00],
[0.0, 0.0, 0.02],
[0.0, 0.0, 0.04]
])H = ff.sim_h(
sources=[coil],
field_points=field_points
)
print(H)B = ff.sim_b(
sources=[coil],
field_points=field_points
)
print(B)Creates a multi-turn solenoid represented by circular loops.
coil = ff.solenoid(
radius=0.05,
height=0.10,
current=2.0,
turns=10,
n_elements=72
)Parameters:
| Parameter | Description |
|---|---|
radius |
Coil radius (m) |
height |
Solenoid height (m) |
current |
Current (A) |
turns |
Number of turns |
n_elements |
Discretization elements per turn |
Creates a continuous helical winding.
coil = ff.solenoid_spiral(
radius=0.05,
height=0.10,
current=2.0,
turns=10,
n_elements=5000
)Source objects are immutable. Every transformation returns a new source object.
coil2 = coil.move([0.10, 0.00, 0.00])coil2 = coil.move_to([0.0, 0.0, 0.2])coil2 = coil.rotate(
axis=[1, 0, 0],
angle=90,
deg=True
)coil2 = coil.rotate_about(
axis=[0, 1, 0],
angle=45,
deg=True,
point=[0, 0, 0]
)Compute the magnetic field intensity generated by one or more current sources.
H = ff.sim_h(
sources=[coil],
field_points=field_points
)Returns:
array([[Hx, Hy, Hz],
[Hx, Hy, Hz],
...
])with shape:
(N, 3)where N is the number of observation points.
Compute the magnetic flux density:
B = ff.sim_b(
sources=[coil],
field_points=field_points
)FieldForge computes:
B = μ₀ H
where:
μ₀ = 4π × 10⁻⁷ H/m
Display current sources and observation points.
ff.plot_check(
sources=[coil],
field_points=field_points
)ff.plot_solution(
sources=[coil],
field_points=field_points,
h=H,
mode="vector"
)ff.plot_solution(
sources=[coil],
field_points=field_points,
h=H,
mode="scatter"
)import numpy as np
import fieldforge as ff
coil = ff.solenoid(
radius=0.05,
height=0.10,
current=1.0,
turns=20,
n_elements=200
)
z = np.linspace(-0.10, 0.10, 50)
field_points = np.column_stack((
np.zeros_like(z),
np.zeros_like(z),
z
))
H = ff.sim_h(
sources=[coil],
field_points=field_points
)
ff.plot_solution(
sources=[coil],
field_points=field_points,
h=H,
mode="scatter"
)FieldForge uses a fully vectorized NumPy implementation of the Biot–Savart law. By avoiding explicit Python loops over observation points and conductor elements, simulations remain efficient even for large source discretizations and dense observation grids.
| Function | Description |
|---|---|
solenoid() |
Creates a circular multi-turn solenoid |
solenoid_spiral() |
Creates a helical solenoid |
| Method | Description |
|---|---|
move() |
Translate a source |
move_to() |
Move source center to a target position |
rotate() |
Rotate about the origin |
rotate_about() |
Rotate about an arbitrary point |
| Function | Description |
|---|---|
sim_h() |
Computes magnetic field intensity |
sim_b() |
Computes magnetic flux density |
| Function | Description |
|---|---|
plot_check() |
Visualize geometry and observation points |
plot_solution() |
Visualize magnetic field results |
MIT License
Copyright (c) Filipe Brega Có Silva