径向分布函数的Python代码实现

径向分布函数的Python代码实现,python,particle-system,Python,Particle System,我不确定在哪里确切地问这个问题-我似乎找不到合适的平台,我觉得我可以适当地问这个问题。。。它困扰了我这么长时间,我似乎无法理解概念上的图景 我是Python新手,所以学习速度很慢。如果我说了任何被认为愚蠢或不合适的话,我表示歉意。我在学习 我的问题是计算径向分布函数 我在GitHub上找到了这段代码,它计算3D系统的RDF: def pairCorrelationFunction_3D(x, y, z, S, rMax, dr): """Compute the three-dimensi

我不确定在哪里确切地问这个问题-我似乎找不到合适的平台,我觉得我可以适当地问这个问题。。。它困扰了我这么长时间,我似乎无法理解概念上的图景

我是Python新手,所以学习速度很慢。如果我说了任何被认为愚蠢或不合适的话,我表示歉意。我在学习

我的问题是计算径向分布函数

我在GitHub上找到了这段代码,它计算3D系统的RDF:

def pairCorrelationFunction_3D(x, y, z, S, rMax, dr):
    """Compute the three-dimensional pair correlation function for a set of
    spherical particles contained in a cube with side length S.  This simple
    function finds reference particles such that a sphere of radius rMax drawn
    around the particle will fit entirely within the cube, eliminating the need
    to compensate for edge effects.  If no such particles exist, an error is
    returned.  Try a smaller rMax...or write some code to handle edge effects! ;)
    Arguments:
        x               an array of x positions of centers of particles
        y               an array of y positions of centers of particles
        z               an array of z positions of centers of particles
        S               length of each side of the cube in space
        rMax            outer diameter of largest spherical shell
        dr              increment for increasing radius of spherical shell
    Returns a tuple: (g, radii, interior_indices)
        g(r)            a numpy array containing the correlation function g(r)
        radii           a numpy array containing the radii of the
                        spherical shells used to compute g(r)
        reference_indices   indices of reference particles
    """
    from numpy import zeros, sqrt, where, pi, mean, arange, histogram

    # Find particles which are close enough to the cube center that a sphere of radius
    # rMax will not cross any face of the cube
    bools1 = x > rMax
    bools2 = x < (S - rMax) 
    bools3 = y > rMax
    bools4 = y < (S - rMax)
    bools5 = z > rMax
    bools6 = z < (S - rMax)

    interior_indices, = where(bools1 * bools2 * bools3 * bools4 * bools5 * bools6)
    num_interior_particles = len(interior_indices)

    if num_interior_particles < 1:
        raise  RuntimeError ("No particles found for which a sphere of radius rMax\
                will lie entirely within a cube of side length S.  Decrease rMax\
                or increase the size of the cube.")

    edges = arange(0., rMax + 1.1 * dr, dr)
    num_increments = len(edges) - 1
    g = zeros([num_interior_particles, num_increments])
    radii = zeros(num_increments)
    numberDensity = len(x) / S**3

    # Compute pairwise correlation for each interior particle
    for p in range(num_interior_particles):
        index = interior_indices[p]
        d = sqrt((x[index] - x)**2 + (y[index] - y)**2 + (z[index] - z)**2)
        d[index] = 2 * rMax

        (result, bins) = histogram(d, bins=edges, normed=False)
        g[p,:] = result / numberDensity

    # Average g(r) for all interior particles and compute radii
    g_average = zeros(num_increments)
    for i in range(num_increments):
        radii[i] = (edges[i] + edges[i+1]) / 2.
        rOuter = edges[i + 1]
        rInner = edges[i]
        g_average[i] = mean(g[:, i]) / (4.0 / 3.0 * pi * (rOuter**3 - rInner**3))

    return (g_average, radii, interior_indices)
    # Number of particles in shell/total number of particles/volume of shell/number density
    # shell volume = 4/3*pi(r_outer**3-r_inner**3)
def pairCorrelationFunction_3D(x,y,z,S,rMax,dr):
“”“计算一组数据的三维对相关函数。”
包含在边长为S的立方体中的球形粒子。这很简单
函数查找参考粒子,以便绘制半径为rMax的球体
粒子周围将完全适合立方体,无需
用于补偿边缘效应。如果不存在此类粒子,则会出现错误
返回。请尝试较小的rMax…或编写一些代码来处理边缘效果!;)
论据:
x粒子中心的x个位置的数组
y粒子中心的y位置数组
z粒子中心的z位置数组
S立方体在空间中每边的长度
rMax最大球壳的外径
增大球壳半径的dr增量
返回元组:(g,半径,内部索引)
g(r)包含相关函数g(r)的numpy数组
半径一个包含数组半径的numpy数组
用于计算g(r)的球壳
参考粒子的参考指数
"""
从numpy导入零,sqrt,其中,pi,平均值,arange,直方图
#找到距离立方体中心足够近的粒子,使其成为半径为的球体
#rMax不会穿过立方体的任何面
bools1=x>rMax
bools2=x<(S-rMax)
bools3=y>rMax
bools4=y<(S-rMax)
bools5=z>rMax
bools6=z<(S-rMax)
内部指数,=其中(bools1*bools2*bools3*bools4*bools5*bools6)
num_interior_particles=len(interior_索引)
如果内部粒子数小于1:
raise RUNTIMERROR(“未找到半径为rMax的球体的粒子\
将完全位于边长S的立方体内。减小rMax\
或者增加立方体的大小。”)
边缘=arange(0,rMax+1.1*dr,dr)
num_增量=len(边)-1
g=零([num\u内部粒子,num\u增量])
半径=零(数值增量)
numberDensity=len(x)/S**3
#计算每个内部粒子的成对相关性
对于范围内的p(内部粒子数):
指数=内部指数[p]
d=sqrt((x[index]-x)**2+(y[index]-y)**2+(z[index]-z)**2)
d[指数]=2*rMax
(结果,箱)=直方图(d,箱=边,赋范=假)
g[p,:]=结果/数字密度
#所有内部粒子的平均g(r)并计算半径
g_平均值=零(num_增量)
对于范围内的i(num_增量):
半径[i]=(边[i]+边[i+1])/2。
路由器=边[i+1]
rInner=边[i]
g_平均值[i]=平均值(g[:,i])/(4.0/3.0*pi*(路由器**3-rInner**3))
回报率(g_平均值、半径、内部指数)
#壳中的粒子数/粒子总数/壳体积/数密度
#外壳体积=4/3*pi(外壳外部**3-内部**3)
这似乎是一个非常简单的代码,但有一点我非常困惑:

