Skip to content

Add HFB package builder from dataframes#565

Merged
bdestombe merged 7 commits into
gwmod:devfrom
bdestombe:hfb-from-df
Jul 9, 2026
Merged

Add HFB package builder from dataframes#565
bdestombe merged 7 commits into
gwmod:devfrom
bdestombe:hfb-from-df

Conversation

@bdestombe

@bdestombe bdestombe commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

The full workflow from GeoDataFrame line features to a FloPy HFB package is implemented in nlmod, while delegating the horizontal line-to-cell routing to FloPy.

Summary

  • add nlmod.gwf.hfb.hfb_from_df to build MF6 HFB packages from line GeoDataFrames
  • delegate HFB line routing to flopy.utils.make_hfb_array from Feat(hfb_util): add method for creating HFBs modflowpy/flopy#2745
  • keep nlmod-specific depth/elevation hydchr scaling and idomain <= 0 filtering around the FloPy-generated cell pairs
  • use FloPy develop until the HFB utility from flopy#2745 is available in a release (flopy>3.10.0)
  • cover default columns, scalar values, custom columns, MultiLineString input, package naming, zero-SPD cleanup, FloPy routing, and inactive/pass-through filtering in HFB tests

Validation

  • python -m py_compile nlmod\gwf\hfb.py
  • python -m ruff format nlmod\gwf\hfb.py tests\test_023_hfb.py
  • python -m ruff check --fix nlmod\gwf\hfb.py tests\test_023_hfb.py
  • python -m pytest tests\test_023_hfb.py -v
  • python -m prospector nlmod\gwf\hfb.py tests\test_023_hfb.py
  • bandit -q -r nlmod\gwf\hfb.py
  • local availability check for flopy.utils.make_hfb_array

@bdestombe bdestombe marked this pull request as ready for review July 8, 2026 13:33
Comment thread nlmod/gwf/hfb.py

@dbrakenhoff dbrakenhoff left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good to me!

Seeing this reminds me that flopy now has HFB logic: modflowpy/flopy#2745

So I'm not sure how robust our algorithm is, but it could be worth exploring deferring the logic for building the HFBs to flopy? And on a similar note, I at some point added the line_to_hfb_buffer method that was supposed to be more robust and faster for more complex HFBs. I don't know if I actually succeeded, but it could be worth checking out this method as well...

But feel free to open a new issue to tackle those questions :)

@bdestombe

Copy link
Copy Markdown
Collaborator Author

Looks good to me!

Seeing this reminds me that flopy now has HFB logic: modflowpy/flopy#2745

So I'm not sure how robust our algorithm is, but it could be worth exploring deferring the logic for building the HFBs to flopy? And on a similar note, I at some point added the line_to_hfb_buffer method that was supposed to be more robust and faster for more complex HFBs. I don't know if I actually succeeded, but it could be worth checking out this method as well...

But feel free to open a new issue to tackle those questions :)

Thanks for the review! And I'm all in favor, but that would require pinning flopy to something recent. Could that cause any issues with other users?

As for the linting/formatting, I'll update that. I think I was developing from a different folder so that ruff didn't pick up on the formatting configuration file.

@dbrakenhoff

Copy link
Copy Markdown
Collaborator

Thanks for the review! And I'm all in favor, but that would require pinning flopy to something recent. Could that cause any issues with other users?

Good question. I don't really think so since flopy mostly manages I/O and backwards compatibility is something they take very seriously.

@bdestombe

bdestombe commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for the pointer to modflowpy/flopy#2745. I updated this PR to use that FloPy logic instead of maintaining nlmod's own line-routing path for the new package-builder workflow.

Changes since your review:

  • get_hfb_spd now delegates horizontal line-to-cell routing to flopy.utils.make_hfb_array.
  • nlmod still handles the parts that FloPy does not: depth/elevation vertical hydchr scaling and explicit idomain <= 0 filtering.
  • hfb_from_df remains the GeoDataFrame-to-package convenience wrapper on top of get_hfb_spd.
  • The FloPy dependency now points to the develop branch, with an inline pyproject.toml comment noting that this is for the HFB utility from flopy#2745 and can move to flopy>3.10.0 once released.
  • Tests now assert FloPy availability/use, structured known-answer depth/elevation scaling, vertex cell-pair agreement with direct FloPy output, MultiLineString handling, and inactive/pass-through filtering completeness.
  • I also removed the stale commented-out endpoint-handling block in the legacy line_to_hfb path.

Local validation passed with FloPy develop: py_compile, ruff format/check, pytest tests\test_023_hfb.py -v, prospector, bandit, and an explicit flopy.utils.make_hfb_array import check.

@bdestombe

Copy link
Copy Markdown
Collaborator Author

As far as I can tell there is no option to disable the trailing comma in multiline calls in the ruff configuration. Apparently, this complies with the standard, but also nlmod's, formatting rules

@bdestombe

Copy link
Copy Markdown
Collaborator Author

Hi David and others,
Merging this would require nlmod to use the develop branch of flopy. I would understand this is not ideal, however:

  • It would only be until the next flopy version is released (e.g., flopy version > 3.10)
  • it passes the current nlmod CI, so seems stable enough

@bdestombe bdestombe requested a review from dbrakenhoff July 9, 2026 14:02
@dbrakenhoff

Copy link
Copy Markdown
Collaborator

Yeah, ideally we release this only when flopy releases with the hfb utils and pin nlmod to use the newest flopy? From issues on flopy I gather the next release should happen in the next 1-2 weeks, so we won't have to wait that long?

So fine by me to merge this into dev, and hold our next release (which we really should do) until flopy has made theirs.

@bdestombe bdestombe requested a review from Copilot July 9, 2026 14:40
@bdestombe bdestombe merged commit 48669f6 into gwmod:dev Jul 9, 2026
2 of 3 checks passed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Implements an end-to-end workflow for creating MODFLOW 6 Horizontal Flow Barrier (HFB) packages from line GeoDataFrames in nlmod, delegating line-to-cell-pair routing to FloPy while keeping nlmod-specific hydchr scaling and inactive/pass-through filtering.

Changes:

  • Added nlmod.gwf.hfb.hfb_from_df plus supporting helpers, and updated get_hfb_spd to use flopy.utils.make_hfb_array.
  • Deprecated legacy line-routing helpers (line_to_hfb, line2hfb, line_to_hfb_buffer) and updated tests accordingly.
  • Switched flopy dependency to a VCS URL targeting the develop branch to access the new utility.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
nlmod/gwf/hfb.py Adds the DataFrame-to-HFB builder, routes via FloPy make_hfb_array, and introduces deprecation warnings for legacy routing helpers.
tests/test_023_hfb.py Adds/updates tests for hfb_from_df, FloPy routing delegation, scaling behavior, geometry handling, and deprecation warnings.
pyproject.toml Changes flopy dependency to track FloPy develop to obtain the new HFB routing utility.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pyproject.toml
requires-python = ">= 3.10"
dependencies = [
"flopy>=3.10.0",
"flopy @ git+https://github.com/modflowpy/flopy.git@develop", # HFB utility from flopy#2745, to be released in flopy>3.10.0
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