def test_calc_cumulative_erosion(clock_simple, grid_1): em = ErosionModel(grid=grid_1, clock=clock_simple) assert np.array_equiv(em.z, 0.0) is True em.z += 1.0 em.calculate_cumulative_change() assert (np.array_equiv(em.grid.at_node["cumulative_elevation_change"], 1.0) is True)
def test_bad_boundary_condition_yaml(bad_handler_yaml, tmpdir): with tmpdir.as_cwd(): with open("params.yaml", "w") as fp: fp.write(bad_handler_yaml) with pytest.raises(ValueError): ErosionModel.from_file("./params.yaml")
def test_parameters(clock_simple): params = { "grid": { "HexModelGrid": [ {"base_num_rows": 8, "base_num_cols": 5, "dx": 10}, { "fields": { "node": { "topographic__elevation": { "constant": [{"value": 0}] } } } }, ] }, "clock": {"step": 1, "stop": 10}, "output_interval": 2, } em = ErosionModel.from_dict(params) assert isinstance(em.grid, HexModelGrid) assert em.grid.number_of_nodes == 56 for field in at_node_fields: assert field in em.grid.at_node assert isinstance(em.flow_accumulator, FlowAccumulator) is True assert em.flow_accumulator.flow_director._name == "FlowDirectorSteepest" assert em.boundary_handlers == {} assert em.output_writers == {} assert em.save_first_timestep is True assert em._out_file_name == "terrainbento_output" assert em._model_time == 0.0
def test_bad_runoff_instance(clock_simple, grid_1): not_a_runoff_generator = "I am not a runoff_generator" with pytest.raises(ValueError): ErosionModel( grid=grid_1, clock=clock_simple, runoff_generator=not_a_runoff_generator, )
def test_bad_boundary_condition_string(clock_simple, almost_default_grid, keyword): params = { "grid": almost_default_grid, "clock": clock_simple, "boundary_handlers": { keyword: BasicSt }, } with pytest.raises(ValueError): ErosionModel(**params)
def test_input_file(tmpdir, inputs_yaml): with tmpdir.as_cwd(): with open("params.yaml", "w") as fp: fp.write(inputs_yaml) em = ErosionModel.from_file("./params.yaml") assert isinstance(em.grid, HexModelGrid) assert em.grid.number_of_nodes == 56 for field in at_node_fields: assert field in em.grid.at_node assert isinstance(em.flow_accumulator, FlowAccumulator) is True assert em.flow_accumulator.flow_director._name == "FlowDirectorSteepest" assert em.boundary_handlers == {} assert em.output_writers == {} assert em.save_first_timestep is True assert em._out_file_name == "terrainbento_output" assert em._model_time == 0.0
def test_string_D8(tmpdir, inputs_D8_yaml): with tmpdir.as_cwd(): with open("params.yaml", "w") as fp: fp.write(inputs_D8_yaml) with open("./params.yaml", "r") as f: contents = f.read() em = ErosionModel.from_file(contents) assert isinstance(em.grid, RasterModelGrid) assert em.grid.number_of_nodes == 20 for field in at_node_fields: assert field in em.grid.at_node assert isinstance(em.flow_accumulator, FlowAccumulator) is True assert em.flow_accumulator.flow_director._name == "FlowDirectorD8" assert em.boundary_handlers == {} assert em.all_output_writers == [] assert em.save_first_timestep is True assert em.output_prefix == "terrainbento-output" assert em._model_time == 0.0
def test_parameters(clock_simple): params = { "grid": { "HexModelGrid": [ { "shape": (8, 5), "spacing": 10 }, { "fields": { "node": { "topographic__elevation": { "constant": [{ "value": 0.0 }] } } } }, ] }, "clock": { "step": 1, "stop": 10 }, "output_interval": 2, "output_default_netcdf": False, } em = ErosionModel.from_dict(params) assert isinstance(em.grid, HexModelGrid) assert em.grid.number_of_nodes == 56 for field in at_node_fields: assert field in em.grid.at_node assert isinstance(em.flow_accumulator, FlowAccumulator) is True assert em.flow_accumulator.flow_director._name == "FlowDirectorSteepest" assert em.boundary_handlers == {} assert em.all_output_writers == [] assert em.save_first_timestep is True assert em.output_prefix == "terrainbento-output" assert em._model_time == 0.0
def test_no_grid_in_file(tmpdir, basic_inputs_no_grid_yaml): with tmpdir.as_cwd(): with open("params.yaml", "w") as fp: fp.write(basic_inputs_no_grid_yaml) with pytest.raises(ValueError): ErosionModel.from_file("./params.yaml")
def test_no_grid(clock_simple): with pytest.raises(ValueError): ErosionModel(grid="eggs", clock=clock_simple)
def test_no_clock(simple_square_grid): with pytest.raises(ValueError): ErosionModel(clock="spam", grid=simple_square_grid)
def test_not_correct_fields(clock_simple): grid = RasterModelGrid((3, 21)) with pytest.raises(ValueError): ErosionModel(clock=clock_simple, grid=grid)
def test_bad_precipitator_params(tmpdir, basic_inputs_bad_precipitator_yaml): with tmpdir.as_cwd(): with open("params.yaml", "w") as fp: fp.write(basic_inputs_bad_precipitator_yaml) with pytest.raises(ValueError): ErosionModel.from_file("./params.yaml")
def test_bad_precipitator_instance(clock_simple, grid_1): not_a_precipitator = "I am not a precipitator" with pytest.raises(ValueError): ErosionModel(grid=grid_1, clock=clock_simple, precipitator=not_a_precipitator)