-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbacteria.py
74 lines (63 loc) · 2.09 KB
/
bacteria.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from __future__ import print_function, division #compatibility py2 - py3
import random, math, numpy
import matplotlib.pyplot as plt
V = 2e-6
DT = 0.2
L = 100e-6
P1 = 0.9
P2 = 0.5
N = 10
def get_density(x,y): #version A
return 1./(1.+math.hypot(x-L/2.,y-L/2.))
##def get_density(x,y): #version B
## return float(math.hypot(x-L/2.,y-L/2.) < 15e-6)
def draw(b_list, n, t):
m = numpy.zeros((n,n))
for x in range(n):
for y in range(n):
m[x,y] = get_density(x*L/n,y*L/n)
for bacteria in b_list:
x,y = bacteria.x*n/L, bacteria.y*n/L
m[x,y] = 1.
plt.imshow(m) #add interpolation='None' for non-smoothed image
plt.savefig("bacteria"+str(t)+".png")
## plt.show() #directly show the image
class Bacteria(object):
def __init__(self, x, y):
self.x = x
self.y = y
self.vx = None
self.vy = None
self.randomize_velocity()
self.old_density = get_density(self.x, self.y)
def randomize_velocity(self):
alpha = random.random()*math.pi*2
self.vx = math.cos(alpha) * V
self.vy = math.sin(alpha) * V
assert (math.hypot(self.vx, self.vy) - V) < 0.0000001
def update(self):
current_density = get_density(self.x, self.y)
go_forward = False
if current_density > self.old_density:
###### Question 1 #########
if random.random() < P1:
go_forward = True
else:
###### Question 2 #########
if random.random() < P2:
go_forward = True
if not go_forward:
###### Question 3 #########
self.randomize_velocity()
self.x += self.vx * DT
self.y += self.vy * DT
#domain periodicity:
self.x %= L
self.y %= L
self.old_density = current_density
b_list = [Bacteria(random.random()*L, random.random()*L) for i in range(N)]
for t in range(200):
if t%40 == 0:
draw(b_list, 100, t)
for bacteria in b_list:
bacteria.update()