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)
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
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])
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
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()
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]))
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()