def test_boundary_condition(self):
        # Arrange
        n = np.ones(1, dtype=np.int64)
        grid = (1, 1)
        particles = DummyParticles(Default, n_sd=len(n), dt=1)
        particles.set_mesh(grid)
        particles.set_environment(
            DummyEnvironment, ((np.array([[0, 0]]).T, np.array([[0, 0]])), ))
        positions = Default.from_ndarray(np.array([[0, 0]]))
        cell_id, cell_origin, position_in_cell = particles.mesh.cellular_attributes(
            positions)

        particles.state = StateFactory.state(n=n,
                                             intensive={},
                                             extensive={},
                                             cell_id=cell_id,
                                             cell_origin=cell_origin,
                                             position_in_cell=position_in_cell,
                                             particles=particles)
        sut = Displacement(particles=particles, sedimentation=True)
        particles.set_terminal_velocity(ConstantTerminalVelocity)

        # Act
        sut()

        # Assert
        assert particles.state.SD_num == 0
Beispiel #2
0
    def test_sort_by_cell_id(self, n, cells, n_sd, idx, new_idx, cell_start,
                             thread_number):
        # Arrange
        particles = DummyParticles(backend, n_sd=n_sd)
        sut = TestableStateFactory.empty_state(particles)
        sut.n = TestState.storage(n)
        n_cell = max(cells) + 1
        sut.cell_id = TestState.storage(cells)
        sut._State__idx = TestState.storage(idx)
        idx_length = len(sut._State__idx)
        sut._State__tmp_idx = TestState.storage([0] * idx_length)
        sut._State__cell_start = TestState.storage([0] * (n_cell + 1))
        sut._State__cell_start_p = backend.array(
            (thread_number, len(sut._State__cell_start)), dtype=int)
        sut._State__n_sd = particles.n_sd

        # Act
        sut._State__sort_by_cell_id()

        # Assert
        assert len(sut._State__idx) == idx_length
        np.testing.assert_array_equal(
            np.array(new_idx),
            backend.to_ndarray(sut._State__idx[:sut.SD_num]))
        np.testing.assert_array_equal(
            np.array(cell_start), backend.to_ndarray(sut._State__cell_start))
Beispiel #3
0
    def test_permutation_local_repeatable(self):
        n_sd = 800
        idx = range(n_sd)
        u01 = np.random.random(n_sd)
        cell_start = [0, 0, 20, 250, 700, n_sd]

        # Arrange
        particles = DummyParticles(backend, n_sd=n_sd)
        sut = TestableStateFactory.empty_state(particles)
        sut._State__idx = TestState.storage(idx)
        idx_length = len(sut._State__idx)
        sut._State__tmp_idx = TestState.storage([0] * idx_length)
        cell_id = []
        for i in range(len(cell_start) - 1):
            cell_id = cell_id + [i] * cell_start[i + 1]
        sut.cell_id = TestState.storage(cell_id)
        sut._State__cell_start = TestState.storage(cell_start)
        sut._State__sorted = True
        sut._State__n_sd = particles.n_sd
        u01 = TestState.storage(u01)

        # Act
        sut.permutation_local(u01)
        expected = backend.to_ndarray(sut._State__idx)
        sut._State__idx = TestState.storage(idx)
        sut.permutation_local(u01)

        # Assert
        np.testing.assert_array_equal(sut._State__idx, expected)
        assert sut._State__sorted == True
        sut._State__sort_by_cell_id()
        np.testing.assert_array_equal(sut._State__idx[:50], expected[:50])
Beispiel #4
0
    def test_permutation_global_repeatable(self):
        n_sd = 800
        idx = range(n_sd)
        u01 = np.random.random(n_sd)

        # Arrange
        particles = DummyParticles(backend, n_sd=n_sd)
        sut = TestableStateFactory.empty_state(particles)
        sut._State__idx = TestState.storage(idx)
        idx_length = len(sut._State__idx)
        sut._State__tmp_idx = TestState.storage([0] * idx_length)
        sut._State__sorted = True
        sut._State__n_sd = particles.n_sd
        u01 = TestState.storage(u01)

        # Act
        sut.permutation_global(u01)
        expected = backend.to_ndarray(sut._State__idx)
        sut._State__sorted = True
        sut._State__idx = TestState.storage(idx)
        sut.permutation_global(u01)

        # Assert
        np.testing.assert_array_equal(sut._State__idx, expected)
        assert sut._State__sorted == False
Beispiel #5
0
    def test_calculate_displacement(self):
        # Arrange
        n = np.ones(1)
        grid = (1, 1)
        particles = DummyParticles(Default, n_sd=len(n))
        particles.set_mesh(grid)
        a = .1
        b = .2
        w = .25
        particles.set_environment(
            DummyEnvironment, ((np.array([[a, b]]).T, np.array([[0, 0]])), ))
        positions = Default.from_ndarray(np.array([[w, 0]]))
        cell_id, cell_origin, position_in_cell = particles.mesh.cellular_attributes(
            positions)

        particles.state = StateFactory.state(n=n,
                                             intensive={},
                                             extensive={},
                                             cell_id=cell_id,
                                             cell_origin=cell_origin,
                                             position_in_cell=position_in_cell,
                                             particles=particles)
        sut = Advection(particles=particles, scheme='FTFS')

        # Act
        sut.calculate_displacement(sut.displacement, sut.courant,
                                   particles.state.cell_origin,
                                   particles.state.position_in_cell)

        # Assert
        np.testing.assert_equal(sut.displacement[0, 0], (1 - w) * a + w * b)
Beispiel #6
0
    def test_boundary_condition(self):
        # Arrange
        n = np.ones(1)
        grid = (1, 1)
        particles = DummyParticles(Default, n_sd=len(n))
        particles.set_mesh(grid)
        particles.set_environment(
            DummyEnvironment, ((np.array([[0, 0]]).T, np.array([[0, 0]])), ))
        droplet_id = 0
        positions = Default.from_ndarray(np.array([[0, 0]]))
        cell_id, cell_origin, position_in_cell = particles.mesh.cellular_attributes(
            positions)

        particles.state = StateFactory.state(n=n,
                                             intensive={},
                                             extensive={},
                                             cell_id=cell_id,
                                             cell_origin=cell_origin,
                                             position_in_cell=position_in_cell,
                                             particles=particles)
        sut = Advection(particles=particles)
        state = particles.state
        state.cell_origin[droplet_id, 0] = 1.1
        state.cell_origin[droplet_id, 1] = 1.2

        # Act
        sut.boundary_condition(state.cell_origin)

        # Assert
        assert state.cell_origin[droplet_id, 0] == 0
        assert state.cell_origin[droplet_id, 1] == 0
Beispiel #7
0
    def test_update_position(self):
        # Arrange
        n = np.ones(1)
        grid = (1, 1)
        particles = DummyParticles(Default, n_sd=len(n))
        particles.set_mesh(grid)
        particles.set_environment(
            DummyEnvironment, ((np.array([[0, 0]]).T, np.array([[0, 0]])), ))
        droplet_id = 0
        px = .1
        py = .2
        positions = Default.from_ndarray(np.array([[px, py]]))
        cell_id, cell_origin, position_in_cell = particles.mesh.cellular_attributes(
            positions)

        particles.state = StateFactory.state(n=n,
                                             intensive={},
                                             extensive={},
                                             cell_id=cell_id,
                                             cell_origin=cell_origin,
                                             position_in_cell=position_in_cell,
                                             particles=particles)
        sut = Advection(particles=particles)
        sut.displacement[droplet_id, 0] = .1
        sut.displacement[droplet_id, 1] = .2

        # Act
        sut.update_position(particles.state.position_in_cell, sut.displacement)

        # Assert
        for d in range(2):
            assert particles.state.position_in_cell[droplet_id, d] == (
                positions[droplet_id, d] + sut.displacement[droplet_id, d])
