|
| 1 | +from typing import Literal, Optional, Sequence, TypeAlias |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +from imod.mf6.package import Package |
| 6 | +from imod.mf6.utilities.dataset import assign_index |
| 7 | +from imod.schemata import DTypeSchema |
| 8 | + |
| 9 | +ThermalFormulationOption: TypeAlias = Literal["linear", "nonlinear"] |
| 10 | + |
| 11 | + |
| 12 | +class Viscosity(Package): |
| 13 | + """ |
| 14 | + The Viscosity Package in MODFLOW 6 is used to account for the effects of |
| 15 | + solute concentration or temperature on fluid viscosity and thereby their |
| 16 | + effects on hydraulic conductivity and stress-package conductance. If the |
| 17 | + Viscosity package is used, the Groundwater Transport process must also be |
| 18 | + used. In addition, the flow and transport models must be part of the same |
| 19 | + simulation. The Viscosity package will adjust the conductances in the model |
| 20 | + based on the solute concentrations. |
| 21 | +
|
| 22 | + Parameters |
| 23 | + ---------- |
| 24 | +
|
| 25 | + reference_viscosity: float |
| 26 | + Fluid reference viscosity used in the equation of state. |
| 27 | + viscosity_concentration_slope: sequence of floats |
| 28 | + Slope of the (linear) viscosity concentration line used in the viscosity |
| 29 | + equation of state. This value will be used when ``thermal_formulation`` |
| 30 | + is equal to ``"linear"`` (the default) in the OPTIONS block. When |
| 31 | + ``thermal_formulation`` is set to ``"nonlinear"``, a value for DVISCDC |
| 32 | + must be specified though it is not used. |
| 33 | + reference_concentration: sequence of floats |
| 34 | + Reference concentration used in the viscosity equation of state. |
| 35 | + modelname: sequence of strings |
| 36 | + Name of the GroundwaterTransport (GWT) model used for the |
| 37 | + concentrations. |
| 38 | + species: sequence of str |
| 39 | + Name of the species used to calculate a viscosity value. |
| 40 | + temperature_species_name: str |
| 41 | + Name of the species to be interpreted as temperature. This species is |
| 42 | + used to calculate the temperature-dependent viscosity, using all |
| 43 | + thermal_ arguments. |
| 44 | + thermal_formulation: str, optional |
| 45 | + The thermal formulation to use for the temperature-dependent viscosity. |
| 46 | + thermal_a2: float, optional |
| 47 | + Is an empirical parameter specified by the user for calculating |
| 48 | + viscosity using a nonlinear formulation. If thermal_a2 is not specified, |
| 49 | + a default value of 10.0 is assigned (Voss, 1984). |
| 50 | + thermal_a3: float, optional |
| 51 | + Is an empirical parameter specified by the user for calculating |
| 52 | + viscosity using a nonlinear formulation. If thermal_a3 is not specified, |
| 53 | + a default value of 248.37 is assigned (Voss, 1984). |
| 54 | + thermal_a4: float, optional |
| 55 | + Is an empirical parameter specified by the user for calculating |
| 56 | + viscosity using a nonlinear formulation. If thermal_a4 is not specified, |
| 57 | + a default value of 133.15 is assigned (Voss, 1984). |
| 58 | + viscosityfile: str, optional |
| 59 | + Name of the binary output file to write viscosity information. |
| 60 | + validate: bool, optional |
| 61 | + Flag to indicate whether the package should be validated upon |
| 62 | + initialization. This raises a ValidationError if package input is |
| 63 | + provided in the wrong manner. Defaults to True. |
| 64 | +
|
| 65 | + Examples |
| 66 | + -------- |
| 67 | +
|
| 68 | + The viscosity input for a single species called "salinity", which is |
| 69 | + simulated by a GWT model called "gwt-1" are specified as follows: |
| 70 | +
|
| 71 | + >>> vsc = imod.mf6.Viscosity( |
| 72 | + ... reference_viscosity=8.904E-04, |
| 73 | + ... viscosity_concentration_slope=[1.92e-6], |
| 74 | + ... reference_concentration=[0.0], |
| 75 | + ... modelname=["gwt-1"], |
| 76 | + ... species=["salinity"], |
| 77 | + ... ) |
| 78 | +
|
| 79 | + Multiple species can be specified by presenting multiple values with an |
| 80 | + associated species coordinate. Two species, called "c1" and "c2", simulated |
| 81 | + by the GWT models "gwt-1" and "gwt-2" are specified as: |
| 82 | +
|
| 83 | + >>> coords = {"species": ["c1", "c2"]} |
| 84 | + >>> vsc = imod.mf6.Viscosity( |
| 85 | + ... reference_viscosity=8.904E-04, |
| 86 | + ... viscosity_concentration_slope=[1.92e-6, 3.4e-6], |
| 87 | + ... reference_concentration=[0.0, 0.0], |
| 88 | + ... modelname=["gwt-1", "gwt-2], |
| 89 | + ... species=["c1", "c2"], |
| 90 | + ... ) |
| 91 | +
|
| 92 | + You can also specify thermal properties, even with a nonlinear thermal |
| 93 | + formulation. |
| 94 | +
|
| 95 | + >>> coords = {"species": ["salinity", "temperature"]} |
| 96 | + >>> vsc = imod.mf6.Viscosity( |
| 97 | + ... reference_viscosity=8.904E-04, |
| 98 | + ... viscosity_concentration_slope=[1.92e-6, 0.0], |
| 99 | + ... reference_concentration=[0.0, 25.0], |
| 100 | + ... modelname=["gwt-1", "gwt-2"], |
| 101 | + ... species=["salinity", "temperature"], |
| 102 | + ... temperature_species_name="temperature", |
| 103 | + ... thermal_formulation="nonlinear", |
| 104 | + ... thermal_a2=10.0, |
| 105 | + ... thermal_a3=248.37, |
| 106 | + ... thermal_a4=133.15, |
| 107 | + ... ) |
| 108 | +
|
| 109 | + """ |
| 110 | + |
| 111 | + _pkg_id = "vsc" |
| 112 | + _template = Package._initialize_template(_pkg_id) |
| 113 | + |
| 114 | + _init_schemata = { |
| 115 | + "reference_viscosity": [DTypeSchema(np.floating)], |
| 116 | + "viscosity_concentration_slope": [DTypeSchema(np.floating)], |
| 117 | + "reference_concentration": [DTypeSchema(np.floating)], |
| 118 | + } |
| 119 | + _write_schemata = {} |
| 120 | + |
| 121 | + def __init__( |
| 122 | + self, |
| 123 | + reference_viscosity: float, |
| 124 | + viscosity_concentration_slope: Sequence[float], |
| 125 | + reference_concentration: Sequence[float], |
| 126 | + modelname: Sequence[str], |
| 127 | + species: Sequence[str], |
| 128 | + temperature_species_name: Optional[str] = None, |
| 129 | + thermal_formulation: ThermalFormulationOption = "linear", |
| 130 | + thermal_a2: float = 10.0, |
| 131 | + thermal_a3: float = 248.37, |
| 132 | + thermal_a4: float = 133.15, |
| 133 | + viscosityfile: Optional[str] = None, |
| 134 | + validate: bool = True, |
| 135 | + ): |
| 136 | + dict_dataset = { |
| 137 | + "reference_viscosity": reference_viscosity, |
| 138 | + # Assign a shared index: this also forces equal lengths |
| 139 | + "viscosity_concentration_slope": assign_index( |
| 140 | + viscosity_concentration_slope |
| 141 | + ), |
| 142 | + "reference_concentration": assign_index(reference_concentration), |
| 143 | + "modelname": assign_index(modelname), |
| 144 | + "species": assign_index(species), |
| 145 | + "temperature_species_name": temperature_species_name, |
| 146 | + "thermal_formulation": thermal_formulation, |
| 147 | + "thermal_a2": thermal_a2, |
| 148 | + "thermal_a3": thermal_a3, |
| 149 | + "thermal_a4": thermal_a4, |
| 150 | + "viscosityfile": viscosityfile, |
| 151 | + } |
| 152 | + super().__init__(dict_dataset) |
| 153 | + self._validate_init_schemata(validate) |
| 154 | + |
| 155 | + def _render(self, directory, pkgname, globaltimes, binary): |
| 156 | + ds = self.dataset |
| 157 | + packagedata = [] |
| 158 | + |
| 159 | + for i, (a, b, c, d) in enumerate( |
| 160 | + zip( |
| 161 | + ds["viscosity_concentration_slope"].values, |
| 162 | + ds["reference_concentration"].values, |
| 163 | + ds["modelname"].values, |
| 164 | + ds["species"].values, |
| 165 | + ) |
| 166 | + ): |
| 167 | + packagedata.append((i + 1, a, b, c, d)) |
| 168 | + |
| 169 | + d = { |
| 170 | + "nviscspecies": self.dataset["species"].size, |
| 171 | + "packagedata": packagedata, |
| 172 | + } |
| 173 | + |
| 174 | + for varname in [ |
| 175 | + "temperature_species_name", |
| 176 | + "thermal_formulation", |
| 177 | + "thermal_a2", |
| 178 | + "thermal_a3", |
| 179 | + "thermal_a4", |
| 180 | + "reference_viscosity", |
| 181 | + "viscosityfile", |
| 182 | + ]: |
| 183 | + value = self.dataset[varname].values[()] |
| 184 | + if self._valid(value): |
| 185 | + d[varname] = value |
| 186 | + |
| 187 | + return self._template.render(d) |
| 188 | + |
| 189 | + def _update_transport_models(self, new_modelnames: Sequence[str]): |
| 190 | + """ |
| 191 | + The names of the transport models can change in some cases, for example |
| 192 | + when partitioning. Use this function to update the names of the |
| 193 | + transport models. |
| 194 | + """ |
| 195 | + transport_model_names = self._get_transport_model_names() |
| 196 | + if not len(transport_model_names) == len(new_modelnames): |
| 197 | + raise ValueError("the number of transport models cannot be changed.") |
| 198 | + for modelname, new_modelname in zip(transport_model_names, new_modelnames): |
| 199 | + if modelname not in new_modelname: |
| 200 | + raise ValueError( |
| 201 | + "new transport model names do not match the old ones. The new names should be equal to the old ones, with a suffix." |
| 202 | + ) |
| 203 | + self.dataset["modelname"] = assign_index(new_modelnames) |
| 204 | + |
| 205 | + def _get_transport_model_names(self) -> list[str]: |
| 206 | + """ |
| 207 | + Returns the names of the transport models used by this buoyancy package. |
| 208 | + """ |
| 209 | + return list(self.dataset["modelname"].values) |
0 commit comments