forked from hsayama/PyCX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabm-keller-segel.py
62 lines (49 loc) · 1.7 KB
/
abm-keller-segel.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
import pycxsimulator
from pylab import *
n = 1000 # number of agents
w = 100 # number of rows/columns in spatial array
k = 1 # rate of cAMP decay
Dc = 0.001 # diffusion constant of cAMP
Dh = 0.01 # spatial resolution for cAMP simulation
Dt = 0.01 # time resolution for cAMP simulation
f = 1 # rate of cAMP secretion by an agent
class agent:
pass
def initialize():
global agents, env, nextenv
agents = []
for i in range(n):
ag = agent()
ag.x = randint(w)
ag.y = randint(w)
agents.append(ag)
env = zeros([w, w])
nextenv = zeros([w, w])
def observe():
global agents, env, nextenv
cla()
imshow(env, cmap = cm.binary, vmin = 0, vmax = 1)
axis('image')
x = [ag.x for ag in agents]
y = [ag.y for ag in agents]
plot(y, x, 'b.') # x and y are swapped to match the orientation of env
def update():
global agents, env, nextenv
# simulating diffusion and evaporation of cAMP
for x in range(w):
for y in range(w):
C, R, L, U, D = env[x,y], env[(x+1)%w,y], env[(x-1)%w,y], \
env[x,(y+1)%w], env[x,(y-1)%w]
lap = (R + L + U + D - 4 * C) / (Dh**2)
nextenv[x,y] = env[x,y] + (- k * C + Dc * lap) * Dt
env, nextenv = nextenv, env
# simulating secretion of cAMP by agents
for ag in agents:
env[ag.x, ag.y] += f * Dt
# simulating chemotaxis of agents
for ag in agents:
newx, newy = (ag.x + randint(-1, 2)) % w, (ag.y + randint(-1, 2)) % w
diff = (env[newx, newy] - env[ag.x, ag.y]) / 0.1
if random() < exp(diff) / (1 + exp(diff)):
ag.x, ag.y = newx, newy
pycxsimulator.GUI().start(func=[initialize, observe, update])