Source code for imod.prepare.topsystem.allocation

"""
This module contains all kinds of utilities to prepare rivers
"""

from enum import Enum

import numpy as np

from imod.common.utilities.layer import create_layered_top
from imod.logging import logger
from imod.schemata import DimsSchema
from imod.select.layers import (
    get_upper_active_grid_cells,
    get_upper_active_layer_number,
)
from imod.typing import GridDataArray, GridDataDict
from imod.util.dims import enforced_dim_order


[docs] class ALLOCATION_OPTION(Enum): """ Enumerator for settings to allocate planar grid with RIV, DRN, GHB, or RCH cells over the vertical layer dimensions. Numbers match the IDEFLAYER options in iMOD 5.6. * ``stage_to_riv_bot``: RIV. Allocate cells spanning from the river stage up to the river bottom elevation. This matches the iMOD 5.6 IDEFLAYER = 0 option. * ``first_active_to_elevation``: RIV, DRN, GHB. Allocate cells spanning from first upper active cell up to the river bottom elevation. This matches the iMOD 5.6 IDEFLAYER = -1 option. * ``stage_to_riv_bot_drn_above``: RIV. Allocate cells spanning from first upper active cell up to the river bottom elevation. Method returns both allocated cells for a river package as well as a drain package. Cells above river stage are allocated as drain cells, cells below are as river cells. This matches the iMOD 5.6 IDEFLAYER = 1 option. * ``at_elevation``: RIV, DRN, GHB. Allocate cells containing the river bottom elevation, drain elevation, or head respectively for river, drain and general head boundary. This matches the iMOD 5.6 IDEFLAYER = 2 option. * ``at_first_active``: RIV, DRN, GHB, RCH. Allocate cells at the upper active cells. This has no equivalent option in iMOD 5.6. Examples -------- >>> from imod.prepare.topsystem import ALLOCATION_OPTION >>> setting = ALLOCATION_OPTION.at_first_active >>> allocated = allocate_rch_cells(setting, active, rate) """ stage_to_riv_bot = 0 first_active_to_elevation = -1 stage_to_riv_bot_drn_above = 1 at_elevation = 2 at_first_active = 9 # Not an iMOD 5.6 option
class LAYERS_USED(Enum): """ What this result represents. It can be either: - ALL: nothing to trim (all layers used) - SOME: some layers used, some empty - NONE: nothing to keep (no layer has any allocated cell) """ ALL = 0 SOME = 1 NONE = 2 PLANAR_GRID = ( DimsSchema("time", "y", "x") | DimsSchema("y", "x") | DimsSchema("time", "{face_dim}") | DimsSchema("{face_dim}") )
[docs] def allocate_riv_cells( allocation_option: ALLOCATION_OPTION, active: GridDataArray, top: GridDataArray, bottom: GridDataArray, stage: GridDataArray, bottom_elevation: GridDataArray, drop_empty_layers: bool = True, ) -> tuple[GridDataArray, GridDataArray | None]: """ Allocate river cells from a planar grid across the vertical dimension. Multiple options are available, which can be selected in ALLOCATION_OPTION. Parameters ---------- allocation_option: ALLOCATION_OPTION Chosen allocation option, can be selected using the ALLOCATION_OPTION enumerator. active: DataArray | UgridDatarray Boolean array containing active model cells. For Modflow 6, this is the equivalent of ``idomain > 0``. top: DataArray | UgridDatarray Grid containing tops of model layers. If has no layer dimension, is assumed as top of upper layer and the other layers are padded with bottom values of the overlying model layer. bottom: DataArray | UgridDatarray Grid containing bottoms of model layers. stage: DataArray | UgridDatarray Planar grid containing river stages. Is not allowed to have a layer dimension. bottom_elevation: DataArray | UgridDatarray Planar grid containing river bottom elevations. Is not allowed to have a layer dimension. drop_empty_layers: bool, default True If True, drop layers that contain no allocated cells anywhere in the domain (or at any time), so the returned grids only span the layers that are actually used. Reduces memory use and speeds up later operations such as regridding, clipping and splitting. If no cells are allocated in any layer, nothing is dropped and the full layer range is returned, as a layer dimension of size 0 is not valid. Note that allocation and conductance distribution are always computed over the full layer range first, so dropping layers does not change the computed values. Returns ------- tuple(DataArray | UgridDatarray) Allocated river cells Examples -------- >>> from imod.prepare.topsystem import ALLOCATION_OPTION, allocate_riv_cells >>> setting = ALLOCATION_OPTION.stage_to_riv_bot >>> allocated = allocate_riv_cells(setting, active, top, bottom, stage, bottom_elevation) """ match allocation_option: case ALLOCATION_OPTION.stage_to_riv_bot: riv_cells, drn_cells = _allocate_cells__stage_to_riv_bot( top, bottom, stage, bottom_elevation ) case ALLOCATION_OPTION.first_active_to_elevation: riv_cells, drn_cells = _allocate_cells__first_active_to_elevation( active, top, bottom, bottom_elevation ) case ALLOCATION_OPTION.stage_to_riv_bot_drn_above: riv_cells, drn_cells = _allocate_cells__stage_to_riv_bot_drn_above( active, top, bottom, stage, bottom_elevation ) case ALLOCATION_OPTION.at_elevation: riv_cells, drn_cells = _allocate_cells__at_elevation( top, bottom, bottom_elevation ) case ALLOCATION_OPTION.at_first_active: riv_cells, drn_cells = _allocate_cells__at_first_active( active, bottom_elevation ) case _: raise ValueError( "Received incompatible setting for rivers, only" f"'{ALLOCATION_OPTION.stage_to_riv_bot.name}' and" f"'{ALLOCATION_OPTION.first_active_to_elevation.name}' and" f"'{ALLOCATION_OPTION.stage_to_riv_bot_drn_above.name}' and" f"'{ALLOCATION_OPTION.at_elevation.name}' and" f"'{ALLOCATION_OPTION.at_first_active.name}' supported." f"got: '{allocation_option.name}'" ) if drop_empty_layers: riv_cells = _drop_empty_layers(riv_cells) if drn_cells is not None: drn_cells = _drop_empty_layers(drn_cells) return riv_cells, drn_cells
[docs] def allocate_drn_cells( allocation_option: ALLOCATION_OPTION, active: GridDataArray, top: GridDataArray, bottom: GridDataArray, elevation: GridDataArray, drop_empty_layers: bool = True, ) -> GridDataArray: """ Allocate drain cells from a planar grid across the vertical dimension. Multiple options are available, which can be selected in ALLOCATION_OPTION. Parameters ---------- allocation_option: ALLOCATION_OPTION Chosen allocation option, can be selected using the ALLOCATION_OPTION enumerator. active: DataArray | UgridDatarray Boolean array containing active model cells. For Modflow 6, this is the equivalent of ``idomain > 0``. top: DataArray | UgridDatarray Grid containing tops of model layers. If has no layer dimension, is assumed as top of upper layer and the other layers are padded with bottom values of the overlying model layer. bottom: DataArray | UgridDatarray Grid containing bottoms of model layers. elevation: DataArray | UgridDatarray Planar grid containing drain elevation. Is not allowed to have a layer dimension. drop_empty_layers: bool, default True If True, drop layers that contain no allocated cells anywhere in the domain (or at any time), so the returned grids only span the layers that are actually used. Reduces memory use and speeds up later operations such as regridding, clipping and splitting. If no cells are allocated in any layer, nothing is dropped and the full layer range is returned, as a layer dimension of size 0 is not valid. Note that allocation and conductance distribution are always computed over the full layer range first, so dropping layers does not change the computed values. Returns ------- DataArray | UgridDatarray Allocated drain cells Examples -------- >>> from imod.prepare.topsystem import ALLOCATION_OPTION, allocate_drn_cells >>> setting = ALLOCATION_OPTION.at_elevation >>> allocated = allocate_drn_cells(setting, active, top, bottom, stage, drain_elevation) """ match allocation_option: case ALLOCATION_OPTION.first_active_to_elevation: result = _allocate_cells__first_active_to_elevation( active, top, bottom, elevation )[0] case ALLOCATION_OPTION.at_elevation: result = _allocate_cells__at_elevation(top, bottom, elevation)[0] case ALLOCATION_OPTION.at_first_active: result = _allocate_cells__at_first_active(active, elevation)[0] case _: raise ValueError( "Received incompatible setting for drains, only" f"'{ALLOCATION_OPTION.first_active_to_elevation.name}', " f"'{ALLOCATION_OPTION.at_elevation.name}' and" f"'{ALLOCATION_OPTION.at_first_active.name}' supported." f"got: '{allocation_option.name}'" ) return _drop_empty_layers(result) if drop_empty_layers else result
[docs] def allocate_ghb_cells( allocation_option: ALLOCATION_OPTION, active: GridDataArray, top: GridDataArray, bottom: GridDataArray, head: GridDataArray, drop_empty_layers: bool = True, ) -> GridDataArray: """ Allocate general head boundary (GHB) cells from a planar grid across the vertical dimension. Multiple options are available, which can be selected in ALLOCATION_OPTION. Parameters ---------- allocation_option: ALLOCATION_OPTION Chosen allocation option, can be selected using the ALLOCATION_OPTION enumerator. active: DataArray | UgridDatarray Boolean array containing active model cells. For Modflow 6, this is the equivalent of ``idomain > 0``. top: DataArray | UgridDatarray Grid containing tops of model layers. If has no layer dimension, is assumed as top of upper layer and the other layers are padded with bottom values of the overlying model layer. bottom: DataArray | UgridDatarray Grid containing bottoms of model layers. head: DataArray | UgridDatarray Planar grid containing general head boundary's head. Is not allowed to have a layer dimension. drop_empty_layers: bool, default True If True, drop layers that contain no allocated cells anywhere in the domain (or at any time), so the returned grids only span the layers that are actually used. Reduces memory use and speeds up later operations such as regridding, clipping and splitting. If no cells are allocated in any layer, nothing is dropped and the full layer range is returned, as a layer dimension of size 0 is not valid. Note that allocation and conductance distribution are always computed over the full layer range first, so dropping layers does not change the computed values. Returns ------- DataArray | UgridDatarray Allocated general head boundary cells Examples -------- >>> from imod.prepare.topsystem import ALLOCATION_OPTION, allocate_ghb_cells >>> setting = ALLOCATION_OPTION.at_elevation >>> allocated = allocate_ghb_cells(setting, active, top, bottom, head) """ match allocation_option: case ALLOCATION_OPTION.first_active_to_elevation: result = _allocate_cells__first_active_to_elevation( active, top, bottom, head )[0] case ALLOCATION_OPTION.at_elevation: result = _allocate_cells__at_elevation(top, bottom, head)[0] case ALLOCATION_OPTION.at_first_active: result = _allocate_cells__at_first_active(active, head)[0] case _: raise ValueError( "Received incompatible setting for general head boundary, only" f"'{ALLOCATION_OPTION.first_active_to_elevation.name}', " f"'{ALLOCATION_OPTION.at_elevation.name}' and" f"'{ALLOCATION_OPTION.at_first_active.name}' supported." f"got: '{allocation_option.name}'" ) return _drop_empty_layers(result) if drop_empty_layers else result
[docs] def allocate_rch_cells( allocation_option: ALLOCATION_OPTION, active: GridDataArray, rate: GridDataArray, drop_empty_layers: bool = True, ) -> GridDataArray: """ Allocate recharge cells from a planar grid across the vertical dimension. Multiple options are available, which can be selected in ALLOCATION_OPTION. Parameters ---------- allocation_option: ALLOCATION_OPTION Chosen allocation option, can be selected using the ALLOCATION_OPTION enumerator. active: DataArray | UgridDataArray Boolean array containing active model cells. For Modflow 6, this is the equivalent of ``idomain > 0``. rate: DataArray | UgridDataArray Array with recharge rates. This will only be used to infer where recharge cells are defined. drop_empty_layers: bool, default True If True, drop layers that contain no allocated cells anywhere in the domain (or at any time), so the returned grids only span the layers that are actually used. Reduces memory use and speeds up later operations such as regridding, clipping and splitting. If no cells are allocated in any layer, nothing is dropped and the full layer range is returned, as a layer dimension of size 0 is not valid. Note that allocation and conductance distribution are always computed over the full layer range first, so dropping layers does not change the computed values. Returns ------- DataArray | UgridDataArray Allocated recharge cells Examples -------- >>> from imod.prepare.topsystem import ALLOCATION_OPTION, allocate_rch_cells >>> setting = ALLOCATION_OPTION.at_first_active >>> allocated = allocate_rch_cells(setting, active, rate) """ match allocation_option: case ALLOCATION_OPTION.at_first_active: result = _allocate_cells__at_first_active(active, rate)[0] case _: raise ValueError( "Received incompatible setting for recharge, only" f"'{ALLOCATION_OPTION.at_first_active.name}' supported." f"got: '{allocation_option.name}'" ) return _drop_empty_layers(result) if drop_empty_layers else result
def _is_layered(grid: GridDataArray) -> bool: return "layer" in grid.sizes and grid.sizes["layer"] > 1 def _enforce_layered_top(top: GridDataArray, bottom: GridDataArray) -> GridDataArray: if _is_layered(top): return top else: return create_layered_top(bottom, top) def get_above_lower_bound( bottom_elevation: GridDataArray, top_layered: GridDataArray ) -> GridDataArray: """ Returns boolean array that indicates cells are above the lower vertical limit of the topsystem. These are the cells located above the bottom_elevation grid or in the first layer. """ top_layer_label = {"layer": min(top_layered.coords["layer"])} is_above_lower_bound = bottom_elevation < top_layered # Bottom elevation above top surface is allowed, so these are set to True # regardless. is_above_lower_bound.loc[top_layer_label] = ~bottom_elevation.isnull() return is_above_lower_bound @enforced_dim_order def _allocate_cells__stage_to_riv_bot( top: GridDataArray, bottom: GridDataArray, stage: GridDataArray, bottom_elevation: GridDataArray, ) -> tuple[GridDataArray, None]: """ Allocate cells inbetween river stage and river bottom_elevation. Compared to iMOD5.6, this is similar to setting IDEFFLAYER=0 in the RUNFILE function. Note that ``stage`` and ``bottom_elevation`` are not allowed to have a layer dimension. Parameters ---------- top: GridDataArray top of model layers bottom: GridDataArray bottom of model layers stage: GridDataArray river stage, cannot contain a layer dimension. Can contain a time dimension. bottom_elevation: GridDataArray river bottom elevation, cannot contain a layer dimension. Can contain a time dimension. Returns ------- GridDataArray River cells """ PLANAR_GRID.validate(stage) PLANAR_GRID.validate(bottom_elevation) top_layered = _enforce_layered_top(top, bottom) is_above_lower_bound = get_above_lower_bound(bottom_elevation, top_layered) is_below_upper_bound = stage >= bottom riv_cells = is_below_upper_bound & is_above_lower_bound return riv_cells, None @enforced_dim_order def _allocate_cells__first_active_to_elevation( active: GridDataArray, top: GridDataArray, bottom: GridDataArray, bottom_elevation: GridDataArray, ) -> tuple[GridDataArray, None]: """ Allocate cells inbetween first active layer and river bottom elevation. Compared to iMOD5.6, this is similar to setting IDEFFLAYER=-1 in the RUNFILE function. Note that ``bottom_elevation`` is not allowed to have a layer dimension. Parameters ---------- active: GridDataArray active model cells top: GridDataArray top of model layers bottom: GridDataArray bottom of model layers bottom_elevation: GridDataArray river bottom elevation, cannot contain a layer dimension. Can contain a time dimension. Returns ------- GridDataArray River cells """ PLANAR_GRID.validate(bottom_elevation) upper_active_layer = get_upper_active_layer_number(active) layer = active.coords["layer"] top_layered = _enforce_layered_top(top, bottom) is_above_lower_bound = get_above_lower_bound(bottom_elevation, top_layered) is_below_upper_bound = upper_active_layer <= layer riv_cells = is_below_upper_bound & is_above_lower_bound & active return riv_cells, None @enforced_dim_order def _allocate_cells__stage_to_riv_bot_drn_above( active: GridDataArray, top: GridDataArray, bottom: GridDataArray, stage: GridDataArray, bottom_elevation: GridDataArray, ) -> tuple[GridDataArray, GridDataArray]: """ Allocate cells inbetween first active layer and river bottom elevation. Cells above river stage are deactivated and returned as drn cells. Compared to iMOD5.6, this is similar to setting IDEFFLAYER=1 in the RUNFILE function. Note that ``stage`` and ``bottom_elevation`` are not allowed to have a layer dimension. Parameters ---------- active: GridDataArray active grid cells top: GridDataArray top of model layers bottom: GridDataArray bottom of model layers stage: GridDataArray river stage, cannot contain a layer dimension. Can contain a time dimension. bottom_elevation: GridDataArray river bottom elevation, cannot contain a layer dimension. Can contain a time dimension. Returns ------- riv_cells: GridDataArray River cells (below stage) drn_cells: GridDataArray Drainage cells (above stage) """ PLANAR_GRID.validate(stage) PLANAR_GRID.validate(bottom_elevation) top_layered = _enforce_layered_top(top, bottom) is_above_lower_bound = get_above_lower_bound(bottom_elevation, top_layered) upper_active_layer = get_upper_active_layer_number(active) layer = active.coords["layer"] is_below_upper_bound = upper_active_layer <= layer is_below_upper_bound_and_active = is_below_upper_bound & active drn_cells = is_below_upper_bound_and_active & (bottom > stage) riv_cells = (is_below_upper_bound_and_active & is_above_lower_bound) != drn_cells return riv_cells, drn_cells @enforced_dim_order def _allocate_cells__at_elevation( top: GridDataArray, bottom: GridDataArray, elevation: GridDataArray ) -> tuple[GridDataArray, None]: """ Allocate cells in river bottom elevation layer. Compared to iMOD5.6, this is similar to setting IDEFFLAYER=2 in the RUNFILE function. Note that ``bottom_elevation`` is not allowed to have a layer dimension. Parameters ---------- top: GridDataArray top of model layers bottom: GridDataArray bottom of model layers elevation: GridDataArray elevation. Can be river bottom, drain elevation or head of GHB. Cannot contain a layer dimension. Can contain a time dimension. Returns ------- GridDataArray Topsystem cells """ PLANAR_GRID.validate(elevation) top_layered = _enforce_layered_top(top, bottom) is_above_lower_bound = get_above_lower_bound(elevation, top_layered) is_below_upper_bound = elevation >= bottom riv_cells = is_below_upper_bound & is_above_lower_bound return riv_cells, None @enforced_dim_order def _allocate_cells__at_first_active( active: GridDataArray, planar_topsystem_grid: GridDataArray, ) -> tuple[GridDataArray, None]: """ Allocate cells inbetween first active layer and river bottom elevation. Compared to iMOD5.6, this is similar to setting IDEFFLAYER=-1 in the RUNFILE function. Note that ``bottom_elevation`` is not allowed to have a layer dimension. Parameters ---------- active: GridDataArray active model cells planar_topsystem_grid: GridDataArray Grid with planar topsystem cells, assumed active where not nan. Returns ------- GridDataArray Topsystem cells """ PLANAR_GRID.validate(planar_topsystem_grid) upper_active = get_upper_active_grid_cells(active) topsystem_upper_active = upper_active & ~np.isnan(planar_topsystem_grid) return topsystem_upper_active, None def _used_layers(mask: GridDataArray) -> tuple[GridDataArray | None, LAYERS_USED]: """ Return the layer coordinate values of ``mask`` that contain at least one True value anywhere in the domain (and, if present, at any timestep), or None if there is nothing to trim (no layer dimension, or every layer has data). Parameters ---------- mask: GridDataArray Boolean array with a "layer" dimension, typically one of the ``allocated`` grids returned by an ``_allocate_cells__*`` function. Returns ------- GridDataArray Layer coordinate values with data. If all or none of the layers have data, this will be None. In the latter case, do not return an empty layer coordinate: a layer dimension of size 0 fails package validation and breaks downstream operations. Keep the full layer range instead, so empty packages can be removed by callers, e.g. ``mask_package__drop_if_empty``. LAYERS_USED Enumerator indicating whether all, none, or some layers are used. """ if "layer" not in mask.dims: return None, LAYERS_USED.ALL if mask.dtype != bool: raise ValueError( f"Expected a boolean grid to drop empty layers from, got: {mask.dtype}" ) # Reduce the mask to the first time step if a time dimension exists for # performance reasons mask = mask.isel(time=0, drop=True, missing_dims="ignore") reduce_dims = [d for d in mask.dims if d != "layer"] has_data_per_layer = mask.any(dim=reduce_dims) # Force to plain numpy/bool to avoid triggering a dask compute deep # inside indexing logic more than once. has_data_per_layer = has_data_per_layer.compute() # Never return an empty layer coordinate: keep the full range and # let callers (e.g. # mask_package__drop_if_empty) remove empty packages. if bool(has_data_per_layer.all()): return None, LAYERS_USED.ALL elif not bool(has_data_per_layer.any()): return None, LAYERS_USED.NONE return mask["layer"].where(has_data_per_layer, drop=True), LAYERS_USED.SOME def _drop_empty_layers(grid: GridDataArray) -> GridDataArray: """ Drop layers that contain no True values in any spatial cell (and, if present, at any timestep). Keeps the `layer` coordinate but only for layers that actually contain data - this is what lets downstream regridding/clipping/masking/splitting operate over a much smaller layer range when the topsystem package only spans a handful of the model's total layers. Parameters ---------- grid: GridDataArray Boolean array with a "layer" dimension, typically the output of one of the ``_allocate_cells__*`` functions. Returns ------- GridDataArray Same array, subset to layers with data. """ used_layers, layers_used_option = _used_layers(grid) name = grid.name if hasattr(grid, "name") else "<unnamed grid>" match layers_used_option: case LAYERS_USED.NONE: logger.warning( f"No layers have data in grid '{name}', the package should be removed by the caller." ) return grid case LAYERS_USED.ALL: return grid case LAYERS_USED.SOME: logger.debug( f"Dropping empty layers from grid '{name}', keeping only used layers." ) return grid.sel(layer=used_layers) case _: raise ValueError( f"Unexpected value for layers_used_option: {layers_used_option}" ) def drop_empty_layers_from_dict( data: GridDataDict, mask: GridDataArray ) -> GridDataDict: """ Trim every layered grid in ``data`` down to the layers that contain data in ``mask``. Unlike :func:`_drop_empty_layers`, this can be applied to grids (such as distributed conductances) that are not themselves boolean, as long as a boolean ``mask`` with the same (full) layer range is available to decide which layers to keep. Parameters ---------- data: GridDataDict Dictionary of grids, e.g. the layered package data returned by ``_allocate_and_distribute_planar_data``. Grids without a "layer" dimension are returned unchanged. mask: GridDataArray Boolean array with a "layer" dimension, e.g. the ``allocated`` grid that was used to build the grids in ``data``. Returns ------- GridDataDict Same dictionary, with every layered grid subset to layers with data. If mask has no True values, data is returned unchanged. To fully remove empty empty packages, additional logic outside this function is required. """ used_layers, layers_used = _used_layers(mask) names = list(data.keys()) match layers_used: case LAYERS_USED.NONE: logger.warning( f"No layers have data in grid '{names}', the package should be removed by the caller." ) return data case LAYERS_USED.ALL: return data # return as-is, let caller handle what to do with LAYERS_USED.NONE or LAYERS_USED.ALL case LAYERS_USED.SOME: logger.debug( f"Dropping empty layers from grid '{names}', keeping only used layers." ) return { key: grid.sel(layer=used_layers) if "layer" in grid.dims else grid for key, grid in data.items() } case _: raise ValueError(f"Unexpected value for layers_used: {layers_used}")