bools1 = x > rMax
bools2 = x < (S - rMax) 
bools3 = y > rMax
bools4 = y < (S - rMax)
bools5 = z > rMax
bools6 = z < (S - rMax)
bools1=x>rMax
bools2=x<(S-rMax)
bools3=y>rMax
bools4=y<(S-rMax)
bools5=z>rMax
bools6=z<(S-rMax)
我能理解
bools1=x>rMax
,它表示x坐标大于
rMax
值的粒子的坐标。在RDF中,我们寻找给定球体/半径内的粒子

然而,下一个问题正吸引着我,就我的一生而言,我似乎无法正确理解它的意义:
bools2=x<(S-rMax)


bools2是指rMax中的内部粒子吗?如果是这样,为什么它不能只指定
x
?从立方体的边长度
S
中减去
rMax
的意义是什么?我似乎无法理解从
S
中减去
rMax
的概念意义……

我认为这是因为rMax是外壳的半径(而不是变量解释中所述的直径)。假设我们有一个立方体盒子30x30x30(x,y,z),因此s=30,因此盒子中心=15x15x15,输入rmax为10

如果你在(19,15,15)中考虑一个粒子,那么你就在X方向的边界内,因此评估布尔1<代码> x> rmax < /C>(19>10)是正确的。

如果你在BooL2<代码> X相同的点(19<10)考虑,那么BoOL2将失败,尽管粒子是有效的。但是,如果考虑<代码> x(s -rmax)< /> >(19<30(10)=true),则布尔是真的。条件的要点是说粒子在封闭球体的直径内

如果选择相反方向的粒子:

在(14,15,15)处的粒子,则在x方向的边界内,因此求值bool1
x>rmax
(14>10)为真

再一次,您对bool2
x
的建议在同一点(14<10)时失败。现在考虑<代码> x(S- Rmax)< /C>(14<30(10)=true),布尔是真的。< /P> 如果我们在(26,15,15)处取一个粒子: bool1=x>rmax:(26>10)真 bool2=x<(S-rmax):(26<(30-10))假 防止包含该颗粒