Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong results with models that use time series #72

Open
pya opened this issue Mar 20, 2025 · 2 comments
Open

Wrong results with models that use time series #72

pya opened this issue Mar 20, 2025 · 2 comments

Comments

@pya
Copy link

pya commented Mar 20, 2025

Approach

I ran all models from MODFLOW 6 examples: built 02/06/2025 19:17.

Code

This code was used to run all models:

from modflowapi import Callbacks, run_simulation

def callback(sim, callback_step):
    pass

run_simulation(
    dll='path/to/libmf6.dylib',
    sim_path='path/tomodel',
    callback=callback,
)

Versions

  • modflowapi: 0.3.0.dev0
  • xmipy: 1.3.1
  • MODFLOW: 6.6.1

Results

Most model runs produce the same output compared with the standalone MODFLOW executable. But these models:

  • ex-gwf-csub-p03a
  • ex-gwf-fhb
  • ex-gwf-advtidal
  • ex-gwf-csub-p03b

produce significantly different results in the balance values as listed in each <mode-name>.lst file. All of these models use of time series.

These models also use time series:

  • ex-gwf-csub-p01
  • ex-gwf-sfr-pindersauera
  • ex-gwf-sfr-pindersauerb

but show no differences in results compared to the standalone runs.

Potential problem

The problems seems to be in the solution loop in modflowapi.extensions.runner.run_simulation().

Replacing this loop:

    for sol_id, slnobj in sorted(sim.solutions.items()):
            models = {}
            maxiter = slnobj.mxiter
            solution = {sol_id: slnobj}
            for model in sim.models:
                if sol_id == model.solution_id:
                    models[model.name.lower()] = model

            sim_grp = ApiSimulation(
                mf6, models, solution, sim._exchanges, sim.tdis, sim.ats
            )
            mf6.prepare_solve(sol_id)
            if sim.kper != kperold[sol_id - 1]:
                callback(sim_grp, Callbacks.stress_period_start)
                kperold[sol_id - 1] += 1
            elif current_time == 0:
                callback(sim_grp, Callbacks.stress_period_start)

            kiter = 0
            callback(sim_grp, Callbacks.timestep_start)

            if sim_grp.ats_period[0]:
                mindt = sim_grp.ats_period[-1]
                while sim_grp.delt > mindt:
                    sim_grp.iteration = kiter
                    callback(sim_grp, Callbacks.iteration_start)
                    has_converged = mf6.solve(sol_id)
                    callback(sim_grp, Callbacks.iteration_end)
                    kiter += 1
                    if has_converged and sim_grp.allow_convergence:
                        break

            else:
                while kiter < maxiter:
                    sim_grp.iteration = kiter
                    callback(sim_grp, Callbacks.iteration_start)
                    has_converged = mf6.solve(sol_id)
                    callback(sim_grp, Callbacks.iteration_end)
                    kiter += 1
                    if has_converged and sim_grp.allow_convergence:
                        break

            callback(sim_grp, Callbacks.timestep_end)
            mf6.finalize_solve(sol_id)

with:

mf6.do_time_step()

fixes the problem and all models produce the same results as the standalone runs.

@jlarsen-usgs
Copy link
Contributor

@pya

This is something that I'll need to explore more to see what's going on. Unfortunately, we can't simply remove/replace the solve loop with do_time_step() in the extensions source code because it will remove the callback entry points from user access. Instead, it looks like we'll have to find out 1) which modflow function isn't being called and 2) create either an api call that executes the function or write some python code to mirror the function in the api extensions.

It'll go on the list of needed improvements. Thanks for identifying this issue!

@pya
Copy link
Author

pya commented Mar 24, 2025

@jlarsen-usgs

The "do_time_step() shortcut hack" is more a debugging tool than a solution. I am not sure if this could be more a problem in xmipy than in modflowapi.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants