|
1 | 1 | # LoopFieldCalc |
2 | 2 |
|
3 | | -[](https://archermarx.github.io/LoopFieldCalc.jl/stable) |
4 | | -[](https://archermarx.github.io/LoopFieldCalc.jl/dev) |
5 | 3 | [](https://travis-ci.com/archermarx/LoopFieldCalc.jl) |
6 | 4 | [](https://codecov.io/gh/archermarx/LoopFieldCalc.jl) |
| 5 | + |
| 6 | +Compute the magnetic field due to current-carrying loops at arbitrary locations. |
| 7 | + |
| 8 | +## Example 1: Field due to a single loop |
| 9 | + |
| 10 | +We define a current loop as follows |
| 11 | + |
| 12 | +```julia |
| 13 | +# Define current loop with radius of 50 cm, current of 1 A, centered at the origin |
| 14 | +loop = LoopFieldCalc.CurrentLoop( |
| 15 | + radius = 0.5, # radius in meters |
| 16 | + current = 1.0, # current in Amperes |
| 17 | + center = LoopFieldCalc.CartesianPoint(0.0, 0.0, 0.0) # cartesian coordinates of the loop center |
| 18 | +) |
| 19 | +``` |
| 20 | + |
| 21 | +Loop center coordinates can be specified using either cartesian coordinates or cylindrical coordinates. |
| 22 | + |
| 23 | +```julia |
| 24 | +# Define current loop with radius of 50 cm, current of 1 A, centered at the origin |
| 25 | +loop = LoopFieldCalc.CurrentLoop( |
| 26 | + radius = 0.5, # radius in meters |
| 27 | + current = 1.0, # current in Amperes |
| 28 | + center = LoopFieldCalc.CylindricalPoint(0.0, 0.0, 0.0) # cartesian coordinates of the loop center |
| 29 | +) |
| 30 | +``` |
| 31 | + |
| 32 | +To compute the magnetic field components at a given point, use the `field_at_point` function. The loop normal vector is assumed to be parallel to the `z` axis. |
| 33 | + |
| 34 | +```julia |
| 35 | +# Define the point at which the magnetic field is computed in Cartesian space |
| 36 | +point_to_compute = LoopFieldCalc.CartesianPoint(1.0, 1.0, 1.0) |
| 37 | + |
| 38 | +# Compute the field strength, stream function, and scalar potential |
| 39 | +Bx, By, Bz, stream_func, scalar_potential = LoopFieldCalc.field_at_point(loop, point_to_compute) |
| 40 | +``` |
| 41 | + |
| 42 | +If you want to compute the magnetic field at many points, you can broadcast `field_at_point` over a `Vector` of `CartesianPoints` |
| 43 | + |
| 44 | +```julia |
| 45 | + |
| 46 | +# Define equally-spaced points along the z-axis |
| 47 | +points = [LoopFieldCalc.CartesianPoint(0.0, 0.0, z) for z in 0.0:0.1:2.0] |
| 48 | + |
| 49 | +# Compute the field at these points |
| 50 | +fields = LoopFieldCalc.field_at_point.(loop, points) |
| 51 | + |
| 52 | +``` |
| 53 | + |
| 54 | +If your points form a regularly-spaced grid, you can use the `field_on_grid` function to compute the magnetic field due to multiple loops at the grid points |
| 55 | + |
| 56 | +```julia |
| 57 | +# Define grid points along x, y, and z axes. We have a 2D grid of points in the y-z plane. |
| 58 | +xs = 0.0:0.0 |
| 59 | +ys = -1.0:0.01:1.0 |
| 60 | +zs = -2.0:0.01:2.0 |
| 61 | + |
| 62 | +# Define current loops. Here, we only have the single loop, defined above |
| 63 | +loops = [loop] |
| 64 | + |
| 65 | +# Compute the magnetic field components |
| 66 | +Bx, By, Bz = LoopFieldCalc.field_on_grid(loops, xs, ys, zs) |
| 67 | +``` |
| 68 | + |
| 69 | +You can write output of this function to a Tecplot-compatible ASCII data format |
| 70 | +```julia |
| 71 | +LoopFieldCalc.write_field("examples/example_1.dat", ys, zs, By[1, :, :], Bz[1, :, :]) |
| 72 | +``` |
| 73 | + |
| 74 | +We can visualize this in Tecplot: |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +## Example 2: 2D magnetic field due to offset concentric coils |
| 80 | + |
| 81 | +This example computes the field generated by the magnetic coil configuration below |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +```julia |
| 86 | +using LoopFieldCalc |
| 87 | + |
| 88 | +# Define grid on which we compute magnetic field strength. |
| 89 | +# This is a 2D grid in the y-z plane |
| 90 | +xs = 0.0:0.0 |
| 91 | +ys = -0.4:0.005:0.4 |
| 92 | +zs = -0.3:0.005:0.3 |
| 93 | + |
| 94 | +# Define number of wire turns per coil |
| 95 | +nturns = 10 |
| 96 | + |
| 97 | +# Define geometry of inner coil |
| 98 | +inner_coil_radius = 0.1 |
| 99 | +inner_coil_width = 0.15 |
| 100 | +inner_coil_offset = -0.1 |
| 101 | +inner_coil_current = 1.0 |
| 102 | + |
| 103 | +# Generate loops for inner coil |
| 104 | +inner_coil = [ |
| 105 | + LoopFieldCalc.CurrentLoop( |
| 106 | + radius = inner_coil_radius, |
| 107 | + current = inner_coil_current / nturns, |
| 108 | + center = LoopFieldCalc.CylindricalPoint(inner_coil_offset + z, 0.0, 0.0) |
| 109 | + ) |
| 110 | + for z in LinRange(0.0, inner_coil_width, nturns) |
| 111 | +] |
| 112 | + |
| 113 | +# Define geometry for outer coil |
| 114 | +outer_coil_radius = 0.25 |
| 115 | +outer_coil_width = 0.15 |
| 116 | +outer_coil_offset = -0.15 |
| 117 | +outer_coil_current = 0.3 * inner_coil_current |
| 118 | + |
| 119 | +# Generate loops for outer coil |
| 120 | +outer_coil = [ |
| 121 | + LoopFieldCalc.CurrentLoop( |
| 122 | + radius = outer_coil_radius, |
| 123 | + current = outer_coil_current / nturns, |
| 124 | + center = LoopFieldCalc.CylindricalPoint(outer_coil_offset + z, 0.0, 0.0) |
| 125 | + ) |
| 126 | + for z in LinRange(0.0, outer_coil_width, nturns) |
| 127 | +] |
| 128 | + |
| 129 | +# Combine inner and outer coil loops into a single vector and compute field |
| 130 | +loops = [outer_coil; inner_coil] |
| 131 | +Bx, By, Bz = LoopFieldCalc.field_on_grid(loops, xs, ys, zs) |
| 132 | + |
| 133 | +# Write output. By reversing z and y and transposing the magnetic field matrices, we |
| 134 | +# can rotate the output so that the loop axis is aligned with the x axis |
| 135 | +LoopFieldCalc.write_field("examples/example_2.dat", zs, ys, Bz[1, :, :]', By[1, :, :]') |
| 136 | +``` |
| 137 | + |
| 138 | +The result (visualized in Tecplot) is below |
| 139 | + |
| 140 | + |
0 commit comments