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())
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])
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
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))
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
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())
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
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)
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
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()
def test_permutation_global(backend): from PySDM.backends import ThrustRTC if backend is ThrustRTC: return # TODO n_sd = 8 u01 = [.1, .4, .2, .5, .9, .1, .6, .3] # Arrange core = DummyCore(backend, n_sd=n_sd) sut = ParticlesFactory.empty_particles(core, n_sd) idx_length = len(sut._Particles__idx) sut._Particles__tmp_idx = TestParticles.make_storage( backend, [0] * idx_length) sut._Particles__sorted = True sut._Particles__n_sd = core.n_sd u01 = TestParticles.make_storage(backend, u01) # Act sut.permutation(u01, local=False) # Assert expected = np.array([1, 3, 5, 7, 6, 0, 4, 2]) np.testing.assert_array_equal(sut._Particles__idx, expected) assert not sut._Particles__sorted
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()
def test_permutation_global_repeatable(backend): from PySDM.backends import ThrustRTC if backend is ThrustRTC: return # TODO n_sd = 800 u01 = np.random.random(n_sd) # Arrange core = DummyCore(backend, n_sd=n_sd) sut = ParticlesFactory.empty_particles(core, n_sd) idx_length = len(sut._Particles__idx) sut._Particles__tmp_idx = TestParticles.make_storage( backend, [0] * idx_length) sut._Particles__sorted = True sut._Particles__n_sd = core.n_sd u01 = TestParticles.make_storage(backend, u01) # Act sut.permutation(u01, local=False) expected = sut._Particles__idx.to_ndarray() sut._Particles__sorted = True sut._Particles__idx = TestParticles.make_storage(backend, range(n_sd)) sut.permutation(u01, local=False) # Assert np.testing.assert_array_equal(sut._Particles__idx, expected) assert not sut._Particles__sorted
def test_approximation(backend, plot=False): r = np.array([ .078, .1, .2, .3, .4, .5, .6, .7, .8, .9, 1.0, 1.2, 1.4, 1.6 ]) * const.si.mm / 2 r = backend.Storage.from_ndarray(r) u = np.array([ 18, 27, 72, 117, 162, 206, 247, 287, 327, 367, 403, 464, 517, 565 ]) / 100 n_sd = len(r) core = DummyCore(backend, n_sd=n_sd) # radius = np.linspace(4e-6, 200e-6, 1000, endpoint=True) u_term_ry = core.backend.Storage.empty((len(u), ), float) RogersYau(core)(u_term_ry, r) u_term_inter = backend.Storage.from_ndarray(u_term_ry.to_ndarray()) Interpolation(core)(u_term_inter, r) assert np.mean((u - u_term_ry)**2) < 2e-2 assert np.mean((u - u_term_inter)**2) < 1e-6 if plot: # r, u = r[:5], u[:5] plt.plot(r, u_term_ry) plt.plot(r, u_term_inter) plt.plot(r, u) plt.grid() plt.show()
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)
def test_observer(backend): class Observer: def __init__(self, core): self.steps = 0 self.core = core self.core.observers.append(self) def notify(self): self.steps += 1 assert self.steps == self.core.n_steps steps = 33 core = DummyCore(backend, 44) observer = Observer(core) core.run(steps) assert observer.steps == steps
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)
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
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)
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
def test_permutation_global(self): n_sd = 8 u01 = [.1, .4, .2, .5, .9, .1, .6, .3] # Arrange particles = DummyCore(backend, n_sd=n_sd) sut = StateFactory.empty_state(particles, n_sd) 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(u01, local=False) # 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
def test_permutation_global_as_implemented_in_Numba(): n_sd = 8 u01 = [.1, .4, .2, .5, .9, .1, .6, .3] # Arrange core = DummyCore(CPU, n_sd=n_sd) sut = ParticlesFactory.empty_particles(core, n_sd) idx_length = len(sut._Particles__idx) sut._Particles__tmp_idx = TestParticles.make_indexed_storage(CPU, [0] * idx_length) sut._Particles__sorted = True sut._Particles__n_sd = core.n_sd u01 = TestParticles.make_indexed_storage(CPU, u01) # Act sut.permutation(u01, local=False) # Assert expected = np.array([1, 3, 5, 7, 6, 0, 4, 2]) np.testing.assert_array_equal(sut._Particles__idx, expected) assert not sut._Particles__sorted
def test_permutation_global_repeatable(self): n_sd = 800 u01 = np.random.random(n_sd) # Arrange particles = DummyCore(backend, n_sd=n_sd) sut = StateFactory.empty_state(particles, n_sd) 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(u01, local=False) expected = sut._State__idx.to_ndarray() sut._State__sorted = True sut._State__idx = TestState.storage(range(n_sd)) sut.permutation(u01, local=False) # Assert np.testing.assert_array_equal(sut._State__idx, expected) assert sut._State__sorted == False
def test_permutation_local(backend): n_sd = 8 u01 = [.1, .4, .2, .5, .9, .1, .6, .3] cell_start = [0, 0, 2, 5, 7, n_sd] # Arrange core = DummyCore(backend, n_sd=n_sd) sut = ParticlesFactory.empty_particles(core, n_sd) idx_length = len(sut._Particles__idx) sut._Particles__tmp_idx = TestParticles.make_storage( backend, [0] * idx_length) 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) # Assert expected = np.array([1, 0, 2, 3, 4, 5, 6, 7]) np.testing.assert_array_equal(sut._Particles__idx, expected) assert sut._Particles__sorted
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()
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()