# More efficient way to calculate magnetic field using Biot-Savart

I am writing a program in python that is supposed to calculate the magnetic field along a conducting coil that is made up of a bunch of points, and the magnetic field is generated by other conducting coils which were made by rotating the points that make up the original conducting coil. I have tried doing this using Biot-Savart law (code below), but each coil is made up of ~300 points, and there are 10 coils in total, so my method is called ~810,000 times, but I think creating and subtracting 2 vectors and then crossing them with each iteration makes it take a very long time to run, so I was wondering if someone knows of a more efficient way to do Biot-Savart, or if I should try a different method. Any insight would be very much appreciated, thank you!

My Biot-Savart method:

def biotsav(l,lprev,pointpos,cur,mu0=1e-7):

#l is the point along the wire generating the magnetic field
#lprev is the previous point along the wire that generated a magnetic field
#pointpos is the position where the magnetic field will be measured at
#cur is the current running through the wire

#if the point causing the magnetic field is the point where the magnetic field is being measured at, return 0 otherwise it would return infinity
if l[0] == pointpos[0] and l[1] == pointpos[1] and l[2] == pointpos[2]:
return np.array([0,0,0])

dl = np.subtract(l,lprev) #vector going from previous point along wire to current point
r = np.subtract(pointpos,l) #vector going from point causing magnetic field to point where magnetic field is being measured
rmag = np.sqrt(r[0]**2 + r[1]**2 + r[2]**2) #magnitude of r
return mu0*cur*np.cross(dl,r)/(rmag**3) #biot savart equation


The loop where the method is called:

#array that will hold magnetic field values along each point on the coil
Bfield = []

#Caclculates the magnetic field at a point on the TF caused by all points on all coils
for i in range(numpts): #this for loop runs through all points along original coil

Btemp = [0,0,0] #placeholder array for updating magnetic field measured at a point along original coil

for j in range(len(allxs)): #this for loop goes through all coils created through symmetry

for k in range(len(allxs[j])): #this for loop goes through all points along coils created by symmetry
currentpt = [allxs[j][k],allys[j][k],allzs[j][k]]
prevpt = [allxs[j][k-1],allys[j][k-1],allzs[j][k-1]]
coilpt = [xlist[i],ylist[i],zlist[i]]

Bfield.append(np.array([Btemp[0],Btemp[1],Btemp[2]])) #updates array with net magnetic field measured at point along original coil


**Note: allxs, allys, and allzs are arrays that contain the x, y, and z coordinates of the 9 coils created by symmetry. Also, all coils have equal current running through them

For reference, the red coil is the original coil and the one I want to measure the magnetic field caused by the other 9 white coils along the original coil.

• If all coils have the same shape you need to calculate the magnetic field from one of them only, for the rest use geometric rotation, that's a factor of 10 saving. BTW, if you need the field far away from the coils, the dipole approximation would simplify things a great deal. Aug 28 '20 at 4:04
• @MaximUmansky Hi Maxim, thanks for your reply! I did what you suggested and it did reduce the runtime by about 10x, so thank you very much! However, I think I messed something up because elsewhere in my code I plotted the x,y, and z components of the magnetic force on the coil, and they have now changed. I used the same rotation matrix as before, but applied it to the magnetic field instead, but since that didn't change the z components, I was thinking that is the issue. If you could see what I did wrong, it would be much appreciated! [link]gyazo.com/5fa4b001cdb65da76fb2395e582f65be Aug 28 '20 at 6:57
• Not sure where the problem is in your code; but the topic of rotating a vector field is discussed in math.stackexchange.com/questions/912070/… and it looks correct there. I suggest implementing the rotation transformation as a separate function and verify and debug first on some simple vector fields. Aug 29 '20 at 5:43
• Also, in the picture you have an additional mirror plane. So you can cut the computation time in half again.
– Ron
Aug 31 '20 at 14:06
• @Ron - How would you take advantage of the symmetry plane? I am not sure it would save you anything. Sep 5 '20 at 6:39