Bradley Worley about
sci
sw
hw
cv
etc
GPUfield A fast magnetostatics solver

Introduction

Magnetic fields are not the simplest beasties to tame. When any precision is required in the estimated field characteristics of an electromagnet, coil designers turn to their computers and start running simulations.

The mathematics

Computing the magnetic field generated by an electric current flowing through an infinitely narrow wire in a vacuum (or air) is an exceptionally simple task, requiring the use of the Biot-Savart equation:

Without too much headache, any three-dimensional curve may be approximated by line segments, and the magnetic field at any point may be computed using a few vector operations! :)

For every line segment, the angles between the wire ends ($\mathbf{A}$ and $\mathbf{B}$) and the computation point ($\mathbf{r}$) are calculated:

Then the magnetic field unit vector is calculated:

where…

The final magnetic field at $\mathbf{r}$ is then as follows:

Where $\mathbf{A}$ and $\mathbf{B}$ are the starting and ending points of each line segment, $\mathbf{r}$ is the point at which the field is to be computed, and $\mathbf{v}$ is the perpendicular vector from $\mathbf{r}$ to the segment $\mathbf{AB}$.

Whew! Now wasn’t that simple?

Image gallery

I’ve always found magnetic fields to give the most beautiful images, so here are a few computed results for your enjoyment. :)

The code

The GPUfield program runs as an interpreter that parses a very simple set of commands. Wires are added using moveto and lineto statements, or through higher-level commands like arc, solenoid, maxwell or golay. Once the wires are in place, the magnetic field may be computed along a surface using the grid command, or along an arbitrary linear trajectory using the traj command.

GPUfield runs all field computations on the graphics card using the CUDA C API. Calculations that would’ve taken ten hours on a single CPU core can be reduced to a few seconds using CUDA, thanks to the embarrassingly parallel nature of the task.

Enough of my rambling… If you want to check out the source code, the latest version is available on GitHub:

geekysuavo.github.io