Example #1
0
def test_get_grid_face_nodes(flopy_disu, modflow_lib_path):
    assert True
    return
    # todo: fix this test
    """Tests if the grid_face_nodes can be extracted"""
    mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_disu.sim_path)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)

    try:
        # Initialize
        mf6.initialize()

        # First 5 prescribed elements
        prescribed_grid_face_nodes = np.array([1, 2, 6, 5, 1])

        # Getting the grid id from the model, requires specifying one variable
        k11_tag = mf6.get_var_address("K11", flopy_disu.model_name, "NPF")
        grid_id = mf6.get_var_grid(k11_tag)
        grid_face_count = mf6.get_grid_face_count(grid_id)
        grid_nodes_per_face = np.empty(
            shape=(grid_face_count,), dtype=np.int32, order="F"
        )
        mf6.get_grid_nodes_per_face(grid_id, grid_nodes_per_face)
        face_nodes_count = np.sum(grid_nodes_per_face + 1)

        actual_grid_face_nodes = np.empty(
            shape=(face_nodes_count,), dtype=np.int32, order="F"
        )
        mf6.get_grid_face_nodes(grid_id, actual_grid_face_nodes)

        assert np.array_equal(prescribed_grid_face_nodes, actual_grid_face_nodes[:5])
    finally:
        mf6.finalize()
Example #2
0
def test_get_grid_rank(flopy_dis, modflow_lib_path):
    """Tests if the the grid rank can be extracted"""

    mf6 = XmiWrapper(lib_path=modflow_lib_path,
                     working_directory=flopy_dis.sim_path)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)

    try:
        mf6.initialize()

        if flopy_dis.nlay == 1:
            prescribed_grid_rank = 2
        else:
            prescribed_grid_rank = 3

        # Getting the grid id from the model, requires specifying one variable
        k11_tag = mf6.get_var_address("K11", flopy_dis.model_name, "NPF")
        grid_id = mf6.get_var_grid(k11_tag)

        actual_grid_rank = mf6.get_grid_rank(grid_id)

        assert prescribed_grid_rank == actual_grid_rank
    finally:
        mf6.finalize()
Example #3
0
def test_get_current_time(flopy_dis, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path,
                     working_directory=flopy_dis.sim_path)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)

    try:
        # Initialize
        mf6.initialize()

        # Advance model by single time step
        mf6.update()

        # prescribed_start_time for modflow models is always 0
        start_time = 0.0

        perlen, nstp, tsmult = flopy_dis.tdis_rc[0]

        if math.isclose(tsmult, 1):
            prescribed_current_time = start_time + perlen / nstp
        else:
            prescribed_current_time = start_time + perlen * (tsmult - 1.0) / (
                tsmult**nstp - 1.0)

        actual_current_time = mf6.get_current_time()

        assert math.isclose(prescribed_current_time, actual_current_time)
    finally:
        mf6.finalize()
Example #4
0
def test_solve_default_solution_id(flopy_dis, modflow_lib_path):
    """Should no longer be needed to put in the solution id,
    when there is only one (or you want to use the first one
    in the sequence"""
    mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)

    try:
        # Initialize
        mf6.initialize()

        #
        mf6.prepare_solve()

        # Get max iteration
        mxit_tag = mf6.get_var_address("MXITER", "SLN_1")
        max_iter_arr = mf6.get_value_ptr(mxit_tag)
        max_iter = max_iter_arr[0]

        kiter = 0
        while kiter < max_iter:
            has_converged = mf6.solve()
            kiter += 1

            if has_converged:
                break

        assert has_converged

        mf6.finalize_solve()
    finally:
        mf6.finalize()
Example #5
0
def test_solve(flopy_dis, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)

    try:
        # Initialize
        mf6.initialize()

        # Prepare solve
        sol_id = 1
        mf6.prepare_solve(sol_id)

        # Get max iteration
        mxit_tag = mf6.get_var_address("MXITER", "SLN_1")
        max_iter_arr = mf6.get_value_ptr(mxit_tag)
        max_iter = max_iter_arr[0]

        kiter = 0
        while kiter < max_iter:
            has_converged = mf6.solve(sol_id)
            kiter += 1

            if has_converged:
                break

        assert has_converged
    finally:
        mf6.finalize()
Example #6
0
def test_get_value_ptr_modelname(flopy_dis, modflow_lib_path):
    """`flopy_dis` sets constant head values.
    This test checks if these can be properly extracted with origin=modelname."""

    mf6 = XmiWrapper(lib_path=modflow_lib_path,
                     working_directory=flopy_dis.sim_path)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)

    try:
        # Initialize
        mf6.initialize()

        stress_period_data = flopy_dis.stress_period_data
        ncol = flopy_dis.ncol

        mf6.update()
        head_tag = mf6.get_var_address("X", flopy_dis.model_name)
        actual_head = mf6.get_value_ptr(head_tag)

        for cell_id, presciped_head in stress_period_data:
            layer, row, column = cell_id
            head_index = column + row * ncol
            assert math.isclose(presciped_head, actual_head[head_index])
    finally:
        mf6.finalize()
