Tuesday, February 23, 2010

Connor's Ideal Gas Sim

from visual import *

ball = sphere(pos=(-5.5, 0, 0), radius = 0.5, color=color.cyan)
ball.velocity = vector(25,5,25)

ball2 = sphere(pos=(2, -5.5, 0), radius = 0.5, color=color.red)
ball2.velocity = vector(30,-10,5)

wallR = box(pos=(6,0,0), size=(0.2,12.2,12), color=color.green, opacity = 0.5)
wallL = box(pos=(-6,0,0), size=(0.2,12.2,12), color=color.green, opacity = 0.5)
wallT = box(pos=(0,6,0), size=(12,0.2,12), color=color.green, opacity = 0.5)
wallB = box(pos=(0,-6,0), size=(12,0.2,12), color=color.green, opacity = 0.5)
wallBack = box(pos=(0,0,-6), size=(12,12,0.2), color=color.green, opacity = 0.5)
wallFront = box(pos=(0,0,6), size=(12,12,0.2), color=color.green, opacity = 0.08)

deltat = 0.005
t = 0

vscale = 0.1
varr = arrow(pos=ball.pos, axis=vscale*ball.velocity, color=color.yellow)
varr2 = arrow(pos=ball2.pos, axis=vscale*ball2.velocity, color=color.yellow)

scene.autoscale = False

ball.trail = curve(color=ball.color)
ball2.trail = curve(color=ball2.color)

while t<3e4:
rate(100)

if ball.pos.x+ball.radius > wallR.pos.x or ball.pos.x-ball.radius < wallL.pos.x:
ball.velocity.x = -ball.velocity.x
if ball.pos.y+ball.radius > wallT.pos.y or ball.pos.y-ball.radius < wallB.pos.y:
ball.velocity.y = -ball.velocity.y
if ball.pos.z-ball.radius < wallBack.pos.z or ball.pos.z+ball.radius > wallFront.pos.z:
ball.velocity.z = -ball.velocity.z

if ball2.pos.x+ball2.radius > wallR.pos.x or ball2.pos.x-ball2.radius < wallL.pos.x:
ball2.velocity.x = -ball2.velocity.x
if ball2.pos.y+ball2.radius > wallT.pos.y or ball2.pos.y-ball2.radius < wallB.pos.y:
ball2.velocity.y = -ball2.velocity.y
if ball2.pos.z-ball2.radius < wallBack.pos.z or ball2.pos.z+ball2.radius > wallFront.pos.z:
ball2.velocity.z = -ball2.velocity.z

ball.pos = ball.pos + ball.velocity*deltat
ball.trail.append(pos=ball.pos)
ball2.pos = ball2.pos + ball2.velocity*deltat
ball2.trail.append(pos=ball2.pos)

varr.pos = ball.pos
varr.axis = vscale*ball.velocity
varr2.pos = ball2.pos
varr2.axis = vscale*ball2.velocity

t = t + deltat

No comments:

Post a Comment