示例#1
0
def test_simulation_1(dt):
    # initial condition and simulation parameters
    domain = ([-2, 12], [0, 3])
    t_max = 6
    dt = 0.5
    loc_0 = np.array([[0, 1.5], [10, 1.5]])
    vel_0 = np.array([[1, 0], [-1, 0]])
    radius = 1
    mass = [1, 1]

    # running the simulation
    loc, vel = sim.simulation(t_max, dt, mass, radius, loc_0, vel_0, domain)

    # creating a movie
    movie = Movie_2d(sim.simulation_step, dt, t_max - dt, loc, vel, domain,
                     mass, radius)
    movie.animate("pytest_movie_1d_dt_{}".format(dt))

    # test location and velocities after collision
    assert loc[0][0] < 5
    assert loc[1][0] > 5
    # Y component should stay the same
    assert (loc[0][1], loc[1][1]) == (loc_0[0][1], loc_0[1][1])

    # particles will exchange vel_x
    assert vel[0][0] == -1
    assert vel[1][0] == 1
    # Y component should stay the same
    assert (vel[0][1], vel[1][1]) == (vel_0[0][1], vel_0[1][1])
示例#2
0
def data(request):

    print("\n create data")
    # initial condition and simulation parameters
    domain = ([-2, 12], [0, 3])
    dt = 0.5
    t_max = 6
    loc_0 = np.array([[0, 1.5], [10, 1.5]])
    vel_0 = np.array([[1, 0], [-1, 0]])
    radius = 1
    mass = [1, 1]

    loc, vel = sim.simulation(t_max, dt, mass, radius, loc_0, vel_0, domain)

    my_data = {}
    my_data["loc_0"] = loc_0
    my_data["vel_0"] = vel_0
    my_data["loc"] = loc
    my_data["vel"] = vel
    my_data["mass"] = mass

    def data_cleanup():
        print("\n removing data")
        my_data.clear()

    request.addfinalizer(data_cleanup)
    return my_data
示例#3
0
def test_regression():

    # initial condition and simulation params

    # run simulation
    loc, vel = sim2d.simulation(...)

    # save reference simulations (only once when the test is created)
    np.save(
        os.path.join(os.path.dirname(__file__), "../data/locations_ref.npy"),
        loc)

    # read in reference values (every time the test is run)
    loc_ref = np.load(
        os.path.join(os.path.dirname(__file__), "../data/locations_ref.npy"))

    # from scipy docs:
    # The tolerance values are positive, typically very small numbers.
    # The relative difference (rtol * abs(b)) and the absolute difference atol
    # are added together to compare against the absolute difference
    # between a and b.
    np.testing.assert_allclose(loc,
                               loc_ref,
                               atol=0,
                               rtol=1e-9,
                               err_msg="for locations")
示例#4
0
def test_regression():

    # initial condition and simulation params
    vel_0 = np.array([[12., 12.], [-8, -15.], [-3, 4], [10, 0], [2., 12.],
                      [-8, -15.], [3, -4], [-10, 0], [-12., 12.], [8, -15.],
                      [-3, -4], [-10, 0]])
    loc_0 = np.array([[20., 45.], [65, 70.], [85., 90.], [10.,
                                                          10.], [50., 45.],
                      [15, 70.], [15., 90.], [45., 10.], [75., 45.], [40, 70.],
                      [45., 90.], [90., 10.]])
    domain = np.array([[0., 100.], [0., 100.]])
    mass = np.array([1., 2., 1., 3, 1., 2., 1., 3, 1., 2., 1., 3])
    dt = 1 / 30.
    t_max = 30
    radius = 5

    # run simulation
    loc, vel = sim2d.simulation(t_max,
                                dt,
                                mass,
                                radius,
                                loc_0,
                                vel_0,
                                domain,
                                t0=dt)

    # save reference simulations
    #np.save(os.path.join(os.path.dirname(__file__),"../data/loc_ref.npy"), loc)
    #np.save(os.path.join(os.path.dirname(__file__),"../data/vel_ref.npy"), vel)

    # read in reference values
    loc_ref = np.load(
        os.path.join(os.path.dirname(__file__), "../data/loc_ref.npy"))
    vel_ref = np.load(
        os.path.join(os.path.dirname(__file__), "../data/vel_ref.npy"))

    # from scipy docs:
    # The tolerance values are positive, typically very small numbers.
    # The relative difference (rtol * abs(b)) and the absolute difference atol
    # are added together to compare against the absolute difference
    # between a and b.
    np.testing.assert_allclose(loc,
                               loc_ref,
                               atol=0,
                               rtol=1e-9,
                               err_msg="for locations")
    np.testing.assert_allclose(vel,
                               vel_ref,
                               atol=0,
                               rtol=1e-7,
                               err_msg="for velocities")
示例#5
0
def test_simulation_1_fail():
    # initial condition and simulation parameters
    domain = ([-2, 12], [0, 3])
    dt = 1.
    t_max = 12
    loc_0 = np.array([[0, 1.5], [10, 1.5]])
    vel_0 = np.array([[1, 0], [-1, 0]])
    radius = 1
    mass = [1, 1]
    # checking if exception is raised properly
    with pytest.raises(Exception) as excinfo:
        loc, vel = sim.simulation(t_max, dt, mass, radius, loc_0, vel_0,
                                  domain)
    assert "two particles are exactly in the same place" in str(excinfo.value)
示例#6
0
def test_energy_hypothesis(mass1, mass2):
    # initial condition and simulation parameters
    domain = ([0, 20], [0, 20])
    dt = 0.5
    t_max = 6
    loc_0 = np.array([[3, 4], [15, 2]])
    vel_0 = np.array([[1, 0.5], [-1, -.25]])
    radius = 1

    # mass chosen by hypothesis
    mass = [mass1, mass2]

    # run the simulation
    loc, vel = sim2d.simulation(t_max, dt, mass, radius, loc_0, vel_0, domain)

    E_ini = E_kin(vel_0, mass)
    E_end = E_kin(vel, mass)

    print("testing for mass = {}. E_ini= {}. E_end = {}".format(
        mass, E_ini, E_end))
    pass