.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "user-guide\08-regridding.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_user-guide_08-regridding.py: Regridding ========== Most MF6 packages have spatial data arrays as input. These arrays are discrete: they are defined over the simulation grid and contain values associated to each cell. Regridding these package means: create a new package with spatial data arrays defined over a different grid. Computing what the values in these new arrays should be, is done by xugrid. xugrid will compute a regridded array based on: - the original array - the original discretization (this is described in the coordinates of the original arry) - the new discretization - a regridding method More information on the available regridding methods can be found in the xugrid documentation https://deltares.github.io/xugrid/user_guide.html The regridding method that should be used depends on the property being regridded. For example a thermodynamically intensive property (whose value do not depend intrinsically on the grid block size) such as temperature or density can be regridded by an averaging approach (for upscaling) or sampling (for downscaling). Extensive properties (whose values do depend on the grid block size) include (water) mass and pore volume of a gridblock, and the regridding method should be chosen to reflect that. Finally regridding methods for conductivity-like properties follow the rules for parallel or serial resistors- at least when the tensor rotation angles are constant or comparable in the involved gridblocks. Note that the different regridding methods may have a different output domain when regridding: if the original array has no-data values in some cells, then the output array may have no-data values as well, and where these end up depends on the chosen regridding method. Also note that regridding is only possible in the xy-plane, and not across the layer dimension. The output array will have the same number of layers as the input array. .. GENERATED FROM PYTHON SOURCE LINES 40-42 .. code-block:: Python :dedent: 1 .. GENERATED FROM PYTHON SOURCE LINES 44-108 Obtaining the final (i)domain ============================= In many real-world models, some cells will be inactive or marked as "vertical passthrough" (VPT) in the idomain array of the simulation. Some packages require that all cells that are inactictive or VPT in idomain are excluded from the package as well. An example is the npf package: cells that are inactive or VPT in idomain, should not have conductivity data in the npf package. Therefore at the end of the regridding process, a final step consists in enforcing consistency between those of idomain and all the packages. This is a 2-step process: 1) for cells that do not have inputs in crucial packages like npf or storage, idomain will be set to inactive. 2) for cells that are marked as inactive or VPT in idomain, all package inputs will be removed from all the packages This synchronization step between idomain and the packages is automated, and it is carried out when calling regrid_like on the simulation object or the model object. There are 2 caveats: 1) the synchronization between idomain and the package domains is done on the model-level. If we have a simulation containing both a flow model and a transport model then idomain for flow is determined independent from that for transport. These models may therefore end up using different domains (this may lead to undesired results, so a manual synchronization may be necessary between the flow regridding and the transport regridding) This manual synchronization can be done using the "mask_all_packages" function- this function removes input from all packages that are marked as inactive or VPT in the idomain passed to this method. 2) The idomain/packages synchronization step is carried out when regridding a model, and when regridding a model it will use default methods for all the packages. So if you have regridded some packages yourself with non-default methods, then these are not taken into account during this synchonization step. Regridding using default methods ================================ The regrid_like function is available on packages, models and simulations. When the default methods are acceptable, regridding the whole simulation is the most convenient from a user-perspective. Regridding using non-default methods ==================================== When non-default methods are used for one or more packages, these should be regridded separately. In that case, the most convenient approach is likely: - pop the packages that should use non-default methods from the source simulation (the popping is optional, and is only recommended for packages whose presence is not mandatory for validation.) - regrid the source simulation: this takes care of all the packages that should use default methods. - regrid the package(s) where you want to use non-standard rergridding methods indivudually starting from the packages in the source simulation - insert the custom-regridded packages to the regridded simulation (or replace the package regridded with default methods with the one you just regridded with non-default methods if it was not popped) In code, consider an example where we want to regrid the recharge package using non default methods then we would do the following. First we'll load some example simulation. There is a separate example contained in :doc:`/examples/mf6/hondsrug` that you should look at if you are interested in the model building .. GENERATED FROM PYTHON SOURCE LINES 108-114 .. code-block:: Python import imod tmpdir = imod.util.temporary_directory() original_simulation = imod.data.hondsrug_simulation(tmpdir / "hondsrug_saved") .. GENERATED FROM PYTHON SOURCE LINES 115-117 To reduce computational overhead for this example, we are going to clip off most of the timesteps. .. GENERATED FROM PYTHON SOURCE LINES 117-120 .. code-block:: Python original_simulation = original_simulation.clip_box(time_max="2010-01-01") .. GENERATED FROM PYTHON SOURCE LINES 121-122 Let's take a look at the original discretization: .. GENERATED FROM PYTHON SOURCE LINES 122-125 .. code-block:: Python original_simulation["GWF"]["dis"] .. raw:: html
StructuredDiscretization
<xarray.Dataset> Size: 11MB
    Dimensions:  (x: 500, y: 200, layer: 13)
    Coordinates:
      * x        (x) float64 4kB 2.375e+05 2.375e+05 2.376e+05 ... 2.5e+05 2.5e+05
      * y        (y) float64 2kB 5.64e+05 5.64e+05 5.639e+05 ... 5.59e+05 5.59e+05
        dx       float64 8B ...
        dy       float64 8B ...
      * layer    (layer) int32 52B 1 2 3 4 5 6 7 8 9 10 11 12 13
    Data variables:
        idomain  (layer, y, x) int32 5MB -1 -1 -1 -1 -1 -1 -1 -1 ... 1 1 1 1 1 1 1 1
        top      (y, x) float32 400kB 6.329 6.329 6.329 6.329 ... 3.198 3.198 3.198
        bottom   (layer, y, x) float32 5MB 6.329 6.329 6.329 ... -166.8 -166.8


