def test_use_walk_data(): # use walk data without using the generator function particle1 = particle_track.Particles(params) np.random.seed(0) particle1.generate_particles(Np_tracer, seed_xloc, seed_yloc) particle1.generate_particles(Np_tracer, seed_xloc, seed_yloc) old_walk_data = particle1.run_iteration() # create new particles class and try to use that old walk data particle2 = particle_track.Particles(params) particle2.generate_particles(0, [], [], previous_walk_data=old_walk_data) all_walk_data = particle2.run_iteration() assert all_walk_data['xinds'][0][0] == 1 assert all_walk_data['yinds'][0][0] == 1 assert all_walk_data['travel_times'][0][0] == 0.0 assert len(all_walk_data['xinds']) == 2
def test_depth_via_stage_topo_broken(self): params = particle_track.modelParams() params.dx = 1 params.stage = 'badstring' params.topography = np.zeros((3,3)) with pytest.raises(ValueError): particle = particle_track.Particles(params)
def test_stage_broken(self): params = particle_track.modelParams() params.dx = 1 params.stage = 'badstring' params.depth = np.ones((3,3)) with pytest.raises(ValueError): particle = particle_track.Particles(params)
def test_exact_locations(): num_ps = 2 particle = particle_track.Particles(params) particle.generate_particles(num_ps, seed_xloc, seed_yloc, method='exact') all_walk_data = particle.run_iteration() assert all_walk_data['xinds'][0] == seed_xloc assert all_walk_data['yinds'][0] == seed_yloc
def test_big_diffcoeff(): params.diff_coeff = 3.0 with pytest.warns( UserWarning, match="Diffusion behaves non-physically when coefficient >= 2"): particle = particle_track.Particles(params) assert particle.diff_coeff == 3.0
def test_invalid_seedtime(self): particle = particle_track.Particles(goodparams) with pytest.raises(TypeError): particle.generate_particles(1, seed_xloc, seed_yloc, seed_time='invalid')
def test_get_weight_deep(): ''' Test for function get_weight within Tools class ''' tools = pt.modelParams() # define a bunch of expected values tools.stage = np.ones((5, 5)) tools.cell_type = np.zeros_like(tools.stage) tools.qy = tools.stage.copy() tools.qx = np.zeros((5, 5)) tools.ivec = ivec tools.jvec = jvec tools.distances = distances * np.nan tools.dry_depth = 0.1 tools.gamma = 0.02 tools.theta = 1 tools.steepest_descent = True tools.depth = tools.stage.copy() tools.depth[2, 2] = 10.0 # define index 8 as the deepest tools.seed_xloc = [1] tools.seed_yloc = [1] tools.Np_tracer = 1 tools.dx = 1 # define particles particles = pt.Particles(tools) # set the current index ind = (1, 1) # set seed np.random.seed(0) # make assertion assert lw.get_weight(particles, ind) == 8
def test_manual_reset(): # test resetting walk data particle = particle_track.Particles(params) particle.generate_particles(Np_tracer, seed_xloc, seed_yloc) assert particle.walk_data['xinds'][0][0] == 1 particle.clear_walk_data() assert (particle.walk_data is None) is True
def test_model_uv_broken(self): params = particle_track.modelParams() params.dx = 1 params.depth = np.ones((3,3)) params.topography = np.zeros((3,3)) params.u = np.ones((3,3)) with pytest.raises(ValueError): particle = particle_track.Particles(params)
def test_exact_locations_overflow(): """Test where number of particles exceeds number of seed locations.""" num_ps = 3 particle = particle_track.Particles(params) particle.generate_particles(num_ps, [0, 1], [0, 1], method='exact') all_walk_data = particle.run_iteration() assert len(all_walk_data['xinds']) == num_ps assert len(all_walk_data['yinds']) == num_ps
def test_missing_stage(self): params = particle_track.modelParams() params.dx = 1 params.depth = np.ones((3,3)) params.u = np.ones((3,3)) params.v = np.ones((3,3)) with pytest.raises(ValueError): particle = particle_track.Particles(params)
def test_neg_diffcoeff(): params.diff_coeff = -1.0 with pytest.warns( UserWarning, match= "Specified diffusion coefficient is negative. Rounding up to zero" ): particle = particle_track.Particles(params) assert particle.diff_coeff == 0.0
def test_exact_locations_multidims(): """Test case where seed locs given as multi-dimensional arrays.""" num_ps = 5 particle = particle_track.Particles(params) x_seed = np.array(((0, 0), (1, 1))) y_seed = np.array(((0, 0), (1, 1))) particle.generate_particles(num_ps, x_seed, y_seed, method='exact') all_walk_data = particle.run_iteration() assert len(all_walk_data['xinds']) == num_ps assert len(all_walk_data['yinds']) == num_ps
def test_travel_time(): # Particle doesn't travel in the 3x3 space due to the 'sticky' edge # conditions so check that travel time is 0 and particle hasn't moved particle = particle_track.Particles(params) np.random.seed(0) particle.generate_particles(Np_tracer, seed_xloc, seed_yloc) all_walk_data = particle.run_iteration() assert all_walk_data['xinds'][0][0] == 1 assert all_walk_data['yinds'][0][0] == 1 assert all_walk_data['travel_times'][0][0] == 0.0
def test_depth_via_stage_topo(self): params = particle_track.modelParams() params.dx = 1 params.stage = np.ones((3,3)) params.topography = np.zeros((3,3)) params.u = np.ones((3,3)) params.v = np.ones((3,3)) particle = particle_track.Particles(params) # should work so make assertion assert np.all(particle.depth == params.stage-params.topography)
def test_seed_time_float(): num_ps = 2 particle = particle_track.Particles(params) with pytest.warns(UserWarning, match="Particle seed time is nonzero, be aware when post-processing."): particle.generate_particles(num_ps, seed_xloc, seed_yloc, seed_time=11.1, method='exact') all_walk_data = particle.run_iteration() assert all_walk_data['xinds'][0] == seed_xloc assert all_walk_data['yinds'][0] == seed_yloc assert all_walk_data['travel_times'][0] == [11.1] assert all_walk_data['travel_times'][1] == [11.1]
def test_stage_via_depth_topo(self): params = particle_track.modelParams() params.dx = 1 params.topography = np.zeros((3,3)) params.depth = np.ones((3,3)) params.depth[0,0] = 0 params.u = np.ones((3,3)) params.v = np.ones((3,3)) particle = particle_track.Particles(params) # should work so make assertion assert np.isnan(particle.stage[0,0]) == True assert particle.stage[1,1] == 1.
def test_previous_walk_data(): # test of loading previously defined walk data particle = particle_track.Particles(params) np.random.seed(0) particle.generate_particles(Np_tracer, seed_xloc, seed_yloc) old_walk_data = particle.run_iteration() # try to do another walk - test just makes sure code doesn't break np.random.seed(0) all_walk_data = particle.run_iteration() assert all_walk_data['xinds'][0][0] == 1 assert all_walk_data['yinds'][0][0] == 1 assert all_walk_data['travel_times'][0][0] == 0.0
def test_model_q(self): params = particle_track.modelParams() params.dx = 1 params.depth = np.ones((3,3)) params.topography = np.zeros((3,3)) params.qx = np.ones((3,3)) params.qy = np.ones((3,3)) particle = particle_track.Particles(params) # should work so make assertion assert np.all(particle.v == particle.qy*particle.depth/(particle.depth**2+1e-8)) assert np.all(particle.u == particle.qx*particle.depth/(particle.depth**2+1e-8)) assert np.all(particle.qx == -1*params.qy) assert np.all(particle.qy == params.qx)
def test_get_weight(): ''' Test for function get_weight ''' # set the current index ind = (1, 1) # set seed np.random.seed(0) # define particles class params.depth = params.stage.copy() particles = pt.Particles(params) # make assertion assert lw.get_weight(particles, ind) == 5
def test_rcm_model_uv(self): params = particle_track.modelParams() params.dx = 1 params.depth = np.ones((3,3)) params.topography = np.zeros((3,3)) params.u = np.ones((3,3)) params.v = np.ones((3,3)) params.model = 'DeltaRCM' particle = particle_track.Particles(params) # should work so make assertion assert np.all(particle.v == params.v) assert np.all(particle.u == params.u) assert np.all(particle.qx == params.u*params.depth) assert np.all(particle.qy == params.v*params.depth)
def test_generate_twice(): # test ability to generate particles multiple times particle = particle_track.Particles(params) np.random.seed(0) particle.generate_particles(Np_tracer, seed_xloc, seed_yloc) particle.generate_particles(Np_tracer, seed_xloc, seed_yloc) old_walk_data = particle.run_iteration() # try to do another walk - test just makes sure code doesn't break np.random.seed(0) all_walk_data = particle.run_iteration() assert all_walk_data['xinds'][0][0] == 1 assert all_walk_data['yinds'][0][0] == 1 assert all_walk_data['travel_times'][0][0] == 0.0 assert len(all_walk_data['xinds']) == 2
def test_no_explicit_generation(): """Test reading of Np_tracer from self if some walk_data exists.""" # create some walk data (would exist from a previous run) walk_data = {'xinds': [[1], [1]], 'yinds': [[1], [1]], 'travel_times': [[0.0], [0.0]]} # init particle class and stick walk data in it # init defines Np_tracer as 0, but it is 2 in walk_data particle = particle_track.Particles(params) particle.walk_data = walk_data # assert that Np_tracer is 0 assert particle.Np_tracer == 0 # run an iteration and see if Np_tracer is corrected particle.run_iteration() # assert that number of particles has been correctly identified assert particle.Np_tracer == 2
def test_get_weight_norm(): ''' Test for function get_weight within Tools class ''' # define particles params.qy = np.ones_like(params.stage) params.qx = np.zeros_like(params.stage) params.stage[1, 1] = 100.0 particles = pt.Particles(params) # set the current index ind = (1, 1) # set seed np.random.seed(0) # make assertion assert lw.get_weight(particles, ind) == 5
def test_calc_travel_times(): ''' Test for function calc_travel_times within Tools class ''' # define particles params.diff_coeff = 0 params.depth = np.ones_like(params.stage) params.qx = np.zeros_like(params.stage) params.qy = np.ones_like(params.stage) particle = pt.Particles(params) # define old ind old_ind = [1, 1] # define new ind new_ind = [0, 1] # hardset angles/velocities to make computation more obvious particle.velocity = np.ones_like(particle.velocity) particle.velocity[0, 0:2] = 3 particle.velocity_angle = np.ones_like(particle.velocity_angle) # get time trav_time = lw.calc_travel_times(particle, 1, old_ind, new_ind, 1) # expect to return the value 0.5 (inverse of the avg velocity 2) assert trav_time == pytest.approx(0.5609806565385976)
def test_steep_true(): params.diff_coeff = 'invalid' params.steepest_descent = True particle = particle_track.Particles(params) assert particle.steepest_descent == True
def test_no_initialized_particles(self): particle = particle_track.Particles(goodparams) with pytest.raises(ValueError): particle.run_iteration()
def test_bad_method(self): particle = particle_track.Particles(goodparams) with pytest.raises(ValueError): particle.generate_particles(1, seed_xloc, seed_yloc, method='bad')
def test_invalid_method(self): particle = particle_track.Particles(goodparams) with pytest.raises(TypeError): particle.generate_particles(1, seed_xloc, seed_yloc, method=5)
def test_bad_seed_xloc(self): particle = particle_track.Particles(goodparams) with pytest.raises(TypeError): particle.generate_particles(1, 'invalid', seed_yloc)