-
Notifications
You must be signed in to change notification settings - Fork 303
/
test_modflow.py
438 lines (347 loc) · 11.6 KB
/
test_modflow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import filecmp
from os.path import join, splitext
from pathlib import Path
from shutil import copytree
import pytest
from autotest.conftest import get_example_data_path, requires_exe, requires_pkg
from flopy.modflow import Modflow, ModflowOc
@pytest.fixture
def mf2005_test_path(example_data_path):
return example_data_path / "mf2005_test"
@pytest.fixture
def uzf_example_path(example_data_path):
return example_data_path / "uzf_examples"
@requires_exe("mf2005")
@requires_pkg("pymake")
@pytest.mark.slow
@pytest.mark.regression
def test_uzf_unit_numbers(tmpdir, uzf_example_path):
import pymake
mfnam = "UZFtest2.nam"
ws = str(tmpdir / "ws")
copytree(uzf_example_path, ws)
m = Modflow.load(
mfnam,
verbose=True,
model_ws=ws,
forgive=False,
exe_name="mf2005",
)
assert m.load_fail is False, "failed to load all packages"
# reset the oc file
m.remove_package("OC")
output = ["save head", "print budget"]
spd = {}
for iper in range(1, m.dis.nper):
for istp in [0, 4, 9, 14]:
spd[(iper, istp)] = output
spd[(0, 0)] = output
spd[(1, 1)] = output
spd[(1, 2)] = output
spd[(1, 3)] = output
oc = ModflowOc(m, stress_period_data=spd)
oc.write_file()
success, buff = m.run_model(silent=False)
assert success, "base model run did not terminate successfully"
fn0 = join(ws, mfnam)
# change uzf iuzfcb2 and add binary uzf output file
m.uzf.iuzfcb2 = 61
m.add_output_file(m.uzf.iuzfcb2, extension="uzfcb2.bin", package="UZF")
# change the model work space
model_ws2 = join(ws, "flopy")
m.change_model_ws(model_ws2, reset_external=True)
# rewrite files
m.write_input()
# run and compare the output files
success, buff = m.run_model(silent=False)
assert success, "new model run did not terminate successfully"
fn1 = join(model_ws2, mfnam)
# compare budget terms
fsum = join(str(tmpdir), f"{splitext(mfnam)[0]}.budget.out")
success = pymake.compare_budget(
fn0, fn1, max_incpd=0.1, max_cumpd=0.1, outfile=fsum
)
assert success, "budget comparison failure"
@requires_exe("mf2005")
@requires_pkg("pymake")
@pytest.mark.slow
@pytest.mark.regression
def test_unitnums(tmpdir, mf2005_test_path):
import pymake
mfnam = "testsfr2_tab.nam"
ws = str(tmpdir / "ws")
copytree(mf2005_test_path, ws)
m = Modflow.load(mfnam, verbose=True, model_ws=ws, exe_name="mf2005")
assert m.load_fail is False, "failed to load all packages"
v = (m.nlay, m.nrow, m.ncol, m.nper)
assert v == (1, 7, 100, 50), (
"modflow-2005 testsfr2_tab does not have "
"1 layer, 7 rows, and 100 columns"
)
success, buff = m.run_model(silent=False)
assert success, "base model run did not terminate successfully"
fn0 = join(ws, mfnam)
# rewrite files
model_ws2 = join(ws, "flopy")
m.change_model_ws(model_ws2, reset_external=True)
m.write_input()
success, buff = m.run_model(silent=False)
assert success, "base model run did not terminate successfully"
fn1 = join(model_ws2, mfnam)
fsum = join(ws, f"{splitext(mfnam)[0]}.budget.out")
success = pymake.compare_budget(
fn0, fn1, max_incpd=0.1, max_cumpd=0.1, outfile=fsum
)
assert success, "budget comparison failure"
@requires_exe("mf2005")
@requires_pkg("pymake")
@pytest.mark.slow
@pytest.mark.regression
def test_gage(tmpdir, example_data_path):
"""
test043 load and write of MODFLOW-2005 GAGE example problem
"""
import pymake
pth = str(example_data_path / "mf2005_test")
fpth = join(pth, "testsfr2_tab.nam")
ws = str(tmpdir / "ws")
copytree(pth, ws)
# load the modflow model
mf = Modflow.load(
"testsfr2_tab.nam", verbose=True, model_ws=ws, exe_name="mf2005"
)
# run the modflow-2005 model
success, buff = mf.run_model()
assert success, "could not run original MODFLOW-2005 model"
files = mf.gage.files
model_ws2 = join(ws, "flopy")
mf.change_model_ws(new_pth=model_ws2, reset_external=True)
# write the modflow model in to the new path
mf.write_input()
# run the modflow-2005 model
success, buff = mf.run_model()
assert success, "could not run new MODFLOW-2005 model"
# compare the two results
for f in files:
pth0 = join(ws, f)
pth1 = join(model_ws2, f)
assert filecmp.cmp(
pth0, pth1
), f'new and original gage file "{f}" are not binary equal.'
__example_data_path = get_example_data_path()
@requires_exe("mf2005")
@requires_pkg("pymake")
@pytest.mark.slow
@pytest.mark.regression
@pytest.mark.parametrize(
"namfile",
[
str(__example_data_path / "pcgn_test" / nf)
for nf in ["twri.nam", "MNW2.nam"]
],
)
def test_mf2005pcgn(tmpdir, namfile):
import pymake
ws = tmpdir / "ws"
copytree(Path(namfile).parent, ws)
nf = Path(namfile).name
m = Modflow.load(
nf,
model_ws=str(ws),
verbose=True,
exe_name="mf2005",
)
assert m.load_fail is False
if nf in ["twri.nam"]: # update this list for fixed models
assert m.free_format_input is False
else:
assert m.free_format_input is True
success, buff = m.run_model(silent=False)
assert success, "base model run did not terminate successfully"
fn0 = str(ws / nf)
# rewrite files
ws2 = tmpdir / "ws2"
m.change_model_ws(str(ws2))
m.write_input()
success, buff = m.run_model(silent=False)
assert success, "new model run did not terminate successfully"
fn1 = str(ws2 / nf)
fsum = str(tmpdir / f"{Path(namfile).stem}.head.out")
success = pymake.compare_heads(fn0, fn1, outfile=fsum, htol=0.005)
assert success, "head comparison failure"
fsum = str(tmpdir / f"{Path(namfile).stem}.budget.out")
success = pymake.compare_budget(
fn0, fn1, max_incpd=0.1, max_cumpd=0.1, outfile=fsum
)
assert success, "budget comparison failure"
@requires_exe("mf2005")
@requires_pkg("pymake")
@pytest.mark.slow
@pytest.mark.regression
@pytest.mark.parametrize(
"namfile", [str(__example_data_path / "secp" / nf) for nf in ["secp.nam"]]
)
def test_mf2005gmg(tmpdir, namfile):
import pymake
ws = tmpdir / "ws"
copytree(Path(namfile).parent, ws)
nf = Path(namfile).name
m = Modflow.load(
namfile,
model_ws=str(ws),
verbose=True,
exe_name="mf2005",
)
assert m.load_fail is False
success, buff = m.run_model(silent=False)
assert success, "base model run did not terminate successfully"
fn0 = str(ws / nf)
# rewrite files
m.change_model_ws(str(tmpdir))
m.write_input()
success, buff = m.run_model(silent=False)
assert success, "new model run did not terminate successfully"
fn1 = str(tmpdir / nf)
fsum = str(tmpdir / f"{Path(namfile).stem}.head.out")
success = pymake.compare_heads(fn0, fn1, outfile=fsum)
assert success, "head comparison failure"
fsum = str(tmpdir / f"{Path(namfile).stem}.budget.out")
success = pymake.compare_budget(
fn0, fn1, max_incpd=0.1, max_cumpd=0.1, outfile=fsum
)
assert success, "budget comparison failure"
@requires_exe("mf2005")
@requires_pkg("pymake")
@pytest.mark.regression
@pytest.mark.parametrize(
"namfile",
[str(__example_data_path / "freyberg" / nf) for nf in ["freyberg.nam"]],
)
def test_mf2005(tmpdir, namfile):
"""
test045 load and write of MODFLOW-2005 GMG example problem
"""
import pymake
compth = tmpdir / "flopy"
ws = tmpdir / "ws"
copytree(Path(namfile).parent, str(ws))
m = Modflow.load(
Path(namfile).name,
model_ws=str(ws),
verbose=True,
exe_name="mf2005",
)
assert m.load_fail is False
success, buff = m.run_model(silent=False)
assert success, "base model run did not terminate successfully"
fn0 = str(ws / Path(namfile).name)
# change model workspace
m.change_model_ws(compth)
# recreate oc file
oc = m.oc
unitnumber = [oc.unit_number[0], oc.iuhead, oc.iuddn, 0, 0]
spd = {(0, 0): ["save head", "save drawdown"]}
chedfm = "(10(1X1PE13.5))"
cddnfm = "(10(1X1PE13.5))"
oc = ModflowOc(
m,
stress_period_data=spd,
chedfm=chedfm,
cddnfm=cddnfm,
unitnumber=unitnumber,
)
# rewrite files
m.write_input()
success, buff = m.run_model()
assert success, "new model run did not terminate successfully"
fn1 = str(compth / Path(namfile).name)
# compare heads
fsum = str(ws / f"{Path(namfile).stem}.head.out")
success = pymake.compare_heads(fn0, fn1, outfile=fsum)
assert success, "head comparison failure"
# compare heads
fsum = str(ws / f"{Path(namfile).stem}.ddn.out")
success = pymake.compare_heads(fn0, fn1, outfile=fsum, text="drawdown")
assert success, "head comparison failure"
# compare budgets
fsum = str(ws / f"{Path(namfile).stem}.budget.out")
success = pymake.compare_budget(
fn0, fn1, max_incpd=0.1, max_cumpd=0.1, outfile=fsum
)
assert success, "budget comparison failure"
mf2005_namfiles = [
str(__example_data_path / "mf2005_test" / nf)
for nf in [
"fhb.nam",
"l1a2k.nam",
"l1b2k.nam",
"l1b2k_bath.nam",
"lakeex3.nam",
]
]
@requires_exe("mf2005")
@requires_pkg("pymake")
@pytest.mark.slow
@pytest.mark.regression
@pytest.mark.parametrize("namfile", mf2005_namfiles)
def test_mf2005fhb(tmpdir, namfile):
import pymake
ws = str(tmpdir / "ws")
copytree(Path(namfile).parent, ws)
m = Modflow.load(
Path(namfile).name, model_ws=ws, verbose=True, exe_name="mf2005"
)
assert m.load_fail is False
success, buff = m.run_model(silent=False)
assert success, "base model run did not terminate successfully"
fn0 = join(ws, Path(namfile).name)
# rewrite files
m.change_model_ws(str(tmpdir), reset_external=True)
m.write_input()
success, buff = m.run_model()
assert success, "new model run did not terminate successfully"
fn1 = join(str(tmpdir), Path(namfile).name)
fsum = join(ws, f"{Path(namfile).stem}.head.out")
success = pymake.compare_heads(fn0, fn1, outfile=fsum)
assert success, "head comparison failure"
fsum = join(ws, f"{Path(namfile).stem}.budget.out")
success = pymake.compare_budget(
fn0, fn1, max_incpd=0.1, max_cumpd=0.1, outfile=fsum
)
assert success, "budget comparison failure"
@requires_exe("mf2005")
@requires_pkg("pymake")
@pytest.mark.slow
@pytest.mark.regression
@pytest.mark.parametrize("namfile", mf2005_namfiles)
def test_mf2005_lake(tmpdir, namfile, mf2005_test_path):
import pymake
ws = str(tmpdir / "ws")
copytree(mf2005_test_path, ws)
m = Modflow.load(
Path(namfile).name,
model_ws=str(ws),
verbose=True,
forgive=False,
exe_name="mf2005",
)
assert m.load_fail is False
success, buff = m.run_model(silent=True)
assert success
fn0 = join(ws, Path(namfile).name)
# write free format files - wont run without resetting to free format - evt external file issue
m.free_format_input = True
# rewrite files
model_ws2 = join(ws, "external")
m.change_model_ws(
model_ws2, reset_external=True
) # l1b2k_bath wont run without this
m.write_input()
success, buff = m.run_model()
assert success
fn1 = join(model_ws2, Path(namfile).name)
fsum = join(ws, f"{Path(namfile).stem}.budget.out")
success = pymake.compare_budget(
fn0, fn1, max_incpd=0.1, max_cumpd=0.1, outfile=fsum
)
assert success, "budget comparison failure"