Skip to content

Simulation of Biot-Savart law - Analytical solution to calculate magnetic field of an arbitrarily shaped coil. MATLAB and Python interface are provided.

Notifications You must be signed in to change notification settings

aghaeifar/Biot-Savart

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Magnetic Field Calculator with Biot-Savart Law

Lates Release

This software computes the magnetic field of a coil with an arbitrary shape using the Biot-Savart law. The coil may consist of one or multiple distinct segments. The program employs the analytical solution suggested in this source to determine the magnetic field generated by a finite thin wire. Input and output units adhere to the International System of Units (SI), "meter" for spatial positions and "tesla" for magnetic field.

Calculation is parallelized over given spatial points. Parallelization is implemented based on openMP algorithms.

Python interface:

Simply use cmake to compile program as a shared libary. See python example below.

MATLAB interface:

Program can be compiled as mex file without any change. see below.

Installation:

Build shared library

Clone Biot-Savart simulator from repository:

git clone https://github.com/aghaeifar/Biot-Savart.git

Build and install as a shared library:

$ cd Biot-Savart
$ cmake -B ./build
$ cmake --build ./build --config Release

Build MATLAB mex

Linux

mex biot_savart.cpp  -lm -ldl -ltbb -R2018a CXXFLAGS="\$CXXFLAGS -std=c++17"

Windows

mex biot_savart.cpp  -R2018a COMPFLAGS="$COMPFLAGS /std:c++17"

Program building is tested in Ubuntu 22.04 with g++ 11.4 and Windows 11 with MATLAB R2023a and Microsoft Visual C++ 2022.

Example:

MATLAB

N = 100;
rng = linspace(-0.1, 0.1, N);
[x, y, z] = ndgrid(rng, rng, rng);
xyz = [x(:), y(:), z(:)]';  % in the form of 3xN, where N is number of spatial points

start = [-0.05, 0, 0]'; % in the form of 3xL, where L is number of segments
stop  = [0.05, 0, 0]';  % in the form of 3xL

B = biot_savart(start, stop, xyz); % B is in the form of 3xN

% show z component of magnetic field from a strait line 
Bz = reshape(B(3,:), size(x));
tiledlayout('horizontal', 'TileSpacing','tight','Padding', 'tight'); nexttile;
plot3([start(1), stop(1)], [start(2), stop(2)], [start(3), stop(3)], 'LineWidth', 3); grid on; nexttile;
imagesc(squeeze(Bz(:,:,N/2))); clim([-.3e-4, 0.3e-4]); axis image; axis off; nexttile;
imagesc(squeeze(Bz(:,N/2,:))); clim([-.3e-4, 0.3e-4]); axis image; axis off; nexttile;
imagesc(squeeze(Bz(N/2,:,:))); clim([-.3e-4, 0.3e-4]); axis image; axis off; 

z component of magnetic field from a strait line

N = 200;
rng = linspace(-0.1, 0.1, N);
[x, y, z] = ndgrid(rng, rng, rng);
xyz = [x(:), y(:), z(:)]';  % in the form of 3xN, where N is number of spatial points

L = 60;
radius = 0.05;
theta  = linspace(0, 2*pi, L+1)';
segments = radius * [cos(theta), sin(theta), zeros(L+1,1)]'; % in the form of 3xL, where L is number of segments
start = segments(:, 1:end-1);  % in the form of 3xL
stop  = segments(:, 2:end);  % in the form of 3xL

B = biot_savart(start, stop, xyz); % B is in the form of 3xN

% show z component of magnetic field from a circular coil
Bz = reshape(B(3,:), size(x));
tiledlayout('horizontal', 'TileSpacing','tight','Padding', 'tight'); nexttile;
plot3(segments(1,:), segments(2,:), segments(3,:), 'LineWidth', 3); grid on; nexttile;
imagesc(squeeze(Bz(:,:,N/2))); clim([-.4e-4, 0.4e-4]); axis image; axis off; nexttile;
imagesc(squeeze(Bz(:,N/2,:))); clim([-.4e-4, 0.4e-4]); axis image; axis off; nexttile;
imagesc(squeeze(Bz(N/2,:,:))); clim([-.4e-4, 0.4e-4]); axis image; axis off; 

z component of magnetic field from a strait line

Python

To do...

Contributing:

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

About

Simulation of Biot-Savart law - Analytical solution to calculate magnetic field of an arbitrarily shaped coil. MATLAB and Python interface are provided.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published