Ejemplo n.º 1
0
def simulation(n_particles, temperature, rho, equilib_steps, n_steps, sampling_freq, dt):
    """ Run a simulation of lennard jones fluid
    """
    # TODO Finish function
    R, V, F, box_width = initialize_particles(n_particles, temperature, rho)

    return
Ejemplo n.º 2
0
def simulation(n_atoms, n_steps, rho, T_want, eps, dt):
    dist = []
    t_together = 0.0
    t_apart = 0.0
    N_a = 2 * n_atoms
    R, V, F, box_width = md.initialize_particles(n_atoms, T_want, rho, eps)
    KinE = []
    PotE = []
    TotE = []
    Temp = []
    for i in range(n_steps):
        Rnew = R + V*dt + 0.5*dt**2*F

        if i%1000 ==0:
            video.add_frame(R[0],R[1], i)
        if i%100000 == 0:
            print i

        energy_potential, Fnew = md.lennard_jones(Rnew, box_width, eps)
        V = scale_temp(V, T_want, N_a)
        ke = (1.0/2.0) * np.sum(V*V)
        d = dister(0,1,R, box_width)
        dist.append(d)
        if d < 1.52:
            t_together += 1.0
        if d > 1.52:
            t_apart += 1.0
        KinE.append(ke)
        PotE.append(energy_potential)
        TotE.append(ke + energy_potential)
        Temp.append(2 * ke / (N_a))
        Vnew = V + 0.5*dt*(F + Fnew)

        V = Vnew.copy()
        R = Rnew.copy()
    video.save("colorparticle", periodic_boundary=True)
    return PotE, KinE, TotE, Temp, dist, t_together/t_apart
Ejemplo n.º 3
0
def calc_temperature(V):
    return np.mean(V*V)

n_steps = 10000
save_freq = 50
video = 0

temperature = 0.5
n_atoms = 100
rho = 0.7
dt = 0.001

np.random.random(555)

R, V, F, box_width = initialize_particles(n_atoms,       # n_atoms
                                          temperature,   # temperature
                                          rho)           # rho

#print R, box_width
#exit()

R_frames = []
R_frames.append(R)
k_list = []
p_list = []
t_list = []
te_list = []
pr_list = []


p_tail =  16.0/3.0 * np.pi * rho *( 2.0/3.0 * (2.5**-9) - (2.5**-3))
Ejemplo n.º 4
0
save_video = True

# Simulation constants
n_steps     = 2000
temperature = 2.0
n_atoms     = 40
rho         = 0.1
dt          = 0.001

# Save frequency
save_freq = 10


# Initialize simulation
R, V, F, box_size = md.initialize_particles(n_atoms, temperature, rho)

# Calculate the tail correction
p_tail = 16.0/3.0 * np.pi * rho * (2.0/3.0 * (2.5**-9) - (2.5**-3))


# Storage arrays
ek_list = [] # Kinetic energy
ep_list = [] # Potential energy
et_list = [] # Total energy
pr_list = [] # Pressure
te_list = [] # Temperature


# Run simulation for n_steps
for n in range(n_steps+1):
Ejemplo n.º 5
0
    return np.mean(V * V)


n_steps = 10000
save_freq = 50
video = 0

temperature = 0.5
n_atoms = 100
rho = 0.7
dt = 0.001

np.random.random(555)

R, V, F, box_width = initialize_particles(
    n_atoms,  # n_atoms
    temperature,  # temperature
    rho)  # rho

#print R, box_width
#exit()

R_frames = []
R_frames.append(R)
k_list = []
p_list = []
t_list = []
te_list = []
pr_list = []

