Skip to content

dev-FilipeBCS/Magnetic-Field

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

FieldForge

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.

Features

  • 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

Installation

From Source

Clone the repository and install the package:

git clone https://github.com/dev-FilipeBCS/Magnetic-Field.git
cd Magnetic-Field

pip install .

Development Installation

For editable installs during development:

pip install -e .

Requirements

  • Python 3.10+
  • NumPy
  • Matplotlib

These dependencies are installed automatically.


Package Structure

Magnetic-Field/
├── pyproject.toml
├── README.md
└── src/
    └── fieldforge/
        ├── source.py
        ├── simulation.py
        ├── plot.py
        └── __init__.py

Quick Start

Import the Package

import numpy as np
import fieldforge as ff

Create a Solenoid

coil = ff.solenoid(
    radius=0.05,
    height=0.10,
    current=1.0,
    turns=20,
    n_elements=144
)

Create Observation Points

field_points = np.array([
    [0.0, 0.0, 0.00],
    [0.0, 0.0, 0.02],
    [0.0, 0.0, 0.04]
])

Compute Magnetic Field Intensity

H = ff.sim_h(
    sources=[coil],
    field_points=field_points
)

print(H)

Compute Magnetic Flux Density

B = ff.sim_b(
    sources=[coil],
    field_points=field_points
)

print(B)

Creating Sources

Circular Solenoid

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

Spiral Solenoid

Creates a continuous helical winding.

coil = ff.solenoid_spiral(
    radius=0.05,
    height=0.10,
    current=2.0,
    turns=10,
    n_elements=5000
)

Source Transformations

Source objects are immutable. Every transformation returns a new source object.

Translation

coil2 = coil.move([0.10, 0.00, 0.00])

Move to a New Position

coil2 = coil.move_to([0.0, 0.0, 0.2])

Rotation About the Origin

coil2 = coil.rotate(
    axis=[1, 0, 0],
    angle=90,
    deg=True
)

Rotation About an Arbitrary Point

coil2 = coil.rotate_about(
    axis=[0, 1, 0],
    angle=45,
    deg=True,
    point=[0, 0, 0]
)

Running Simulations

Magnetic Field Intensity (H)

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.


Magnetic Flux Density (B)

Compute the magnetic flux density:

B = ff.sim_b(
    sources=[coil],
    field_points=field_points
)

FieldForge computes:

B = μ₀ H

where:

μ₀ = 4π × 10⁻⁷ H/m

Visualization

Geometry Verification

Display current sources and observation points.

ff.plot_check(
    sources=[coil],
    field_points=field_points
)

Vector Field Visualization

ff.plot_solution(
    sources=[coil],
    field_points=field_points,
    h=H,
    mode="vector"
)

Magnitude Scatter Plot

ff.plot_solution(
    sources=[coil],
    field_points=field_points,
    h=H,
    mode="scatter"
)

Complete Example

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"
)

Performance

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.


Available Functions

Source Generation

Function Description
solenoid() Creates a circular multi-turn solenoid
solenoid_spiral() Creates a helical solenoid

Source Transformations

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

Simulation

Function Description
sim_h() Computes magnetic field intensity
sim_b() Computes magnetic flux density

Visualization

Function Description
plot_check() Visualize geometry and observation points
plot_solution() Visualize magnetic field results

License

MIT License

Copyright (c) Filipe Brega Có Silva

About

FieldForge is a Python package for fast magnetic field simulations using the Biot–Savart law. It provides vectorized numerical implementations for computing magnetic field intensity (H) and magnetic flux density (B) generated by current-carrying conductors.

Topics

Resources

Stars

Watchers

Forks

Contributors

Languages