-
Notifications
You must be signed in to change notification settings - Fork 356
Expand file tree
/
Copy pathbenchmark_binaryfile_write.py
More file actions
151 lines (122 loc) · 4.64 KB
/
benchmark_binaryfile_write.py
File metadata and controls
151 lines (122 loc) · 4.64 KB
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
"""Benchmark tests for binaryfile write methods."""
from pathlib import Path
import numpy as np
import pytest
from flopy.mf6.utils import MfGrdFile
from flopy.utils import CellBudgetFile, HeadFile
@pytest.fixture
def freyberg_hds_path(example_data_path):
return example_data_path / "freyberg_multilayer_transient" / "freyberg.hds"
@pytest.fixture
def freyberg_cbc_path(example_data_path):
return example_data_path / "freyberg_multilayer_transient" / "freyberg.cbc"
@pytest.fixture
def mfgrd_dis_path(example_data_path):
return example_data_path / "mf6-freyberg" / "freyberg.dis.grb"
@pytest.fixture
def mfgrd_disv_path(example_data_path):
return (
example_data_path
/ "mf6"
/ "test006_gwf3_disv"
/ "expected_output"
/ "flow.disv.grb"
)
@pytest.mark.slow
def test_headfile_write_benchmark(benchmark, freyberg_hds_path, tmp_path):
hds = HeadFile(freyberg_hds_path)
nsteps = min(100, len(hds.kstpkper))
kstpkper = hds.kstpkper[:nsteps]
output_file = tmp_path / "benchmark_output.hds"
def write_head():
hds.write(output_file, kstpkper=kstpkper)
benchmark(write_head)
assert output_file.exists()
@pytest.mark.slow
def test_cellbudgetfile_write_benchmark(benchmark, freyberg_cbc_path, tmp_path):
cbc = CellBudgetFile(freyberg_cbc_path)
nsteps = min(50, len(cbc.kstpkper))
kstpkper = cbc.kstpkper[:nsteps]
output_file = tmp_path / "benchmark_output.cbc"
def write_budget():
cbc.write(output_file, kstpkper=kstpkper)
benchmark(write_budget)
assert output_file.exists()
@pytest.mark.slow
def test_mfgrdfile_write_benchmark_dis(benchmark, tmp_path):
nlay, nrow, ncol = 10, 100, 100
nodes = nlay * nrow * ncol
nja = 7 * nodes - 2 * (nlay * nrow + nlay * ncol + nrow * ncol)
grb_data = {
"NCELLS": nodes,
"NLAY": nlay,
"NROW": nrow,
"NCOL": ncol,
"NJA": nja,
"XORIGIN": 0.0,
"YORIGIN": 0.0,
"ANGROT": 0.0,
"DELR": np.ones(ncol, dtype=np.float64) * 100.0,
"DELC": np.ones(nrow, dtype=np.float64) * 100.0,
"TOP": np.ones(nodes, dtype=np.float64) * 100.0,
"BOTM": np.arange(nodes, dtype=np.float64),
"IA": np.arange(nodes + 1, dtype=np.int32),
"JA": np.arange(nja, dtype=np.int32) % nodes,
"IDOMAIN": np.ones(nodes, dtype=np.int32),
"ICELLTYPE": np.ones(nodes, dtype=np.int32),
}
from flopy.utils.utils_def import FlopyBinaryData
temp_grb = tmp_path / "temp_input.grb"
writer = FlopyBinaryData()
writer.precision = "double"
with open(temp_grb, "wb") as f:
writer.file = f
writer.write_text("GRID DIS\n", 50)
writer.write_text("VERSION 1\n", 50)
writer.write_text("NTXT 16\n", 50)
writer.write_text("LENTXT 100\n", 50)
var_list = [
("NCELLS", "INTEGER", 0, []),
("NLAY", "INTEGER", 0, []),
("NROW", "INTEGER", 0, []),
("NCOL", "INTEGER", 0, []),
("NJA", "INTEGER", 0, []),
("XORIGIN", "DOUBLE", 0, []),
("YORIGIN", "DOUBLE", 0, []),
("ANGROT", "DOUBLE", 0, []),
("DELR", "DOUBLE", 1, [ncol]),
("DELC", "DOUBLE", 1, [nrow]),
("TOP", "DOUBLE", 1, [nodes]),
("BOTM", "DOUBLE", 1, [nodes]),
("IA", "INTEGER", 1, [nodes + 1]),
("JA", "INTEGER", 1, [nja]),
("IDOMAIN", "INTEGER", 1, [nodes]),
("ICELLTYPE", "INTEGER", 1, [nodes]),
]
for name, dtype_str, ndim, dims in var_list:
if ndim == 0:
line = f"{name} {dtype_str} NDIM {ndim}\n"
else:
dims_str = " ".join(str(d) for d in dims[::-1])
line = f"{name} {dtype_str} NDIM {ndim} {dims_str}\n"
writer.write_text(line, 100)
for name, dtype_str, ndim, dims in var_list:
value = grb_data[name]
if ndim == 0:
if dtype_str == "INTEGER":
writer.write_integer(int(value))
else:
writer.write_real(float(value))
else:
arr = np.asarray(value)
if dtype_str == "INTEGER":
arr = arr.astype(np.int32)
elif dtype_str == "DOUBLE":
arr = arr.astype(np.float64)
writer.write_record(arr.flatten(order="F"), dtype=arr.dtype)
grb = MfGrdFile(str(temp_grb), verbose=False)
output_file = tmp_path / "benchmark_output.grb"
def write_grb():
grb.write(output_file, verbose=False)
benchmark(write_grb)
assert output_file.exists()