Beispiel #8
0
    def test_moment_0d():
        # Arrange
        n_part = 10000
        v_mean = 2e-6
        d = 1.2

        v_min = 0.01e-6
        v_max = 10e-6
        n_sd = 32

        spectrum = Lognormal(n_part, v_mean, d)
        v, n = linear(n_sd, spectrum, (v_min, v_max))
        T = np.full_like(v, 300.)
        n = discretise_n(n)
        particles = DummyParticles(backend, n_sd)
        state = TestableStateFactory.state_0d(n=n,
                                              extensive={'volume': v},
                                              intensive={'temperature': T},
                                              particles=particles)

        true_mean, true_var = spectrum.stats(moments='mv')

        # TODO: add a moments_0 wrapper
        moment_0 = np.empty((1, ), dtype=int)
        moments = np.empty((1, 1), dtype=float)

        # Act
        state.moments(moment_0, moments, specs={'volume': (0, )})
        discr_zero = moments[0, 0]

        state.moments(moment_0, moments, specs={'volume': (1, )})
        discr_mean = moments[0, 0]

        state.moments(moment_0, moments, specs={'volume': (2, )})
        discr_mean_radius_squared = moments[0, 0]

        state.moments(moment_0, moments, specs={'temperature': (0, )})
        discr_zero_T = moments[0, 0]

        state.moments(moment_0, moments, specs={'temperature': (1, )})
        discr_mean_T = moments[0, 0]

        state.moments(moment_0, moments, specs={'temperature': (2, )})
        discr_mean_T_squared = moments[0, 0]

        # Assert
        assert abs(discr_zero - 1) / 1 < 1e-3

        assert abs(discr_mean - true_mean) / true_mean < .01e-1

        true_mrsq = true_var + true_mean**2
        assert abs(discr_mean_radius_squared - true_mrsq) / true_mrsq < .05e-1

        assert discr_zero_T == discr_zero
        assert discr_mean_T == 300.
        assert discr_mean_T_squared == 300.**2
Beispiel #9
0
 def get_dummy_particles_and_sdm(n_length):
     particles = DummyParticles(backend, n_sd=n_length, dt=0)
     dv = 1
     particles.set_mesh_0d(dv)
     particles.set_environment(Box, {})
     sdm = SDM(particles, StubKernel(particles.backend))
     return particles, sdm
Beispiel #10
0
def test_final_state(croupier):
    # Arrange
    n_part = 10000
    v_mean = 2e-6
    d = 1.2
    v_min = 0.01e-6
    v_max = 10e-6
    n_sd = 64
    x = 4
    y = 4

    spectrum = Lognormal(n_part, v_mean, d)
    v, n = linear(n_sd, spectrum, (v_min, v_max))
    n = discretise_n(n)
    particles = DummyParticles(backend, n_sd)
    particles.set_mesh((x, y))
    particles.croupier = croupier

    cell_id = backend.array((n_sd, ), dtype=int)
    cell_origin_np = np.concatenate(
        [np.random.randint(0, x, n_sd),
         np.random.randint(0, y, n_sd)]).reshape((-1, 2))
    cell_origin = backend.from_ndarray(cell_origin_np)
    position_in_cell_np = np.concatenate(
        [np.random.rand(n_sd), np.random.rand(n_sd)]).reshape((-1, 2))
    position_in_cell = backend.from_ndarray(position_in_cell_np)
    state = TestableStateFactory.state(n=n,
                                       extensive={'volume': v},
                                       intensive={},
                                       cell_id=cell_id,
                                       cell_origin=cell_origin,
                                       position_in_cell=position_in_cell,
                                       particles=particles)
    particles.state = state

    # Act
    u01 = backend.from_ndarray(np.random.random(n_sd))
    particles.permute(u01)
    _ = particles.state.cell_start

    # Assert
    assert (np.diff(state.cell_id[state._State__idx]) >= 0).all()
