python-捕获分子的晶格结构-无法正常工作 [英] python- construction of lattice which traps molecules - doesn't work right

查看:115
本文介绍了python-捕获分子的晶格结构-无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题:

创建一个程序,该程序构造一维(1)维的晶格, 100000个网站.在这个格子中随机放置一些陷阱 分子,其浓度为c.随机放入1个粒子 放置在晶格上并让其执行随机游走.在这个步行 您不会设置时间限制,即您不会声明 具体的步骤数.当粒子掉落时,行走将停止 在陷阱上..................注意边界 情况.当粒子到达晶格的边界时 不应逃避它,而要保留在格子中, 通过返回原来的位置或放置在 晶格的相对位置........

Create a program which constructs a lattice of one (1) dimension and 100000 sites. In this lattice put at random positions a number of trap molecules, which will have concentration c. Put 1 particle in a random position on the lattice and let it perform a random walk. In this walk you will not place a time restriction, namely you will not declare a specific number of steps. The walk will stop when the particle falls on a trap.............................. ...Beware of boundary conditions. When the particle reaches the borders of the lattice it shouldn’t be allowed to escape from it but to remain in the lattice, either by returning on it former position or by being placed in the opposite site of the lattice........

我创建的代码中显示了我的方法(其中有注释).

My approach is shown in the code i created (i have comments in it).

def steps1d(self,pos,c):
    #pos: number of positions
    #c:   concentration of trap-particles

    # array full of traps (zeros)
    myzeros = sc.zeros(self.c*self.pos)

    # grid full of available positions(ones)
    grid = sc.ones(self.pos)

    # distribute c*pos zeros(traps) in random positions (number of positions is pos)
    traps = sc.random.permutation(pos)[:c*pos]

    # the grid in which the particle is moving which has traps inside it 
    grid[traps] = myzeros
    steps_count = []    # list which holds the number of steps
    free = 0
    for i in range(pos):
        # the step of the particle can be 0 or 1
        step=sc.random.random_integers(0,1)
        for step in grid[:]:
            if step == 1:
                free += 1
                steps_count.append(free)
            else:
                break
    return steps_count

我有3个问题:

1)我以pos = 10为例的结果如下:

1) The results i am taking for example for pos=10 are sth like:

[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 ...]

[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...]

我希望每次运行10个数字(可变pos).

I would expect 10 numbers each for 1 run (variable pos).

2)我不确定如何处理边界条件.我在想类似的东西:

2) I am not sure how to handle the boundary conditions. I am thinking something like:

if free > grid.size:
    free = free - 1

但是我无法测试.另外,我不确定这是否适用于网格的两个边界.

But I can't test it. Also,i am not sure if this applies for both borders of the grid.

3)如果我希望第一步从网格的中间开始,该怎么办?

3) If i want the first step to begin from the middle of the grid, how can I do it?

如果有人对此有所提示,我将不胜感激.

If someone has a hint on that, I'll be grateful.

推荐答案

在一个较小的格子上,查看正在发生的事情:

On a smaller lattice, to see what is happening:

import numpy

# Populate the lattice
lattice = numpy.concatenate([numpy.ones(90), numpy.zeros(10)])
numpy.random.shuffle(lattice)

# Intialize problem
in_trap = False
steps = 0
pos = int(numpy.random.randint(0,len(lattice),1))
history = []

while in_trap == False:
    # Step of -1 is backward, 1 is forward
    step = numpy.random.permutation([-1,1])[0]

    # Check position for edges and fix if required
    if pos + step > len(lattice) - 1:
        pos = 0
    elif pos + step < 0:
        pos = len(lattice) - 1
    else:
        pos += step

    # Keep track of random walk
    history.append(pos)

    # Check if it's a trap
    if lattice[pos] == 0:
        in_trap = True

    # If not, continue
    steps += 1


print steps
print history
print lattice

我鼓励您在整个语句中使用print语句,以查看每个变量所持有的值.在较小的格子上进行尝试将有助于您了解其工作原理.

I would encourage you to thrown in print statements throughout to see what values each variable is holding. Trying it out on smaller lattices will help you understand how this works.

我将让您弄清楚具体细节,但是我会将其包装在如下函数中.它设置功能,然后准备空的步骤和历史记录列表以保存每次运行的结果.我们运行该函数,然后将结果附加到这些列表中.

I'm going to let you figure out the specifics, but I would wrap this in a function like follows. It sets up the function, then prepares empty steps and histories lists to hold the results of each run. We run the function, then append the results to those lists.

def lattice():
    code
    return steps, history

steps = []
histories = []
for i in range(0,10):
    num_steps, history = lattice()
    steps.append(num_steps)
    histories.append(history)

这篇关于python-捕获分子的晶格结构-无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