Spel23. Molekylernas Dans

600px--Isothermal-Isobaric_Molecular_Dynamics_Simulation_of_Water.webm

Vi programmerar molekylers rörelse där varje molekyl består av en positiv kärna omgiven av ett visst antal elektroner, som samverkar genom fjädrar som är ömsom attraktiva ömsom repulsiva.

Template 1: (Titta på molekyldans)

— Molekyldynamik
I=10
M=4
F=10
E=1
S=30
x={}
xold={}
xm={}
v={}
a={}
f={}
mass={}
Origo = vec2(WIDTH/2,HEIGHT/2)

for i=1,I do
x[i]={}
xold[i]={}
xm[i]={}
v[i]={}
f[i]={}
a[i]={}
mass[i]={}
end

for i=1,I do
for m=1,M do
x[i][m]=vec2(0,0)
xold[i][m]=vec2(0,0)
xm[i][m]=vec2(0,0)
v[i][m]=vec2(0,0)
f[i][m]=vec2(0,0)
a[i][m]=vec2(0,0)
mass[i][m]=1
end
end

for i=1,I do
x[i][1]=vec2(math.random(-200,200),math.random(-200,200))+Origo
xold[i][1]=x[i][1]
xm[i][1]=x[i][1]
mass[i][1]=M
for m=2,M do
x[i][m]=S*vec2(math.cos(m*2*math.pi/M),math.sin(m*2*math.pi/M))+x[i][1]
mass[i][m]=1
xold[i][m]=x[i][m]
xm[i][m]=x[i][m]
end
end

length=0
dt=0.001

function setup()
parameter.number(“S”,1,100,10)
parameter.number(“F”,0,1000,500)
parameter.number(“E”,1,100,20)
end

function draw()
background(40, 40, 50)

for p=1,4 do
for i=1,I do
for m=1,M do
f[i][m]=vec2(0,0)
for n=1,M do
length=(x[i][m]-x[i][n]):len()
length=length
if n==1 or m==1 then
f[i][m]=f[i][m]-F*(length-2*S)*(xm[i][m]-xm[i][n])/(length+1)+0.1*(Origo-xm[i][1])
else
f[i][m]=f[i][m]-F*(length-4*S)*(xm[i][m]-xm[i][n])/(length+1)
end
a[i][m]=f[i][m]/mass[i][m]
v[i][m]=v[i][m]+a[i][m]*dt
x[i][m]=xold[i][m]+v[i][m]*dt
xm[i][m]=(x[i][m]+xold[i][m])/2
end
xold[i][m]=x[i][m]
fill(0, 54, 255, 255)
for m=2,M do
ellipse(x[i][m].x,x[i][m].y,10)
end
fill(255, 0, 40, 255)
ellipse(x[i][1].x,x[i][1].y,20)
end
end
end

for i=1,I do
for j=1,I do
f[i][1]=vec2(0,0)
for j=1,I do
length=(x[i][1]-x[j][1]):len()
length=length
f[i][1]=f[i][1]-E*(length-200)*(x[i][1]-x[j][1])/(length+1)
end
a[i][1]=f[i][1]
v[i][1]=v[i][1]+a[i][1]*dt
x[i][1]=x[i][1]+v[i][1]*dt
end
end
end

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s