.. GENERATED FROM PYTHON SOURCE LINES 126-127 We want to regrid this to the following target grid: .. GENERATED FROM PYTHON SOURCE LINES 127-143 .. code-block:: Python import xarray as xr import imod dx = 100 dy = -100 xmin = 237500.0 xmax = 250000.0 ymin = 559000.0 ymax = 564000.0 target_grid = imod.util.empty_2d( dx=dx, dy=dy, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax ) target_grid .. raw:: html
<xarray.DataArray (y: 50, x: 125)> Size: 50kB
    array([[nan, nan, nan, ..., nan, nan, nan],
           [nan, nan, nan, ..., nan, nan, nan],
           [nan, nan, nan, ..., nan, nan, nan],
           ...,
           [nan, nan, nan, ..., nan, nan, nan],
           [nan, nan, nan, ..., nan, nan, nan],
           [nan, nan, nan, ..., nan, nan, nan]])
    Coordinates:
      * x        (x) float64 1kB 2.376e+05 2.376e+05 2.378e+05 ... 2.498e+05 2.5e+05
      * y        (y) float64 400B 5.64e+05 5.638e+05 ... 5.592e+05 5.59e+05
        dx       float64 8B 100.0
        dy       float64 8B -100.0


.. GENERATED FROM PYTHON SOURCE LINES 144-146 This is a grid of nans, and we require a grid of ones, which can create with xarray: .. GENERATED FROM PYTHON SOURCE LINES 146-150 .. code-block:: Python target_grid = xr.ones_like(target_grid) target_grid .. raw:: html
<xarray.DataArray (y: 50, x: 125)> Size: 50kB
    array([[1., 1., 1., ..., 1., 1., 1.],
           [1., 1., 1., ..., 1., 1., 1.],
           [1., 1., 1., ..., 1., 1., 1.],
           ...,
           [1., 1., 1., ..., 1., 1., 1.],
           [1., 1., 1., ..., 1., 1., 1.],
           [1., 1., 1., ..., 1., 1., 1.]])
    Coordinates:
      * x        (x) float64 1kB 2.376e+05 2.376e+05 2.378e+05 ... 2.498e+05 2.5e+05
      * y        (y) float64 400B 5.64e+05 5.638e+05 ... 5.592e+05 5.59e+05
        dx       float64 8B 100.0
        dy       float64 8B -100.0


