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

Issue #1154 imod coupler #323

Draft
wants to merge 8 commits into
base: coupler_regrid_feature_branch
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"python.testing.pytestArgs": [
"tests"
"."
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,25 @@ Try running:
```sh
pixi run update-git-dependencies
```


##coupling imod-coupler and imod python metsawap regridding branch:


1) create imod_suite directory
2) in it, create a subdirectory imod_coupler and a subdirectory imod-python.
3) check out both projects in their own directory. For imod-python, make sure to
check out the metaswap_regridding_feature branch or a development branch forked from it.
for imod_coupler check out "coupler_regrid_feature_branch" or a development branch forked from it.

4) in the imod suite directory, create a batch file that will overwrite the coupler's
pixi install of imod with the branch we have checked out in the imod-python directory.
This is done for each pixi environment. Assuming we have 2, the batch file looks like this.


xcopy /e /k /h /i imod-python\imod imod_coupler\.pixi\envs\default\Lib\site-packages\imod
xcopy /e /k /h /i imod-python\imod imod_coupler\.pixi\envs\dev\Lib\site-packages\imod


The batch file will overwrite the pixi install( in .pixi\envs\default\Lib\site-packages\imod) with whatever
is checked out locally in the imod-python folder.
302 changes: 302 additions & 0 deletions pixi.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ scipy = "*"
tomli = "*"
tomli-w = "*"
xmipy = "*"
ribasim = ">=2024.10.0,<2024.11"
pytest = ">=8.3.2,<8.4"
pytest-cases = ">=3.8.5,<3.9"

#[pypi-dependencies]
#ribasim = {git = "https://github.com/Deltares/Ribasim.git/#subdirectory=python/ribasim"}
Expand Down
19 changes: 19 additions & 0 deletions pre-processing/primod/metamod.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import tomli_w
from imod.mf6 import Modflow6Simulation
from imod.mf6.utilities.regrid import RegridderWeightsCache
from imod.msw import MetaSwapModel
from imod.typing.grid import GridDataArray

from primod.coupled_model import CoupledModel
from primod.driver_coupling.metamod import MetaModDriverCoupling
Expand Down Expand Up @@ -159,3 +161,20 @@ def write_toml(
tomli_w.dump(coupler_toml, f)

return

def regrid_like(self, new_grid: GridDataArray) -> "MetaMod":
regridded_mf6_simulation = self.mf6_simulation.regrid_like(
"regridded", new_grid, True
)
models = regridded_mf6_simulation.get_models()
dis = list(models.values())[0]["dis"]
regrid_context = RegridderWeightsCache()
regridded_msw = self.msw_model.regrid_like(dis, True, regrid_context)

regridded_metamod = MetaMod(
msw_model=regridded_msw,
mf6_simulation=regridded_mf6_simulation,
coupling_list=self.coupling_list,
)

return regridded_metamod
7 changes: 5 additions & 2 deletions tests/test_imod_coupler/test_metamod.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
import pytest
import tomli
import tomli_w
from common_scripts.mf6_water_balance.combine import create_modflow_waterbalance_file
from imod.mf6 import open_cbc, open_hds
from numpy.testing import assert_array_almost_equal
from primod.metamod import MetaMod
from pytest_cases import parametrize_with_cases
from test_utilities import numeric_csvfiles_equal

from tests.common_scripts.mf6_water_balance.combine import (
create_modflow_waterbalance_file,
)
from tests.test_imod_coupler.test_utilities import numeric_csvfiles_equal


def mf6_output_files(path: Path) -> tuple[Path, Path, Path, Path]:
Expand Down
76 changes: 76 additions & 0 deletions tests/test_imod_coupler/test_metamod_regrid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from collections.abc import Callable
from pathlib import Path

import xarray as xr
from primod.metamod import MetaMod
from pytest_cases import parametrize_with_cases


@parametrize_with_cases("metamod_regrid")
def test_metamod_original(
tmp_path_dev: Path,
metamod_regrid: MetaMod,
metaswap_dll_devel: Path,
metaswap_dll_dep_dir_devel: Path,
modflow_dll_devel: Path,
run_coupler_function: Callable[[Path], None],
) -> None:
"""
Test if coupled models run with the iMOD Coupler development version.
"""
metamod_regrid.write(
tmp_path_dev,
modflow6_dll=modflow_dll_devel,
metaswap_dll=metaswap_dll_devel,
metaswap_dll_dependency=metaswap_dll_dep_dir_devel,
)

run_coupler_function(tmp_path_dev / metamod_regrid._toml_name)

hds = metamod_regrid.mf6_simulation.open_head()
print(hds.sel(layer=2).isel(time=126).values)
pass


@parametrize_with_cases("metamod_regrid")
def test_metamod_regrid(
tmp_path_dev: Path,
metamod_regrid: MetaMod,
metaswap_dll_devel: Path,
metaswap_dll_dep_dir_devel: Path,
modflow_dll_devel: Path,
run_coupler_function: Callable[[Path], None],
) -> None:
"""
Test if coupled models run with the iMOD Coupler development version.
"""

x = [100.0, 200.0, 300.0, 400.0, 500.0]
y = [300.0, 250.0, 200.0, 150.0, 100.0]
dx = 50.0
dy = -50.0
layer = [1, 2, 3]

new_grid = xr.DataArray(
1,
dims=("layer", "y", "x"),
coords={"layer": layer, "y": y, "x": x, "dx": dx, "dy": dy},
)

regridded_metamod = metamod_regrid.regrid_like(new_grid)

regridded_metamod.msw_model.simulation_settings["unsa_svat_path"] = (
metamod_regrid.msw_model.simulation_settings["unsa_svat_path"]
)
regridded_metamod.write(
tmp_path_dev,
modflow6_dll=modflow_dll_devel,
metaswap_dll=metaswap_dll_devel,
metaswap_dll_dependency=metaswap_dll_dep_dir_devel,
)

run_coupler_function(tmp_path_dev / regridded_metamod._toml_name)

hds = regridded_metamod.mf6_simulation.open_head()
print(hds.sel(layer=2).isel(time=126).values)
pass
20 changes: 20 additions & 0 deletions tests/test_imod_coupler/test_metamod_regrid_cases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from imod.mf6 import Modflow6Simulation
from imod.msw import MetaSwapModel
from primod import MetaMod, MetaModDriverCoupling


def case_regrid_no_sprinkling(
coupled_mf6_model: Modflow6Simulation,
prepared_msw_model: MetaSwapModel,
) -> MetaMod:
prepared_msw_model.pop("sprinkling")
coupled_mf6_model["GWF_1"].pop("wells_msw")

driver_coupling = MetaModDriverCoupling(
mf6_model="GWF_1", mf6_recharge_package="rch_msw"
)
return MetaMod(
prepared_msw_model,
coupled_mf6_model,
coupling_list=[driver_coupling],
)
Loading