3

I am unsure where exactly to ask this question - I cant seem to find the proper platform where I feel I can properly ask this... it has been bugging me for so long and I cant seem to grasp the conceptual picture.

I am new to Python, and so am learning very slowly. My apologies if I say anything that's deemed silly or unfit. I'm learning

My question is to do with calculating the Radial Distribution Function.

I found this code on GitHub which calculates RDF of a 3D system:

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)

It seems to be a very simple code, but I am extremely confused on one part:

bools1 = x > rMax
bools2 = x < (S - rMax) 
bools3 = y > rMax
bools4 = y < (S - rMax)
bools5 = z > rMax
bools6 = z < (S - rMax)

I can understand bools1 = x > rMax which stands to mean the coordinates of the particles whose x coordinates are greater than the rMax value. In RDF we are looking for particles which are within the given sphere/radius.

The next one, however, is the one which is catching me out and for the life of me I cant seem to understand its significance properly: bools2 = x < (S - rMax).

Is bools2 referring to the interior particles within rMax? If so, why cant it just specify x < rMax? What is the significance of subtracting rMax from the edge length, S, of the cube? I just cant seem to understand the conceptual significance of subtracting rMax from S...

Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
user1697
  • 31
  • 1
  • 4
  • maybe? `finds reference particles such that a sphere of radius rMax drawn around the particle will fit entirely within the cube,` – f5r5e5d Feb 01 '17 at 00:33
  • https://github.com/cfinch/Shocksolution_Examples/blob/master/PairCorrelation/paircorrelation.py is the link for the code. – beliz Feb 16 '21 at 11:33

1 Answers1

2

I think this is because rmax is the radius of the shell (Not the diameter as stated in the variable explanation). Let's say we have a cubic box 30x30x30 (x,y,z) so S = 30, hence box center = 15x15x15, and input rmax of 10.

If you consider a particle at (19,15,15) then you are inside the boundary in the x direction, hence evaluate bool1 x > rmax (19 > 10) is true.

If you consider your suggestion for bool2 x < rmax at the same point (19 < 10) then bool2 would fail, despite the fact that the particle is valid. But, if you consider x < (S - rmax) (19 < (30 - 10) = True), the bool is true. The point of the condition is to say particle is inside the diameter of the enclosing sphere.

If you choose a particle in the opposite direction:

A particle at (14,15,15) then you are inside the boundary in the x direction, hence evaluate bool1 x > rmax (14 > 10) is true.

Once again, your suggestion for bool2 x < rmax at the same point (14 < 10) then bool2 would fail. Now consider x < (S - rmax) (14 < (30 - 10) = True), the bool is true.

If we took a particle at (26,15,15): bool1 = x > rmax: (26 > 10) true bool2 = x < (S - rmax): (26 < (30 - 10)) false The inclusion of this particle is prevented in the np.where the multiplication will evaluate to 0

If we took a particle at (9,15,15): bool1 = x > rmax: (9 > 10) false bool2 = x < (s - rmax): (9 < (30 - 10)) true The inclusion of this particle is prevented in the np.where the multiplication will evaluate to 0

I think this is the reasoning behind this line, hope it helps.

James
  • 223
  • 1
  • 3
  • 9