.. GENERATED FROM PYTHON SOURCE LINES 151-152 Next, we'll remove the recharge package and obtain it as a variable: .. GENERATED FROM PYTHON SOURCE LINES 152-157 .. code-block:: Python original_rch_package = original_simulation["GWF"].pop("rch") original_rch_package .. raw:: html
Recharge
<xarray.Dataset> Size: 21MB
    Dimensions:        (y: 200, x: 500, layer: 13, time: 2)
    Coordinates:
      * y              (y) float64 2kB 5.64e+05 5.64e+05 ... 5.59e+05 5.59e+05
      * x              (x) float64 4kB 2.375e+05 2.375e+05 ... 2.5e+05 2.5e+05
        dx             float64 8B ...
        dy             float64 8B ...
      * layer          (layer) int32 52B 1 2 3 4 5 6 7 8 9 10 11 12 13
      * time           (time) datetime64[ns] 16B 2009-12-30T23:59:59 2009-12-31
    Data variables:
        rate           (time, layer, y, x) float64 21MB nan nan nan ... nan nan nan
        print_input    bool 1B False
        print_flows    bool 1B False
        save_flows     bool 1B False
        observations   object 8B None
        repeat_stress  object 8B None


.. GENERATED FROM PYTHON SOURCE LINES 158-159 Now regrid the simulation (without recharge): .. GENERATED FROM PYTHON SOURCE LINES 159-161 .. code-block:: Python regridded_simulation = original_simulation.regrid_like("regridded", target_grid) .. GENERATED FROM PYTHON SOURCE LINES 162-163 Let's look at the discretization again: .. GENERATED FROM PYTHON SOURCE LINES 163-165 .. code-block:: Python regridded_simulation["GWF"]["dis"] .. raw:: html
StructuredDiscretization
<xarray.Dataset> Size: 1MB
    Dimensions:  (layer: 13, y: 50, x: 125)
    Coordinates:
        dx       float64 8B 100.0
        dy       float64 8B -100.0
      * layer    (layer) int32 52B 1 2 3 4 5 6 7 8 9 10 11 12 13
      * y        (y) float64 400B 5.64e+05 5.638e+05 ... 5.592e+05 5.59e+05
      * x        (x) float64 1kB 2.376e+05 2.376e+05 2.378e+05 ... 2.498e+05 2.5e+05
    Data variables:
        idomain  (layer, y, x) int64 650kB -16 -16 -16 -16 -16 -16 ... 1 1 1 1 1 1
        top      (y, x) float32 25kB nan nan nan nan nan nan ... nan nan nan nan nan
        bottom   (layer, y, x) float32 325kB 6.329 6.432 6.536 ... -166.8 -166.8


.. GENERATED FROM PYTHON SOURCE LINES 166-167 All packages have been regridded, for example the NPF package: .. GENERATED FROM PYTHON SOURCE LINES 167-170 .. code-block:: Python regridded_simulation["GWF"]["npf"] .. raw:: html
NodePropertyFlow
<xarray.Dataset> Size: 652kB
    Dimensions:                              (layer: 13, y: 50, x: 125)
    Coordinates:
        dx                                   float64 8B 100.0
        dy                                   float64 8B -100.0
      * layer                                (layer) int32 52B 1 2 3 4 ... 11 12 13
      * y                                    (y) float64 400B 5.64e+05 ... 5.59e+05
      * x                                    (x) float64 1kB 2.376e+05 ... 2.5e+05
    Data variables: (12/22)
        icelltype                            int32 4B 0
        k                                    (layer, y, x) float32 325kB nan ... ...
        rewet                                bool 1B False
        rewet_layer                          object 8B None
        rewet_factor                         object 8B None
        rewet_iterations                     object 8B None
        ...                                   ...
        dewatered                            bool 1B True
        perched                              bool 1B True
        save_specific_discharge              bool 1B False
        save_saturation                      bool 1B False
        xt3d_option                          bool 1B False
        rhs_option                           bool 1B False


.. GENERATED FROM PYTHON SOURCE LINES 171-172 Let's make a comparison plot of the hydraulic conductivities: .. GENERATED FROM PYTHON SOURCE LINES 172-188 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np fig, axes = plt.subplots(nrows=2, sharex=True) plot_kwargs = {"colors": "viridis", "levels": np.linspace(0.0, 100.0, 21), "fig": fig} imod.visualize.spatial.plot_map( original_simulation["GWF"]["npf"]["k"].sel(layer=3), ax=axes[0], **plot_kwargs ) imod.visualize.spatial.plot_map( regridded_simulation["GWF"]["npf"]["k"].sel(layer=3), ax=axes[1], **plot_kwargs ) axes[0].set_ylabel("original") axes[1].set_ylabel("regridded") .. image-sg:: /user-guide/images/sphx_glr_08-regridding_001.png :alt: 08 regridding :srcset: /user-guide/images/sphx_glr_08-regridding_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(28.472222222222243, 0.5, 'regridded') .. GENERATED FROM PYTHON SOURCE LINES 189-192 Set up the input needed for custom regridding. Create a regridder weight-cache. This object can (and should) be reused for all the packages that undergo custom regridding at this stage. .. GENERATED FROM PYTHON SOURCE LINES 192-198 .. code-block:: Python from imod.mf6.utilities.regrid import RegridderWeightsCache regrid_cache = RegridderWeightsCache() regrid_cache .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 199-202 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. .. GENERATED FROM PYTHON SOURCE LINES 202-215 .. code-block:: Python from imod.mf6.regrid import RechargeRegridMethod from imod.mf6.utilities.regrid import RegridderType regridder_types = RechargeRegridMethod(rate=(RegridderType.CENTROIDLOCATOR,)) regridded_recharge = original_rch_package.regrid_like( target_grid, regrid_cache=regrid_cache, regridder_types=regridder_types, ) regridded_recharge .. raw:: html
Recharge
<xarray.Dataset> Size: 1MB
    Dimensions:        (layer: 13, time: 2, y: 50, x: 125)
    Coordinates:
        dx             float64 8B 100.0
        dy             float64 8B -100.0
      * layer          (layer) int32 52B 1 2 3 4 5 6 7 8 9 10 11 12 13
      * time           (time) datetime64[ns] 16B 2009-12-30T23:59:59 2009-12-31
      * y              (y) float64 400B 5.64e+05 5.638e+05 ... 5.592e+05 5.59e+05
      * x              (x) float64 1kB 2.376e+05 2.376e+05 ... 2.498e+05 2.5e+05
    Data variables:
        rate           (time, layer, y, x) float64 1MB nan nan nan ... nan nan nan
        print_input    bool 1B False
        print_flows    bool 1B False
        save_flows     bool 1B False
        observations   object 8B None
        repeat_stress  object 8B None
        fixed_cell     bool 1B False


