Skip to content

Commit 59923f0

Browse files
mjrenomjreno
andauthored
write runnable baseline gwf simulations (#43)
* write runnable gwf base simulation * remove temp path checks * add gwf disu test * add gwf disv test * misc cleanup --------- Co-authored-by: mjreno <mjreno@IGSAAA071L01144.gs.doi.net>
1 parent 76dee30 commit 59923f0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+11815
-363
lines changed

docs/examples/array_example.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,12 @@
6262
constant = data_path / "constant.txt"
6363
external = data_path / "external.txt"
6464
shape = (1000, 100)
65+
dtype = "double"
6566

6667
# Open and load a NumPy array representation
6768

6869
fhandle = open(internal)
69-
imfa = MFArray.load(fhandle, data_path, shape, header=False)
70+
imfa = MFArray.load(fhandle, data_path, shape, type=dtype, header=False)
7071

7172
# Get values
7273

@@ -87,7 +88,7 @@
8788
plt.colorbar()
8889

8990
fhandle = open(constant)
90-
cmfa = MFArray.load(fhandle, data_path, shape, header=False)
91+
cmfa = MFArray.load(fhandle, data_path, shape, type=dtype, header=False)
9192
cvals = cmfa.value
9293
plt.imshow(cvals[0:100])
9394
plt.colorbar()
@@ -110,7 +111,7 @@
110111
# External
111112

112113
fhandle = open(external)
113-
emfa = MFArray.load(fhandle, data_path, shape, header=False)
114+
emfa = MFArray.load(fhandle, data_path, shape, type=dtype, header=False)
114115
evals = emfa.value
115116
evals
116117

@@ -135,7 +136,9 @@
135136

136137
fhandle = open(ilayered)
137138
shape = (3, 1000, 100)
138-
ilmfa = MFArray.load(fhandle, data_path, shape, header=False, layered=True)
139+
ilmfa = MFArray.load(
140+
fhandle, data_path, shape, type=dtype, header=False, layered=True
141+
)
139142
vals = ilmfa.value
140143

141144
ilmfa._value # internal storage
@@ -182,7 +185,9 @@
182185

183186
fhandle = open(clayered)
184187
shape = (3, 1000, 100)
185-
clmfa = MFArray.load(fhandle, data_path, shape, header=False, layered=True)
188+
clmfa = MFArray.load(
189+
fhandle, data_path, shape, type=dtype, header=False, layered=True
190+
)
186191

187192
clmfa._value
188193

@@ -235,7 +240,9 @@
235240

236241
fhandle = open(mlayered)
237242
shape = (3, 1000, 100)
238-
mlmfa = MFArray.load(fhandle, data_path, shape, header=False, layered=True)
243+
mlmfa = MFArray.load(
244+
fhandle, data_path, shape, type=dtype, header=False, layered=True
245+
)
239246

240247
mlmfa.how
241248

flopy4/array.py

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,16 @@ def load(cls, f, cwd, shape, header=True, **kwargs):
434434
model_shape = kwargs.pop("model_shape", None)
435435
params = kwargs.pop("blk_params", {})
436436
mempath = kwargs.pop("mempath", None)
437+
atype = kwargs.get("type", None)
438+
439+
if atype is not None:
440+
if atype == "integer":
441+
dtype = np.int32
442+
elif atype == "double":
443+
dtype = np.float64
444+
else:
445+
raise ValueError("array spec type not defined")
446+
437447
if model_shape and isinstance(shape, str):
438448
if shape == "(nodes)":
439449
n = math.prod([x for x in model_shape])
@@ -446,20 +456,30 @@ def load(cls, f, cwd, shape, header=True, **kwargs):
446456
nlay = params.get("dimensions").get("nlay")
447457
nrow = params.get("dimensions").get("nrow")
448458
ncol = params.get("dimensions").get("ncol")
459+
shape = (nlay, nrow, ncol)
460+
elif "disv" in mempath.split("/"):
461+
nlay = params.get("dimensions").get("nlay")
449462
ncpl = params.get("dimensions").get("ncpl")
450-
nodes = params.get("dimensions").get("nodes")
451-
if nrow and ncol:
452-
shape = (nlay, nrow, ncol)
453-
elif ncpl:
463+
nvert = params.get("dimensions").get("nvert")
464+
if shape == "(ncpl)":
465+
shape = ncpl
466+
elif shape == "(ncpl, nlay)":
454467
shape = (nlay, ncpl)
455-
elif nodes:
468+
elif shape == "(nvert)":
469+
shape = nvert
470+
elif "disu" in mempath.split("/"):
471+
nodes = params.get("dimensions").get("nodes")
472+
nja = params.get("dimensions").get("nja")
473+
if "nodes" in shape:
456474
shape = nodes
475+
elif "nja" in shape:
476+
shape = nja
457477
if layered:
458478
nlay = shape[0]
459479
lshp = shape[1:]
460480
objs = []
461481
for _ in range(nlay):
462-
mfa = cls._load(f, cwd, lshp, name)
482+
mfa = cls._load(f, cwd, lshp, dtype=dtype, name=name)
463483
objs.append(mfa)
464484

465485
return MFArray(
@@ -474,11 +494,17 @@ def load(cls, f, cwd, shape, header=True, **kwargs):
474494
else:
475495
kwargs.pop("layered", None)
476496
return cls._load(
477-
f, cwd, shape, layered=layered, name=name, **kwargs
497+
f,
498+
cwd,
499+
shape,
500+
layered=layered,
501+
dtype=dtype,
502+
name=name,
503+
**kwargs,
478504
)
479505

480506
@classmethod
481-
def _load(cls, f, cwd, shape, layered=False, **kwargs):
507+
def _load(cls, f, cwd, shape, layered=False, dtype=None, **kwargs):
482508
control_line = multi_line_strip(f).split()
483509

484510
if CommonNames.iprn.lower() in control_line:
@@ -491,25 +517,31 @@ def _load(cls, f, cwd, shape, layered=False, **kwargs):
491517
clpos = 1
492518

493519
if how == MFArrayType.internal:
494-
array = cls.read_array(f)
520+
array = cls.read_array(f, dtype)
495521

496522
elif how == MFArrayType.constant:
497-
array = float(control_line[clpos])
523+
if dtype == np.float64:
524+
array = float(control_line[clpos])
525+
else:
526+
array = int(control_line[clpos])
498527
clpos += 1
499528

500529
elif how == MFArrayType.external:
501530
extpath = Path(control_line[clpos])
502531
fpath = cwd / extpath
503532
with open(fpath) as foo:
504-
array = cls.read_array(foo)
533+
array = cls.read_array(foo, dtype)
505534
clpos += 1
506535

507536
else:
508537
raise NotImplementedError()
509538

510539
factor = None
511540
if len(control_line) > 2:
512-
factor = float(control_line[clpos + 1])
541+
if dtype == np.float64:
542+
factor = float(control_line[clpos + 1])
543+
else:
544+
factor = int(control_line[clpos + 1])
513545

514546
return cls(
515547
shape,
@@ -521,7 +553,7 @@ def _load(cls, f, cwd, shape, layered=False, **kwargs):
521553
)
522554

523555
@staticmethod
524-
def read_array(f):
556+
def read_array(f, dtype):
525557
"""
526558
Read a MODFLOW 6 array from an open file
527559
into a flat NumPy array representation.
@@ -532,11 +564,11 @@ def read_array(f):
532564
pos = f.tell()
533565
line = f.readline()
534566
line = line_strip(line)
535-
if not re.match("^[0-9. ]+$", line):
567+
if not re.match("^[-0-9. ]+$", line):
536568
f.seek(pos, 0)
537569
break
538570
astr.append(line)
539571

540572
astr = StringIO(" ".join(astr))
541-
array = np.genfromtxt(astr).ravel()
573+
array = np.genfromtxt(astr, dtype=dtype).ravel()
542574
return array

flopy4/block.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ def load(cls, f, **kwargs):
225225
name = None
226226
index = None
227227
found = False
228+
period = False
228229
params = dict()
229230
members = cls.params
230231

@@ -235,15 +236,20 @@ def load(cls, f, **kwargs):
235236
line = f.readline()
236237
if line == "":
237238
raise ValueError("Early EOF, aborting")
238-
if line == "\n":
239+
if line == "\n" or line.lstrip().startswith("#"):
239240
continue
240241
words = strip(line).lower().split()
241-
key = words[0]
242+
if period:
243+
key = "stress_period_data"
244+
else:
245+
key = words[0]
242246
if key == "begin":
243247
found = True
244248
name = words[1]
245249
if len(words) > 2 and str.isdigit(words[2]):
246250
index = int(words[2])
251+
if name == "period":
252+
period = True
247253
elif key == "end":
248254
break
249255
elif found:
@@ -268,20 +274,22 @@ def load(cls, f, **kwargs):
268274
# TODO: inject from model somehow?
269275
# and remove special handling here
270276
kwrgs["cwd"] = ""
277+
# kwrgs["type"] = param.type
271278
kwrgs["mempath"] = f"{mempath}/{name}"
272-
if ptype is not MFArray:
279+
if ptype is not MFArray and ptype is not MFList:
273280
kwrgs.pop("model_shape", None)
274281
kwrgs.pop("blk_params", None)
275282

276283
params[param.name] = ptype.load(f, **kwrgs)
284+
period = False
277285

278286
return cls(name=name, index=index, params=params)
279287

280288
def write(self, f):
281289
"""Write the block to file."""
282290
index = self.index if self.index is not None else ""
283291
begin = f"BEGIN {self.name.upper()} {index}\n"
284-
end = f"END {self.name.upper()}\n"
292+
end = f"END {self.name.upper()}\n\n"
285293

286294
f.write(begin)
287295
super().write(f)

0 commit comments

Comments
 (0)