Skip to content

Running model with recharge time series only uses first value when running with API #85

@dbrakenhoff

Description

@dbrakenhoff

I have single cell Modflow model that I'm trying to run with the API. The model contains ~9500 stress periods so I'm adding the recharge using a time series file. (No idea if this might be related to #72 in any way?)

When I run the model using the API, this is the head output I'm seeing:

Image

Trying to observe the RCH inside the solution loop with model.rch.stress_period_data.dataframe or mf6.get_value("API/RCH/RECHARGE") both show the first time series value being picked up for each stress period.

When I rerun the same model in the directory using mf6:

Image

Versions:

  • OS: Ubuntu 24.04
  • mf6: mf6: 6.6.3 09/29/2025
  • flopy: 3.9.5
  • modflowapi: 0.3.0.dev0
  • xmipy: 1.5.0

Attached is the model:

mftest.zip

And the code used to run the model with the API:

mf6 = modflowapi.ModflowApi("bin/libmf6.so", working_directory="mftest")
mf6.initialize()

success = False

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

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

# 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()
    while kiter < max_iter:
        # solve
        has_converged = mf6.solve()
        kiter += 1

        if has_converged:
            break

    # finalize solve
    mf6.finalize_solve()

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

    # terminate if model did not converge
    if not has_converged:
        break

    # increment counter
    idx += 1

# cleanup
try:
    mf6.finalize()
    success = True
except Exception as e:
    pass

Final note: I tried setting the recharge value in each iteration using the API using the code below, which solves the issue, but that significantly slows down solving the model using the API (by about a factor 6). I want to solve the model as quickly as possible, so if you have any tips on how to do so most efficiently using the API I'd love to hear it.

    df = rch_pkg.stress_period_data.dataframe
    df.loc[0, "recharge"] = prec.iloc[idx] - evap.iloc[idx]  # prec, evap are Series with data
    rch_pkg.stress_period_data.dataframe = df

Thanks in advance for your help and any insights!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions