Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
54e9c0e
Change type of _regrid_methods from dict to special dataclass
JoerivanEngelen May 23, 2024
adf1928
Implement leftover transport schemes and move
JoerivanEngelen May 23, 2024
b83e626
Continue working on feature
JoerivanEngelen May 23, 2024
b072a69
format
JoerivanEngelen May 23, 2024
8ec4788
Add RegridderMethodType and fix mypy errors
JoerivanEngelen May 23, 2024
82909e7
Format
JoerivanEngelen May 23, 2024
06f0736
Add pydantic dependency
JoerivanEngelen May 23, 2024
e43164d
Fix pydantic error for Interpolator, without method
JoerivanEngelen May 23, 2024
a60de6d
format
JoerivanEngelen May 23, 2024
37ac15a
Update pyproject.toml
JoerivanEngelen May 24, 2024
e298f1d
Rename EmptyRegridderMethod to EmptyRegridMethod, consistent with the…
JoerivanEngelen May 24, 2024
3f937be
Clarify scope of test module
JoerivanEngelen May 24, 2024
9e15dc6
Assign config dict to variable to generalize settings
JoerivanEngelen May 24, 2024
06ebb3a
Add unittests
JoerivanEngelen May 24, 2024
231dfd1
Update examples to new API
JoerivanEngelen May 24, 2024
de10c05
Expose RegridMethods under regrid namespace
JoerivanEngelen May 24, 2024
6c982a0
Make _regrid_method class variable instead of instance variable again
JoerivanEngelen May 24, 2024
38e55ef
Preserve line_data attribute
JoerivanEngelen May 24, 2024
3bcd02e
Format
JoerivanEngelen May 24, 2024
dca70a0
Add docstrings
JoerivanEngelen May 24, 2024
5f6c225
Move changelog to the top
JoerivanEngelen May 24, 2024
da60098
Added regrid methods to docs
JoerivanEngelen May 24, 2024
eef2bd4
format
JoerivanEngelen May 24, 2024
0ee7b4e
Update docstring to new API
JoerivanEngelen May 24, 2024
3f6cd7a
Type annotate with RegridMethodType
JoerivanEngelen May 28, 2024
ad4ef6f
Return RegridMethodType, instead of dict
JoerivanEngelen May 28, 2024
a28e9fc
update example
JoerivanEngelen May 28, 2024
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
42 changes: 42 additions & 0 deletions docs/api/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,48 @@ All notable changes to this project will be documented in this file.
The format is based on `Keep a Changelog`_, and this project adheres to
`Semantic Versioning`_.

[Unreleased]
------------

Added
~~~~~
- Added objects with regrid settings. These can be used to provide custom
settings: :class:`imod.mf6.regrid.ConstantHeadRegridMethod`,
:class:`imod.mf6.regrid.DiscretizationRegridMethod`,
:class:`imod.mf6.regrid.DispersionRegridMethod`,
:class:`imod.mf6.regrid.DrainageRegridMethod`,
:class:`imod.mf6.regrid.EmptyRegridMethod`,
:class:`imod.mf6.regrid.EvapotranspirationRegridMethod`,
:class:`imod.mf6.regrid.GeneralHeadBoundaryRegridMethod`,
:class:`imod.mf6.regrid.InitialConditionsRegridMethod`,
:class:`imod.mf6.regrid.MobileStorageTransferRegridMethod`,
:class:`imod.mf6.regrid.NodePropertyFlowRegridMethod`,
:class:`imod.mf6.regrid.RechargeRegridMethod`,
:class:`imod.mf6.regrid.RiverRegridMethod`,
:class:`imod.mf6.regrid.SpecificStorageRegridMethod`,
:class:`imod.mf6.regrid.StorageCoefficientRegridMethod`.

Changed
~~~~~~~
- Instead of providing a dictionary with settings to ``Package.regrid_like``,
provide one of the following ``RegridMethod`` objects:
:class:`imod.mf6.regrid.ConstantHeadRegridMethod`,
:class:`imod.mf6.regrid.DiscretizationRegridMethod`,
:class:`imod.mf6.regrid.DispersionRegridMethod`,
:class:`imod.mf6.regrid.DrainageRegridMethod`,
:class:`imod.mf6.regrid.EmptyRegridMethod`,
:class:`imod.mf6.regrid.EvapotranspirationRegridMethod`,
:class:`imod.mf6.regrid.GeneralHeadBoundaryRegridMethod`,
:class:`imod.mf6.regrid.InitialConditionsRegridMethod`,
:class:`imod.mf6.regrid.MobileStorageTransferRegridMethod`,
:class:`imod.mf6.regrid.NodePropertyFlowRegridMethod`,
:class:`imod.mf6.regrid.RechargeRegridMethod`,
:class:`imod.mf6.regrid.RiverRegridMethod`,
:class:`imod.mf6.regrid.SpecificStorageRegridMethod`,
:class:`imod.mf6.regrid.StorageCoefficientRegridMethod`.



[0.17.1] - 2024-05-16
---------------------

Expand Down
4 changes: 2 additions & 2 deletions docs/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This page provides an auto-generated summary of imod's API.
.. toctree::
:maxdepth: 2

changelog

io
prepare
select
Expand All @@ -19,5 +21,3 @@ This page provides an auto-generated summary of imod's API.
flow
msw
metamod

changelog
27 changes: 27 additions & 0 deletions docs/api/mf6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,30 @@ Transport Packages
MassSourceLoading
SourceSinkMixing
SourceSinkMixing.from_flow_model


.. currentmodule:: imod.mf6.regrid

Regrid
======

Regrid Method Settings
----------------------

.. autosummary::
:toctree: generated/mf6/regrid

ConstantHeadRegridMethod
DiscretizationRegridMethod
DispersionRegridMethod
DrainageRegridMethod
EmptyRegridMethod
EvapotranspirationRegridMethod
GeneralHeadBoundaryRegridMethod
InitialConditionsRegridMethod
MobileStorageTransferRegridMethod
NodePropertyFlowRegridMethod
RechargeRegridMethod
RiverRegridMethod
SpecificStorageRegridMethod
StorageCoefficientRegridMethod
3 changes: 2 additions & 1 deletion examples/mf6/different_ways_to_regrid_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import xarray as xr
from example_models import create_twri_simulation

from imod.mf6.regrid import NodePropertyFlowRegridMethod
from imod.mf6.utilities.regrid import RegridderType, RegridderWeightsCache

# %%
Expand Down Expand Up @@ -98,7 +99,7 @@
# a performance increase if that package uses the same regridding method,
# because initializing a regridder is costly.

regridder_types = {"k": (RegridderType.CENTROIDLOCATOR, None)}
regridder_types = NodePropertyFlowRegridMethod(k=(RegridderType.CENTROIDLOCATOR,))
regrid_context = RegridderWeightsCache()
npf_regridded = model["npf"].regrid_like(
target_grid=target_grid,
Expand Down
7 changes: 5 additions & 2 deletions examples/user-guide/08-regridding.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,10 @@
# Regrid the recharge package with a custom regridder. In this case we opt
# for the centroid locator regridder. This regridder is similar to using a
# "nearest neighbour" lookup.
from imod.mf6.regrid import RechargeRegridMethod
from imod.mf6.utilities.regrid import RegridderType

regridder_types = {"rate": (RegridderType.CENTROIDLOCATOR, None)}
regridder_types = RechargeRegridMethod(rate=(RegridderType.CENTROIDLOCATOR,))

regridded_recharge = original_rch_package.regrid_like(
target_grid,
Expand Down Expand Up @@ -412,6 +413,8 @@ def plot_histograms_side_by_side(array_original, array_regridded, title):
#
# This code snippet prints all default methods:
#
from dataclasses import asdict

import pandas as pd

from imod.tests.fixtures.package_instance_creation import ALL_PACKAGE_INSTANCES
Expand All @@ -428,7 +431,7 @@ def plot_histograms_side_by_side(array_original, array_regridded, title):
for pkg in ALL_PACKAGE_INSTANCES:
if hasattr(pkg, "_regrid_method"):
package_name = type(pkg).__name__
regrid_methods = pkg._regrid_method
regrid_methods = asdict(pkg.get_regrid_methods())
for array_name in regrid_methods.keys():
method_name = regrid_methods[array_name][0].name
function_name = ""
Expand Down
6 changes: 0 additions & 6 deletions imod/mf6/adv.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@
"""

from copy import deepcopy
from typing import Optional, Tuple

from imod.mf6.interfaces.iregridpackage import IRegridPackage
from imod.mf6.package import Package
from imod.mf6.utilities.regrid import RegridderType


class Advection(Package, IRegridPackage):
_pkg_id = "adv"
_template = Package._initialize_template(_pkg_id)
_regrid_method: dict[str, tuple[RegridderType, str]] = {}

def __init__(self, scheme: str):
dict_dataset = {"scheme": scheme}
Expand All @@ -37,9 +34,6 @@ def mask(self, _) -> Package:
"""
return deepcopy(self)

def get_regrid_methods(self) -> Optional[dict[str, Tuple[RegridderType, str]]]:
return self._regrid_method


class AdvectionUpstream(Advection):
"""
Expand Down
17 changes: 3 additions & 14 deletions imod/mf6/chd.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from typing import Optional, Tuple

import numpy as np

from imod.logging import init_log_decorator
from imod.mf6.boundary_condition import BoundaryCondition
from imod.mf6.interfaces.iregridpackage import IRegridPackage
from imod.mf6.utilities.regrid import RegridderType
from imod.mf6.regrid.regrid_schemes import ConstantHeadRegridMethod
from imod.mf6.validation import BOUNDARY_DIMS_SCHEMA, CONC_DIMS_SCHEMA
from imod.schemata import (
AllInsideNoDataSchema,
Expand Down Expand Up @@ -108,14 +106,7 @@ class ConstantHead(BoundaryCondition, IRegridPackage):
_keyword_map = {}
_auxiliary_data = {"concentration": "species"}
_template = BoundaryCondition._initialize_template(_pkg_id)

_regrid_method = {
"head": (
RegridderType.OVERLAP,
"mean",
), # TODO: should be set to barycentric once supported
"concentration": (RegridderType.OVERLAP, "mean"),
}
_regrid_method = ConstantHeadRegridMethod()
Copy link
Contributor

Choose a reason for hiding this comment

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

why don't you make it an object variable instead of a class variable? Then tweaking these methods would be very readable:

my_special_ConstantHead_package._regrid_method.concentration = Barycentric

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As I wrote in the description:

I kept _regrid_method as a class variable, as it gave problems as an attribute with imod.mf6.Package.from_file classmethod, where attributes are not forwarded.


@init_log_decorator()
def __init__(
Expand All @@ -141,6 +132,7 @@ def __init__(
"repeat_stress": repeat_stress,
}
super().__init__(dict_dataset)

self._validate_init_schemata(validate)

def _validate(self, schemata, **kwargs):
Expand All @@ -149,6 +141,3 @@ def _validate(self, schemata, **kwargs):
errors = super()._validate(schemata, **kwargs)

return errors

def get_regrid_methods(self) -> Optional[dict[str, Tuple[RegridderType, str]]]:
return self._regrid_method
8 changes: 0 additions & 8 deletions imod/mf6/cnc.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
from typing import Optional, Tuple

import numpy as np

from imod.logging import init_log_decorator
from imod.mf6.boundary_condition import BoundaryCondition
from imod.mf6.interfaces.iregridpackage import IRegridPackage
from imod.mf6.utilities.regridding_types import RegridderType
from imod.mf6.validation import BOUNDARY_DIMS_SCHEMA
from imod.schemata import (
AllInsideNoDataSchema,
Expand Down Expand Up @@ -53,8 +50,6 @@ class ConstantConcentration(BoundaryCondition, IRegridPackage):
_period_data = ("concentration",)
_template = BoundaryCondition._initialize_template(_pkg_id)

_regrid_method: dict[str, tuple[RegridderType, str]] = {}

_init_schemata = {
"concentration": [
DTypeSchema(np.floating),
Expand Down Expand Up @@ -91,6 +86,3 @@ def __init__(
}
super().__init__(dict_dataset)
self._validate_init_schemata(validate)

def get_regrid_methods(self) -> Optional[dict[str, Tuple[RegridderType, str]]]:
return self._regrid_method
14 changes: 3 additions & 11 deletions imod/mf6/dis.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pathlib
from typing import List, Optional, Tuple
from typing import List

import numpy as np

Expand All @@ -8,7 +8,7 @@
from imod.mf6.interfaces.imaskingsettings import IMaskingSettings
from imod.mf6.interfaces.iregridpackage import IRegridPackage
from imod.mf6.package import Package
from imod.mf6.utilities.regrid import RegridderType
from imod.mf6.regrid.regrid_schemes import DiscretizationRegridMethod
from imod.mf6.validation import DisBottomSchema
from imod.schemata import (
ActiveCellsConnectedSchema,
Expand Down Expand Up @@ -85,12 +85,7 @@ class StructuredDiscretization(Package, IRegridPackage, IMaskingSettings):
_grid_data = {"top": np.float64, "bottom": np.float64, "idomain": np.int32}
_keyword_map = {"bottom": "botm"}
_template = Package._initialize_template(_pkg_id)

_regrid_method = {
"top": (RegridderType.OVERLAP, "mean"),
"bottom": (RegridderType.OVERLAP, "mean"),
"idomain": (RegridderType.OVERLAP, "mode"),
}
_regrid_method = DiscretizationRegridMethod()

@property
def skip_variables(self) -> List[str]:
Expand Down Expand Up @@ -151,6 +146,3 @@ def _validate(self, schemata, **kwargs):
errors = super()._validate(schemata, **kwargs)

return errors

def get_regrid_methods(self) -> Optional[dict[str, Tuple[RegridderType, str]]]:
return self._regrid_method
14 changes: 3 additions & 11 deletions imod/mf6/disv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Optional, Tuple
from typing import List

import numpy as np
import pandas as pd
Expand All @@ -7,7 +7,7 @@
from imod.mf6.interfaces.imaskingsettings import IMaskingSettings
from imod.mf6.interfaces.iregridpackage import IRegridPackage
from imod.mf6.package import Package
from imod.mf6.utilities.regrid import RegridderType
from imod.mf6.regrid.regrid_schemes import DiscretizationRegridMethod
from imod.mf6.validation import DisBottomSchema
from imod.mf6.write_context import WriteContext
from imod.schemata import (
Expand Down Expand Up @@ -67,12 +67,7 @@ class VerticesDiscretization(Package, IRegridPackage, IMaskingSettings):
_grid_data = {"top": np.float64, "bottom": np.float64, "idomain": np.int32}
_keyword_map = {"bottom": "botm"}
_template = Package._initialize_template(_pkg_id)

_regrid_method = {
"top": (RegridderType.OVERLAP, "mean"),
"bottom": (RegridderType.OVERLAP, "mean"),
"idomain": (RegridderType.OVERLAP, "mode"),
}
_regrid_method = DiscretizationRegridMethod()

@property
def skip_variables(self) -> List[str]:
Expand Down Expand Up @@ -159,6 +154,3 @@ def _validate(self, schemata, **kwargs):
errors = super()._validate(schemata, **kwargs)

return errors

def get_regrid_methods(self) -> Optional[dict[str, Tuple[RegridderType, str]]]:
return self._regrid_method
15 changes: 2 additions & 13 deletions imod/mf6/drn.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from typing import Optional, Tuple

import numpy as np

from imod.logging import init_log_decorator
from imod.mf6.boundary_condition import BoundaryCondition
from imod.mf6.interfaces.iregridpackage import IRegridPackage
from imod.mf6.utilities.regrid import RegridderType
from imod.mf6.regrid.regrid_schemes import DrainageRegridMethod
from imod.mf6.validation import BOUNDARY_DIMS_SCHEMA, CONC_DIMS_SCHEMA
from imod.schemata import (
AllInsideNoDataSchema,
Expand Down Expand Up @@ -110,12 +108,7 @@ class Drainage(BoundaryCondition, IRegridPackage):
_keyword_map = {}
_template = BoundaryCondition._initialize_template(_pkg_id)
_auxiliary_data = {"concentration": "species"}

_regrid_method = {
"elevation": (RegridderType.OVERLAP, "mean"),
"conductance": (RegridderType.RELATIVEOVERLAP, "conductance"),
"concentration": (RegridderType.OVERLAP, "mean"),
}
_regrid_method = DrainageRegridMethod()

@init_log_decorator()
def __init__(
Expand Down Expand Up @@ -143,7 +136,6 @@ def __init__(
"repeat_stress": repeat_stress,
}
super().__init__(dict_dataset)

self._validate_init_schemata(validate)

def _validate(self, schemata, **kwargs):
Expand All @@ -152,6 +144,3 @@ def _validate(self, schemata, **kwargs):
errors = super()._validate(schemata, **kwargs)

return errors

def get_regrid_methods(self) -> Optional[dict[str, Tuple[RegridderType, str]]]:
return self._regrid_method
Loading