.. GENERATED FROM PYTHON SOURCE LINES 216-217 Next, add the recharge package to the regridded simulation .. GENERATED FROM PYTHON SOURCE LINES 217-222 .. code-block:: Python regridded_simulation["GWF"]["rch"] = regridded_recharge # We can also reattach the original again original_simulation["GWF"]["rch"] = original_rch_package .. GENERATED FROM PYTHON SOURCE LINES 223-230 Comparison with histograms ========================== In the next segment we will compare the input of the models on different grids. We advice to always check how your input is regridded. In this example we upscaled grid, many input parameters are regridded with a ``mean`` method. This means that their input range is reduced, which can be seen in tailings in the histograms becoming shorter .. GENERATED FROM PYTHON SOURCE LINES 230-242 .. code-block:: Python def plot_histograms_side_by_side(array_original, array_regridded, title): """This function creates a plot of normalized histograms of the 2 input DataArray. It plots a title above each histogram.""" _, (ax0, ax1) = plt.subplots(1, 2, sharex=True, sharey=True, tight_layout=True) array_original.plot.hist(ax=ax0, bins=25, density=True) array_regridded.plot.hist(ax=ax1, bins=25, density=True) ax0.title.set_text(f"{title} (original)") ax1.title.set_text(f"{title} (regridded)") .. GENERATED FROM PYTHON SOURCE LINES 243-244 Compare constant head arrays. .. GENERATED FROM PYTHON SOURCE LINES 244-250 .. code-block:: Python plot_histograms_side_by_side( original_simulation["GWF"]["chd"]["head"], regridded_simulation["GWF"]["chd"]["head"], "chd head", ) .. image-sg:: /user-guide/images/sphx_glr_08-regridding_002.png :alt: chd head (original), chd head (regridded) :srcset: /user-guide/images/sphx_glr_08-regridding_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 251-252 Compare horizontal hydraulic conductivities. .. GENERATED FROM PYTHON SOURCE LINES 252-257 .. code-block:: Python plot_histograms_side_by_side( original_simulation["GWF"]["npf"]["k"], regridded_simulation["GWF"]["npf"]["k"], "npf k", ) .. image-sg:: /user-guide/images/sphx_glr_08-regridding_003.png :alt: npf k (original), npf k (regridded) :srcset: /user-guide/images/sphx_glr_08-regridding_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 258-259 Compare vertical hydraulic conductivities. .. GENERATED FROM PYTHON SOURCE LINES 259-264 .. code-block:: Python plot_histograms_side_by_side( original_simulation["GWF"]["npf"]["k33"], regridded_simulation["GWF"]["npf"]["k33"], "npf k33", ) .. image-sg:: /user-guide/images/sphx_glr_08-regridding_004.png :alt: npf k33 (original), npf k33 (regridded) :srcset: /user-guide/images/sphx_glr_08-regridding_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 265-266 Compare starting heads. .. GENERATED FROM PYTHON SOURCE LINES 266-272 .. code-block:: Python plot_histograms_side_by_side( original_simulation["GWF"]["ic"]["start"], regridded_simulation["GWF"]["ic"]["start"], "ic start", ) .. image-sg:: /user-guide/images/sphx_glr_08-regridding_005.png :alt: ic start (original), ic start (regridded) :srcset: /user-guide/images/sphx_glr_08-regridding_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 273-274 Compare river stages. .. GENERATED FROM PYTHON SOURCE LINES 274-280 .. code-block:: Python plot_histograms_side_by_side( original_simulation["GWF"]["riv"]["stage"], regridded_simulation["GWF"]["riv"]["stage"], "riv stage", ) .. image-sg:: /user-guide/images/sphx_glr_08-regridding_006.png :alt: riv stage (original), riv stage (regridded) :srcset: /user-guide/images/sphx_glr_08-regridding_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 281-282 Compare river bottom elevations. .. GENERATED FROM PYTHON SOURCE LINES 282-288 .. code-block:: Python plot_histograms_side_by_side( original_simulation["GWF"]["riv"]["bottom_elevation"], regridded_simulation["GWF"]["riv"]["bottom_elevation"], "riv bottom elevation", ) .. image-sg:: /user-guide/images/sphx_glr_08-regridding_007.png :alt: riv bottom elevation (original), riv bottom elevation (regridded) :srcset: /user-guide/images/sphx_glr_08-regridding_007.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 289-290 Compare riverbed conductance. .. GENERATED FROM PYTHON SOURCE LINES 290-296 .. code-block:: Python plot_histograms_side_by_side( original_simulation["GWF"]["riv"]["conductance"], regridded_simulation["GWF"]["riv"]["conductance"], "riv conductance", ) .. image-sg:: /user-guide/images/sphx_glr_08-regridding_008.png :alt: riv conductance (original), riv conductance (regridded) :srcset: /user-guide/images/sphx_glr_08-regridding_008.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 297-298 Compare recharge rates. .. GENERATED FROM PYTHON SOURCE LINES 298-304 .. code-block:: Python plot_histograms_side_by_side( original_simulation["GWF"]["rch"]["rate"], regridded_simulation["GWF"]["rch"]["rate"], "rch rate", ) .. image-sg:: /user-guide/images/sphx_glr_08-regridding_009.png :alt: rch rate (original), rch rate (regridded) :srcset: /user-guide/images/sphx_glr_08-regridding_009.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 305-306 Compare drainage elevations. .. GENERATED FROM PYTHON SOURCE LINES 306-312 .. code-block:: Python plot_histograms_side_by_side( original_simulation["GWF"]["drn-pipe"]["elevation"], regridded_simulation["GWF"]["drn-pipe"]["elevation"], "drn-pipe elevation", ) .. image-sg:: /user-guide/images/sphx_glr_08-regridding_010.png :alt: drn-pipe elevation (original), drn-pipe elevation (regridded) :srcset: /user-guide/images/sphx_glr_08-regridding_010.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 313-314 Compare drain conductances. .. GENERATED FROM PYTHON SOURCE LINES 314-320 .. code-block:: Python plot_histograms_side_by_side( original_simulation["GWF"]["drn-pipe"]["conductance"], regridded_simulation["GWF"]["drn-pipe"]["conductance"], "drn-pipe conductance", ) .. image-sg:: /user-guide/images/sphx_glr_08-regridding_011.png :alt: drn-pipe conductance (original), drn-pipe conductance (regridded) :srcset: /user-guide/images/sphx_glr_08-regridding_011.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 321-322 Let's compare outputs now. .. GENERATED FROM PYTHON SOURCE LINES 322-331 .. code-block:: Python # Write simulation original_simulation.write(tmpdir / "original") # Run and open heads original_simulation.run() hds_original = original_simulation.open_head() hds_original .. raw:: html
<xarray.DataArray 'head' (time: 2, layer: 13, y: 200, x: 500)> Size: 21MB
    dask.array<stack, shape=(2, 13, 200, 500), dtype=float64, chunksize=(1, 13, 200, 500), chunktype=numpy.ndarray>
    Coordinates:
      * x        (x) float64 4kB 2.375e+05 2.375e+05 2.376e+05 ... 2.5e+05 2.5e+05
      * y        (y) float64 2kB 5.64e+05 5.64e+05 5.639e+05 ... 5.59e+05 5.59e+05
        dx       float64 8B 25.0
        dy       float64 8B -25.0
      * layer    (layer) int64 104B 1 2 3 4 5 6 7 8 9 10 11 12 13
      * time     (time) float64 16B 1.157e-05 365.0


