Ejemplo n.º 1
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)
Ejemplo n.º 2
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 = DummyCore(backend, n_sd=1)
        particles.environment = DummyEnvironment(grid=grid)
        cell_id, cell_origin, position_in_cell = particles.mesh.cellular_attributes(
            initial_position)
        cell_origin[0, droplet_id] = .1
        cell_origin[1, droplet_id] = .2
        cell_id[droplet_id] = -1
        attribute = {
            'n': n,
            'cell id': cell_id,
            'cell origin': cell_origin,
            'position in cell': position_in_cell
        }
        particles.build(attribute)
        sut = particles.state

        # Act
        sut.recalculate_cell_id()

        # Assert
        assert sut['cell id'][droplet_id] == 0
Ejemplo n.º 3
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
Ejemplo n.º 4
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])
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
    def test_compute_gamma():
        # Arrange
        n = 87
        prob = np.linspace(0, 3, n, endpoint=True)
        rand = np.linspace(0, 1, n, endpoint=False)

        from PySDM.backends.default import Default
        backend = Default()

        expected = lambda p, r: p // 1 + (r < p - p // 1)

        for p in prob:
            for r in rand:
                # Act
                prob_arr = backend.from_ndarray(np.full((1, ), p))
                rand_arr = backend.from_ndarray(np.full((1, ), r))
                backend.compute_gamma(prob_arr, rand_arr)

                # Assert
                assert expected(p, r) == backend.to_ndarray(prob_arr)[0]
Ejemplo n.º 7
0
    def test_recalculate_cell_id(self):
        # Arrange
        n = np.ones(1)
        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
Ejemplo n.º 8
0
    def get_displacement(self):
        particles = DummyParticles(Default, n_sd=len(self.n))
        particles.set_environment(
            DummyEnvironment, {
                'dt': self.dt,
                'grid': self.grid,
                'courant_field_data': self.courant_field_data
            })
        positions = Default.from_ndarray(np.array(self.positions))
        cell_id, cell_origin, position_in_cell = particles.mesh.cellular_attributes(
            positions)
        attributes = {
            'n': self.n,
            'cell id': cell_id,
            'cell origin': cell_origin,
            'position in cell': position_in_cell
        }
        particles.get_particles(attributes)
        sut = Displacement(particles_builder=particles,
                           scheme=self.scheme,
                           sedimentation=self.sedimentation)

        return sut, particles
Ejemplo n.º 9
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]))
Ejemplo n.º 10
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()