示例#1
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')
    mf6 = AmiWrapper(exe)

    # 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 sc2 array and update flag
    cdata = "{} STO/IRESETSC2".format(name)
    update_sc2 = mf6.get_value_ptr(cdata)
    cdata = "{} STO/SC2".format(name)
    sc2 = mf6.get_value_ptr(cdata)

    # reset sc2 and update flag
    sc2 += sy_val
    update_sc2[0] = 1

    # 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)
示例#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)
示例#3
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()

    # 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)
示例#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)
示例#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')
    mf6 = AmiWrapper(exe)

    # 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 = mf6.get_value_ptr("SLN_1/X")

    # maximum outer iterations
    max_iter = mf6.get_value_ptr("SLN_1/MXITER")

    # get sc2 array and update flag
    cdata = "{} WEL_0/BOUND".format(name)
    well = mf6.get_value_ptr(cdata)
    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[:]

            # 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)
示例#7
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)
示例#8
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')
    mf6 = AmiWrapper(exe)

    # 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
    max_iter = mf6.get_value_ptr("SLN_1/MXITER")

    # get recharge array
    cdata = "{} RCHA/BOUND".format(name)
    recharge = mf6.get_value_ptr(cdata)

    # 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
        recharge[:, 0] = rch_spd[idx] * area

        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)