简体   繁体   中英

How to find the electric field from the potential?

That's my script to find a potentiel in a ionization room:

# Programme de résolution de l'équation de Laplace
# par la méthode de Gauss-seidel

# importation des librairies
import numpy as np
import time
import matplotlib.pyplot as plt

# définition des paramétres physiques de l'expérience
Vi = 300.0           # le couvercle est à  300 V
V0 = 0.0             # les cotés sont au potentiel nul
x = 0
# définition de la grille de calcul
M = 50
N = 300                     # nombre de pas sur la grile (identique en Ox et Oy)
V = np.zeros([M,N])     # grille de calcul courante
Vnew = np.zeros([M,N])  # grille de stockage des calculs nouveaux

# critère de précision du calcul
EPS = 1e-3
# initialisation des compteurs        
ecart = 1.0
iteration = 0

# définition des conditions aux limites
V[0:8, 0:20] = x
V[8:15, 0:10] = x
V[35:50, 0:10] = x
V[42:50, 0:20] = x
V[0,20:300]  = Vi  # bord supérieur
V[0:8, 20]  = Vi  # vue que 1.6/0,2 = 8
V[41:50, 20] = Vi # bas de la bord gauche
V[8, 10:21] = Vi
V[15, 0:11] = Vi
V[34, 0:11] = Vi
V[41, 10:21] = Vi
V[8:15, 10] = Vi
V[34:41, 10] = Vi
V[15:35, 0] = Vi
V[:,-1] = Vi  # bord droit
V[-1,20:300] = Vi  # bord inférieur
V[23:27,15:299] = V0

# début du calcul - enregistrement de la durée
tdebut = time.time()

# boucle de calcul - méthode de Gauss-Seidel
while ecart > EPS:
  iteration += 1
  # sauvegarde de la grille courante pour calculer l'écart
  Vavant = V.copy()
  # calcul de la nouvelle valeur du potentiel sur la grille
  V[1:-1,1:-1]= 0.25*(Vavant[0:-2,1:-1] +V[2:,1:-1] + Vavant[1:-1,0:-2] + V[1:-1,2:])
  # on repose les même conditions
  V[0:8, 0:20] = x
  V[8:15, 0:10] = x
  V[35:50, 0:10] = x
  V[42:50, 0:20] = x
  V[0,20:300]  = Vi 
  V[0:8, 20]  = Vi  
  V[41:50, 20] = Vi 
  V[8, 10:21] = Vi
  V[15, 0:11] = Vi
  V[34, 0:11] = Vi
  V[41, 10:21] = Vi
  V[8:15, 10] = Vi
  V[34:41, 10] = Vi
  V[15:35, 0] = Vi
  V[:,-1] = Vi  
  V[-1,20:300] = Vi  
  V[23:27,15:299] = V0
  # critère de convergence
  ecart = np.max(np.abs(V-Vavant))

# fin du calcul - affichage de la durée
print ('Nombre iterations : ',iteration  )  
print ('Temps de calcul (s) : ',time.time() - tdebut)

v, u = np.gradient(V)

x = np.linspace(0, 1, N) # x-axis
y = np.linspace(0, 1, M) # y-axis

X, Y= np.meshgrid(x,y)

# Create figure
fig, axarr = plt.subplots(1, 1, figsize=(14, 8), dpi=300)

# Axes 1: Electric potential and field
im1 = axarr[0].contourf(x, y, V, 20)
axarr[0].streamplot(x, y, -u, -v, color="k")
axarr[0].set_title("Electric potential and field")
fig.colorbar(im1, orientation='horizontal', ax=axarr[0],
                  label=r"Electric potential, $V/V_0$")
axarr[0].set_xlabel("$x/L$")
axarr[0].set_ylabel("$y/L$")

but I have a problem in the affichage method the error is in line 90:

line 90, in <module>
    im1 = axarr[0].contour(x, y, V, 20)
TypeError: 'AxesSubplot' object is not subscriptable

I almost tried everything but I couldn't find a solution. How can I fix this error?

matplotlib.pyplot.subplots returns either an array of matplotlib.axes.Axes instances, or a single matplotlib.axes.Axes instance. The latter only when the number of rows (the first positional argument) and the number of columns (second positional argument) are both 1 . So the syntax

fig, ax = plt.subplots(1, 1)

creates a single Axes instance (also referred to as an AxesSubplot ), therefore ax[0] is illegal - it would be valid only if multiple subplots were created by plt.subplots . The correct syntax in your code is

fig, axarr = plt.subplots(1, 1, figsize=(14, 8), dpi=300)

im1 = axarr.contourf(x, y, V, 20)
axarr.streamplot(x, y, -u, -v, color="k")
axarr.set_title("Electric potential and field")
fig.colorbar(im1, orientation='horizontal', ax=axarr,
                  label=r"Electric potential, $V/V_0$")
axarr.set_xlabel("$x/L$")
axarr.set_ylabel("$y/L$")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM