1

我有这个问题:

创建一个程序,该程序构建一 (1) 维和 100000 个站点的晶格。在这个晶格中,在随机位置放置了许多陷阱分子,它们的浓度为 c。将 1 个粒子放在格子上的随机位置,让它执行随机游走。在此步行中,您不会设置时间限制,即您不会声明特定的步数。当粒子落在陷阱上时,行走将停止............................注意边界条件。当粒子到达格子的边界时,它不应该被允许从格子中逃脱,而是留在格子中,要么回到原来的位置,要么被放置在格子的相反位置…… ..

我的方法显示在我创建的代码中(我在其中有评论)。

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, 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)。

2)我不确定如何处理边界条件。我在想类似的事情:

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

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

3)如果我想让第一步从网格中间开始,我该怎么做?

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

4

2 回答 2

2

在一个较小的格子上,看看发生了什么:

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

我鼓励您在整个过程中加入打印语句,以查看每个变量所持有的值。在较小的格子上进行尝试将帮助您了解其工作原理。

编辑:

我会让你弄清楚细节,但我会把它包装在一个函数中,如下所示。它设置函数,然后准备空步骤和历史列表来保存每次运行的结果。我们运行该函数,然后将结果附加到这些列表中。

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)
于 2011-11-07T17:57:54.313 回答
0

创建网格的部分是可以的(虽然你使用traps了两次 - 我想你不需要第一行,第四行应该是grid[traps]=0)。

然后根据问题你必须放一个分子并让它在网格上行走,而你的这部分程序是完全错误的。您需要做的是找到分子的随机起点(sc.random.randint(pos)可能),然后计算分子在落入陷阱之前所走的步数。一维随机游走的步骤可以向左 ( starting_point - 1) 或向右 ( starting_point + 1)。您必须在 之间随机选择[-1, +1],将步骤添加到网格上分子的索引,如果生成的索引在网格上似乎是空闲的,则增加您的free变量。如果结果索引命中陷阱,则将free变量附加到steps_count列表中。

To answer your second question, periodic boundary conditions can be applied seamlessly to the grid if you take the remainder of index % pos division as the index of the molecule.

于 2011-11-07T17:58:11.290 回答