.. GENERATED FROM PYTHON SOURCE LINES 332-338 .. code-block:: Python regridded_simulation.write(tmpdir / "regridded", validate=False) regridded_simulation.run() hds_regridded = regridded_simulation.open_head() hds_regridded .. raw:: html
<xarray.DataArray 'head' (time: 2, layer: 13, y: 50, x: 125)> Size: 1MB
    dask.array<stack, shape=(2, 13, 50, 125), dtype=float64, chunksize=(1, 13, 50, 125), chunktype=numpy.ndarray>
    Coordinates:
      * x        (x) float64 1kB 2.376e+05 2.376e+05 2.378e+05 ... 2.498e+05 2.5e+05
      * y        (y) float64 400B 5.64e+05 5.638e+05 ... 5.592e+05 5.59e+05
        dx       float64 8B 100.0
        dy       float64 8B -100.0
      * layer    (layer) int64 104B 1 2 3 4 5 6 7 8 9 10 11 12 13
      * time     (time) float64 16B 1.157e-05 365.0


.. GENERATED FROM PYTHON SOURCE LINES 339-340 Let's make a comparison plot of the regridded heads. .. GENERATED FROM PYTHON SOURCE LINES 340-353 .. code-block:: Python fig, axes = plt.subplots(nrows=2, sharex=True) plot_kwargs = {"colors": "viridis", "levels": np.linspace(0.0, 11.0, 12), "fig": fig} imod.visualize.spatial.plot_map( hds_original.isel(layer=6, time=-1), ax=axes[0], **plot_kwargs ) imod.visualize.spatial.plot_map( hds_regridded.isel(layer=6, time=-1), ax=axes[1], **plot_kwargs ) axes[0].set_ylabel("original") axes[1].set_ylabel("regridded") .. image-sg:: /user-guide/images/sphx_glr_08-regridding_012.png :alt: 08 regridding :srcset: /user-guide/images/sphx_glr_08-regridding_012.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(28.472222222222243, 0.5, 'regridded') .. GENERATED FROM PYTHON SOURCE LINES 354-416 A note on regridding conductivity ================================= In the npf package, it is possible to use for definining the conductivity tensor: - 1 array (K) - 2 arrays (K and K22) - 3 arrays (K, K22, K33) If 1 array is given the tensor is called isotropic. Defining only K gives the same behavior as specifying K, K22 and K33 with the same value. When regridding, K33 has a default method different from that of K and K22, but it can only be applied if K33 exists in the source model in the first place. So it is recommended to introduce K33 as a separate array in the source model even if it is isotropic. Also note that default regridding methods were chosen assuming that K and K22 are roughly horizontal and K33 roughly vertical. But this may not be the case if the input arrays angle2 and/or angle3 have large values. Regridding boundary conditions ============================== Special care must be taken when regridding boundary conditions, and it is recommended that users verify the balance output of a regridded simulation and compare it to the original model. If the regridded simulation is a good representation of the original simulation, the mass contributions on the balance by the different boundary conditions should be comparable in both simulations. To achieve this, it may be necessary to tweak the input or the regridding methods. An example of this is upscaling recharge (so the target grid has coarser cells than the source grid). Its default method is averaging, with the following rules: - if a cell in the source grid is inactive in the source recharge package (meaning no recharge), it will not count when averaging. So if a target cell has partial overlap with one source recharge cell, and the rest of the target cell has no overlap with any source active recharge cell, it will get the recharge of the one cell it has overlap with. But since the target cell is larger, this effectively means the regridded recharge will be more in the regridded simulation than it was in the source simulation - but we do the same regridding this time assigning a zero recharge to cells without recharge then the averaging will take the zero-recharge cells into account and the regridded recharge will be the same as the source recharge. A note on regridding transport ============================== Transport simulations can be unstable if constraints related to the grid Peclet number and the courant number are exceeded. This can easily happen when regridding. It may be necessary to reduce the simulation's time step size especially when downscaling, to prevent numerical issues. Increasing dispersivities or the molecular diffusion constant can also help to stabilize the simulation. Inversely, when upscaling, a larger time step size can be acceptable. Unsupported packages ==================== Some packages cannot be regridded. This includes the Lake package and the UZF package. Such packages should be removed from the simulation before regridding, and then new packages should be created by the user and then added to the regridded simulation. This code snippet prints all default methods: .. GENERATED FROM PYTHON SOURCE LINES 416-457 .. code-block:: Python from dataclasses import asdict import pandas as pd from imod.tests.fixtures.package_instance_creation import ALL_PACKAGE_INSTANCES regrid_method_setup = { "package name": [], "array name": [], "method name": [], "function name": [], } regrid_method_table = pd.DataFrame(regrid_method_setup) counter = 0 for pkg in ALL_PACKAGE_INSTANCES: if hasattr(pkg, "_regrid_method"): package_name = type(pkg).__name__ regrid_methods = asdict(pkg.get_regrid_methods()) for array_name in regrid_methods.keys(): method_name = regrid_methods[array_name][0].name function_name = "" if len(regrid_methods[array_name]) > 0: function_name = regrid_methods[array_name][1] regrid_method_table.loc[counter] = ( package_name, array_name, method_name, function_name, ) counter = counter + 1 # Set multi index to group with packages regrid_method_table = regrid_method_table.set_index(["package name", "array name"]) # Pandas by default displays at max 60 rows, which this table exceeds. nrows = regrid_method_table.shape[0] # Configure pandas to increase the max amount of displayable rows. pd.set_option("display.max_rows", nrows + 1) # Display rows: regrid_method_table .. rst-class:: sphx-glr-script-out .. code-block:: none C:\buildagent\work\4b9080cbb3354582\imod-python\imod\tests\fixtures\package_instance_creation.py:84: UserWarning: Converting non-nanosecond precision datetime values to nanosecond precision. This behavior can eventually be relaxed in xarray, as it is an artifact from pandas which is now beginning to support non-nanosecond precision values. This warning is caused by passing non-nanosecond np.datetime64 or np.timedelta64 values to the DataArray or Variable constructor; it can be silenced by converting the values to nanosecond precision ahead of time. numeric = xr.DataArray( C:\buildagent\work\4b9080cbb3354582\imod-python\imod\tests\fixtures\package_instance_creation.py:84: UserWarning: Converting non-nanosecond precision datetime values to nanosecond precision. This behavior can eventually be relaxed in xarray, as it is an artifact from pandas which is now beginning to support non-nanosecond precision values. This warning is caused by passing non-nanosecond np.datetime64 or np.timedelta64 values to the DataArray or Variable constructor; it can be silenced by converting the values to nanosecond precision ahead of time. numeric = xr.DataArray( .. raw:: html
method name function name
package name array name
StructuredDiscretization top OVERLAP mean
bottom OVERLAP mean
idomain OVERLAP mode
Dispersion diffusion_coefficient OVERLAP mean
longitudinal_horizontal OVERLAP mean
transversal_horizontal1 OVERLAP mean
longitudinal_vertical OVERLAP mean
transversal_horizontal2 OVERLAP mean
transversal_vertical OVERLAP mean
InitialConditions start OVERLAP mean
MobileStorageTransfer porosity OVERLAP mean
decay OVERLAP mean
decay_sorbed OVERLAP mean
bulk_density OVERLAP mean
distcoef OVERLAP mean
sp2 OVERLAP mean
NodePropertyFlow icelltype OVERLAP mode
k OVERLAP geometric_mean
k22 OVERLAP geometric_mean
k33 OVERLAP harmonic_mean
angle1 OVERLAP mean
angle2 OVERLAP mean
angle3 OVERLAP mean
rewet_layer OVERLAP mean
SpecificStorage convertible OVERLAP mode
specific_storage OVERLAP mean
specific_yield OVERLAP mean
StorageCoefficient convertible OVERLAP mode
storage_coefficient OVERLAP mean
specific_yield OVERLAP mean
ConstantHead head OVERLAP mean
concentration OVERLAP mean
ibound OVERLAP mode
Drainage elevation OVERLAP mean
conductance RELATIVEOVERLAP conductance
concentration OVERLAP mean
Evapotranspiration surface OVERLAP mean
rate OVERLAP mean
depth OVERLAP mean
proportion_rate OVERLAP mean
proportion_depth OVERLAP mean
GeneralHeadBoundary head OVERLAP mean
conductance RELATIVEOVERLAP conductance
concentration OVERLAP mean
Recharge rate OVERLAP mean
concentration OVERLAP mean
River stage OVERLAP mean
conductance RELATIVEOVERLAP conductance
bottom_elevation OVERLAP mean
concentration OVERLAP mean
infiltration_factor OVERLAP mean
VerticesDiscretization top OVERLAP mean
bottom OVERLAP mean
idomain OVERLAP mode
Dispersion diffusion_coefficient OVERLAP mean
longitudinal_horizontal OVERLAP mean
transversal_horizontal1 OVERLAP mean
longitudinal_vertical OVERLAP mean
transversal_horizontal2 OVERLAP mean
transversal_vertical OVERLAP mean
InitialConditions start OVERLAP mean
MobileStorageTransfer porosity OVERLAP mean
decay OVERLAP mean
decay_sorbed OVERLAP mean
bulk_density OVERLAP mean
distcoef OVERLAP mean
sp2 OVERLAP mean
NodePropertyFlow icelltype OVERLAP mode
k OVERLAP geometric_mean
k22 OVERLAP geometric_mean
k33 OVERLAP harmonic_mean
angle1 OVERLAP mean
angle2 OVERLAP mean
angle3 OVERLAP mean
rewet_layer OVERLAP mean
SpecificStorage convertible OVERLAP mode
specific_storage OVERLAP mean
specific_yield OVERLAP mean
StorageCoefficient convertible OVERLAP mode
storage_coefficient OVERLAP mean
specific_yield OVERLAP mean
ConstantHead head OVERLAP mean
concentration OVERLAP mean
ibound OVERLAP mode
Drainage elevation OVERLAP mean
conductance RELATIVEOVERLAP conductance
concentration OVERLAP mean
Evapotranspiration surface OVERLAP mean
rate OVERLAP mean
depth OVERLAP mean
proportion_rate OVERLAP mean
proportion_depth OVERLAP mean
GeneralHeadBoundary head OVERLAP mean
conductance RELATIVEOVERLAP conductance
concentration OVERLAP mean
Recharge rate OVERLAP mean
concentration OVERLAP mean
River stage OVERLAP mean
conductance RELATIVEOVERLAP conductance
bottom_elevation OVERLAP mean
concentration OVERLAP mean
infiltration_factor OVERLAP mean


.. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 50.261 seconds) .. _sphx_glr_download_user-guide_08-regridding.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 08-regridding.ipynb <08-regridding.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 08-regridding.py <08-regridding.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 08-regridding.zip <08-regridding.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_