p_tail = 16.0 / 3.0 * np.pi * rho * (2.0 / 3.0 * (2.5**-9) - (2.5**-3))
Ejemplo n.º 6
0
def simulation(n_atoms, n_steps, rho, temperature, dt,
               eps = [],
               save_freq=60,
               video_filename='',
               constant_temperature=False):

    n_eq = 200*save_freq

    if video_filename != '':
        save_video = True


    if len(eps) == 0:
        eps = np.ones((n_atoms, n_atoms))
    else:
        # set particle 1 and 2 to other color
        colors = ['#4daf4a' for _ in range(n_atoms)]
        colors[0] = "#377eb8"
        colors[1] = "#377eb8"
        video.add_color(colors)



    # Initialize simulation
    R, V, F, box_width = md.initialize_particles(n_atoms, temperature, rho, eps)

    print 'Initialized'

    # # Calculate the tail correction
    # p_tail = 16.0/3.0 * np.pi * rho * (2.0/3.0 * (2.5**-9) - (2.5**-3))

    # Storage arrays
    ek_list = [] # Kinetic energy
    ep_list = [] # Potential energy
    et_list = [] # Total energy
    # pr_list = [] # Pressure
    te_list = [] # Temperature
    r_list = [] # distance list


    # Run simulation for n_steps
    for n in range(n_steps + 1):

        # Step 1: Calculate new positions
        R += V*dt + 0.5*F*dt*dt

        # Step 2: Calculate new forces
        F0 = copy.copy(F)
        potential_energy, F = md.lennard_jones(R, box_width, eps)

        # Step 3: Calculate new velocities
        V += 0.5*(F + F0)*dt

        # if temperture is set to constant, then scale the velocities
        if constant_temperature:
            V = md.scale_temp(V, temperature)


        # for every save_freq steps
        if n % save_freq == 0 and n > n_eq:

            print "step:", n

            # Calculate properties
            kinetic_energy = calc_kinetic(V)
            total_energy = kinetic_energy + potential_energy
            temperature = calc_temperature(V)
            # pressure = rho*temperature + 1.0/(3.0*box_width**3)*vir + p_tail


            ek_list.append(kinetic_energy)
            ep_list.append(potential_energy)
            et_list.append(total_energy)
            # pr_list.append(pressure)
            te_list.append(temperature)


            # calculate the distance between 0 and 1
            X  = R[0, 0] - R[0, 1]
            Y  = R[1, 0] - R[1, 1]

            # Periodic boundary condition
            X  -= box_width * np.rint(X/box_width)
            Y  -= box_width * np.rint(Y/box_width)
            d = np.sqrt(X**2 + Y**2)
            r_list.append(d)


            # Add frame to video
            if save_video:
                video.add_frame(R[0], R[1])


    # Remember not to save a video for large n_steps
    if save_video:
        video.save(video_filename, box_width, periodic_boundary=True)


    return ek_list, ep_list, et_list, te_list, r_list
Ejemplo n.º 7
0

save_video = True

# Simulation constants
n_steps = 2000
temperature = 2.0
n_atoms = 40
rho = 0.1
dt = 0.001

# Save frequency
save_freq = 10

# Initialize simulation
R, V, F, box_size = md.initialize_particles(n_atoms, temperature, rho)

# Calculate the tail correction
p_tail = 16.0 / 3.0 * np.pi * rho * (2.0 / 3.0 * (2.5**-9) - (2.5**-3))

# Storage arrays
ek_list = []  # Kinetic energy
ep_list = []  # Potential energy
et_list = []  # Total energy
pr_list = []  # Pressure
te_list = []  # Temperature

# Run simulation for n_steps
for n in range(n_steps + 1):

    # Step 1: Calculate new positions
Ejemplo n.º 8
0
def simulation(n_atoms, n_steps, rho, temperature, dt,
               eps = [],
               save_freq=60,
               video_filename='',
               constant_temperature=False):

    n_eq = 200*save_freq

    if video_filename != '':
        save_video = True


    if len(eps) == 0:
        eps = np.ones((n_atoms, n_atoms))
    else:
        # set particle 1 and 2 to other color
        colors = ['#4daf4a' for _ in range(n_atoms)]
        colors[0] = "#377eb8"
        colors[1] = "#377eb8"
        video.add_color(colors)



    # Initialize simulation
    R, V, F, box_width = md.initialize_particles(n_atoms, temperature, rho, eps)

    print 'Initialized'

    # # Calculate the tail correction
    # p_tail = 16.0/3.0 * np.pi * rho * (2.0/3.0 * (2.5**-9) - (2.5**-3))

    # Storage arrays
    ek_list = [] # Kinetic energy
    ep_list = [] # Potential energy
    et_list = [] # Total energy
    # pr_list = [] # Pressure
    te_list = [] # Temperature
    r_list = [] # distance list


    # Run simulation for n_steps
    for n in range(n_steps + 1):

        # Step 1: Calculate new positions
        R += V*dt + 0.5*F*dt*dt

        # Step 2: Calculate new forces
        F0 = copy.copy(F)
        potential_energy, F = md.lennard_jones(R, box_width, eps)

        # Step 3: Calculate new velocities
        V += 0.5*(F + F0)*dt

        # if temperture is set to constant, then scale the velocities
        if constant_temperature:
            V = md.scaletemp(V, temperature)


        # for every save_freq steps
        if n % save_freq == 0 and n > n_eq:

            print "step:", n

            # Calculate properties
            kinetic_energy = calc_kinetic(V)
            total_energy = kinetic_energy + potential_energy
            temperature = calc_temperature(V)
            # pressure = rho*temperature + 1.0/(3.0*box_width**3)*vir + p_tail


            ek_list.append(kinetic_energy)
            ep_list.append(potential_energy)
            et_list.append(total_energy)
            # pr_list.append(pressure)
            te_list.append(temperature)


            # calculate the distance between 0 and 1
            X  = R[0, 0] - R[0, 1]
            Y  = R[1, 0] - R[1, 1]

            # Periodic boundary condition
            X  -= box_width * np.rint(X/box_width)
            Y  -= box_width * np.rint(Y/box_width)
            d = np.sqrt(X**2 + Y**2)
            r_list.append(d)


            # Add frame to video
            if save_video:
                video.add_frame(R[0], R[1])


    # Remember not to save a video for large n_steps
    if save_video:
        video.save(video_filename, box_width, periodic_boundary=True)


    return ek_list, ep_list, et_list, te_list, r_list