I have two 2d arrays of electric field components:
Ex with (256, 512) and Ez with (256, 512) sizes.
I need to calculate scalar potential. Gradient of scalar potential is the electric field vector. So I need the reverse of the gradient.
I tried using the following loop to calculate the potential Φ (phi
):
nx = 256
nz = 512
phi = np.zeros([nx, nz])
for i in range(1, nz):
phi[:, i] = -dx * Ex[:, i-1] + phi[:, i-1]
for j in range(1, nx):
phi[j, :] = -dz * Ez[j-1, :] + phi[j-1, :]
This gives me an ok but weird Φ (these vertical lines are suspicious):
I expected to receive something like this (a colleague calculated the same value in Fortran and his profile is different):
This is their Fortran code:
allocate(phi(nx, nz))
phi(1, 1) = 0.0
do i = 2, nx
phi(i, 1) = -dx * ex(i-1, 1) + phi(i-1, 1)
enddo
do i = 1, nx
do j = 2, nz
phi(i, j) = -dz * ez(i, j-1) + phi(i, j-1)
enddo
enddo
Where did I make a mistake? Or is there a better way to calculate the scalar potential from an electric field?