Coverage for C:\src\imod-python\imod\flow\dis.py: 100%
27 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-08 13:27 +0200
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-08 13:27 +0200
1import jinja2
3import imod.util
4from imod.flow.pkgbase import Package
7class TimeDiscretization(Package):
8 """
9 Time discretisation package class.
11 Parameters
12 ----------
13 timestep_duration: xr.DataArray
14 is the length of the current stress period (PERLEN). If the flow
15 solution is transient, timestep_duration specified here must be equal
16 to that specified for the flow model. If the flow solution is
17 steady-state, timestep_duration can be set to any desired length.
18 n_timesteps: int, optional
19 is the number of time steps for the transient flow solution in the
20 current stress period (NSTP). If the flow solution is steady-state,
21 n_timestep=1. Default value is 1.
22 transient: bool, optional
23 Flag indicating wether the flow simulation is transient (True) or False
24 (Steady State). Default is True.
25 timestep_multiplier: float, optional
26 is the multiplier for the length of successive time steps used in the
27 transient flow solution (TSMULT); it is used only if n_timesteps>1.
28 timestep_multiplier>0, the length of each flow time step within the
29 current stress period is calculated using the geometric progression as
30 in MODFLOW. Note that both n_timesteps and timestep_multiplier
31 specified here must be identical to those specified in the flow model
32 if the flow model is transient. If timestep_multiplier ≤ 0, the length
33 of each flow time step within the current stress period is read from
34 the record TSLNGH. This option is needed in case the length of time
35 steps for the flow solution is not based on a geometric progression in
36 a flow model, unlike MODFLOW. Default is 1.0.
37 """
39 _pkg_id = "dis"
40 _variable_order = [
41 "timestep_duration",
42 "n_timesteps",
43 "transient",
44 "timestep_multiplier",
45 ]
47 def __init__(
48 self,
49 timestep_duration,
50 endtime,
51 n_timesteps=1,
52 transient=True,
53 timestep_multiplier=1.0,
54 ):
55 super().__init__()
56 self.dataset["timestep_duration"] = timestep_duration
57 self.dataset["n_timesteps"] = n_timesteps
58 self.dataset["transient"] = transient
59 self.dataset["timestep_multiplier"] = timestep_multiplier
60 self.endtime = endtime
62 def _render(self):
63 """Render iMOD TIM file, which is the time discretization of the iMODFLOW model"""
64 _template = jinja2.Template(
65 "{% for time in timestrings%}"
66 "{{time}},1,{{n_timesteps}},{{timestep_multiplier}}\n"
67 "{% endfor %}\n"
68 )
69 times = self.dataset["time"].values
70 timestrings = [imod.util.time._compose_timestring(time) for time in times]
71 timestrings.append(imod.util.time._compose_timestring(self.endtime))
73 d = dict(
74 timestrings=timestrings,
75 n_timesteps=self.dataset["n_timesteps"].item(),
76 timestep_multiplier=self.dataset["timestep_multiplier"].item(),
77 )
79 return _template.render(**d)
81 def save(self, path):
82 tim_content = self._render()
84 with open(path, "w") as f:
85 f.write(tim_content)
87 def _pkgcheck(self, **kwargs):
88 to_check = [
89 "timestep_duration",
90 "n_timesteps",
91 ]
93 self._check_positive(to_check)