Beispiel #1
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()
Beispiel #2
0
def bmifunc(exe, idx, model_ws=None):
    success = False

    name = ex[idx].upper()
    init_wd = os.path.abspath(os.getcwd())
    if model_ws is not None:
        os.chdir(model_ws)

    mf6_config_file = os.path.join(model_ws, 'mfsim.nam')
    try:
        mf6 = XmiWrapper(exe)
    except Exception as e:
        print("Failed to load " + exe)
        print("with message: " + str(e))
        return bmi_return(success, model_ws)

    # initialize the model
    try:
        mf6.initialize(mf6_config_file)
    except:
        return bmi_return(success, model_ws)

    # time loop
    current_time = mf6.get_current_time()
    end_time = mf6.get_end_time()

    # get pointer to simulated heads
    head_tag = mf6.get_var_address("X", "LIBGWF_EVT01")
    head = mf6.get_value_ptr(head_tag)

    # maximum outer iterations
    mxit_tag = mf6.get_var_address("MXITER", "SLN_1")
    max_iter = mf6.get_value(mxit_tag)

    # get copy of well data
    well_tag = mf6.get_var_address("BOUND", name, "WEL_0")
    well = mf6.get_value(well_tag)

    twell = np.zeros(ncol, dtype=np.float64)

    # model time loop
    idx = 0
    while current_time < end_time:

        # get dt and prepare for non-linear iterations
        dt = mf6.get_time_step()
        mf6.prepare_time_step(dt)

        # convergence loop
        kiter = 0
        mf6.prepare_solve(1)

        while kiter < max_iter:

            # update well rate
            twell[:] = head2et_wellrate(head[0])
            well[:, 0] = twell[:]
            mf6.set_value(well_tag, well)

            # solve with updated well rate
            has_converged = mf6.solve(1)
            kiter += 1

            if has_converged:
                msg = "Component {}".format(1) + \
                      " converged in {}".format(kiter) + " outer iterations"
                print(msg)
                break

        if not has_converged:
            return bmi_return(success, model_ws)

        # finalize time step
        mf6.finalize_solve(1)

        # finalize time step and update time
        mf6.finalize_time_step()
        current_time = mf6.get_current_time()

        # increment counter
        idx += 1
    # cleanup
    try:
        mf6.finalize()
        success = True
    except:
        return bmi_return(success, model_ws)

    if model_ws is not None:
        os.chdir(init_wd)

    # cleanup and return
    return bmi_return(success, model_ws)
def bmifunc(exe, idx, model_ws=None):
    success = False

    name = ex[idx].upper()
    init_wd = os.path.abspath(os.getcwd())
    if model_ws is not None:
        os.chdir(model_ws)

    mf6_config_file = os.path.join(model_ws, "mfsim.nam")
    try:
        mf6 = XmiWrapper(exe)
    except Exception as e:
        print("Failed to load " + exe)
        print("with message: " + str(e))
        return bmi_return(success, model_ws)

    # initialize the model
    try:
        mf6.initialize(mf6_config_file)
    except:
        return bmi_return(success, model_ws)

    # time loop
    current_time = mf6.get_current_time()
    end_time = mf6.get_end_time()

    # maximum outer iterations
    mxit_tag = mf6.get_var_address("MXITER", "SLN_1")
    max_iter = mf6.get_value(mxit_tag)

    # get copy of recharge array
    rch_tag = mf6.get_var_address("BOUND", name, "RCHA")
    new_recharge = mf6.get_value(rch_tag)

    # model time loop
    idx = 0
    while current_time < end_time:

        # get dt and prepare for non-linear iterations
        dt = mf6.get_time_step()
        mf6.prepare_time_step(dt)

        # convergence loop
        kiter = 0
        mf6.prepare_solve(1)

        # update recharge
        new_recharge[:, 0] = rch_spd[idx] * area
        mf6.set_value(rch_tag, new_recharge)

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

            if has_converged:
                msg = ("Component {}".format(1) +
                       " converged in {}".format(kiter) + " outer iterations")
                print(msg)
                break

        if not has_converged:
            return bmi_return(success, model_ws)

        # finalize time step
        mf6.finalize_solve(1)

        # finalize time step and update time
        mf6.finalize_time_step()
        current_time = mf6.get_current_time()

        # increment counter
        idx += 1

    # cleanup
    try:
        mf6.finalize()
        success = True
    except:
        return bmi_return(success, model_ws)

    if model_ws is not None:
        os.chdir(init_wd)

    # cleanup and return
    return bmi_return(success, model_ws)