Example #7
0
def test_finalize_without_initialize(flopy_dis, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path,
                     working_directory=flopy_dis.sim_path)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)

    # Test if finalize fails, if initialize was not called yet
    with pytest.raises(InputError):
        mf6.finalize()
Example #8
0
def test_get_output_item_count(flopy_dis, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path,
                     working_directory=flopy_dis.sim_path)

    try:
        # Initialize
        mf6.initialize()
        assert mf6.get_output_item_count() > 0

    finally:
        mf6.finalize()
Example #9
0
def test_initialize(flopy_dis, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path,
                     working_directory=flopy_dis.sim_path)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)

    try:
        # Run initialize
        mf6.initialize()
    finally:
        mf6.finalize()
Example #10
0
def test_get_input_var_names(flopy_dis, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path,
                     working_directory=flopy_dis.sim_path)

    try:
        # Initialize
        mf6.initialize()

        var_names = mf6.get_input_var_names()
        assert "TEST_MODEL_DIS/X" in var_names

    finally:
        mf6.finalize()
Example #11
0
def test_prepare_time_step(flopy_dis, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)

    try:
        # Initialize
        mf6.initialize()

        dt = mf6.get_time_step()
        mf6.prepare_time_step(dt)
    finally:
        mf6.finalize()
Example #12
0
def test_get_output_var_names(flopy_dis, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path,
                     working_directory=flopy_dis.sim_path)

    try:
        # Initialize
        mf6.initialize()

        var_names = mf6.get_output_var_names()
        assert "TEST_MODEL_DIS/X" in var_names  # this is readwrite
        assert "SLN_1/IA" in var_names  # and this is readonly

    finally:
        mf6.finalize()
Example #13
0
def test_prepare_solve(flopy_dis, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)

    try:
        # Initialize
        mf6.initialize()

        # Prepare solve
        sol_id = 1
        mf6.prepare_solve(sol_id)
    finally:
        mf6.finalize()
Example #14
0
def test_update(flopy_dis, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path,
                     working_directory=flopy_dis.sim_path)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)

    try:
        # Initialize
        mf6.initialize()

        # Advance model by single time step
        mf6.update()
    finally:
        mf6.finalize()
Example #15
0
def test_double_initialize(flopy_dis, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path,
                     working_directory=flopy_dis.sim_path)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)
    try:
        # Run initialize
        mf6.initialize()

        # Test if initialize fails, if initialize was called a second time
        with pytest.raises(InputError):
            mf6.initialize()
    finally:
        mf6.finalize()
Example #16
0
def test_get_var_type_double(flopy_dis, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path,
                     working_directory=flopy_dis.sim_path)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)

    try:
        # Initialize
        mf6.initialize()

        head_tag = mf6.get_var_address("X", "SLN_1")
        var_type = mf6.get_var_type(head_tag)
        assert var_type == "DOUBLE (90)"
    finally:
        mf6.finalize()
Example #17
0
def test_get_subcomponent_count(flopy_dis, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)

    try:
        # Initialize
        mf6.initialize()

        n_solutions = mf6.get_subcomponent_count()

        # Modflow 6 BMI does only support the use of a single solution groups
        assert n_solutions == 1
    finally:
        mf6.finalize()
Example #18
0
def test_deactivated_timing(flopy_dis, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path,
                     working_directory=flopy_dis.sim_path,
                     timing=False)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)

    try:
        # Run initialize
        mf6.initialize()

        with pytest.raises(TimerError):
            mf6.report_timing_totals()
    finally:
        mf6.finalize()
Example #19
0
def test_get_var_type_int(flopy_dis, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path,
                     working_directory=flopy_dis.sim_path)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)

    try:
        # Initialize
        mf6.initialize()

        iactive_tag = mf6.get_var_address("IACTIVE", "SLN_1")
        var_type = mf6.get_var_type(iactive_tag)
        assert var_type == "INTEGER (90)"
    finally:
        mf6.finalize()
Example #20
0
def test_get_value_int(flopy_dis_idomain, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path,
                     working_directory=flopy_dis_idomain.sim_path)

    try:
        mf6.initialize()

        nodes_reduced_tag = next(var for var in mf6.get_output_var_names()
                                 if var.endswith("/NODEREDUCED"))
        tgt_arr = mf6.get_value(nodes_reduced_tag)

        # compare to array in MODFLOW memory:
        orig_arr = mf6.get_value_ptr(nodes_reduced_tag)
        assert np.array_equal(tgt_arr, orig_arr)

    finally:
        mf6.finalize()
Example #21
0
def test_get_value_double(flopy_dis, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path,
                     working_directory=flopy_dis.sim_path)

    try:
        mf6.initialize()

        some_output_var = next(var for var in mf6.get_output_var_names()
                               if var.endswith("/X"))
        copy_arr = mf6.get_value(some_output_var)

        # compare to array in MODFLOW memory:
        orig_arr = mf6.get_value_ptr(some_output_var)
        assert np.array_equal(copy_arr, orig_arr)

    finally:
        mf6.finalize()
