Python Assignment : Diffusion limited aggregation - Fractal

python

#1

Hi, could anyone help me with this code please ? I am kind of stuck and don’t know how to fill the spaces (“Add the code here”) in order to do what it is asking for. Here’s the code:

#!/usr/bin/env python

"""Simulation of a diffusion-limited aggregation process."""

import random

# Simulation parameters
Lo2 = 10  # half box size

# Initialization
# These are global variables (sic!)
Lx = 2*Lo2+1  # box dimensions : Lx, Ly
Ly = 2*Lo2+1
L0x = Lo2  # central position: Lx/2
L0y = Lo2
x = L0x  # position of currently diffusing particle (first one is at center).
y = L0y

# Set up lattice as a list of lists 
# s[x][y] is the lattice site of coordinates (x, y)
# The value of s[x][y] can be
# 0 = empty site
# 1 = diffusing particle
# 2 = particle attached to cluster
s = []
for i in range(Lx):
    s.append([0]*Ly)


# Functions:

def display(s):
    """Show lattice in simple (ascii) manner."""
    for i in range(Ly):
        for j in range(Lx):
            if s[i][j] != 0 :
                print s[i][j],
            else:
                print ".",
        print
    print

def random_boundary(Lx, Ly):
    """Pick a random position anywhere on the edge of the lattice."""
    edge = random.choice([0, 1, 2, 3])
    if   edge == 0:  # bottom
        x, y = (random.randint(0, Lx-2), 0)
    elif edge == 1:  # right
        x, y = (Lx-1, random.randint(0, Ly-2))
    elif edge == 2:  # top
        x, y = (random.randint(1, Lx-1), Ly-1)
    elif edge == 3:  # left
        x, y = (0, random.randint(1, Ly-1))
    return x, y

def move(s, x, y):
    
    """

    Randomly move a particle located at (x,y) to a direct neighbour
    site.

    This function accepts the lattice s as a list of lists
    and the current coordinates x, y of the moving particles.

    It should return the new coordinates x, y of the particle and
    modify the lattice s accordingly.
    """
    **#**
**    # Add the code here !**
**    #**

def is_contact(s, x0, y0):
    """
    Return True if site (x0, y0) is in contact with an existing
    cluster.
    Note: periodic boundary conditions are assumed, via x%Lx, etc
    """
    return s[(x0+1)%Lx][y0] == 2 or s[(x0-1)%Lx][y0] == 2 \
        or s[x0][(y0+1)%Ly] == 2 or s[x0][(y0-1)%Ly] == 2

def measure(s):
    """
    Measure fractal dimension of a cluster.

    This function accepts the lattice s and prints the number of
    occupied sites up to a distance r, n(r), as a function of r.
    """
    **#**
**    # Add the code here !**
**    #**
    return

# Main code

# Setup

s[L0x][L0y] = 2  # seed cluster at central site

while True:
    # Insert particle at random position on boundary
    # Keep doing this until the new particle touches the boundary.
    x, y = random_boundary(Lx, Ly)
    if is_contact(s, x, y):
        print "# cluster has reached the boundary."
        break
    s[x][y] = 1  # insert particle at x,y

    # Keep moving particle until it binds to cluster
    while True:  
        x, y = move(s, x, y)
        display(s)
        if is_contact(s, x, y):
            s[x][y] = 2  # attached
            break
    display(s)

# Show final cluster
display(s)
measure(s)

Thanks !


#2

How to learn it .
It is quite difficult for me.