Beispiel #11
0
    def test_housekeeping(self, volume, n):
        # Arrange
        particles = DummyParticles(backend, n_sd=len(n))
        sut = TestableStateFactory.state_0d(n=n,
                                            extensive={'volume': volume},
                                            intensive={},
                                            particles=particles)
        # TODO
        sut.healthy = TestState.storage([0])

        # Act
        n_sd = sut.SD_num

        # Assert
        assert sut['volume'].shape == sut['n'].shape
        assert sut.SD_num == (n != 0).sum()
        assert sut['n'].sum() == n.sum()
        assert (sut['volume'] * sut['n']).sum() == (volume * n).sum()
Beispiel #12
0
    def test_permutation_global(self):
        n_sd = 8
        idx = range(n_sd)
        u01 = [.1, .4, .2, .5, .9, .1, .6, .3]

        # Arrange
        particles = DummyParticles(backend, n_sd=n_sd)
        sut = TestableStateFactory.empty_state(particles)
        sut._State__idx = TestState.storage(idx)
        idx_length = len(sut._State__idx)
        sut._State__tmp_idx = TestState.storage([0] * idx_length)
        sut._State__sorted = True
        sut._State__n_sd = particles.n_sd
        u01 = TestState.storage(u01)

        # Act
        sut.permutation_global(u01)

        # Assert
        expected = np.array([1, 3, 5, 7, 6, 0, 4, 2])
        np.testing.assert_array_equal(sut._State__idx, expected)
        assert sut._State__sorted == False
Beispiel #13
0
    def test_advection(self):
        n = np.ones(1)
        grid = (3, 3)
        particles = DummyParticles(Default, n_sd=len(n))
        particles.set_mesh(grid)
        particles.set_environment(DummyEnvironment, ((np.ones(
            (4, 3)), np.zeros((3, 4))), ))
        positions = Default.from_ndarray(np.array([[1.5, 1.5]]))
        cell_id, cell_origin, position_in_cell = particles.mesh.cellular_attributes(
            positions)

        particles.state = StateFactory.state(n=n,
                                             intensive={},
                                             extensive={},
                                             cell_id=cell_id,
                                             cell_origin=cell_origin,
                                             position_in_cell=position_in_cell,
                                             particles=particles)
        sut = Advection(particles=particles)

        sut()

        np.testing.assert_array_equal(particles.state.cell_origin[0, :],
                                      np.array([2, 1]))
Beispiel #14
0
    def test_single_cell(self):

        # Arrange
        n = np.ones(1)
        grid = (1, 1)
        particles = DummyParticles(Default, n_sd=len(n))
        particles.set_mesh(grid)
        particles.set_environment(
            DummyEnvironment,
            ((np.array([[.1, .2]]).T, np.array([[.3, .4]])), ))
        positions = Default.from_ndarray(np.array([[0.5, 0.5]]))
        cell_id, cell_origin, position_in_cell = particles.mesh.cellular_attributes(
            positions)
        particles.state = StateFactory.state(n=n,
                                             intensive={},
                                             extensive={},
                                             cell_id=cell_id,
                                             cell_origin=cell_origin,
                                             position_in_cell=position_in_cell,
                                             particles=particles)
        sut = Advection(particles=particles)

        # Act
        sut()
Beispiel #15
0
    def test_recalculate_cell_id(self):
        # Arrange
        n = np.ones(1, dtype=np.int64)
        droplet_id = 0
        initial_position = Default.from_ndarray(np.array([[0, 0]]))
        grid = (1, 1)
        particles = DummyParticles(backend, n_sd=1)
        particles.set_mesh(grid)
        particles.set_environment(DummyEnvironment, (None, ))
        sut = TestableStateFactory.state_2d(n=n,
                                            intensive={},
                                            extensive={},
                                            particles=particles,
                                            positions=initial_position)
        sut.cell_origin[droplet_id, 0] = .1
        sut.cell_origin[droplet_id, 1] = .2
        sut.cell_id[droplet_id] = -1

        # Act
        sut.recalculate_cell_id()

        # Assert
        assert sut.cell_id[droplet_id] == 0