Created
April 9, 2015 02:09
-
-
Save Kojirion/c4c6dbccb50ac534f572 to your computer and use it in GitHub Desktop.
This file contains 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
xi_star = 0.4 | |
max_diameter = 4 | |
def u(xi): | |
return xi_star - xi | |
def f1(xi): | |
return (1 / (xi + 0.1)) ** 2 | |
def f2(xi): | |
return (1 / xi) ** 3 | |
def f0(x, xi): | |
if x < 0 and 0 <= xi < xi_star: | |
return f1(xi) | |
elif x >= 0 and xi_star <= xi < 1: | |
return f2(xi) | |
return 0 | |
def f(t, x, xi): | |
return f0(x - u(xi) * t, xi) | |
import turtle | |
class Pen: | |
def __init__(self): | |
self.pen = turtle.Pen() | |
self.pen.fillcolor("") | |
self.pen.color("") | |
self.pen.speed(0) | |
self.pen.pensize(3) | |
def draw(self, x, y, xi): | |
self.pen.setposition(x, y) | |
self.pen.dot(xi * max_diameter, "yellow") | |
turtle.bgcolor("black") | |
turtle.tracer(0, 0) | |
pen = Pen() | |
nx = 10 # number of x intervals below and above 0 | |
nxi = 10 # number of xi steps | |
nt = 20 # number of time steps | |
ppi = 10 # particles per interval | |
x_interval = (ppi + 1) * max_diameter | |
from numpy import linspace | |
import time | |
for t in range(nt): | |
pen.pen.clear() | |
for x in range(-nx, nx): | |
count = 0 | |
for xi in linspace(0, 1, nxi): | |
nr_particles = f(t, x, xi) | |
for i in range(int(nr_particles)): | |
pos_x = i % ppi | |
if i != 0 and pos_x == 0: | |
count += 1 | |
pen.draw(x * x_interval + pos_x * max_diameter, count * max_diameter, xi) | |
count += 1 | |
turtle.update() | |
time.sleep(0.1) | |
turtle.done() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why are you using this
turtle
thing at all? Isn't it better to plot publication ready graphs using matplotlib? Also, I would generally keep calculation and post-processing code separate and run them separately too. You could always save usingpickle
orcsv
modules.More comments in an e-mail that is soon to follow.