python-捕获分子的晶格结构-无法正常工作

发布于 2024-12-14 06:13:25 字数 1676 浏览 2 评论 0原文

我有这个问题:

创建一个程序,构造一 (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...]

我希望 1 次运行各有 10 个数字(位置可变)。

2)我不知道如何处理边界条件。我在想:

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

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

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

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

I have this problem :

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

I have 3 problems :

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

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

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) 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不回头走下去 2024-12-21 06:13:25

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

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 语句,以查看每个变量持有什么值。在较小的晶格上进行尝试将有助于您了解其工作原理。

编辑:

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

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)

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

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.

EDIT:

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)
海拔太高太耀眼 2024-12-21 06:13:25

创建网格的部分没问题(尽管您使用了两次 traps - 我想您不需要第一行,第四行应该是 grid[traps]=0)代码>)。

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

为了回答你的第二个问题,如果你将 index % pos 除法的余数作为分子的索引,周期性边界条件可以无缝地应用于网格。

The part where the grid is created is OK (although you used traps twice - I suppose you don't need the 1st line and the 4th line should be grid[traps]=0).

Then according to the problem you have to put a molecule and make it walk on the grid, and this part of your program is completely wrong. What you need to do is find a random starting point for the molecule (with sc.random.randint(pos) maybe), and then count the number of steps the molecule makes before falling in a trap. Steps in 1d random walk can be either to the left (starting_point - 1) or to the right (starting_point + 1). You have to choose randomly between [-1, +1], add the step to the index of the molecule on the grid and if the resultant index appears to be free on the grid then increment your free variable. If the resultant index hits the trap, then append the free variable to the steps_count list.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文