【发布时间】:2018-05-30 07:50:25
【问题描述】:
我正在 Python 中运行 TASEP 模拟,其中给定大小的晶格上的晶格点可以是空的,也可以是被占用的(0 或 1)。
模拟给出了给定模拟时间的晶格配置图(一个状态是否被占用),但没有被占用的状态的数量(数量)。
我无法让 Python 计算占用状态的数量,因为图表来自模拟而不是列表。
TASEP 代码:
import random, pylab, math
random.seed()
L=100 # Number of lattice sites
alpha=.2 # Rate of entry
beta=.4 # Rate of exit
Ntime=200000 # Simulation steps
state=[0 for k in range(L+1)]
for iter in range(Ntime):
k=random.randint(0,L)
if k==0:
if random.random() < alpha: state[1]=1
elif k==L:
if random.random() < beta: state[L]=0
elif state[k]==1 and state[k+1]==0:
state[k+1]=1
state[k]=0
if iter%2000 == 0:
yaxis=[]
for i in range(L):
if state[i+1]==1: yaxis.append(i)
xaxis=[iter for k in range(len(yaxis))]
pylab.plot(xaxis,yaxis,'r.')
pylab.xlabel('Number of steps')
pylab.ylabel('System configuration')
pylab.show()
【问题讨论】:
-
状态存储在一个名为
state的列表中。你试过sum(state)吗?这将为您提供给定时间步长的占用状态数... -
不要使用
iter作为变量,它会影响内置的iter函数。pylab已被弃用。请改用import matplotlib.pyplot as plt。random.seed()实际上并没有做任何事情,除非您传递一个变量,请查看 python style guide(例如,在运算符两侧放置空格)。 -
我会试试你的建议。
标签: python plot count simulation