Beispiel #4
0
def bmifunc(exe, idx, model_ws=None):
    print('\nBMI implementation test:')
    success = False

    name = ex[idx].upper()
    init_wd = os.path.abspath(os.getcwd())
    if model_ws is not None:
        os.chdir(model_ws)

    # get the observations from the standard run
    fpth = os.path.join('..', '{}.head.obs.csv'.format(ex[idx]))
    hobs = np.genfromtxt(fpth, delimiter=',', names=True)['H1_6_6']

    mf6_config_file = os.path.join(model_ws, 'mfsim.nam')
    try:
        mf6 = XmiWrapper(exe)
    except Exception as e:
        print("Failed to load " + exe)
        print("with message: " + str(e))
        return bmi_return(success, model_ws)

    # initialize the model
    try:
        mf6.initialize(mf6_config_file)
    except:
        return bmi_return(success, model_ws)

    # time loop
    current_time = mf6.get_current_time()
    end_time = mf6.get_end_time()

    # get pointer to simulated heads
    head_tag = mf6.get_var_address("X", name)
    head = mf6.get_value_ptr(head_tag)

    # maximum outer iterations
    mxit_tag = mf6.get_var_address("MXITER", "SLN_1")
    max_iter = mf6.get_value(mxit_tag)

    # get copy of recharge array
    rch_tag = mf6.get_var_address("BOUND", name, "RCHA")
    new_recharge = mf6.get_value(rch_tag).copy()

    # determine initial recharge value
    np.random.seed(0)
    rch = np.random.normal(1) * avg_rch

    # model time loop
    idx = 0
    while current_time < end_time:

        # target head
        htarget = hobs[idx]

        # get dt and prepare for non-linear iterations
        dt = mf6.get_time_step()
        mf6.prepare_time_step(dt)

        est_iter = 0
        while est_iter < 100:
            # base simulation loop
            has_converged = run_perturbation(mf6, max_iter, new_recharge,
                                             rch_tag, rch)
            if not has_converged:
                return bmi_return(success, model_ws)
            h0 = head.reshape((nrow, ncol))[5, 5]
            r0 = h0 - htarget

            # perturbation simulation loop
            has_converged = run_perturbation(mf6, max_iter, new_recharge,
                                             rch_tag, rch + drch)
            if not has_converged:
                return bmi_return(success, model_ws)
            h1 = head.reshape((nrow, ncol))[5, 5]
            r1 = h1 - htarget

            # calculate update terms
            dqdr = drch / (r0 - r1)
            dr = r1 * dqdr

            # evaluate if the estimation iterations need to continue
            if abs(r0) < 1e-5:
                msg = "Estimation for time {:5.1f}".format(current_time) + \
                      " converged in {:3d}".format(est_iter) + \
                      " iterations" + \
                      " -- final recharge={:10.5f}".format(rch) + \
                      " residual={:10.2g}".format(rch - rch_rates[idx])
                print(msg)
                break
            else:
                est_iter += 1
                rch += dr

        # solution with final estimated recharge for the timestep
        has_converged = run_perturbation(mf6, max_iter, new_recharge, rch_tag,
                                         rch)
        if not has_converged:
            return bmi_return(success, model_ws)

        # finalize time step
        mf6.finalize_solve(1)

        # finalize time step and update time
        mf6.finalize_time_step()
        current_time = mf6.get_current_time()

        # increment counter
        idx += 1
    # cleanup
    try:
        mf6.finalize()
        success = True
    except:
        return bmi_return(success, model_ws)

    if model_ws is not None:
        os.chdir(init_wd)

    # cleanup and return
    return bmi_return(success, model_ws)
def bmifunc(exe, idx, model_ws=None):
    success = False

    name = ex[idx].upper()
    init_wd = os.path.abspath(os.getcwd())
    if model_ws is not None:
        os.chdir(model_ws)

    mf6_config_file = os.path.join(model_ws, 'mfsim.nam')
    try:
        mf6 = XmiWrapper(exe)
    except Exception as e:
        print("Failed to load " + exe)
        print("with message: " + str(e))
        return bmi_return(success, model_ws)

    # initialize the model
    try:
        mf6.initialize(mf6_config_file)
    except:
        return bmi_return(success, model_ws)

    # time loop
    current_time = mf6.get_current_time()
    end_time = mf6.get_end_time()

    # get copy of (multi-dim) array with river parameters
    riv_tag = mf6.get_var_address("BOUND", name, riv_packname)
    new_spd = mf6.get_value(riv_tag)

    # model time loop
    idx = 0
    while current_time < end_time:

        # get dt
        dt = mf6.get_time_step()

        # prepare... and reads the RIV data from file!
        mf6.prepare_time_step(dt)

        # set the RIV data through the BMI
        if current_time < 5:
            # set columns of BOUND data (we're setting entire columns of the
            # 2D array for convenience, setting only the value for the active
            # stress period should work too)
            new_spd[:] = [riv_stage, riv_cond, riv_bot]
            mf6.set_value(riv_tag, new_spd)
        else:
            # change only stage data
            new_spd[:] = [riv_stage2, riv_cond, riv_bot]
            mf6.set_value(riv_tag, new_spd)

        kiter = 0
        mf6.prepare_solve(1)

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

            if has_converged:
                msg = "Component {}".format(1) + \
                      " converged in {}".format(kiter) + " outer iterations"
                print(msg)
                break

        if not has_converged:
            return bmi_return(success, model_ws)

        # finalize time step
        mf6.finalize_solve(1)

        # finalize time step and update time
        mf6.finalize_time_step()
        current_time = mf6.get_current_time()

        # increment counter
        idx += 1

    # cleanup
    try:
        mf6.finalize()
        success = True
    except:
        return bmi_return(success, model_ws)

    if model_ws is not None:
        os.chdir(init_wd)

    # cleanup and return
    return bmi_return(success, model_ws)
Beispiel #6
0
def bmifunc(exe, idx, model_ws=None):
    success = False

    name = ex[idx].upper()
    init_wd = os.path.abspath(os.getcwd())
    if model_ws is not None:
        os.chdir(model_ws)

    mf6_config_file = os.path.join(model_ws, 'mfsim.nam')
    try:
        mf6 = XmiWrapper(exe)
    except Exception as e:
        print("Failed to load " + exe)
        print("with message: " + str(e))
        return bmi_return(success, model_ws)

    # initialize the model
    try:
        mf6.initialize(mf6_config_file)
    except:
        return bmi_return(success, model_ws)

    # time loop
    current_time = mf6.get_current_time()
    end_time = mf6.get_end_time()

    # reset sc2 with bmi set_value
    sc2_tag = mf6.get_var_address("SC2", name, "STO")
    new_sc2 = mf6.get_value(sc2_tag)
    new_sc2.fill(sy_val)

    mf6.set_value(sc2_tag, new_sc2)

    # model time loop
    idx = 0
    while current_time < end_time:

        # run the time step
        try:
            mf6.update()
        except:
            return bmi_return(success, model_ws)

        # update time
        current_time = mf6.get_current_time()

        # increment counter
        idx += 1

    # cleanup
    try:
        mf6.finalize()
        success = True
    except:
        return bmi_return(success, model_ws)

    if model_ws is not None:
        os.chdir(init_wd)

    # cleanup and return
    return bmi_return(success, model_ws)
Beispiel #7
0
for o, a in opts:
    if o == "-i":
        mf6_config_file = a
    elif o == "-s":
        mf6_dll = a

# load the wrapper and cd to model dir
old_dir = os.getcwd()
model_dir = os.path.dirname(mf6_config_file)
print("\n", "Change to model directory: ", model_dir, "\n")
os.chdir(model_dir)

mf6 = XmiWrapper(mf6_dll)

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

# run the model
mf6.initialize(mf6_config_file)

current_time = mf6.get_current_time()
end_time = mf6.get_end_time()

while current_time < end_time:
    mf6.update()
    current_time = mf6.get_current_time()

mf6.finalize()

os.chdir(old_dir)