Created
July 10, 2018 21:57
-
-
Save Ephraim-Bryski/8955f07d82a6ca7644c747dde14ce938 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tkinter import * | |
tk=Tk() | |
WIDTH=4000 | |
HEIGHT=4000 | |
canvas=Canvas(tk,width=WIDTH,height=HEIGHT) | |
tk.title('conduction') | |
canvas.pack() | |
COLUMNS=20 | |
ROWS=20 | |
SIDE=25 | |
ITERATIONS=50 | |
dt=.01 | |
class Particles: | |
def __init__(self,x,y,press): | |
self.vel=[0,0] | |
self.shape=canvas.create_rectangle(SIDE*x,SIDE*y,SIDE*x+SIDE,SIDE*y+SIDE,fill=('#'+2*str(press)+'00'+2*str(9-press))) | |
self.line=canvas.create_line(SIDE*x,SIDE*y,SIDE*x+self.vel[0],SIDE*y+self.vel[1],width=5) | |
self.press=press | |
def press_transfer(self,below,right,Source): | |
try: | |
vert_grad=self.press-below.press | |
below.vel[1]+=vert_grad*dt | |
self.vel[1]-=vert_grad*dt | |
below.press+=below.vel[1]*self.press*dt | |
self.press-=below.vel[1]*self.press*dt | |
except: | |
self.press-=self.vel[1]*self.press*dt | |
try: | |
hor_grad=self.press-right.press | |
right.vel[0]+=hor_grad*dt | |
right.press+=right.vel[0]*self.press*dt | |
self.press-=right.vel[0]*self.press*dt | |
except: | |
self.press-=self.vel[0]*self.press*dt | |
def color_change(self): | |
canvas.itemconfigure(self.shape,fill='#'+2*str(int(self.press))+'00'+2*str(int(9-self.press))) | |
canvas.coords(self.line,SIDE*x+SIDE/2,SIDE*y+SIDE/2,SIDE*x+self.vel[0]*20+SIDE/2,SIDE*y+self.vel[1]*20+SIDE/2) | |
tk.update() | |
particles=[] | |
for x in range (COLUMNS): | |
particles.append([]) | |
for y in range(ROWS): | |
if x<10 and y<10: | |
press=9 | |
else: | |
press=0 | |
particles[x].append(Particles(x,y,press)) | |
for t in range(ITERATIONS): | |
for x in range (COLUMNS): | |
for y in range (ROWS): | |
try: | |
below=particles[x][y+1] | |
except: | |
below='none' | |
try: | |
right=particles[x+1][y] | |
except: | |
right='none' | |
particle=particles[x][y] | |
if x==0 and y==0 or x==COLUMNS-1 and y==ROWS-1: | |
source=True | |
particle.press_transfer(below,right,source) | |
particle.color_change() | |
tk.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment