def get_dummy_core_and_sdm(n_length):
     core = DummyCore(backend, n_sd=n_length)
     dv = 1
     core.environment = Box(dv=dv, dt=0)
     sdm = Coalescence(StubKernel(core.backend))
     sdm.register(core)
     return core, sdm
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_update(backend):
        # Arrange
        core = DummyCore(backend)
        halo = 3
        grid = (11, 13)
        env = DummyEnvironment(grid=grid, halo=halo)
        env.register(core)
        env.qv[:] = 7.3
        env.thd[:] = 59.5
        env.pred['qv'][:] = 3.7
        env.pred['thd'][:] = 5.59
        core.environment = env

        sut = EulerianAdvection(lambda: None)
        sut.register(core)

        # Act
        sut()

        # Assert
        np.testing.assert_array_equal(
            env.get_qv(),
            env.get_predicted('qv').to_ndarray().reshape(grid))
        np.testing.assert_array_equal(
            env.get_thd(),
            env.get_predicted('thd').to_ndarray().reshape(grid))
Example #4
0
def get_dummy_core_and_sdm(backend,
                           n_length,
                           optimized_random=False,
                           environment=None,
                           substeps=1):
    core = DummyCore(backend, n_sd=n_length)
    core.environment = environment or Box(dv=1, dt=default_dt_coal_range[1])
    sdm = Coalescence(StubKernel(core.backend),
                      optimized_random=optimized_random,
                      substeps=substeps)
    sdm.register(core)
    return core, sdm
Example #5
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 #6
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 #7
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 #8
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()