def test_finalize_opt_duration_stochastic_true(clock_07, grid_1): params = { "grid": grid_1, "opt_stochastic_duration": True, "clock": clock_07, "record_rain": True, "m_sp": 0.5, "n_sp": 1.0, "water_erodibility": 0.01, "regolith_transport_parameter": 0.1, "infiltration_capacity": 0.0, "mean_storm_duration": 2.0, "mean_interstorm_duration": 3.0, "mean_storm_depth": 1.0, "random_seed": 1234, } model = BasicSt(**params) model.reset_random_seed() model.run_for(model.clock.step, model.clock.stop) model.finalize() # assert that these are correct truth_file = os.path.join(_TEST_DATA_DIR, "opt_dur_true_storm_sequence.txt") assert filecmp("storm_sequence.txt", truth_file) is True os.remove("storm_sequence.txt")
def test_finalize_opt_duration_stochastic_false(clock_07, grid_1): params = { "grid": grid_1, "opt_stochastic_duration": False, "clock": clock_07, "record_rain": True, "m_sp": 0.5, "n_sp": 1.0, "water_erodibility": 0.01, "regolith_transport_parameter": 0.1, "infiltration_capacity": 0.0, "rainfall__mean_rate": 1.0, "rainfall_intermittency_factor": 0.1, "rainfall__shape_factor": 0.6, "number_of_sub_time_steps": 1, "random_seed": 1234, } model = BasicSt(**params) model.reset_random_seed() model.run_for(model.clock.step, model.clock.stop) model.finalize() # assert that these are correct truth_file = os.path.join(_TEST_DATA_DIR, "opt_dur_false_storm_sequence.txt") assert filecmp("storm_sequence.txt", truth_file) is True truth_file = os.path.join(_TEST_DATA_DIR, "opt_dur_false_exceedance_summary.txt") assert filecmp("exceedance_summary.txt", truth_file) is True os.remove("storm_sequence.txt") os.remove("exceedance_summary.txt")
def test_finalize_opt_duration_stochastic_false_too_short(clock_05, grid_1): params = { "grid": grid_1, "opt_stochastic_duration": False, "clock": clock_05, "record_rain": True, "m_sp": 0.5, "n_sp": 1.0, "water_erodibility": 0.01, "regolith_transport_parameter": 0.1, "infiltration_capacity": 0.0, "rainfall__mean_rate": 1.0, "rainfall_intermittency_factor": 0.1, "rainfall__shape_factor": 0.6, "number_of_sub_time_steps": 1, "random_seed": 1234, } model = BasicSt(**params) model.reset_random_seed() model.run_for(model.clock.step, model.clock.stop) with pytest.raises(RuntimeError): model.finalize() os.remove("storm_sequence.txt")
def test_not_specifying_record_rain(clock_05, grid_1): params = { "grid": grid_1, "opt_stochastic_duration": False, "clock": clock_05, "record_rain": False, "m_sp": 0.5, "n_sp": 1.0, "water_erodibility": 0.01, "regolith_transport_parameter": 0.1, "infiltration_capacity": 0.0, "rainfall__mean_rate": 1.0, "rainfall_intermittency_factor": 0.1, "rainfall__shape_factor": 0.6, "number_of_sub_time_steps": 1, "random_seed": 1234, } model = BasicSt(**params) model.reset_random_seed() model.run_for(model.clock.step, model.clock.stop) with pytest.raises(ValueError): model.write_storm_sequence_to_file() with pytest.raises(ValueError): model.write_exceedance_frequency_file()
def test_run_stochastic_opt_true(clock_04, grid_0): params = { "grid": grid_0, "opt_stochastic_duration": True, "clock": clock_04, "record_rain": True, "m_sp": 0.5, "n_sp": 1.0, "water_erodibility": 0.01, "regolith_transport_parameter": 0.1, "infiltration_capacity": 0.0, "mean_storm_duration": 2.0, "mean_interstorm_duration": 3.0, "mean_storm_depth": 1.0, "random_seed": 1234, } model = BasicSt(**params) assert model.opt_stochastic_duration is True model.run_for(model.clock.step, 400 * model.clock.step) rainfall_rate = np.asarray( model.rain_record["rainfall_rate"]).round(decimals=5) event_duration = np.asarray( model.rain_record["event_duration"]).round(decimals=5) dry_times = event_duration[rainfall_rate == 0] wet_times = event_duration[rainfall_rate > 0] np.testing.assert_almost_equal( np.round(np.mean(dry_times), decimals=1), params["mean_interstorm_duration"], decimal=1, ) np.testing.assert_almost_equal( np.round(np.mean(wet_times), decimals=1), params["mean_storm_duration"], decimal=1, ) avg_storm_depth = np.sum((rainfall_rate * event_duration)) / len(wet_times) np.testing.assert_array_almost_equal(avg_storm_depth, params["mean_storm_depth"], decimal=1)
def test_run_stochastic_opt_false(clock_05, grid_1): params = { "grid": grid_1, "opt_stochastic_duration": False, "clock": clock_05, "record_rain": True, "m_sp": 0.5, "n_sp": 1.0, "water_erodibility": 0.01, "regolith_transport_parameter": 0.1, "infiltration_capacity": 0.0, "rainfall__mean_rate": 1.0, "rainfall_intermittency_factor": 0.1, "rainfall__shape_factor": 0.6, "number_of_sub_time_steps": 1, "random_seed": 1234, } model = BasicSt(**params) assert model.opt_stochastic_duration is False model.run_for(model.clock.step, 10000.0) rainfall_rate = np.asarray(model.rain_record["rainfall_rate"]) event_duration = np.asarray(model.rain_record["event_duration"]) dry_times = event_duration[rainfall_rate == 0] wet_times = event_duration[rainfall_rate > 0] assert (np.array_equiv( dry_times, model.clock.step * (1.0 - params["rainfall_intermittency_factor"]), ) is True) assert (np.array_equiv( wet_times, model.clock.step * (params["rainfall_intermittency_factor"]), ) is True) avg_storm_depth = np.sum((rainfall_rate * event_duration)) / len(wet_times) np.testing.assert_array_almost_equal(avg_storm_depth, params["rainfall__mean_rate"], decimal=1)
def test_run_opt_false_with_changer(clock_06, grid_1, precip_defaults): precip_changer = PrecipChanger(grid_1, **precip_defaults) params = { "grid": grid_1, "opt_stochastic_duration": False, "clock": clock_06, "record_rain": True, "m_sp": 0.5, "n_sp": 1.0, "water_erodibility": 0.01, "regolith_transport_parameter": 0.1, "infiltration_capacity": 0.0, "rainfall__mean_rate": 1.0, "rainfall_intermittency_factor": 0.5, "rainfall__shape_factor": 0.65, "number_of_sub_time_steps": 1, "random_seed": 1234, "boundary_handlers": { "PrecipChanger": precip_changer }, } model = BasicSt(**params) model.reset_random_seed() model.run_for(model.clock.step, model.clock.stop) assert "PrecipChanger" in model.boundary_handlers predicted_intermittency = params[ "rainfall_intermittency_factor"] + precip_defaults[ "daily_rainfall__intermittency_factor_time_rate_of_change"] * ( model.clock.stop - model.clock.step) predicted_intensity = params["rainfall__mean_rate"] + precip_defaults[ "rainfall__mean_rate_time_rate_of_change"] * (model.clock.stop - model.clock.step) assert model.rainfall_intermittency_factor == predicted_intermittency assert model.rainfall__mean_rate == predicted_intensity