Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中数组的子数组中追加值_Python_Arrays_List_Numpy_Modeling - Fatal编程技术网

在python中数组的子数组中追加值

在python中数组的子数组中追加值,python,arrays,list,numpy,modeling,Python,Arrays,List,Numpy,Modeling,这里的目标是构造在布朗动力学下演化的系统的单粒子分布函数;必须从高斯分布中产生一个随机数。为了构造这个数量,我考虑运行几个模拟,在每个模拟中的特定时间,保存每个粒子到2D正方形中心的距离,最后创建所有值的直方图 我的问题是,在每个模拟过程中,时间从零开始,并以某个特定的时间步长继续,对于每个时间步长,粒子随机移动。因此,要保存的距离必须正确标记相应的时间 所以,我的想法是创建一个数组,每行有5个子数组,每次我想保存粒子到正方形中心的距离时,一个子数组。我正试图与numpy合作,但没有成功。对于每

这里的目标是构造在布朗动力学下演化的系统的单粒子分布函数;必须从高斯分布中产生一个随机数。为了构造这个数量,我考虑运行几个模拟,在每个模拟中的特定时间,保存每个粒子到2D正方形中心的距离,最后创建所有值的直方图

我的问题是,在每个模拟过程中,时间从零开始,并以某个特定的时间步长继续,对于每个时间步长,粒子随机移动。因此,要保存的距离必须正确标记相应的时间

所以,我的想法是创建一个数组,每行有5个子数组,每次我想保存粒子到正方形中心的距离时,一个子数组。我正试图与numpy合作,但没有成功。对于每个模拟和特定时间,我创建一个具有所有距离的数组,并尝试使用numpy.append将其附加到特定子数组,但这无法正常工作;据我所知,问题在于我不知道如何正确索引子阵列(以及所有模拟)

除此之外,我认为这种方法并不是最好的:要么我必须放弃使用numpy的想法,想办法用两个索引正确地索引数组,要么想办法更有效地使用numpy

因此,这里的一般问题是如何将值添加/附加到数组的特定子数组(使用numpy预构造或不使用numpy预构造并作为列表处理)

另一种选择是有人提出一种更有效的方法来创建布朗运动问题的单粒子分布函数,这将非常有用

我在下面添加相关代码。提前谢谢大家

代码:

随机导入
输入数学
将matplotlib.pyplot作为plt导入
将numpy作为np导入
#def卸载(颗粒,步骤,n):
#fileoutput=open('coord.txt','a')
#fileoutput.write(“项:TIMESTEP\n”)
#fileoutput.write(“%i\n”%step)
#fileoutput.write(“项:原子数\n”)
#fileoutput.write(“%i\n”%n)
#fileoutput.write(“项:框边界\n”)
#fileoutput.write(“%e%e xlo xhi\n”%(0.01100))
#fileoutput.write(“%e%e xlo xhi\n”%(0.01100))
#fileoutput.write(“%e%e xlo xhi\n”%(-0.25,0.25))
#fileoutput.write(“项:原子id类型x y z\n”)
#i=0
#而i
我已尝试了解详细信息,但可以提供一些注意事项。在互动会话中尝试一个小案例。不要试图模仿
numpy
中的list
[]
append
方法。在尝试使用之前,请先查看
np.empty
数组。远离
np.append
。使用
np。如果必须,请直接连接
。它将迫使您了解阵列形状。请记住,它并没有在适当的位置运行。不能单独增长二维阵列的行。事实上,你不能增加一个数组;您只能新建一个。@hpaulj感谢您的评论;我同意你所说的,它们很有道理。但是接下来我在如何建模方面遇到了一个严重的问题,特别是因为问题本身并没有从导师那里以最清晰的方式写出来。无论如何,再次感谢你的建议。
    import random
    import math
    import matplotlib.pyplot as plt
    import numpy as np



    # def dump(particles,step,n):
    #   fileoutput = open('coord.txt', 'a')
    #   fileoutput.write("ITEM: TIMESTEP \n")
    #   fileoutput.write("%i \n" % step)
    #   fileoutput.write("ITEM: NUMBER OF ATOMS \n")
    #   fileoutput.write("%i \n" % n)
    #   fileoutput.write("ITEM: BOX BOUNDS \n")
    #   fileoutput.write("%e %e xlo xhi \n" % (0.0, 100))
    #   fileoutput.write("%e %e xlo xhi \n" % (0.0, 100))
    #   fileoutput.write("%e %e xlo xhi \n" % (-0.25, 0.25))
    #   fileoutput.write("ITEM: ATOMS id type x y z \n")
    #   i = 0
    #   while i < n:
    #       x = particles[i][0] 
    #       y = particles[i][1]
    #       #fileoutput.write("%i %i %f %f %f \n" % (i, 1, x*1e10, y*1e10, z*1e10))
    #       fileoutput.write("%i %i %f %f %f \n" % (i, 1, x, y, 0))
    #       i += 1

    #   fileoutput.close()  



    num_sims = 2
    N = 49
    L = 10
    meanz = 0
    varz = 1
    sigma = 1
    # tau = sigma**2*ksi/(kT)

    # Starting time
    t_0 = 0

    # Time increments
    dt = 10**(-4)  # dt/tau

    # Ending time
    T = 10**2  # T/tau




    # Produce random particles and avoid overlap:

    particles = np.full((N, 2), L/2)
    times = np.arange(t_0, T, dt) 



    check = 0
    distances = np.empty([50*num_sims, 5])

    for sim in range(0, num_sims):
        step = 0    
        t_index = 0
        for t in times:
            r=[]
            for i in range(0,N):
                z = np.random.normal(meanz, varz) 
                particles[i][0] = particles[i][0] + ((2*dt*sigma**2)**(1/2))*z
                z = random.gauss(meanz, varz) 
                particles[i][1] = particles[i][1] + ((2*dt*sigma**2)**(1/2))*z

            if (t%(2*(10**5)*dt) == 0):
                for j in range (0,N):
                    rj = ((particles[j][0]-L/2)**2 + (particles[j][1]-L/2)**2)**(1/2)
                    r.append(rj)
                distances[t_index] = np.append(distances[t_index],r)
                t_index += 1