Skip to content

Instantly share code, notes, and snippets.

@Ephraim-Bryski
Last active June 6, 2018 23:44
Show Gist options
  • Save Ephraim-Bryski/34cc347767e00947f971d2b142ac1e81 to your computer and use it in GitHub Desktop.
Save Ephraim-Bryski/34cc347767e00947f971d2b142ac1e81 to your computer and use it in GitHub Desktop.
from tkinter import *
import random
import math
import pickle
SIDE=500
DIAM=5
dt=.001
R=8.314
n=5
T=100
m=1
class Particles:
def __init__(self,x,y,part_num):
self.shape=[x,y,x+DIAM,y+DIAM]
self.vel=[(math.sqrt(2*(2*T*part_num/n)/m))/math.sqrt(2),(math.sqrt(2*(2*T*part_num/n)/m))/math.sqrt(2)]
def move(self):
self.shape=[self.shape[0]+self.vel[0]*dt,self.shape[1]+self.vel[1]*dt,self.shape[0]+self.vel[0]*dt+DIAM,self.shape[1]+self.vel[1]*dt+DIAM]
def vert_wall(self):
self.vel[0]=-self.vel[0]
def hor_wall(self):
self.vel[1]=-self.vel[1]
def collide(self,other_particle,angle,dist):
a=self.vel
b=other_particle.vel
a=[a[0]*math.cos(-angle)-a[1]*math.sin(-angle),a[0]*math.sin(-angle)+a[1]*math.cos(-angle)]
b=[b[0]*math.cos(-angle)-b[1]*math.sin(-angle),b[0]*math.sin(-angle)+b[1]*math.cos(-angle)]
(a[0],b[0])=(b[0],a[0])
a=[a[0]*math.cos(angle)-a[1]*math.sin(angle),a[0]*math.sin(angle)+a[1]*math.cos(angle)]
b=[b[0]*math.cos(angle)-b[1]*math.sin(angle),b[0]*math.sin(angle)+b[1]*math.cos(angle)]
self.vel=a
other_particle.vel=b
parts=[]
i=0
space=int(SIDE/(2*DIAM))
for x in range(1,space):
for y in range(1,space):
if i<n:
parts.append(Particles(x*2*DIAM,y*2*DIAM,i))
i+=1
for t in range(100):
for part in parts:
part.move()
if part.shape[0]<0 or part.shape[2]>SIDE:
part.vert_wall()
if part.shape[1]<0 or part.shape[3]>SIDE:
part.hor_wall()
for a in range(len(parts)):
for b in range(a+1,len(parts)):
ax=parts[a].shape[0]
ay=parts[a].shape[1]
bx=parts[b].shape[0]
by=parts[b].shape[1]
dist=math.hypot(bx-ax,by-ay)
if dist<DIAM:
try:
angle=math.atan((by-ay)/(bx-ax))
except:
angle=math.pi/2
parts[a].collide(parts[b],angle,dist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment