swmod 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. swmod-0.0.1/.github/workflows/release.yml +65 -0
  2. swmod-0.0.1/.gitignore +85 -0
  3. swmod-0.0.1/.python-version +1 -0
  4. swmod-0.0.1/LICENSE +21 -0
  5. swmod-0.0.1/PKG-INFO +75 -0
  6. swmod-0.0.1/README.md +48 -0
  7. swmod-0.0.1/docs/concentration_tracking.md +288 -0
  8. swmod-0.0.1/docs/conf.py +44 -0
  9. swmod-0.0.1/docs/index.md +67 -0
  10. swmod-0.0.1/docs/mwe_coupling.md +292 -0
  11. swmod-0.0.1/docs/mwe_standalone_sw.md +67 -0
  12. swmod-0.0.1/docs/network_elements.md +439 -0
  13. swmod-0.0.1/docs/purpose_and_scope.md +45 -0
  14. swmod-0.0.1/docs/tutorials/01_two_lakes_and_weir.ipynb +261 -0
  15. swmod-0.0.1/docs/tutorials/02_transient_rainfall_event.ipynb +314 -0
  16. swmod-0.0.1/docs/tutorials/03_complex_network_bifurcation.ipynb +383 -0
  17. swmod-0.0.1/docs/tutorials/04_realworld_example.ipynb +1028 -0
  18. swmod-0.0.1/docs/tutorials/beek/beek.qgz +0 -0
  19. swmod-0.0.1/docs/tutorials/beek/download_data.py +32 -0
  20. swmod-0.0.1/docs/tutorials/beek/generate_dataset.py +69 -0
  21. swmod-0.0.1/docs/tutorials/beek/generate_dataset_raise_crest_elevation.py +21 -0
  22. swmod-0.0.1/docs/tutorials/beek/generate_dataset_reference.py +18 -0
  23. swmod-0.0.1/docs/tutorials/beek/grid_surface_water.py +83 -0
  24. swmod-0.0.1/docs/tutorials/beek/postprocess_model.py +139 -0
  25. swmod-0.0.1/docs/tutorials/beek/prepare_dtm.py +36 -0
  26. swmod-0.0.1/docs/tutorials/beek/prepare_surface_water.py +140 -0
  27. swmod-0.0.1/docs/tutorials/beek/run_model.py +172 -0
  28. swmod-0.0.1/docs/tutorials/beek/snakefile +90 -0
  29. swmod-0.0.1/docs/tutorials/beek/surface_water.py +695 -0
  30. swmod-0.0.1/docs/tutorials/schiermonnikoog/download_data.py +64 -0
  31. swmod-0.0.1/docs/tutorials/schiermonnikoog/generate_dataset.py +69 -0
  32. swmod-0.0.1/docs/tutorials/schiermonnikoog/generate_dataset_inflow.py +27 -0
  33. swmod-0.0.1/docs/tutorials/schiermonnikoog/generate_dataset_reference.py +25 -0
  34. swmod-0.0.1/docs/tutorials/schiermonnikoog/grid_surface_water.py +36 -0
  35. swmod-0.0.1/docs/tutorials/schiermonnikoog/plot_data.py +36 -0
  36. swmod-0.0.1/docs/tutorials/schiermonnikoog/postprocess_model.py +204 -0
  37. swmod-0.0.1/docs/tutorials/schiermonnikoog/prepare_dtm.py +37 -0
  38. swmod-0.0.1/docs/tutorials/schiermonnikoog/prepare_surface_water.py +45 -0
  39. swmod-0.0.1/docs/tutorials/schiermonnikoog/run_model.py +160 -0
  40. swmod-0.0.1/docs/tutorials/schiermonnikoog/schiermonnikoog.qgz +0 -0
  41. swmod-0.0.1/docs/tutorials/schiermonnikoog/snakefile +93 -0
  42. swmod-0.0.1/docs/tutorials/schiermonnikoog/surface_water.py +258 -0
  43. swmod-0.0.1/pyproject.toml +63 -0
  44. swmod-0.0.1/swmod/__init__.py +51 -0
  45. swmod-0.0.1/swmod/_logging.py +94 -0
  46. swmod-0.0.1/swmod/builder.py +558 -0
  47. swmod-0.0.1/swmod/coupling.py +2510 -0
  48. swmod-0.0.1/swmod/network.py +3741 -0
  49. swmod-0.0.1/swmod/plotting.py +909 -0
  50. swmod-0.0.1/swmod/version.py +1 -0
  51. swmod-0.0.1/tests/test_concentration.py +572 -0
  52. swmod-0.0.1/tests/test_coupling.py +1140 -0
  53. swmod-0.0.1/tests/test_network.py +1191 -0
  54. swmod-0.0.1/tests/test_plotting.py +44 -0
  55. swmod-0.0.1/uv.lock +3658 -0
