我正在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()
好的,所以我基本上修复了你的代码,因为,没有冒犯,但它之前有点混乱(见注释)。你知道吗
这个输出
FHTMitchell提出的解决方案是正确的,但效率低下。
sum
操作要求在每次迭代中执行O(L)个工作,使整个程序成为O(L*n\u时间)。你知道吗请注意:
occupied_state_count
为0occupied_state_count
应该只在零状态转换为一状态时递增occupied_state_count
才应该递减random()
的不必要调用elif
),没有必要改变occupied_state_count
。你知道吗应用以上所有方法可以得到以下O(nаu time)实现,这要快得多:
相关问题 更多 >
编程相关推荐