EventDrivenMD / tests / test_edmd.py

import numpy as np
from numpy import array

from edmd import Particle, CollisionRules, Box, load_initial_particles, Simulation
import edmd


np.seterr(all='raise')


box = Box(10)
collisionRules = CollisionRules(box, 0)


def test_time_to_next_collision():
    assert(1 == collisionRules.time_to_next_collision(Particle([2, 0], [-1, 0]),
                                                    Particle([-2, 0], [1, 0])))


def test_time_to_next_collision_when_particles_already_in_contact():
    assert(0 == collisionRules.time_to_next_collision(Particle([2, 0], [-1, 0]),
        Particle([0, 0], [1, 0])))


def test_time_when_there_is_no_collision():
    assert(float('inf') == collisionRules.time_to_next_collision(Particle([2, 0], [1, 0]),
        Particle([-2, 0], [-1, 0], 0)))


def test_time_to_next_collision_with_wall_1():
    assert(1 == collisionRules.time_to_next_collision_with_wall(Particle([8, 0], [1, 0])))


def test_time_to_next_collision_with_wall_2():
    assert(17 == collisionRules.time_to_next_collision_with_wall(Particle([8, 0], [-1, 0])))


def test_time_to_next_collision_with_wall_3():
    assert(1 == collisionRules.time_to_next_collision_with_wall(Particle([0, 8], [0, 1])))


def test_time_to_next_collision_with_wall_4():
    assert(17 == collisionRules.time_to_next_collision_with_wall(Particle([0, 8], [0, -1])))


def test_time_to_next_collision_with_wall_5():
    assert(float('inf') == collisionRules.time_to_next_collision_with_wall(Particle([0, 8], [0, 0])))


def test_collide_1():
    particle_i = Particle(([2, 0]), ([-1, 0]), 0)
    particle_j = Particle(([0, 0]), ([1, 0]), 0)

    collisionRules.collide(particle_i, particle_j)

    assert(particle_i.v[0] == 0 and particle_i.v[1] == 0)
    assert(particle_j.v[0] == 0 and particle_j.v[1] == 0)


def test_collide_2():
    particle_i = Particle([2, 0], [-1, 2])
    particle_j = Particle([0, 0], [3, 1])

    collisionRules.collide(particle_i, particle_j)

    assert(particle_i.v[0] == 1 and particle_i.v[1] == 2)
    assert(particle_j.v[0] == 1 and particle_j.v[1] == 1)


def test_inelastic_collide():
    collision_rules = CollisionRules(Box(), 0.0)
    particle_i = Particle(np.random.random(2) * 10, np.random.random(2) * 10)
    particle_j = Particle(np.random.random(2) * 10, np.random.random(2) * 10)

    collision_rules.collide(particle_i, particle_j)

    vi_norm = np.dot(particle_i.x - particle_j.x, particle_i.v)
    vj_norm = np.dot(particle_i.x - particle_j.x, particle_j.v)

    assert(abs(vi_norm - vj_norm) < 1e-10)


def test_elastic_collide():
    collision_rules = CollisionRules(Box(), 1.0)
    particle_i = Particle(np.random.random(2) * 10, np.random.random(2) * 10)
    particle_j = Particle(np.random.random(2) * 10, np.random.random(2) * 10)
    vi_norm_before = np.dot(particle_i.x - particle_j.x, particle_i.v)
    vj_norm_before = np.dot(particle_i.x - particle_j.x, particle_j.v)

    collision_rules.collide(particle_i, particle_j)

    vi_norm_after = np.dot(particle_i.x - particle_j.x, particle_i.v)
    vj_norm_after = np.dot(particle_i.x - particle_j.x, particle_j.v)

    assert(abs(vi_norm_before - vj_norm_after) < 1e-10)
    assert(abs(vj_norm_before - vi_norm_after) < 1e-10)


def test_move_1():
    particle_i = Particle(array([0, 0]), array([1, 0]), 0)

    particle_i.move(2)

    assert(particle_i.x[0] == 2 and particle_i.x[1] == 0)


def test_move_2():
    particle_i = Particle(array([0, 0]), array([1, 3]), 0)

    particle_i.move(2)

    assert(particle_i.x[0] == 2 and particle_i.x[1] == 6)


def test_collide_with_wall_1():
    particle_i = Particle(array([8, 1]), array([-1, 2]), 0)

    collisionRules.collide_with_wall(particle_i)

    assert(particle_i.v[0] == 1 and particle_i.v[1] == 2)


