Skip to content

Commit fec14eb

Browse files
Issue #1025 shorten unittests (#1029)
Fixes #1025 # Description This reduces the amount of timesteps for the model fixtures, which hopefully speed up unittests. # Checklist - [x] Links to correct issue - [ ] Update changelog, if changes affect users - [x] PR title starts with ``Issue #nr``, e.g. ``Issue #737`` - [ ] Unit tests were added - [ ] **If feature added**: Added/extended example
1 parent 4876cd8 commit fec14eb

11 files changed

Lines changed: 137 additions & 135 deletions

imod/tests/fixtures/flow_transport_simulation_fixture.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,10 @@ def flow_transport_simulation():
207207
relaxation_factor=0.9,
208208
)
209209

210-
duration = pd.to_timedelta("2000d")
210+
duration = pd.to_timedelta("20d")
211211
start = pd.to_datetime("2000-01-01")
212212
simulation.create_time_discretization(additional_times=[start, start + duration])
213-
simulation["time_discretization"]["n_timesteps"] = 100
213+
simulation["time_discretization"]["n_timesteps"] = 10
214214

215215
return simulation
216216
# %%

imod/tests/fixtures/mf6_circle_fixture.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def make_circle_model():
7575
reordering_method=None,
7676
relaxation_factor=0.97,
7777
)
78-
simtimes = pd.date_range(start="2000-01-01", end="2001-01-01", freq="W")
78+
simtimes = pd.date_range(start="2000-01-01", end="2000-01-03")
7979
simulation.create_time_discretization(additional_times=simtimes)
8080
return simulation
8181

@@ -158,7 +158,7 @@ def make_circle_model_flow_with_transport_data(species: list[str]):
158158
reordering_method=None,
159159
relaxation_factor=0.97,
160160
)
161-
simtimes = pd.date_range(start="2000-01-01", end="2001-01-01", freq="W")
161+
simtimes = pd.date_range(start="2000-01-01", end="2000-01-03")
162162
simulation.create_time_discretization(simtimes)
163163
return simulation
164164

@@ -357,7 +357,7 @@ def circle_model_transport():
357357
reordering_method=None,
358358
relaxation_factor=0.97,
359359
)
360-
simtimes = pd.date_range(start="2000-01-01", end="2001-01-01", freq="W")
360+
simtimes = pd.date_range(start="2000-01-01", end="2000-01-03")
361361
simulation.create_time_discretization(additional_times=simtimes)
362362
return simulation
363363

@@ -437,7 +437,7 @@ def circle_model_transport_vsc():
437437
reordering_method=None,
438438
relaxation_factor=0.97,
439439
)
440-
simtimes = pd.date_range(start="2000-01-01", end="2001-01-01", freq="W")
440+
simtimes = pd.date_range(start="2000-01-01", end="2000-01-03")
441441
simulation.create_time_discretization(additional_times=simtimes)
442442
return simulation
443443

@@ -514,6 +514,6 @@ def circle_model_transport_multispecies_variable_density():
514514
reordering_method=None,
515515
relaxation_factor=0.97,
516516
)
517-
simtimes = pd.date_range(start="2000-01-01", end="2001-01-01", freq="W")
517+
simtimes = pd.date_range(start="2000-01-01", end="2000-01-03")
518518
simulation.create_time_discretization(additional_times=simtimes)
519519
return simulation

imod/tests/fixtures/mf6_twri_fixture.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def transient_twri_model():
232232
save_flows=True,
233233
)
234234
simulation.create_time_discretization(
235-
additional_times=pd.date_range("2000-01-01", " 2000-01-31")
235+
additional_times=pd.date_range("2000-01-01", " 2000-01-03")
236236
)
237237
return simulation
238238

@@ -282,7 +282,7 @@ def transient_unconfined_twri_model():
282282
# Write specific discharges
283283
gwf_model["npf"]["save_specific_discharge"] = True
284284
simulation.create_time_discretization(
285-
additional_times=pd.date_range("2000-01-01", " 2000-01-31")
285+
additional_times=pd.date_range("2000-01-01", " 2000-01-03")
286286
)
287287
return simulation
288288

imod/tests/test_mf6/test_circle.py

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from datetime import datetime
44

55
import numpy as np
6+
import pandas as pd
67
import pytest
78
import xugrid as xu
89

@@ -24,17 +25,18 @@ def test_simulation_write_and_run(circle_model, tmp_path):
2425
with pytest.raises(
2526
RuntimeError, match="Simulation circle has not been written yet."
2627
):
27-
circle_model.run()
28+
simulation.run()
2829

2930
modeldir = tmp_path / "circle"
3031

31-
mask = deepcopy(circle_model["GWF_1"].domain)
32+
mask = deepcopy(simulation["GWF_1"].domain)
3233
mask.values[:, 133] = -1
3334
simulation.mask_all_models(mask)
3435

3536
simulation.write(modeldir, binary=False, use_absolute_paths=True)
3637
simulation.run()
37-
38+
# Test opening with different time unit, see if time unit is correctly
39+
# converted to seconds
3840
head = imod.mf6.open_hds(
3941
modeldir / "GWF_1/GWF_1.hds",
4042
modeldir / "GWF_1/disv.disv.grb",
@@ -44,10 +46,10 @@ def test_simulation_write_and_run(circle_model, tmp_path):
4446
assert isinstance(head, xu.UgridDataArray)
4547
assert np.all(
4648
head["time"].values[0]
47-
== np.array(datetime(1999, 1, 1, 0, 0, 7), dtype="datetime64[ns]")
49+
== np.array(datetime(1999, 1, 1, 0, 0, 1), dtype="datetime64[ns]")
4850
)
4951
assert head.dims == ("time", "layer", "mesh2d_nFaces")
50-
assert head.shape == (52, 2, 216)
52+
assert head.shape == (2, 2, 216)
5153

5254

5355
def test_gwfmodel_render(circle_model, tmp_path):
@@ -87,7 +89,7 @@ def test_simulation_write_and_run_evt(circle_model_evt, tmp_path):
8789
with pytest.raises(
8890
RuntimeError, match="Simulation circle has not been written yet."
8991
):
90-
circle_model_evt.run()
92+
simulation.run()
9193

9294
modeldir = tmp_path / "circle_evt"
9395
simulation.write(modeldir, binary=True)
@@ -98,22 +100,22 @@ def test_simulation_write_and_run_evt(circle_model_evt, tmp_path):
98100
)
99101
assert isinstance(head, xu.UgridDataArray)
100102
assert head.dims == ("time", "layer", "mesh2d_nFaces")
101-
assert head.shape == (52, 2, 216)
103+
assert head.shape == (2, 2, 216)
102104

103105

104106
def test_simulation_write_and_run_evt__no_segments(circle_model_evt, tmp_path):
105107
simulation = circle_model_evt
106108

107-
evt = circle_model_evt["GWF_1"].pop("evt")
109+
evt = simulation["GWF_1"].pop("evt")
108110
evt_ds = evt.dataset
109111
evt_ds = evt_ds.drop_vars(["proportion_rate", "proportion_depth"])
110112
evt_dict = {key: evt_ds[key] for key in evt_ds.keys()}
111-
circle_model_evt["GWF_1"]["evt"] = imod.mf6.Evapotranspiration(**evt_dict)
113+
simulation["GWF_1"]["evt"] = imod.mf6.Evapotranspiration(**evt_dict)
112114

113115
with pytest.raises(
114116
RuntimeError, match="Simulation circle has not been written yet."
115117
):
116-
circle_model_evt.run()
118+
simulation.run()
117119

118120
modeldir = tmp_path / "circle_evt"
119121
simulation.write(modeldir, binary=True)
@@ -124,7 +126,7 @@ def test_simulation_write_and_run_evt__no_segments(circle_model_evt, tmp_path):
124126
)
125127
assert isinstance(head, xu.UgridDataArray)
126128
assert head.dims == ("time", "layer", "mesh2d_nFaces")
127-
assert head.shape == (52, 2, 216)
129+
assert head.shape == (2, 2, 216)
128130

129131

130132
def test_gwfmodel_render_evt(circle_model_evt, tmp_path):
@@ -161,11 +163,10 @@ def test_gwfmodel_render_evt(circle_model_evt, tmp_path):
161163

162164
def test_simulation_write_and_run_transport(circle_model_transport, tmp_path):
163165
simulation = circle_model_transport
164-
165166
with pytest.raises(
166167
RuntimeError, match="Simulation circle has not been written yet."
167168
):
168-
circle_model_transport.run()
169+
simulation.run()
169170

170171
modeldir = tmp_path / "circle_transport"
171172
simulation.write(modeldir)
@@ -175,11 +176,11 @@ def test_simulation_write_and_run_transport(circle_model_transport, tmp_path):
175176

176177
assert isinstance(head, xu.UgridDataArray)
177178
assert head.dims == ("time", "layer", "mesh2d_nFaces")
178-
assert head.shape == (52, 2, 216)
179+
assert head.shape == (2, 2, 216)
179180

180181
assert isinstance(concentration, xu.UgridDataArray)
181182
assert concentration.dims == ("time", "layer", "mesh2d_nFaces")
182-
assert concentration.shape == (52, 2, 216)
183+
assert concentration.shape == (2, 2, 216)
183184

184185

185186
def test_simulation_write_and_run_transport_vsc(circle_model_transport_vsc, tmp_path):
@@ -198,11 +199,11 @@ def test_simulation_write_and_run_transport_vsc(circle_model_transport_vsc, tmp_
198199

199200
assert isinstance(head, xu.UgridDataArray)
200201
assert head.dims == ("time", "layer", "mesh2d_nFaces")
201-
assert head.shape == (52, 2, 216)
202+
assert head.shape == (2, 2, 216)
202203

203204
assert isinstance(concentration, xu.UgridDataArray)
204205
assert concentration.dims == ("time", "layer", "mesh2d_nFaces")
205-
assert concentration.shape == (52, 2, 216)
206+
assert concentration.shape == (2, 2, 216)
206207

207208

208209
def run_simulation(simulation, modeldir):
@@ -251,8 +252,8 @@ def test_simulation_clip_and_state_at_boundary(circle_model_transport, tmp_path)
251252

252253
# Test if model runs
253254
head_half, concentration_half = run_simulation(half_simulation, tmp_path / "half")
254-
assert head_half.shape == (52, 2, 108)
255-
assert concentration_half.shape == (52, 2, 108)
255+
assert head_half.shape == (2, 2, 108)
256+
assert concentration_half.shape == (2, 2, 108)
256257

257258

258259
def test_simulation_clip_and_constant_state_at_boundary__transient_chd(
@@ -289,12 +290,12 @@ def test_simulation_clip_and_constant_state_at_boundary__transient_chd(
289290
assert conc_clipped.notnull().sum() == 20
290291
head_clipped = half_simulation["GWF_1"]["chd_clipped"]["head"]
291292
assert head_clipped.sizes["layer"] == 2
292-
assert head_clipped.sizes["time"] == 52
293-
assert head_clipped.notnull().sum() == 20 * 52
293+
assert head_clipped.sizes["time"] == 2
294+
assert head_clipped.notnull().sum() == 20 * 2
294295
# Test if model runs
295296
head_half, concentration_half = run_simulation(half_simulation, tmp_path / "half")
296-
assert head_half.shape == (52, 2, 108)
297-
assert concentration_half.shape == (52, 2, 108)
297+
assert head_half.shape == (2, 2, 108)
298+
assert concentration_half.shape == (2, 2, 108)
298299

299300

300301
def test_simulation_clip_and_transient_state_at_boundary__transient_chd(
@@ -304,9 +305,17 @@ def test_simulation_clip_and_transient_state_at_boundary__transient_chd(
304305
Test with a transient chd boundary condition and transient states for
305306
boundary conditions. The time steps of the chd package are outside of the
306307
time domain of the states_for_boundary.
308+
309+
Extend the time discretization as it was before
310+
https://github.com/Deltares/imod-python/pull/1029, to allow clipping longer
311+
timeseries.
307312
"""
308313
# Arrange
309314
simulation = circle_model_transport
315+
simulation.create_time_discretization(
316+
additional_times=pd.date_range(start="2000-01-01", end="2001-01-01", freq="W")
317+
)
318+
310319
idomain = simulation["GWF_1"]["disv"]["idomain"].compute()
311320
time = simulation["time_discretization"].dataset.coords["time"].values
312321

0 commit comments

Comments
 (0)