python,scipy-作业——如何保留网格中粒子位置的历史记录 [英] python,scipy- homework -- how to keep history of the particle position in the grid

查看:57
本文介绍了python,scipy-作业——如何保留网格中粒子位置的历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:

创建一个程序,其中粒子将在以下两种情况下执行 N=1000 步的随机游走:i) 在 1D 系统中 ii) 在 2D 系统中.程序必须计算均值(S),其中 S 是粒子至少访问过一次的网格位置数.您将运行 10000 次并找到 10 个点(每 100 步一个,从 0 到 1000),这将是 10000 次运行的均值.绘制均值 (S) 与时间 t 的关系图.

Create a program where a particle will execute a random walk for N=1000 steps for these two cases: i) in a 1D system ii) in a 2D system. The program must calculate the mean(S) where S is the number of grid positions where the particle visited at least one time.You will make 10000 runs and find 10 points (one for every 100 steps ,from 0 to 1000) , which will be the means of 10000 runs.Do the plot of mean(S) in relation to time t.

我做了这个代码:

import scipy as sc
import matplotlib.pyplot as plt
import random

plegma=1000
grid=sc.ones(plegma)   # grid full of available positions(ones)    

for p in range(10000):
    #-------------------Initialize problem-------------------------------------------------
    his_pos=[]                  # list which holds the position of the particle in the grid
    in_pos = int(sc.random.randint(0,len(grid),1))            #initial position of particle
    means=[]                                                    #list which holds the means 
    #--------------------------------------------------------------------------------------
    for i in range(0,1000,100):
        step=2*sc.random.random_integers(0,1)-1        #the step of the particle can be -1 or 1
        # Check position for edges and fix if required
        # Move by step
        in_pos += step
        #Correct according to periodic boundaries
        in_pos = in_pos % len(grid)  

        #Keep track of random walk
        his_pos.append(in_pos)
        history=sc.array(his_pos)
        mean_his=sc.mean(history) 
        means.append(mean_his)


plt.plot(means,'bo')
plt.show()

更新 -------------------------------------

UPDATED -------------------------------------

import scipy as sc
import matplotlib.pyplot as plt
import random

plegma=1000
his_pos=[] # list which holds the number of visited cells in the grid
means=[] #list which holds the means

for p in range(10000):
    #-------------------Initialize problem-------------------------------------------------
    grid=sc.ones(plegma)   # grid full of available positions(ones)      
    in_pos = int(sc.random.randint(0,len(grid),1))            #initial position of particle
    num_cells=[]       # list which holds number of visited cells during run                         
    #--------------------------------------------------------------------------------------
    for i in range(1000):
        step=2*sc.random.random_integers(0,1)-1 #the step of the particle can be -1 or 1

        # Check position for edges and fix if required
        # Move by step
        in_pos += step
        #Correct according to periodic boundaries
        in_pos = in_pos % len(grid)  

        grid[in_pos]=0  # mark particle position on grid as "visited"

        if (i+1) % 100 == 0:

            number=1000-sc.sum(grid)  # count the number of "visited positions" in grid
            num_cells.append(number)  # append it to num_cells

      his_pos.append(num_cells) # append num_cells to his_pos
      history=sc.array(his_pos)


mean_his=history.mean(1)
means.append(mean_his)

更新 2 -----------------------------.....

UPDATE 2 ----------------------------- .....

    if (i+1) % 10 == 0:

            number=1000-sc.sum(grid)  # count the number of "visited positions" in grid
            num_cells.append(number)  # append it to num_cells

    his_pos.append(num_cells) # append num_cells to his_pos
    history=sc.array(his_pos)
mean_his=history.mean(0)

plt.plot(mean_his,'bo')
plt.show()

谢谢!

推荐答案

1) 是的,10000 步是指所需的准确度 - 您必须在 t = 100, 200 ... 1000 表示 10000 次随机游走.要获取数据,您必须为每次随机游走(10000 次)累积访问过的单元格数量.为此,您必须初始化您的问题(即初始化 his_posmeans)poutside环形.在 p 循环内,您应该初始化您的随机游走 - 获取您的网格、初始位置和您将写入结果的列表.所以,问题 init 看起来像

1) Yes, 10000 steps refer to desired accuracy - you have to get the mean number of visited cells at time t = 100, 200 ... 1000 for 10000 random walks. To get the data, you have to accumulate the number of visited cells for each random walk you do (of 10000). to do this, you have to initialize your problem (that is, initialize his_pos and means) outside of p loop. Inside of p loop you should initialize your random walk - get your grid, initial positions and the list you'll write results to. So, problem init will look something like

import scipy as sc
import matplotlib.pyplot as plt

plegma=1000
his_pos=[]  # list which holds the position of the particle in the grid
means=[]    #list which holds the means 

for p in range(10000):
    #-------Initialize problem--------
    in_pos = int(sc.random.randint(0,len(grid),1)) #initial position of particle
    grid=sc.ones(plegma)   # grid full of available positions(ones)    
    his_pos.append([])

初始化后,我们需要执行随机游走 - i 循环.目前您只进行了 10 步随机游走 (len(range(0,1000,100)) == 10),但该游走应该包含 1000 步.因此,i 循环应该是

After init we need to perform random walk - the i loop. Presently you make just 10 steps of random walk (len(range(0,1000,100)) == 10), but the walk should contain 1000 steps. Hence, the i loop should be

    for i in range(1000):

在步行过程中,您必须以某种方式标记访问过的单元格.最简单的方法是将 grid[in_pos] 更改为 0.然后,每 100 步您需要计算访问过的单元格的数量.实现这一目标的方法就像

During the walk you have to mark visited cells somehow. The simplest way is to change grid[in_pos] to 0. Then, every 100th step you need to count the number of visited cells. The way to achieve this is like

        if i % 100 == 0:
            # count the number of 0s in grid and append it to his_pos[p]

最后,在您的 1000 次随机游走结束时,您的 his_pos 将是 (10000*10) 列表列表,其中应采用列方式.

Finally, at the end of your 1000 random walks your his_pos will be the (10000*10) list of lists, out of which column-wise means should be taken.

更新:

为了在整个运行过程中不丢失信息,我们应该将存储 p 次运行结果的列表附加到包含所有结果的列表中.后者是 his_pos.为了实现这一点,我们可以将空列表附加到 his_pos 并在 p 次运行期间用结果填充它,或者在 before 初始化空列表 p-th run 并将其附加到 his_pos after p-th run.该算法将如下所示:

In order for not to lose information throughout runs, we should append the list where results for p-th run are stored, to the list with all results. The latter is his_pos. To achieve this, we can either append empty list to his_pos and populate it with results during p-th run, or initialize empty list before p-th run and append it to his_pos after p-th run. The algorithm then will look as follows:

#-------Initialize problem--------
plegma=1000
his_pos=[]  # list which holds the number of visited cells in the grid
means=[]    #list which holds the means 

for p in range(10000):
    #-------Initialize run--------
    in_pos = int(sc.random.randint(0,len(grid),1)) #initial position of particle
    grid=sc.ones(plegma)   # grid full of available positions(ones)    
    num_cells = []  # list which holds number of visited cells during run  
    for i in range(1000):
        # make i-th step, get particle position
        # mark particle position on grid as "visited"
        if (i+1) % 100 == 0: # on each 100th step (steps count from 0, thus i+1)
            # count the number of "visited positions" in grid
            # append it to num_cells
    # append num_cells to his_pos
# find column-wise means for his_pos

这篇关于python,scipy-作业——如何保留网格中粒子位置的历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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