def test_collide_with_wall_2():
    particle_i = Particle(array([-8, 1]), array([-1, 2]), 0)

    collisionRules.collide_with_wall(particle_i)

    assert(particle_i.v[0] == 1 and particle_i.v[1] == 2)


def test_collide_with_wall_3():
    particle_i = Particle(array([1, 8]), array([-1, 2]), 0)

    collisionRules.collide_with_wall(particle_i)

    assert(particle_i.v[0] == -1 and particle_i.v[1] == -2)


def test_collide_with_wall_4():
    particle_i = Particle(array([-1, 8]), array([-1, 2]), 0)

    collisionRules.collide_with_wall(particle_i)

    assert(particle_i.v[0] == -1 and particle_i.v[1] == -2)


def test_collide_with_wall_5():
    particle_i = Particle(array([8, -1]), array([-1, 2]), 0)

    collisionRules.collide_with_wall(particle_i)

    assert(particle_i.v[0] == 1 and particle_i.v[1] == 2)


def test_collide_with_wall_6():
    particle_i = Particle(array([8, 1]), array([-1, 2]), 0)

    collisionRules.collide_with_wall(particle_i)

    assert(particle_i.v[0] == 1 and particle_i.v[1] == 2)


def test_collide_with_wall_7():
    particle_i = Particle(array([1, -8]), array([-1, 2]), 0)

    collisionRules.collide_with_wall(particle_i)

    assert(particle_i.v[0] == -1 and particle_i.v[1] == -2)


def test_collide_with_wall_8():
    particle_i = Particle(array([-9, -8]), array([-1, 2]), 0)

    collisionRules.collide_with_wall(particle_i)

    assert(particle_i.v[0] == 1 and particle_i.v[1] == 2)


def test_load_initial_particles():
    particles = load_initial_particles("./tests/data/coords_ini.txt",
        "./tests/data/v_ini.txt")

    # velocities
    assert(particles[0].v[0] == 0.566198 and particles[0].v[1] == 0.59688)
    assert(particles[1].v[0] == -0.329554 and particles[1].v[1] == 0.536459)

    # coordinates
    assert(particles[0].x[0] == 101.376 and particles[0].x[1] == -31.4739)
    assert(particles[1].x[0] == 122.671 and particles[1].x[1] == -90.1297)


def test_that_exception_is_raised_when_initial_particles_files_are_wrong():
    try:
        load_initial_particles("./tests/data/coords_ini.txt",
            "./tests/data/v_ini_wrong.txt")
        assert(False)
    except:
        assert(True)


def test_kinenergy_with_random_velocities_of_module_1():
    particles = []
    for i in xrange(10):
        particle = Particle(array([i, 0]),
                array([np.random.random(), np.random.random()]), 0)
        particle.v /= np.linalg.norm(particle.v)
        particles.append(particle)
    sim = Simulation(particles)
    assert(10 == sim.kinenergy())


def test_box():
    box = Box(10)
    assert(box.contains([-9, 9], 1))


def test_update():
    coords_filename = "./tests/data/coords_ini_test_events.txt"
    vels_filename = "./tests/data/v_ini_test_events.txt"
    sim = edmd.create_simulation_from_files(coords_filename, vels_filename, box_size=10)
    expeted = array([float(line.split(';')[0]) for line in open('tests/data/events.txt')])
    result = array(sorted(sim.events.keys()))
    assert(all(np.abs(expeted - result) < 1e-4))
    expected_particles_events = []
    for line in open('tests/data/events.txt'):
        part1_events, part2_events = line.split(';')[1:3]
        part1_events = array(map(float, part1_events.split()))
        part2_events = array(map(float, part2_events.split()))
        expected_particles_events.append([part1_events, part2_events])
    particles_times = []
    for time in sorted(sim.events.keys()):
        particles_times.append([array(part.event_times) if part is not None else [] for part in sim.events[time]])
    for exp_parts, result_parts in zip(expected_particles_events, particles_times):
        assert(all(np.abs(exp_parts[0] - result_parts[0]) < 1e-4))
        assert(all(np.abs(exp_parts[1] - result_parts[1]) < 1e-4))


# def test_random_particles_generation():
#     particles = edmd.create_random_particles()


# def test_particle_overlap():
#     part1 = Particle([0, 0], [0, 0])
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.