In the previous chapters I’ve describe how to calculate and plot Electric Field Lines for 2 dimensional case with ODE from SciPy. In this tutorial I will show, how you can easily swat these calculations to the 3D case.
NOTE! It is important to understand, that 3D case will use seriously more computer memory and will be much slower as well. Therefore, please pay attention for selection of the domain and grid sizes.
Essential modification from 2D to 3D conversion
ODE integrated Efield lines in 3D space 3D Electric Field lines integrated with ODE from SciPy
Original image: 643 x 518
Previously, all starting points were evenly distributed around point charge in the 2D space. In 3D space it is pretty difficult evenly distribute starting points around the charge. It is very difficult mathematical task about even distributing objects on the sphere. Therefore I will place these starting point randomly.
In total we will take 15 points around each sphere. Also domain will be smaller – from -2 to 2 and step-size will be 0.05. I will place 4 random charges and will make all charges +1 or -1. Again, physically, charges will be located in the space from -1 to +1
Charge now will have 4 parameters, x, y, z and q
n_sphere = 15
s, e, d = -2, 2, 0.05
X, Y, Z = np.mgrid[s:e:d, s:e:d, s:e:d]
NQ = 4
Q = np.random.uniform(low=-1, high=1, size=(NQ,4))
Q[:,3] /= np.abs(Q[:,3]) # make charge +1 or -1
Electric field will be calculated for 3 dimensions. And respectively, it should be interpolated on 3D grid as well
Ex = 0*X
Ey = 0*Y
Ez = 0*Z
for q in Q:
r2 = (X - q)**2 + (Y - q)**2 + (Z - q)**2
Ex += q*(X - q) / r2**(3/2)
Ey += q*(Y - q) / r2**(3/2)
Ez += q*(Z - q) / r2**(3/2)
Exi = RGI((X[:, 0, 0], Y[0, :, 0], Z[0, 0, :]), Ex, method='linear')
Eyi = RGI((X[:, 0, 0], Y[0, :, 0], Z[0, 0, :]), Ey, method='linear')
Ezi = RGI((X[:, 0, 0], Y[0, :, 0], Z[0, 0, :]), Ez, method='linear')
Interpolation function should work in 3D space as well
def IL3(t, coord):
xi = coord
yi = coord
zi = coord
ex = Exi([xi, yi, zi])
ey = Eyi([xi, yi, zi])
ez = Ezi([xi, yi, zi])
abort = True
return [0, 0, 0]
return [ex, ey, ez]
Starting points for electric field lines
It is very difficult to distribute evenly objects on the sphere and this is not a goal for this lesson. Therefore I will do simplest distribution of the starting points on the sphere – random distribution.
I will Do select randomly angled PHI and TETA and then use them as a random spherical coordinated for the starting points.
See how to convert spherical coordinated to orthogonal system: