Ejemplo n.º 1
0
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")
Ejemplo n.º 2
0
def test_two_class_writers(clock_08, almost_default_grid):
    ncnblh = NotCoreNodeBaselevelHandler(almost_default_grid,
                                         modify_core_nodes=True,
                                         lowering_rate=-1)
    # construct and run model
    model = Basic(
        clock_08,
        almost_default_grid,
        save_first_timestep=False,
        water_erodibility=0.0,
        regolith_transport_parameter=0.0,
        boundary_handlers={"NotCoreNodeBaselevelHandler": ncnblh},
        output_writers={
            "class": [output_writer_class_a, output_writer_class_b]
        },
    )
    model.run()

    # assert things were done correctly
    truth_file = os.path.join(_TEST_DATA_DIR, "truth_ow_class_a.20.0.txt")
    test_file = get_output_filepath("ow_class_a.20.0.txt")
    assert filecmp(test_file, truth_file) is True

    truth_file = os.path.join(_TEST_DATA_DIR, "truth_ow_class_b.20.0.txt")
    test_file = get_output_filepath("ow_class_b.20.0.txt")
    assert filecmp(test_file, truth_file) is True

    model.remove_output_netcdfs()
    cleanup_files("ow_class_*.txt")
Ejemplo n.º 3
0
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")
Ejemplo n.º 4
0
def test_multiple_frequencies(clock_08, almost_default_grid):
    ncnblh = NotCoreNodeBaselevelHandler(almost_default_grid,
                                         modify_core_nodes=True,
                                         lowering_rate=-1)

    # construct and run model
    common_interval = 2.0
    uncommon_interval = 5.0
    model = Basic(
        clock_08,
        almost_default_grid,
        water_erodibility=0.0,
        regolith_transport_parameter=0.0,
        boundary_handlers={"NotCoreNodeBaselevelHandler": ncnblh},
        output_writers={
            "common-ow": {
                "class": OWStaticWrapper,
                "kwargs": {
                    "add_id": False,
                    "intervals": common_interval,
                },
            },
            "uncommon-ow": {
                "class": OWStaticWrapper,
                "kwargs": {
                    "add_id": False,
                    "intervals": uncommon_interval,
                },
            },
        },
        # output_interval=6.0,
        output_dir=_TEST_OUTPUT_DIR,
        output_prefix="",
        save_first_timestep=True,
        save_last_timestep=True,
    )
    model.run()

    for name, interval in [
        ("common-ow", common_interval),
        ("uncommon-ow", uncommon_interval),
    ]:
        for output_time in itertools.count(0.0, interval):
            if output_time > clock_08.stop:
                # Break the infinite iterator at the clock stop time
                break
            # assert things were done correctly
            filename = f"{name}_time-{output_time:012.1f}.txt"
            truth_file = os.path.join(_TEST_DATA_DIR, f"truth_{filename}")
            test_file = get_output_filepath(filename)
            assert filecmp(test_file, truth_file) is True

    model.remove_output()
Ejemplo n.º 5
0
def test_out_of_phase_interval_last(clock_08, almost_default_grid):
    ncnblh = NotCoreNodeBaselevelHandler(almost_default_grid,
                                         modify_core_nodes=True,
                                         lowering_rate=-1)

    # construct and run model
    interval = 19.5
    warning_msg_sample = "time that is not divisible by the model step"
    with pytest.warns(UserWarning, match=warning_msg_sample):
        model = Basic(
            clock_08,
            almost_default_grid,
            water_erodibility=0.0,
            regolith_transport_parameter=0.0,
            boundary_handlers={"NotCoreNodeBaselevelHandler": ncnblh},
            output_writers={
                "out-of-phase-ow": {
                    "class": OWStaticWrapper,
                    "kwargs": {
                        "add_id": False,
                        "intervals": interval,
                    },
                },
            },
            # output_interval=6.0,
            output_dir=_TEST_OUTPUT_DIR,
            output_prefix="",
            save_first_timestep=True,
            save_last_timestep=True,
        )
        model.run()

    # The model should still run fine, but any out of phase output times are
    # delayed to the next step.

    # Check that there is no file for t=19.5
    bad_filename = f"out-of-phase-ow_time-{19.5:012.1f}.txt"
    bad_filepath = get_output_filepath(bad_filename)
    assert not os.path.isfile(bad_filepath), f"{bad_filepath} should not exist"

    # Check that the output that should exist
    for time_int in [0, 20]:  # instead of [0, 19.5, 20]
        filename = f"out-of-phase-ow_time-{float(time_int):012.1f}.txt"
        truth_file = os.path.join(_TEST_DATA_DIR, f"truth_{filename}")
        test_file = get_output_filepath(filename)
        assert filecmp(test_file, truth_file) is True

    model.remove_output()
