Samstag, 24. April 2010

Python 044 Charges





#coulomb force 2-dimensional
#visualised with vpython-spheres
#strongest field = color red
#lowest field = color blue

from visual import sphere, scene
from numpy import log
scene.center = (25,25,0)

charge = [ #posx, posy, charge
[20.01, 20.01, 1],
#[15, 0.01, -3],
[20.01, 25.21, -1]
]

#compute the field strength
const = 8.988*10**9 #Vm/As
def field(posxtest,posytest,posx,posy,charge):
dx = abs(posxtest-posx)
dy = abs(posytest-posy)
r = (dx**2+dy**2)**(0.5)
E = const * charge / r
return E

posxtest = range(1,70)
posytest = range(1,40)

for x in posxtest:
for y in posytest:
e = 0
for elem in charge: #ok
e += field(x,y,elem[0],elem[1],elem[2])
colorfactor = 2000000000
#print e/colorfactor
ball = sphere(pos=[x,y],radius=0.7)
if e >= 0:
ball.color = [(e/colorfactor),0,0]
if e < 0:
ball.color = [0,0,(abs(e/colorfactor))]

Keine Kommentare:

 
eXTReMe Tracker