Example #22
0
def test_timing_initialize(flopy_dis, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path,
                     working_directory=flopy_dis.sim_path,
                     timing=True)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)

    try:
        # Run initialize
        mf6.initialize()

        total = mf6.report_timing_totals()

        assert total > 0.0
    finally:
        mf6.finalize()
Example #23
0
def test_get_start_time(flopy_dis, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path,
                     working_directory=flopy_dis.sim_path)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)

    try:
        # Initialize
        mf6.initialize()

        # prescribed_start_time for modflow models is always 0
        prescribed_start_time = 0.0

        actual_start_time = mf6.get_start_time()
        assert math.isclose(prescribed_start_time, actual_start_time)
    finally:
        mf6.finalize()
Example #24
0
def test_get_var_address(flopy_dis, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)

    try:
        # Initialize
        mf6.initialize()

        head_tag = mf6.get_var_address("X", "SLN_1")
        assert head_tag == "SLN_1/X"

        # with lowercase should work too
        k11_tag = mf6.get_var_address("k11", flopy_dis.model_name.lower(), "NPF")
        assert k11_tag == flopy_dis.model_name.upper() + "/NPF/K11"
    finally:
        mf6.finalize()
Example #25
0
def test_err_unknown_var(flopy_dis, modflow_lib_path):
    """Unknown or invalid variable address should trigger python Exception,
    print the kernel error, and not crash the library"""
    mf6 = XmiWrapper(lib_path=modflow_lib_path,
                     working_directory=flopy_dis.sim_path)

    try:
        # Run initialize
        mf6.initialize()

        with pytest.raises(XMIError):
            mf6.get_var_rank("jnexistepas")

        with pytest.raises(XMIError):
            var_address = mf6.get_var_address("X", "dissolution")
            mf6.get_value_ptr(var_address)

    finally:
        mf6.finalize()
Example #26
0
def test_get_end_time(flopy_dis, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path,
                     working_directory=flopy_dis.sim_path)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)

    try:
        # Initialize
        mf6.initialize()

        prescribed_end_time = 0.0
        for perlen, _, _ in flopy_dis.tdis_rc:
            prescribed_end_time += perlen

        actual_end_time = mf6.get_end_time()
        assert math.isclose(prescribed_end_time, actual_end_time)
    finally:
        mf6.finalize()
Example #27
0
def test_get_value_ptr_scalar(flopy_dis, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path,
                     working_directory=flopy_dis.sim_path)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)

    try:
        # Initialize
        mf6.initialize()

        mf6.update()
        id_tag = mf6.get_var_address("ID", flopy_dis.model_name)
        grid_id = mf6.get_value_ptr_scalar(id_tag)

        # Only one model is defined => id should be 1
        # grid_id[0], because even scalars are defined as arrays
        assert grid_id[0] == 1
    finally:
        mf6.finalize()
Example #28
0
def test_get_value_int_scalar(flopy_dis_idomain, modflow_lib_path):
    mf6 = XmiWrapper(lib_path=modflow_lib_path,
                     working_directory=flopy_dis_idomain.sim_path)

    try:
        mf6.initialize()

        # get scalar variable:
        id_tag = next(var for var in mf6.get_output_var_names()
                      if var.endswith("/ID"))
        assert mf6.get_var_rank(id_tag) == 0

        tgt = mf6.get_value(id_tag)

        # compare with value in MODFLOW memory:
        orig = mf6.get_value_ptr(id_tag)
        assert np.array_equal(tgt, orig)

    finally:
        mf6.finalize()
Example #29
0
def test_get_grid_face_count(flopy_disu, modflow_lib_path):
    """Tests if the grid_face_count can be extracted"""
    mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_disu.sim_path)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)

    try:
        # Initialize
        mf6.initialize()

        prescribed_grid_face_count = flopy_disu.nrow * flopy_disu.ncol

        # Getting the grid id from the model, requires specifying one variable
        k11_tag = mf6.get_var_address("K11", flopy_disu.model_name, "NPF")
        grid_id = mf6.get_var_grid(k11_tag)
        actual_grid_face_count = mf6.get_grid_face_count(grid_id)

        assert prescribed_grid_face_count == actual_grid_face_count
    finally:
        mf6.finalize()
Example #30
0
def test_get_grid_type(flopy_dis, modflow_lib_path):
    """Tests if the grid type can be extracted"""

    mf6 = XmiWrapper(lib_path=modflow_lib_path,
                     working_directory=flopy_dis.sim_path)

    # Write output to screen:
    mf6.set_int("ISTDOUTTOFILE", 0)

    try:
        # Initialize
        mf6.initialize()

        # Getting the grid id from the model, requires specifying one variable
        k11_tag = mf6.get_var_address("K11", flopy_dis.model_name, "NPF")
        grid_id = mf6.get_var_grid(k11_tag)
        grid_type = mf6.get_grid_type(grid_id)

        assert grid_type == "rectilinear"
    finally:
        mf6.finalize()