Ejemplo n.º 6
0
def test_save_first_last_and_multiple_times(clock_08, almost_default_grid):
    """Test save_first_timestep, save_last_timestep, and saving at multiple
    timesteps."""
    ncnblh = NotCoreNodeBaselevelHandler(almost_default_grid,
                                         modify_core_nodes=True,
                                         lowering_rate=-1)

    # construct and run model
    model = Basic(
        clock_08,
        almost_default_grid,
        water_erodibility=0.0,
        regolith_transport_parameter=0.0,
        boundary_handlers={"NotCoreNodeBaselevelHandler": ncnblh},
        output_writers={
            "function": [output_writer_function_a, output_writer_function_b],
            "class": [output_writer_class_a, output_writer_class_b],
        },
        output_interval=6.0,
        output_dir=_TEST_OUTPUT_DIR,
        save_first_timestep=True,
        save_last_timestep=True,
    )
    model.run()

    for t in ["0.0", "6.0", "12.0", "18.0", "20.0"]:
        # assert things were done correctly
        filename_bases = [
            f"ow_func_a.{t}.txt",
            f"ow_func_b.{t}.txt",
            f"ow_class_a.{t}.txt",
            f"ow_class_b.{t}.txt",
        ]
        for filename_base in filename_bases:
            truth_file = os.path.join(_TEST_DATA_DIR, f"truth_{filename_base}")
            test_file = os.path.join(os.curdir, "output", filename_base)
            assert filecmp(test_file, truth_file) is True

    model.remove_output_netcdfs()
    cleanup_files(get_output_filepath("ow_func_*.txt"))
    cleanup_files(get_output_filepath("ow_class_*.txt"))
Ejemplo n.º 7
0
def test_custom_iter(clock_08, almost_default_grid):
    ncnblh = NotCoreNodeBaselevelHandler(almost_default_grid,
                                         modify_core_nodes=True,
                                         lowering_rate=-1)

    # construct and run model
    model = Basic(
        clock_08,
        almost_default_grid,
        water_erodibility=0.0,
        regolith_transport_parameter=0.0,
        boundary_handlers={"NotCoreNodeBaselevelHandler": ncnblh},
        output_writers={
            "fibonnaci": {
                "class": OWGenericWrapper,
                "kwargs": {
                    "add_id": False,
                    "times_iter": fibonnaci(),
                },
            },
        },
        # output_interval=6.0,
        output_dir=_TEST_OUTPUT_DIR,
        output_prefix="",
        save_first_timestep=True,
        save_last_timestep=True,
    )
    with pytest.warns(OutputIteratorSkipWarning):
        model.run()

    for time_int in [0, 1, 2, 3, 5, 8, 13, 20]:
        # Note: the second 1 in the fib sequence will be skipped

        # assert things were done correctly
        filename = f"fibonnaci_time-{float(time_int):012.1f}.txt"
        truth_file = os.path.join(_TEST_DATA_DIR, f"truth_{filename}")
        test_file = get_output_filepath(filename)
        assert filecmp(test_file, truth_file) is True

    model.remove_output()