-
Notifications
You must be signed in to change notification settings - Fork 320
/
Copy pathtest_modpathfile.py
364 lines (311 loc) · 9.37 KB
/
test_modpathfile.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
import inspect
import io
import pstats
from itertools import repeat
from shutil import copytree
import numpy as np
import numpy.lib.recfunctions as rfn
import pytest
from modflow_devtools.markers import requires_exe, requires_pkg
from flopy.mf6 import (
MFSimulation,
ModflowGwf,
ModflowGwfdis,
ModflowGwfic,
ModflowGwfnpf,
ModflowGwfoc,
ModflowGwfrcha,
ModflowGwfriv,
ModflowGwfwel,
ModflowIms,
ModflowTdis,
)
from flopy.modpath import Modpath7
from flopy.utils import EndpointFile, PathlineFile
pytestmark = pytest.mark.mf6
def __create_and_run_simulation(
ws,
name,
nrow,
ncol,
perlen,
nstp,
tsmult,
nper,
nlay,
delr,
delc,
top,
botm,
laytyp,
kh,
kv,
rch,
wel_loc,
wel_q,
riv_h,
riv_c,
riv_z,
):
def get_nodes(locs):
nodes = []
for k, i, j in locs:
nodes.append(k * nrow * ncol + i * ncol + j)
return nodes
# Create the Flopy simulation object
sim = MFSimulation(sim_name=name, exe_name="mf6", version="mf6", sim_ws=ws)
# Create the Flopy temporal discretization object
pd = (perlen, nstp, tsmult)
tdis = ModflowTdis(
sim, pname="tdis", time_units="DAYS", nper=nper, perioddata=[pd]
)
# Create the Flopy groundwater flow (gwf) model object
model_nam_file = f"{name}.nam"
gwf = ModflowGwf(
sim, modelname=name, model_nam_file=model_nam_file, save_flows=True
)
# Create the Flopy iterative model solver (ims) Package object
ims = ModflowIms(
sim,
pname="ims",
complexity="SIMPLE",
outer_hclose=1e-6,
inner_hclose=1e-6,
rcloserecord=1e-6,
)
# create gwf file
dis = ModflowGwfdis(
gwf,
pname="dis",
nlay=nlay,
nrow=nrow,
ncol=ncol,
length_units="FEET",
delr=delr,
delc=delc,
top=top,
botm=botm,
)
# Create the initial conditions package
ic = ModflowGwfic(gwf, pname="ic", strt=top)
# Create the node property flow package
npf = ModflowGwfnpf(gwf, pname="npf", icelltype=laytyp, k=kh, k33=kv)
# recharge
ModflowGwfrcha(gwf, recharge=rch)
# wel
wd = [(wel_loc, wel_q)]
ModflowGwfwel(gwf, maxbound=1, stress_period_data={0: wd})
# river
rd = []
for i in range(nrow):
rd.append([(0, i, ncol - 1), riv_h, riv_c, riv_z])
ModflowGwfriv(gwf, stress_period_data={0: rd})
# Create the output control package
headfile = f"{name}.hds"
head_record = [headfile]
budgetfile = f"{name}.cbb"
budget_record = [budgetfile]
saverecord = [("HEAD", "ALL"), ("BUDGET", "ALL")]
oc = ModflowGwfoc(
gwf,
pname="oc",
saverecord=saverecord,
head_filerecord=head_record,
budget_filerecord=budget_record,
)
sim.write_simulation()
success, buff = sim.run_simulation(silent=True)
assert success, "mf6 model did not run"
# get locations to track data
nodew = get_nodes([wel_loc])
cellids = gwf.riv.stress_period_data.get_data()[0]["cellid"]
nodesr = get_nodes(cellids)
nodew = get_nodes([wel_loc])
cellids = gwf.riv.stress_period_data.get_data()[0]["cellid"]
nodesr = get_nodes(cellids)
forward_model_name = name + "_forward"
# create basic forward tracking modpath simulation
mp = Modpath7.create_mp7(
modelname=forward_model_name,
trackdir="forward",
flowmodel=gwf,
model_ws=ws,
rowcelldivisions=1,
columncelldivisions=1,
layercelldivisions=1,
exe_name="mp7",
)
# write modpath datasets and run forward model
mp.write_input()
mp.run_model(silent=True)
backward_model_name = name + "_backward"
# create basic backward tracking modpath simulation
mp = Modpath7.create_mp7(
modelname=backward_model_name,
trackdir="backward",
flowmodel=gwf,
model_ws=ws,
rowcelldivisions=5,
columncelldivisions=5,
layercelldivisions=5,
nodes=nodew + nodesr,
exe_name="mp7",
)
# write modpath datasets and run backward model
mp.write_input()
mp.run_model(silent=True)
return sim, forward_model_name, backward_model_name, nodew, nodesr
@pytest.fixture(scope="module")
def mp7_small(module_tmpdir):
return __create_and_run_simulation(
ws=module_tmpdir / "mp7_small",
name="mp7_small",
nper=1,
nstp=1,
perlen=1.0,
tsmult=1.0,
nlay=3,
nrow=11,
ncol=10,
delr=500.0,
delc=500.0,
top=400.0,
botm=[220.0, 200.0, 0.0],
laytyp=[1, 0, 0],
kh=[50.0, 0.01, 200.0],
kv=[10.0, 0.01, 20.0],
wel_loc=(2, 10, 9),
wel_q=-150000.0,
rch=0.005,
riv_h=320.0,
riv_z=317.0,
riv_c=1.0e5,
)
@pytest.fixture(scope="module")
def mp7_large(module_tmpdir):
return __create_and_run_simulation(
ws=module_tmpdir / "mp7_large",
name="mp7_large",
nper=1,
nstp=1,
perlen=1.0,
tsmult=1.0,
nlay=3,
nrow=21,
ncol=20,
delr=500.0,
delc=500.0,
top=400.0,
botm=[220.0, 200.0, 0.0],
laytyp=[1, 0, 0],
kh=[50.0, 0.01, 200.0],
kv=[10.0, 0.01, 20.0],
wel_loc=(2, 10, 9),
wel_q=-150000.0,
rch=0.005,
riv_h=320.0,
riv_z=317.0,
riv_c=1.0e5,
)
@requires_exe("mf6")
def test_pathline_file_sorts_in_ctor(
function_tmpdir, module_tmpdir, mp7_small
):
sim, forward_model_name, backward_model_name, nodew, nodesr = mp7_small
ws = function_tmpdir / "ws"
# copytree(sim.simulation_data.mfpath.get_sim_path(), ws)
copytree(module_tmpdir / "mp7_small", ws)
forward_path = ws / f"{forward_model_name}.mppth"
assert forward_path.is_file()
pathline_file = PathlineFile(forward_path)
assert np.all(
pathline_file._data[:-1]["particleid"]
<= pathline_file._data[1:]["particleid"]
)
@requires_exe("mf6", "mp7")
@pytest.mark.slow
@pytest.mark.parametrize("direction", ["forward", "backward"])
@pytest.mark.parametrize("locations", ["well", "river"])
def test_get_destination_pathline_data(
function_tmpdir, mp7_large, direction, locations, benchmark
):
sim, forward_model_name, backward_model_name, nodew, nodesr = mp7_large
ws = function_tmpdir / "ws"
copytree(sim.simulation_data.mfpath.get_sim_path(), ws)
forward_path = ws / f"{forward_model_name}.mppth"
backward_path = ws / f"{backward_model_name}.mppth"
assert forward_path.is_file()
assert backward_path.is_file()
pathline_file = PathlineFile(
backward_path if direction == "backward" else forward_path
)
benchmark(
lambda: pathline_file.get_destination_pathline_data(
dest_cells=nodew if locations == "well" else nodesr
)
)
@requires_exe("mf6", "mp7")
@pytest.mark.slow
@pytest.mark.parametrize("direction", ["forward", "backward"])
@pytest.mark.parametrize("locations", ["well", "river"])
def test_get_destination_endpoint_data(
function_tmpdir, mp7_large, direction, locations, benchmark
):
sim, forward_model_name, backward_model_name, nodew, nodesr = mp7_large
ws = function_tmpdir / "ws"
copytree(sim.simulation_data.mfpath.get_sim_path(), ws)
forward_end = ws / f"{forward_model_name}.mpend"
backward_end = ws / f"{backward_model_name}.mpend"
assert forward_end.is_file()
assert backward_end.is_file()
endpoint_file = EndpointFile(
backward_end if direction == "backward" else forward_end
)
benchmark(
lambda: endpoint_file.get_destination_endpoint_data(
dest_cells=nodew if locations == "well" else nodesr
)
)
@pytest.mark.parametrize("longfieldname", [True, False])
@requires_exe("mf6", "mp7")
@requires_pkg("shapefile", "shapely")
def test_write_shapefile(function_tmpdir, mp7_small, longfieldname):
from shapefile import Reader
# setup and run model, then copy outputs to function_tmpdir
sim, forward_model_name, _, _, _ = mp7_small
gwf = sim.get_model()
grid = gwf.modelgrid
ws = function_tmpdir / "ws"
copytree(sim.simulation_data.mfpath.get_sim_path(), ws)
# make sure forward model output exists
forward_path = ws / f"{forward_model_name}.mppth"
assert forward_path.is_file()
# load pathlines from file
pathline_file = PathlineFile(forward_path)
pathlines = pathline_file.get_alldata()
# define shapefile path
shp_file = ws / "pathlines.shp"
# add a column to the pathline recarray
fieldname = "newfield" + ("longname" if longfieldname else "")
fieldval = "x"
pathlines = [
rfn.append_fields(
pl, fieldname, list(repeat(fieldval, len(pl))), dtypes="|S1"
)
for pl in pathlines
]
# write the pathline recarray to shapefile
pathline_file.write_shapefile(
pathline_data=pathlines,
shpname=shp_file,
one_per_particle=False,
mg=grid,
)
# make sure shapefile exists
assert shp_file.is_file()
# load shapefile
with Reader(shp_file) as reader:
fieldnames = [f[0] for f in reader.fields[1:]]
fieldname = "newfiname_" if longfieldname else fieldname
assert fieldname in fieldnames
assert all(r[fieldname] == fieldval for r in reader.iterRecords())