Skip to content

kev-m/magnetic_field_calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Magnetic Field Calculator

Extremely simple and generic Biot-Savart solver.

This is a very simple C project that uses the Biot-Savart Law to calculate the magnetic field generated by a current flowing through a conductor.

Since both the conductor and the target field are specified only as an array of x,y,z coordinates, this program allows users to solve for the magnetic field in any desired volume.

About

I wrote this project to verify the magnetic field calculated by pyCoilGen, a project that I was converting from MATLAB.

Usage

Basic help is available from the application command-line:

$ ./magnetic_field_calculator -h
Usage: ./calculate_magnetic_field -w <wire_path.txt> -t <target_field.txt> -A <current> [-d]

Calculate and print the magnetic field at the target co-ordinates due to the given wire path using the Biot-Savart Law.
See: https://en.wikipedia.org/wiki/Biot%E2%80%93Savart_law

Both the wire path and target field files have the same syntax:
Line 1: The number of subsequent entries, e.g. 100
Lines 2 onward: The comma-separated x,y,z co-ordinates, e.g. -0.117668100970,0.484508134559,0.73639054917

Options:
  -w <path>      Specify the file path for the wire path. Default: wire_path_xyz.csv
  -A <current>   Specify the current flowing through the wire (in Amperes). Default: 1.0
  -t <path>      Specify the file path for the target field. Default: target_field_xyz.csv
  -c             Include the target field co-ordinates in the output (optional).
  -d             Enable debug mode (optional).
  -h             Display this help message.

The output is the comma-separated magnetic field vector at the provided target field positions, i.e. field-x,y,z.
Line 1 of the output is the current that was used to calculate the target field.
Line 2 is the number of subsequent lines.
Line 3 onwards are the comma-separated data, either just the calculated field or the co-ordinates and field:
If -c is specified, the target position is repeated in the output, i.e. target-x,y,z,field-x,y,z.

Example input files are present in the data directory.

Example Wire Path

1539
3.125092638343569e-17,0.5,0.7363905491754281
-0.11766810097066137,0.484508134559504,0.7363905491754281
-0.12940938820518927,0.4829623663092337,0.7359083851890753
-0.14201699520016128,0.47774013329749565,0.7343176593724484
-0.2499997048903709,0.43301221687883523,0.7343176593724484
-0.26323971046966066,0.422852819349198,0.7308214946311531
-0.35355338454246515,0.35355338454246515,0.7308214946311531
-0.42285281934919794,0.26323971046966066,0.7308214946311531
-0.43301226007070165,0.24999972875956028,0.7228759884437708
-0.4504691083791091,0.20785543800074036,0.6975775186966908
...

Example Target Field Co-ordinates

257
0.0,0.0,-0.15
-0.075,-0.0375,-0.11249999999999999
-0.075,0.0,-0.11249999999999999
-0.075,0.0375,-0.11249999999999999
-0.0375,-0.075,-0.11249999999999999
-0.0375,-0.0375,-0.11249999999999999
-0.0375,0.0,-0.11249999999999999
-0.0375,0.0375,-0.11249999999999999
-0.0375,0.075,-0.11249999999999999
...

Example outputs

Field Vector Only

$  ./magnetic_field_calculator -w data/ygradient_coil_0_5_wire_m.txt -A 1 -t data/ygradient_coil_0_5_target_m.txt
1.0000000000
257
6.8368204248e-08,5.1131699638e-06,-4.7058594083e-09
1.1351573674e-07,4.0262629538e-06,1.2279428300e-06
6.6463529962e-08,3.9860284405e-06,-1.6013347400e-08
1.9749208595e-08,4.0208051103e-06,-1.2605374970e-06
1.1063012430e-07,4.1077483989e-06,2.4749722539e-06
9.0100054662e-08,3.9926176619e-06,1.2208303121e-06
7.0320486552e-08,3.9531689102e-06,-1.1243121651e-08
...

Co-ordinates and Field Vector

$ ./magnetic_field_calculator -w data/ygradient_coil_0_5_wire_m.txt -A 1 -t data/ygradient_coil_0_5_target_m.txt -c
1.0000000000
257
0.0000000000e+00,0.0000000000e+00,-1.5000000000e-01,6.8368204248e-08,5.1131699638e-06,-4.7058594083e-09
-7.5000000000e-02,-3.7500000000e-02,-1.1250000000e-01,1.1351573674e-07,4.0262629538e-06,1.2279428300e-06
-7.5000000000e-02,0.0000000000e+00,-1.1250000000e-01,6.6463529962e-08,3.9860284405e-06,-1.6013347400e-08
-7.5000000000e-02,3.7500000000e-02,-1.1250000000e-01,1.9749208595e-08,4.0208051103e-06,-1.2605374970e-06
-3.7500000000e-02,-7.5000000000e-02,-1.1250000000e-01,1.1063012430e-07,4.1077483989e-06,2.4749722539e-06
-3.7500000000e-02,-3.7500000000e-02,-1.1250000000e-01,9.0100054662e-08,3.9926176619e-06,1.2208303121e-06
-3.7500000000e-02,0.0000000000e+00,-1.1250000000e-01,7.0320486552e-08,3.9531689102e-06,-1.1243121651e-08
-3.7500000000e-02,3.7500000000e-02,-1.1250000000e-01,5.0981893969e-08,3.9881877597e-06,-1.2437597675e-06
-3.7500000000e-02,7.5000000000e-02,-1.1250000000e-01,3.1736338706e-08,4.0986633047e-06,-2.4992383288e-06
...

Dependencies

Requires gcc and make.

On Linux Debian or equivalent:

$ sudo apt-get install gcc make

Installation

Clone from GitHub and make:

$ git clone https://github.com/kev-m/magnetic_field_calculator
$ cd magnetic_field_calculator
$ make

About

Extremely simple and generic Biot-Savart solver.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published