@@ -0,0 +1,65 @@
1
+ name: Release to PyPI
2
+
3
+ on:
4
+ release:
5
+ types: [published]
6
+ workflow_dispatch:
7
+
8
+ jobs:
9
+ build:
10
+ name: Build distribution
11
+ runs-on: ubuntu-latest
12
+ steps:
13
+ - name: Checkout repository
14
+ uses: actions/checkout@v4
15
+
16
+ - name: Install uv
17
+ uses: astral-sh/setup-uv@v5
18
+
19
+ - name: Set up Python
20
+ uses: actions/setup-python@v5
21
+ with:
22
+ python-version: "3.12"
23
+
24
+ - name: Verify release tag matches swmod/version.py
25
+ if: github.event_name == 'release'
26
+ shell: bash
27
+ run: |
28
+ PKG_VERSION=$(python -c "ns = {}; exec(open('swmod/version.py').read(), ns); print(ns['__version__'])")
29
+ TAG_VERSION="${GITHUB_REF_NAME#v}"
30
+ echo "Package version: ${PKG_VERSION}, Tag version: ${TAG_VERSION}"
31
+ if [ "${PKG_VERSION}" != "${TAG_VERSION}" ]; then
32
+ echo "::error::Release tag '${GITHUB_REF_NAME}' does not match swmod/version.py '${PKG_VERSION}'"
33
+ exit 1
34
+ fi
35
+
36
+ - name: Build sdist and wheel
37
+ run: uv build
38
+
39
+ - name: Check distributions with twine
40
+ run: uvx twine check dist/*
41
+
42
+ - name: Upload build artifacts
43
+ uses: actions/upload-artifact@v4
44
+ with:
45
+ name: dist
46
+ path: dist/
47
+
48
+ publish:
49
+ name: Publish to PyPI
50
+ needs: build
51
+ runs-on: ubuntu-latest
52
+ environment:
53
+ name: pypi
54
+ url: https://pypi.org/p/swmod
55
+ permissions:
56
+ id-token: write
57
+ steps:
58
+ - name: Download build artifacts
59
+ uses: actions/download-artifact@v4
60
+ with:
61
+ name: dist
62
+ path: dist/
63
+
64
+ - name: Publish package to PyPI
65
+ uses: pypa/gh-action-pypi-publish@release/v1
swmod-0.0.1/.gitignore ADDED
@@ -0,0 +1,85 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C / Fortran shared libraries & binaries
7
+ .bin/
8
+ *.dll
9
+ *.so
10
+ *.dylib
11
+ *.exe
12
+
13
+ # Distribution / packaging
14
+ build/
15
+ develop-eggs/
16
+ dist/
17
+ downloads/
18
+ eggs/
19
+ .eggs/
20
+ lib/
21
+ lib64/
22
+ parts/
23
+ sdist/
24
+ var/
25
+ wheels/
26
+ share/python-wheels/
27
+ *.egg-info/
28
+ .installed.cfg
29
+ *.egg
30
+
31
+ # Virtual environments
32
+ .venv/
33
+ env/
34
+ venv/
35
+ ENV/
36
+
37
+ # Pytest / Coverage
38
+ .pytest_cache/
39
+ .coverage
40
+ htmlcov/
41
+
42
+ # MODFLOW 6 simulation output directories & binary data
43
+ sim_output/
44
+ transient_sim/
45
+ two_lakes_sim/
46
+ complex_sim/
47
+ **/sim_output/
48
+ **/transient_sim/
49
+ **/two_lakes_sim/
50
+ **/complex_sim/
51
+ *.hds
52
+ *.cbc
53
+ *.lst
54
+ *.grb
55
+ *.dis
56
+ *.npf
57
+ *.ic
58
+ *.sto
59
+ *.oc
60
+ *.rch*
61
+ *.riv*
62
+ mfsim.nam
63
+ mfsim.lst
64
+
65
+ # Sphinx documentation build outputs & Jupyter execution caches
66
+ docs/_build/
67
+ .jupyter_cache/
68
+
69
+ # Scratch & temporary workspace files
70
+ scratch/
71
+ main.py
72
+
73
+ # IDE / OS specific files
74
+ .vscode/
75
+ .idea/
76
+ *.swp
77
+ *.swo
78
+ .DS_Store
79
+ Thumbs.db
80
+ /docs/tutorials/schiermonnikoog/download
81
+ /docs/tutorials/schiermonnikoog/models
82
+ /docs/tutorials/schiermonnikoog/.snakemake
83
+ /docs/tutorials/beek/download
84
+ /docs/tutorials/beek/models
85
+ /docs/tutorials/beek/.snakemake
@@ -0,0 +1 @@
1
+ 3.13
swmod-0.0.1/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ruben Calje
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
swmod-0.0.1/PKG-INFO ADDED
@@ -0,0 +1,75 @@
1
+ Metadata-Version: 2.5
2
+ Name: swmod
3
+ Version: 0.0.1
4
+ Summary: Couple lumped surface water network models with MODFLOW 6 via modflowapi and FloPy.
5
+ Project-URL: Homepage, https://github.com/rubencalje/swmod
6
+ Project-URL: Repository, https://github.com/rubencalje/swmod
7
+ Author: Ruben Calje
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Topic :: Scientific/Engineering :: Hydrology
17
+ Requires-Python: >=3.12
18
+ Requires-Dist: flopy>=3.9.0
19
+ Requires-Dist: geopandas>=1.0.0
20
+ Requires-Dist: matplotlib>=3.8.0
21
+ Requires-Dist: modflowapi>=1.0.0
22
+ Requires-Dist: networkx>=3.0
23
+ Requires-Dist: numpy>=1.26.0
24
+ Requires-Dist: pandas>=2.0.0
25
+ Requires-Dist: shapely>=2.0.0
26
+ Description-Content-Type: text/markdown
27
+
28
+ # MODFLOW SWMOD (`swmod`)
29
+
30
+ **`swmod`** is a Python package for coupling custom, physically coherent surface water network models (nodes, channel reaches, control weirs) with MODFLOW 6 groundwater flow models using the MODFLOW API (`modflowapi`) and FloPy.
31
+
32
+ ---
33
+
34
+ ## Package Purpose & Scope
35
+
36
+ **`swmod`** is specifically designed for regional groundwater-surface water interaction modeling:
37
+
38
+ - **Steady-State & Long Time-Step Coupling**:
39
+ Targeted at regional studies operating over daily, monthly, or steady-state stress periods. Because surface water flow reaches equilibrium much faster than groundwater moves through an aquifer, `swmod` solves for steady-state surface water mass balance ($R(h) = 0$) during each coupling iteration, avoiding the computational overhead of full 1D/2D unsteady Saint-Venant solvers.
40
+
41
+ - **Physically Coherent Hydraulics**:
42
+ - **Nodes**: Lumped surface water storage bodies (lakes, ponds, reservoirs, ditch junctions, constant-head outfalls) exchanging flux with MODFLOW 6 RIV package cells.
43
+ - **Reaches**: Open-channel flow based on Manning's friction ($Q = C \cdot \text{sign}(\Delta h) \sqrt{|\Delta h|}$) or linear conveyance, fully integrated with groundwater exchange along their length.
44
+ - **Weirs**: Sharp/broad-crested control structures using a $C^1$-continuous, strictly monotonic Villemonte-type formulation combining free overflow ($Q_{\text{free}} = C_f b (h_u - z_c)^{1.5}$) with a regularized submergence factor $\phi(r)$ ($0 \le |Q| \le Q_{\text{free}}$) to guarantee smooth convergence during non-linear solver iterations.
45
+
46
+ - **GeoPandas & Area-Based Conductances**:
47
+ Native integration with `geopandas` vector geometries (Polygons and LineStrings). Automatically computes cell intersection areas $A_{\text{intersect}}$ and RIV conductances $C_k = \frac{A_{\text{intersect}, k}}{c_{\text{bed}}}$ from riverbed resistance $c_{\text{bed}} = \frac{M}{K_{\text{bed}}}$ [days].
48
+
49
+ ---
50
+
51
+ ## Requirements
52
+
53
+ - Python 3.12 or newer
54
+
55
+ ---
56
+
57
+ ## Quickstart
58
+
59
+ ```bash
60
+ # Install dependencies with uv
61
+ uv sync
62
+
63
+ # Download MODFLOW executables into .bin/ (required on every new machine; not stored in the repo)
64
+ uv run get-modflow .bin
65
+
66
+ # Run test suite
67
+ uv run pytest -v
68
+
69
+ # Run 4-node example
70
+ uv run python examples/two_lakes_ditch_weir.py
71
+
72
+ # Build and preview documentation locally
73
+ uv run sphinx-build -b html docs docs/_build/html
74
+ uv run python -m http.server --directory docs/_build/html 8000
75
+ ```
swmod-0.0.1/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # MODFLOW SWMOD (`swmod`)
2
+
3
+ **`swmod`** is a Python package for coupling custom, physically coherent surface water network models (nodes, channel reaches, control weirs) with MODFLOW 6 groundwater flow models using the MODFLOW API (`modflowapi`) and FloPy.
4
+
5
+ ---
6
+
7
+ ## Package Purpose & Scope
8
+
9
+ **`swmod`** is specifically designed for regional groundwater-surface water interaction modeling:
10
+
11
+ - **Steady-State & Long Time-Step Coupling**:
12
+ Targeted at regional studies operating over daily, monthly, or steady-state stress periods. Because surface water flow reaches equilibrium much faster than groundwater moves through an aquifer, `swmod` solves for steady-state surface water mass balance ($R(h) = 0$) during each coupling iteration, avoiding the computational overhead of full 1D/2D unsteady Saint-Venant solvers.
13
+
14
+ - **Physically Coherent Hydraulics**:
15
+ - **Nodes**: Lumped surface water storage bodies (lakes, ponds, reservoirs, ditch junctions, constant-head outfalls) exchanging flux with MODFLOW 6 RIV package cells.
16
+ - **Reaches**: Open-channel flow based on Manning's friction ($Q = C \cdot \text{sign}(\Delta h) \sqrt{|\Delta h|}$) or linear conveyance, fully integrated with groundwater exchange along their length.
17
+ - **Weirs**: Sharp/broad-crested control structures using a $C^1$-continuous, strictly monotonic Villemonte-type formulation combining free overflow ($Q_{\text{free}} = C_f b (h_u - z_c)^{1.5}$) with a regularized submergence factor $\phi(r)$ ($0 \le |Q| \le Q_{\text{free}}$) to guarantee smooth convergence during non-linear solver iterations.
18
+
19
+ - **GeoPandas & Area-Based Conductances**:
20
+ Native integration with `geopandas` vector geometries (Polygons and LineStrings). Automatically computes cell intersection areas $A_{\text{intersect}}$ and RIV conductances $C_k = \frac{A_{\text{intersect}, k}}{c_{\text{bed}}}$ from riverbed resistance $c_{\text{bed}} = \frac{M}{K_{\text{bed}}}$ [days].
21
+
22
+ ---
23
+
24
+ ## Requirements
25
+
26
+ - Python 3.12 or newer
27
+
28
+ ---
29
+
30
+ ## Quickstart
31
+
32
+ ```bash
33
+ # Install dependencies with uv
34
+ uv sync
35
+
36
+ # Download MODFLOW executables into .bin/ (required on every new machine; not stored in the repo)
37
+ uv run get-modflow .bin
38
+
39
+ # Run test suite
40
+ uv run pytest -v
41
+
42
+ # Run 4-node example
43
+ uv run python examples/two_lakes_ditch_weir.py
44
+
45
+ # Build and preview documentation locally
46
+ uv run sphinx-build -b html docs docs/_build/html
47
+ uv run python -m http.server --directory docs/_build/html 8000
48
+ ```
@@ -0,0 +1,288 @@
1
+ # Solute Concentration Tracking in SWMOD
2
+
3
+ ## Overview
4
+
5
+ SWMOD nodes now support optional solute concentration tracking for modeling groundwater-surface water (GW-SW) transport coupling. This enables simulation of chloride intrusion, contaminant plumes, and other transported species.
6
+
7
+ ## Key Features
8
+
9
+ - **Optional**: Concentration tracking is disabled by default for backward compatibility
10
+ - **Automatic**: Enabled automatically when a GWT (Groundwater Transport) model is coupled via `CoupledSimulation`
11
+ - **Generic**: Not specific to chloride—works with any solute (nitrate, salt, contaminants, etc.)
12
+ - **Mass-Conservative**: Uses rigorous mass balance equations
13
+
14
+ ## Quick Start
15
+
16
+ ### Enable tracking on a node
17
+
18
+ ```python
19
+ from swmod import Node
20
+
21
+ # Create node with tracking enabled
22
+ node = Node("my_node", stage=1.0, area=100.0, track_concentration=True)
23
+ node.concentration = 500.0 # mg/L (example units)
24
+ ```
25
+
26
+ ### Update concentration
27
+
28
+ ```python
29
+ # Track flows and update concentration
30
+ node.update_concentration(
31
+ inflow_volume=100.0, # m³
32
+ inflow_conc=5000.0, # mg/L
33
+ outflow_volume=50.0 # m³
34
+ )
35
+ ```
36
+
37
+ ### Automatic with GWT coupling
38
+
39
+ ```python
40
+ from swmod import SurfaceWaterNetwork
41
+ from swmod.coupling import CoupledSimulation
42
+
43
+ network = SurfaceWaterNetwork()
44
+ # ... add nodes and connections ...
45
+
46
+ # Concentration tracking is automatically enabled for all nodes
47
+ coupled = CoupledSimulation(sw_network=network, ...)
48
+ ```
49
+
50
+ ## Mass Balance Equation
51
+
52
+ For each timestep:
53
+
54
+ ```
55
+ C_new = (V_storage × C_old + Q_in × C_in) / (V_storage + Q_in - Q_out)
56
+ ```
57
+
58
+ Where:
59
+ - **C_old**: Current concentration in node
60
+ - **C_in**: Concentration of inflow
61
+ - **C_new**: Updated concentration after mixing
62
+ - **V_storage**: Volume of water stored in node
63
+ - **Q_in**: Volumetric inflow
64
+ - **Q_out**: Volumetric outflow
65
+
66
+ Outflow carries the *current* concentration C_old, not the mixed concentration C_new.
67
+
68
+ ## API Reference
69
+
70
+ ### Node Attributes
71
+
72
+ - `concentration: float` — Solute concentration [mass/volume, e.g., mg/L]
73
+ - `track_concentration: bool` — Enable/disable tracking (default: False)
74
+ - `precipitation_concentration: float` — Concentration of precipitation input (default: `0.0`)
75
+ - `evaporation_concentration: float | None` — Concentration of evaporation outflow (default: `0.0`; use `None` to use current node concentration)
76
+ - `external_inflow_concentration: float` — Concentration for positive external inflow (default: `0.0`)
77
+ - `external_outflow_concentration: float | None` — Concentration for negative external outflow (default: `None`, which uses current node concentration)
78
+
79
+ ### Node Methods
80
+
81
+ ```python
82
+ def update_concentration(
83
+ inflow_volume: float, # m³
84
+ inflow_conc: float | None = None, # [mass/volume]
85
+ outflow_volume: float = 0.0, # m³
86
+ storage_volume: float = None, # m³, optional
87
+ outflow_conc: float | None = None # [mass/volume], optional
88
+ ) -> None:
89
+ """Update concentration via mass balance."""
90
+ ```
91
+
92
+ **Parameters:**
93
+ - `inflow_volume`: Volumetric inflow during timestep
94
+ - `inflow_conc`: Concentration of inflow water. If `None`, uses `external_inflow_concentration`
95
+ - `outflow_volume`: Volumetric outflow during timestep
96
+ - `storage_volume`: Explicit storage volume. If None, auto-calculated from node area and stage.
97
+ - `outflow_conc`: Concentration of outflow water. If `None`, uses `external_outflow_concentration` (or node concentration when that is `None`)
98
+
99
+ **Notes:**
100
+ - Only updates if `track_concentration=True`
101
+ - Automatically clamps concentration to non-negative values
102
+ - Does nothing if storage volume ≤ 0
103
+
104
+ ## CoupledSimulation Integration
105
+
106
+ When a `CoupledSimulation` is created, it automatically detects GWT models and enables concentration tracking:
107
+
108
+ ```python
109
+ coupled = CoupledSimulation(
110
+ sw_network=network,
111
+ sim_dir="sim_output",
112
+ verbose=True
113
+ )
114
+ # If GWT models exist, concentration tracking is enabled on all nodes
115
+ ```
116
+
117
+ To manually enable/disable:
118
+
119
+ ```python
120
+ # Enable tracking on specific nodes
121
+ for node in network.nodes.values():
122
+ node.track_concentration = True
123
+ ```
124
+
125
+ ## Groundwater Transport (GWT) Coupling
126
+
127
+ When one or more MODFLOW 6 GWT models are present, `CoupledSimulation` can push SW concentrations
128
+ to RIV auxiliary concentration fields used by GWT/SSM.
129
+
130
+ ### Single GWT model
131
+
132
+ For a single GWT model, define one RIV auxiliary concentration column, for example:
133
+
134
+ - `CONCENTRATION`
135
+ - `RIV_SW`
136
+ - `CONC`
137
+ - `RIV_SW_GWT_1`
138
+
139
+ `CoupledSimulation` maps node concentration values to that AUX column at each coupling iteration.
140
+
141
+ ### Multiple GWT models
142
+
143
+ For multiple GWT models, define one RIV AUX concentration column per model using model-specific names, for example:
144
+
145
+ - `RIV_SW_GWT_1`
146
+ - `RIV_SW_GWT_2`
147
+
148
+ Then provide per-model SW concentrations on each node:
149
+
150
+ ```python
151
+ node.concentrations = {
152
+ "GWT_1": 100.0,
153
+ "GWT_2": 250.0,
154
+ }
155
+ ```
156
+
157
+ If `node.concentrations` is not provided, SWMOD falls back to scalar `node.concentration`.
158
+
159
+ ### Practical notes
160
+
161
+ - AUX column names are matched case-insensitively.
162
+ - Concentrations are clamped to non-negative values before writing to RIV AUX.
163
+ - If AUX names are unavailable from the API, SWMOD falls back to model-order mapping when possible.
164
+ - `CoupledSimulation.run()` performs a preflight validation of `mfsim.nam` for
165
+ GWF/GWT coupling:
166
+ - GWF and GWT must use different IMS packages
167
+ - the GWF IMS line must appear before the GWT IMS line in the solutiongroup
168
+ If invalid, SWMOD raises a clear `RuntimeError` before native initialization.
169
+ To bypass this safeguard, construct coupling with `allow_unsafe_gwt_run=True`.
170
+
171
+ ## Use Cases
172
+
173
+ ### Coastal Intrusion Modeling
174
+ Track chloride concentration in polder networks to model saltwater intrusion through boundary weirs.
175
+
176
+ ```python
177
+ coastal_node = Node("sea", stage=2.0, area=150.0, track_concentration=True)
178
+ coastal_node.concentration = 18000.0 # seawater
179
+
180
+ polder_node = Node("polder1", stage=1.8, area=200.0, track_concentration=True)
181
+ # Update with flow from coastal node
182
+ polder_node.update_concentration(
183
+ inflow_volume=50.0,
184
+ inflow_conc=coastal_node.concentration,
185
+ outflow_volume=30.0
186
+ )
187
+ ```
188
+
189
+ ### GW-SW Transport Coupling
190
+ Use surface water concentrations as boundary conditions for GWT models:
191
+
192
+ ```python
193
+ # Update SW node from GWT solver
194
+ sw_node.update_concentration(
195
+ inflow_volume=gw_inflow,
196
+ inflow_conc=gw_conc, # From GWT solution
197
+ outflow_volume=sw_outflow
198
+ )
199
+
200
+ # Use for next GWT timestep's boundary condition
201
+ gwt_boundary_conc = sw_node.concentration
202
+ ```
203
+
204
+ ### Contaminant Plume Tracking
205
+ Model propagation of contaminants through surface water networks:
206
+
207
+ ```python
208
+ for node in network.nodes.values():
209
+ node.track_concentration = True
210
+
211
+ # Simulate daily contaminant transport
212
+ for day in range(365):
213
+ for conn in network.connections:
214
+ q_flow = conn.flow(
215
+ network.nodes[conn.node_from].stage,
216
+ network.nodes[conn.node_to].stage
217
+ )
218
+
219
+ # Update upstream node (outflow)
220
+ network.nodes[conn.node_from].update_concentration(
221
+ inflow_volume=0,
222
+ inflow_conc=0,
223
+ outflow_volume=q_flow
224
+ )
225
+
226
+ # Update downstream node (inflow)
227
+ network.nodes[conn.node_to].update_concentration(
228
+ inflow_volume=q_flow,
229
+ inflow_conc=network.nodes[conn.node_from].concentration,
230
+ outflow_volume=0
231
+ )
232
+ ```
233
+
234
+ ## Testing
235
+
236
+ Run the comprehensive test suite:
237
+
238
+ ```bash
239
+ pytest tests/test_concentration.py -v
240
+ ```
241
+
242
+ Concentration-focused tests cover:
243
+ - Node defaults and `None` fallback semantics for precipitation/evaporation/inflow/outflow
244
+ - Mass-balance update behavior and concentration tracking toggles
245
+ - MODFLOW 6 RIV AUX concentration assignment (single and multiple GWT models)
246
+ - GWT coupling preflight validation and concentration response in the attached groundwater cell
247
+
248
+ ## Backward Compatibility
249
+
250
+ ✅ **100% backward compatible**
251
+ - Existing code with `track_concentration=False` (default) works unchanged
252
+ - No breaking changes to Node, Connection, or CoupledSimulation
253
+ - Concentration tracking is purely opt-in
254
+
255
+ ## Implementation Details
256
+
257
+ **Modified Files:**
258
+ - `swmod/network.py`: Added `track_concentration` parameter and `update_concentration()` method to Node class
259
+ - `swmod/coupling.py`: Added GWT detection and automatic concentration tracking enablement
260
+
261
+ **Concentration Test File:**
262
+ - `tests/test_concentration.py`: consolidated concentration and concentration-coupling tests
263
+
264
+ **Lines of Code:**
265
+ - ~60 lines added to core (network.py)
266
+ - ~25 lines added to coupling detection
267
+ - ~400 lines of tests
268
+ - 0 lines removed
269
+
270
+ ## Assumptions and Limitations
271
+
272
+ 1. **Instantaneous mixing**: Assumes complete instantaneous mixing within each node (no spatial dispersion)
273
+ 2. **Non-negative concentrations**: Clamped to [0, ∞) to maintain physical validity
274
+ 3. **Fixed storage volume**: Uses node area and stage to calculate storage (can be overridden)
275
+ 4. **Outflow carries current concentration**: Not the mixed concentration (conservative assumption)
276
+ 5. **Manual integration**: Must call `update_concentration()` in simulation loop; not automatically integrated into solver
277
+
278
+ ## Future Enhancements
279
+
280
+ - Automatic concentration updates in CoupledSimulation solver loop
281
+ - Temperature tracking (thermal transport)
282
+ - Multiple species tracking
283
+ - Reactive transport (decay, biodegradation)
284
+ - Longitudinal/transverse dispersion
285
+
286
+ ## References
287
+
288
+ See `tests/test_concentration.py` for working examples and edge cases.
@@ -0,0 +1,44 @@
1
+ import sys
2
+ from pathlib import Path
3
+
4
+ # Add project root to sys.path
5
+ sys.path.insert(0, str(Path(__file__).parent.parent))
6
+
7
+ project = "SWMOD"
8
+ copyright = "2026, Ruben"
9
+ author = "Ruben"
10
+ release = "0.1.0"
11
+
12
+ extensions = [
13
+ "myst_nb",
14
+ "sphinx.ext.autodoc",
15
+ "sphinx.ext.napoleon",
16
+ "sphinx.ext.viewcode",
17
+ "sphinx_rtd_theme",
18
+ ]
19
+
20
+ # MyST-NB settings
21
+ myst_enable_extensions = [
22
+ "colon_fence",
23
+ "dollarmath",
24
+ "amsmath",
25
+ ]
26
+
27
+ # Automatically execute unexecuted clean notebooks during Sphinx build
28
+ nb_execution_mode = "auto"
29
+ nb_execution_allow_errors = True
30
+ nb_execution_timeout = 60
31
+
32
+ templates_path = ["_templates"]
33
+ exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
34
+
35
+ html_theme = "sphinx_rtd_theme"
36
+ html_theme_options = {
37
+ "collapse_navigation": False,
38
+ "navigation_depth": 3,
39
+ "includehidden": True,
40
+ "titles_only": True,
41
+ }
42
+
43
+ html_static_path = []
44
+ html_title = "SWMOD Documentation"
@@ -0,0 +1,67 @@
1
+ # MODFLOW SWMOD Documentation
2
+
3
+ Welcome to the documentation for **`swmod`**, a Python package designed to couple custom surface-water network models (nodes, reaches, control structures) with MODFLOW 6 groundwater flow models using the MODFLOW API (`modflowapi`) and FloPy.
4
+
5
+ ```{toctree}
6
+ :maxdepth: 2
7
+ :caption: Overview
8
+
9
+ purpose_and_scope
10
+ network_elements
11
+ mwe_standalone_sw
12
+ mwe_coupling
13
+ concentration_tracking
14
+ ```
15
+
16
+ ```{toctree}
17
+ :maxdepth: 2
18
+ :caption: Minimal Working Examples
19
+
20
+ ```
21
+
22
+ ```{toctree}
23
+ :maxdepth: 2
24
+ :caption: Tutorials & Examples
25
+
26
+ tutorials/01_two_lakes_and_weir
27
+ tutorials/02_transient_rainfall_event
28
+ tutorials/03_complex_network_bifurcation
29
+ tutorials/04_realworld_example
30
+ ```
31
+
32
+ ---
33
+
34
+ ## Quickstart
35
+
36
+ ### Installation & Environment Setup
37
+
38
+ `swmod` requires Python 3.12 or newer.
39
+
40
+ Set up the Python environment using `uv`:
41
+
42
+ ```bash
43
+ uv sync
44
+ ```
45
+
46
+ ### Download MODFLOW Executables
47
+
48
+ The `.bin` directory (containing `mf6.exe`, `libmf6.dll`, etc.) is not stored in the repository. After cloning, populate it by running:
49
+
50
+ ```bash
51
+ uv run get-modflow .bin
52
+ ```
53
+
54
+ This downloads the official pre-built MODFLOW executables from the [USGS modflow-devtools releases](https://github.com/MODFLOW-USGS/executables) into the `.bin` folder. This step is required on every new machine.
55
+
56
+ ### Run Test Suite
57
+
58
+ ```bash
59
+ uv run pytest -v
60
+ ```
61
+
62
+ ### Build & Host Documentation Locally
63
+
64
+ ```bash
65
+ uv run sphinx-build -b html docs docs/_build/html
66
+ uv run python -m http.server --directory docs/_build/html 8000
67
+ ```