Skip to content

fix(sfr): read bedk, manning, and upstream_frac from the input context - #128

Open
jdhughes-dev wants to merge 8 commits into
MODFLOW-ORG:developfrom
jdhughes-dev:sfr-input-context-names
Open

jdhughes-dev wants to merge 8 commits into
MODFLOW-ORG:developfrom
jdhughes-dev:sfr-input-context-names

Conversation

@jdhughes-dev

@jdhughes-dev jdhughes-dev commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

MODFLOW 6.8.2 reads SFR packagedata through IDM and keeps HK, ROUGH, and USTRF only as deprecated API aliases, so every SFR model run through modflowapi writes three deprecation warnings to mfsim.lst. The packagedata hk, rough, and ustrf columns are now read from __INPUT__/<model>/<sfr>/BEDK, MANNING, and UPSTREAM_FRAC when libmf6 has those addresses, and from the legacy names otherwise; the column names are unchanged.

  • adv_input_vars in datamodel.py maps a column to its input context name.
  • test_sfr_input_context_names checks the addresses, values, and a write that persists into the next stress period, against a small SFR model whose input files the test writes as text; it passes against 6.8.1 and current MODFLOW 6 develop.

MODFLOW 6.8.2 reads SFR packagedata through IDM and keeps HK, ROUGH, and
USTRF only as aliases that are reported as deprecated when used through the
API. For libmf6 6.8.2 and later, the packagedata hk, rough, and ustrf columns
are now read from __INPUT__/<model>/<sfr>/BEDK, MANNING, and UPSTREAM_FRAC;
earlier versions still use the legacy names. Column names are unchanged.
@jdhughes-dev

jdhughes-dev commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Reviewer Summary

  • Why the input context: on MODFLOW 6 develop, this%hk, this%rough, and this%ustrf are pointers to BEDK, MANNING, and UPSTREAM_FRAC in the input context (gwf-sfr.f90:567-569). The package path holds only the deprecated aliases from mem_alias_deprecated (gwf-sfr.f90:742-747), which point to the same memory.
  • No change in results: packagedata, period data, and diversions values read through modflowapi match between 6.8.1 and develop for the SFR, LAK, MAW, and UZF examples in modflow6-examples. Final heads agree to about 1e-11, with and without writes to SFR hk and rough and UZF finf.
  • Warnings removed: with this change, develop writes no API VARIABLE ... WAS DEPRECATED warnings to mfsim.lst; without it, every SFR model writes three.
  • Column names unchanged: hk, rough, and ustrf stay the recarray column names for every libmf6 version, so existing scripts are unaffected.
  • Address selection: the input context address is used when it is in get_input_var_names(), read once per SFR package, so a pre-6.8.2 nightly (6.9.0.dev0) falls back to the legacy names. If neither address exists, get_value_ptr raises XMIError (unknown variable); _set_data only catches InputError for unsupported types, so a missing variable is not dropped silently.
  • Other packages: the LAK, MAW, and UZF names modflowapi reads are not aliased on develop. The transport advanced packages (SFT, LKT, MWT, UZT) are not mapped in modflowapi, so their aliases do not apply.
  • Adding renames: a later rename is one entry in adv_input_vars in datamodel.py.

The SFR test used a copy of the ex-gwf-sfr-p01 example from
modflow6-examples. It now uses a three-cell, three-reach model with one
diversion, written by hand so the input runs on every libmf6 version tested.
The SFR test read a hand-written model committed to docs/examples/data. It
now builds the same three-reach model with flopy in the test directory, and
takes the expected hk, rough, and ustrf values from the arrays passed to
flopy. flopy is added to the test dependency group.

@mjreno mjreno left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified

  • On MODFLOW 6 develop, this%hk, this%rough and this%ustrf point at BEDK, MANNING and UPSTREAM_FRAC in the input context, and HK, ROUGH and USTRF are deprecated aliases of the same memory, so the new addresses read and write exactly what the old ones did. These arrays are indexed by reach, so PACKAGEDATA row order doesn't matter. Using them rather than the PACKAGEDATA input arrays (RHK, MAN, USTRF) is correct.
  • Behavior with MODFLOW-ORG/modflow6#3093 (in 6.8.2): a value written through modflowapi persists unless that reach's PACKAGEDATA value is linked to a time series, in which case the series value replaces it at the next time step.
  • test_sfr_input_context_names passes against libmf6 6.8.1 and against current develop (which includes #3093).

Minor suggestions

  1. libmf6_version(): re.match returns None for a version string it doesn't recognize, which surfaces as an AttributeError. A clear error message would help.
  2. The test writes hk and immediately reads the same address back. Reading it again at the next time step, or comparing with the deprecated HK address (same memory), would show the write reaches the package's working array and persists, which the manual comparison in the summary already demonstrates.

@jdhughes-dev

Copy link
Copy Markdown
Contributor Author

Thanks @mjreno.

  1. libmf6_version() now raises ValueError: unrecognized libmf6 version: '<version>' when the version string doesn't match (ca2438b).
  2. test_sfr_input_context_names now reads hk again at the start of the second stress period and checks the written values persisted (7be056a). I didn't compare against the deprecated HK address so the test doesn't trigger the deprecation warning.

Comment thread pyproject.toml Resolved
Comment thread modflowapi/extensions/pakbase.py Resolved
The input context address for hk, rough, and ustrf was chosen by libmf6
version, which picks it for a pre-6.8.2 nightly (6.9.0.dev0) that does not
have it. The address is now used when it is in the libmf6 input variable
names, with the package address as the fallback, and libmf6_version() is
removed.
Drop flopy from the test group so modflowapi does not depend on it.
Removing flopy from the test group in e2bbd7a also removed it from the docs group.

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

3 participants