Example #1
0
    def test_sort_by_cell_id(self, n, cells, n_sd, idx, new_idx, cell_start,
                             thread_number):
        # Arrange
        particles = DummyCore(backend, n_sd=n_sd)
        particles.build(attributes={'n': np.zeros(n_sd)})
        sut = particles.state
        sut._State__idx = TestState.storage(idx)
        sut.attributes['n'].data = TestState.storage(n, sut._State__idx)
        n_cell = max(cells) + 1
        sut.attributes['cell id'].data = TestState.storage(
            cells, sut._State__idx)
        idx_length = len(sut._State__idx)
        sut._State__cell_start = TestState.storage([0] * (n_cell + 1))
        sut._State__n_sd = particles.n_sd
        sut.healthy = 0 not in n
        sut._State__cell_caretaker = backend.make_cell_caretaker(
            sut._State__idx, sut._State__cell_start)

        # Act
        sut.sanitize()
        sut._State__sort_by_cell_id()

        # Assert
        np.testing.assert_array_equal(
            np.array(new_idx), sut._State__idx[:sut.SD_num].to_ndarray())
        np.testing.assert_array_equal(np.array(cell_start),
                                      sut._State__cell_start.to_ndarray())
Example #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
Example #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 = DummyCore(backend, n_sd=n_sd)
        particles.build(attributes={'n': np.zeros(n_sd)})
        sut = particles.state
        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.attributes['cell id'].data = 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(u01, local=True)
        expected = sut._State__idx.to_ndarray()
        sut._State__idx = TestState.storage(idx)
        sut.permutation(u01, local=True)

        # 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])
Example #4
0
    def test_sort_by_cell_id(backend, n, cells, n_sd, idx, new_idx,
                             cell_start):  #, thread_number):
        from PySDM.backends import ThrustRTC
        if backend is ThrustRTC:
            return  # TODO

        # Arrange
        core = DummyCore(backend, n_sd=n_sd)
        core.build(attributes={'n': np.ones(n_sd)})
        sut = core.particles
        sut._Particles__idx = TestParticles.make_storage(backend, idx)
        sut.attributes['n'].data = TestParticles.make_storage(
            backend, n, sut._Particles__idx)
        n_cell = max(cells) + 1
        sut.attributes['cell id'].data = TestParticles.make_storage(
            backend, cells, sut._Particles__idx)
        sut._Particles__cell_start = TestParticles.make_storage(
            backend, [0] * (n_cell + 1))
        sut._Particles__n_sd = core.n_sd
        sut.healthy = 0 not in n
        sut._Particles__cell_caretaker = backend.make_cell_caretaker(
            sut._Particles__idx, sut._Particles__cell_start)

        # Act
        sut.sanitize()
        sut._Particles__sort_by_cell_id()

        # Assert
        np.testing.assert_array_equal(
            np.array(new_idx),
            sut._Particles__idx.to_ndarray()[:sut.SD_num])
        np.testing.assert_array_equal(np.array(cell_start),
                                      sut._Particles__cell_start.to_ndarray())
Example #5
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 = DummyCore(backend, n_sd)
        attribute = {'n': n, 'volume': v, 'temperature': T}
        particles.build(attribute)
        state = particles.state

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

        # TODO: add a moments_0 wrapper
        moment_0 = particles.backend.Storage.empty((1,), dtype=int)
        moments = particles.backend.Storage.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
Example #6
0
    def test_moment_0d(backend):
        # Arrange
        n_part = 100000
        v_mean = 2e-6
        d = 1.2
        n_sd = 32

        spectrum = Lognormal(n_part, v_mean, d)
        v, n = Linear(spectrum).sample(n_sd)
        T = np.full_like(v, 300.)
        n = discretise_n(n)
        particles = DummyCore(backend, n_sd)
        attribute = {'n': n, 'volume': v, 'temperature': T, 'heat': T * v}
        particles.build(attribute)
        state = particles.particles

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

        # TODO #217 : add a moments_0 wrapper
        moment_0 = particles.backend.Storage.empty((1, ), dtype=float)
        moments = particles.backend.Storage.empty((1, 1), dtype=float)

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

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

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

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

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

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

        # 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.
        np.testing.assert_approx_equal(discr_mean_T_squared,
                                       300.**2,
                                       significant=6)
Example #7
0
    def test_housekeeping(self, volume, n):
        # Arrange
        particles = DummyCore(backend, n_sd=len(n))
        attributes = {'n': n, 'volume': volume}
        particles.build(attributes)
        sut = particles.state
        sut.healthy = False

        # Act
        n_sd = sut.SD_num

        # Assert
        assert sut.SD_num == (n != 0).sum()
        assert sut['n'].to_ndarray().sum() == n.sum()
        assert (sut['volume'].to_ndarray() *
                sut['n'].to_ndarray()).sum() == (volume * n).sum()
Example #8
0
    def test_housekeeping(backend, volume, n):
        # Arrange
        core = DummyCore(backend, n_sd=len(n))
        attributes = {'n': n, 'volume': volume}
        core.build(attributes, int_caster=np.int64)
        sut = core.particles
        sut.healthy = False

        # Act
        sut.sanitize()
        _ = sut.SD_num

        # Assert
        assert sut.SD_num == (n != 0).sum()
        assert sut['n'].to_ndarray().sum() == n.sum()
        assert (sut['volume'].to_ndarray() * sut['n'].to_ndarray()).sum() == (volume * n).sum()
Example #9
0
    def test_spectrum_moment_0d(backend):
        # Arrange
        n_part = 100000
        v_mean = 2e-6
        d = 1.2
        n_sd = 32

        spectrum = Lognormal(n_part, v_mean, d)
        v, n = Linear(spectrum).sample(n_sd)
        T = np.full_like(v, 300.)
        n = discretise_n(n)
        particles = DummyCore(backend, n_sd)
        attribute = {'n': n, 'volume': v, 'temperature': T, 'heat': T * v}
        particles.build(attribute)
        state = particles.particles

        v_bins = np.linspace(0, 5e-6, num=5, endpoint=True)

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

        # TODO #217 : add a moments_0 wrapper
        spectrum_moment_0 = particles.backend.Storage.empty(
            (len(v_bins) - 1, 1), dtype=float)
        spectrum_moments = particles.backend.Storage.empty(
            (len(v_bins) - 1, 1), dtype=float)
        moment_0 = particles.backend.Storage.empty((1, ), dtype=float)
        moments = particles.backend.Storage.empty((1, 1), dtype=float)
        v_bins_edges = particles.backend.Storage.from_ndarray(v_bins)

        # Act
        state.spectrum_moments(spectrum_moment_0,
                               spectrum_moments,
                               attr='volume',
                               rank=1,
                               attr_bins=v_bins_edges)
        actual = spectrum_moments.to_ndarray()

        expected = np.empty((len(v_bins) - 1, 1), dtype=float)
        for i in range(len(v_bins) - 1):
            state.moments(moment_0,
                          moments,
                          specs={'volume': (1, )},
                          attr_range=(v_bins[i], v_bins[i + 1]))
            expected[i, 0] = moments[0, 0]

        # Assert
        np.testing.assert_array_almost_equal(actual, expected)
Example #10
0
    def test_permutation_local_repeatable(backend):
        from PySDM.backends import ThrustRTC
        if backend is ThrustRTC:
            return  # TODO

        n_sd = 800
        idx = range(n_sd)
        u01 = np.random.random(n_sd)
        cell_start = [0, 0, 20, 250, 700, n_sd]

        # Arrange
        core = DummyCore(backend, n_sd=n_sd)
        core.build(attributes={'n': np.ones(n_sd)})
        sut = core.particles
        sut._Particles__idx = TestParticles.make_storage(backend, idx)
        idx_length = len(sut._Particles__idx)
        sut._Particles__tmp_idx = TestParticles.make_storage(
            backend, [0] * idx_length)
        cell_id = []
        for i in range(len(cell_start) - 1):
            cell_id += [i] * (cell_start[i + 1] - cell_start[i])
        assert len(cell_id) == n_sd
        sut.attributes['cell id'].data = TestParticles.make_storage(
            backend, cell_id)
        sut._Particles__cell_start = TestParticles.make_storage(
            backend, cell_start)
        sut._Particles__sorted = True
        sut._Particles__n_sd = core.n_sd
        u01 = TestParticles.make_storage(backend, u01)

        # Act
        sut.permutation(u01, local=True)
        expected = sut._Particles__idx.to_ndarray()
        sut._Particles__idx = TestParticles.make_storage(backend, idx)
        sut.permutation(u01, local=True)

        # Assert
        np.testing.assert_array_equal(sut._Particles__idx.to_ndarray(),
                                      expected)
        assert sut._Particles__sorted

        sut._Particles__sort_by_cell_id()
        np.testing.assert_array_equal(sut._Particles__idx.to_ndarray(),
                                      expected)
Example #11
0
    def get_displacement(self):
        core = DummyCore(Default, n_sd=len(self.n))
        core.environment = DummyEnvironment(
            dt=self.dt,
            grid=self.grid,
            courant_field_data=self.courant_field_data)
        positions = np.array(self.positions)
        cell_id, cell_origin, position_in_cell = core.mesh.cellular_attributes(
            positions)
        attributes = {
            'n': self.n,
            'cell id': cell_id,
            'cell origin': cell_origin,
            'position in cell': position_in_cell
        }
        core.build(attributes)
        sut = Displacement(scheme=self.scheme,
                           sedimentation=self.sedimentation)
        sut.register(core)

        return sut, core
Example #12
0
    def test_permutation_local_repeatable(backend):
        if backend==GPU:  # TODO #358
            return
        n_sd = 800
        idx = range(n_sd)
        u01 = np.random.random(n_sd)
        cell_start = [0, 0, 20, 250, 700, n_sd]

        # Arrange
        core = DummyCore(backend, n_sd=n_sd)
        cell_id = []
        core.environment.mesh.n_cell = len(cell_start) - 1
        for i in range(core.environment.mesh.n_cell):
            cell_id += [i] * (cell_start[i + 1] - cell_start[i])
        assert len(cell_id) == n_sd
        core.build(attributes={'n': np.ones(n_sd)})
        sut = core.particles
        sut._Particles__idx = TestParticles.make_indexed_storage(backend, idx)
        idx_length = len(sut._Particles__idx)
        sut._Particles__tmp_idx = TestParticles.make_indexed_storage(backend, [0] * idx_length)
        sut.attributes['cell id'].data = TestParticles.make_indexed_storage(backend, cell_id)
        sut._Particles__cell_start = TestParticles.make_indexed_storage(backend, cell_start)
        sut._Particles__sorted = True
        sut._Particles__n_sd = core.n_sd
        u01 = TestParticles.make_indexed_storage(backend, u01)

        # Act
        sut.permutation(u01, local=True)
        expected = sut._Particles__idx.to_ndarray()
        sut._Particles__idx = TestParticles.make_indexed_storage(backend, idx)
        sut.permutation(u01, local=True)

        # Assert
        np.testing.assert_array_equal(sut._Particles__idx.to_ndarray(), expected)
        assert sut._Particles__sorted

        sut._Particles__sort_by_cell_id()
        np.testing.assert_array_equal(sut._Particles__idx.to_ndarray(), expected)
Example #13
0
def test_final_state(croupier, backend):
    from PySDM.backends import ThrustRTC
    if backend is ThrustRTC:
        return  # TODO #330

    # Arrange
    n_part = 100000
    v_mean = 2e-6
    d = 1.2
    n_sd = 32
    x = 4
    y = 4

    attributes = {}
    spectrum = Lognormal(n_part, v_mean, d)
    attributes['volume'], attributes['n'] = Linear(spectrum).sample(n_sd)
    core = DummyCore(backend, n_sd)
    core.environment = DummyEnvironment(grid=(x, y))
    core.croupier = croupier

    attributes['cell id'] = np.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((2, -1))
    attributes['cell origin'] = cell_origin_np
    position_in_cell_np = np.concatenate(
        [np.random.rand(n_sd), np.random.rand(n_sd)]).reshape((2, -1))
    attributes['position in cell'] = position_in_cell_np
    core.build(attributes)

    # Act
    u01 = backend.Storage.from_ndarray(np.random.random(n_sd))
    core.particles.permutation(u01, local=core.croupier == 'local')
    _ = core.particles.cell_start

    # Assert
    assert (np.diff(core.particles['cell id'][core.particles._Particles__idx])
            >= 0).all()
Example #14
0
    def get_displacement(self, backend, scheme):
        formulae = Formulae(particle_advection=scheme)
        core = DummyCore(backend, n_sd=len(self.n), formulae=formulae)
        core.environment = DummyEnvironment(
            dt=self.dt,
            grid=self.grid,
            courant_field_data=self.courant_field_data)
        positions = np.array(self.positions)
        cell_id, cell_origin, position_in_cell = core.mesh.cellular_attributes(
            positions)
        attributes = {
            'n': self.n,
            'volume': self.volume,
            'cell id': cell_id,
            'cell origin': cell_origin,
            'position in cell': position_in_cell
        }
        core.build(attributes)
        sut = Displacement(courant_field=self.courant_field_data,
                           enable_sedimentation=self.sedimentation)
        sut.register(core)

        return sut, core
Example #15
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

    attributes = {}
    spectrum = Lognormal(n_part, v_mean, d)
    attributes['volume'], attributes['n'] = linear(n_sd, spectrum,
                                                   (v_min, v_max))
    core = DummyCore(backend, n_sd)
    core.environment = DummyEnvironment(grid=(x, y))
    core.croupier = croupier

    attributes['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((2, -1))
    attributes['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((2, -1))
    attributes['position in cell'] = backend.from_ndarray(position_in_cell_np)
    core.build(attributes)

    # Act
    u01 = backend.Storage.from_ndarray(np.random.random(n_sd))
    core.state.permutation(u01)
    _ = core.state.cell_start

    # Assert
    assert (np.diff(core.state['cell id'][core.state._State__idx]) >= 0).all()