Skip to content

Commit

Permalink
add basic MFArray tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mjreno authored and mjreno committed Jul 19, 2024
1 parent 2509c9c commit 4e8ba01
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 12 deletions.
18 changes: 13 additions & 5 deletions flopy4/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def write(self, f):
pass

@classmethod
def load(cls, f, cwd, shape, layered=False):
def load(cls, f, cwd, shape):
"""
Parameters
Expand All @@ -339,29 +339,36 @@ def load(cls, f, cwd, shape, layered=False):
-------
MFArray
"""
layered = False
tokens = multi_line_strip(f).split()
name = tokens[0]
if len(tokens) > 1 and tokens[1] == 'layered':
layered = True

if layered:
nlay = shape[0]
lay_shape = shape[1:]
objs = []
for _ in range(nlay):
mfa = cls._load(f, cwd, lay_shape)
mfa = cls._load(f, cwd, lay_shape, name)
objs.append(mfa)

mfa = MFArray(
np.array(objs, dtype=object),
shape,
how=None,
factor=None,
name=name,
layered=True,
)

else:
mfa = cls._load(f, cwd, shape, layered=layered)
mfa = cls._load(f, cwd, shape, name, layered=layered)

return mfa

@classmethod
def _load(cls, f, cwd, shape, layered=False):
def _load(cls, f, cwd, shape, name, layered=False):
"""
Parameters
Expand Down Expand Up @@ -406,7 +413,7 @@ def _load(cls, f, cwd, shape, layered=False):
if len(control_line) > 2:
factor = float(control_line[clpos + 1])

mfa = cls(array, shape, how, factor=factor)
mfa = cls(array, shape, how, factor=factor, name=name)
return mfa

@staticmethod
Expand All @@ -433,6 +440,7 @@ def read_array(f):
CommonNames.internal in line
or CommonNames.external in line
or CommonNames.constant in line
or CommonNames.end in line.upper()
):
f.seek(pos, 0)
break
Expand Down
6 changes: 5 additions & 1 deletion flopy4/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Any, Dict

from flopy4.parameter import MFParameter, MFParameters
from flopy4.array import MFArray
from flopy4.utils import strip


Expand Down Expand Up @@ -74,7 +75,10 @@ def load(cls, f, strict=False):
f.seek(pos)
param = members[key]
param.block = name
params[key] = type(param).load(f, spec=param)
if type(param) is MFArray:
params[key] = MFArray.load(f, cwd='', shape=(3))
else:
params[key] = type(param).load(f, spec=param)

return cls(name, index, **params)

Expand Down
1 change: 1 addition & 0 deletions flopy4/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ class CommonNames:
vertex = "vertex"
unstructured = "unstructured"
empty = ""
end = "END"
58 changes: 58 additions & 0 deletions test/test_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import numpy as np

from flopy4.array import MFArray


def test_array_load_1d(tmp_path):
name = "array"
fpth = tmp_path / f"{name}.txt"
how = "INTERNAL"
v = [1.0, 2.0, 3.0]
value = " ".join(str(x) for x in v)
with open(fpth, "w") as f:
f.write(f"{name.upper()}\n{how}\n{value}\n")

with open(fpth, "r") as f:
array = MFArray.load(f, cwd=tmp_path, shape=(3))
assert array.name == name
assert np.allclose(array.value, np.array(v))


def test_array_load_3d(tmp_path):
name = "array"
fpth = tmp_path / f"{name}.txt"
how = "INTERNAL"
v = [[[1.0, 2.0, 3.0]], [[4.0, 5.0, 6.0]], [[7.0, 8.0, 9.0]]]
value = ""
for a in v:
for b in a:
value += " ".join(str(x) for x in b)
value += " "
with open(fpth, "w") as f:
f.write(f"{name.upper()}\n{how}\n{value}\n")

with open(fpth, "r") as f:
array = MFArray.load(f, cwd=tmp_path, shape=(3, 1, 3))
assert array.name == name
assert np.allclose(array.value, np.array(v))


def test_array_load_layered(tmp_path):
name = "array"
fpth = tmp_path / f"{name}.txt"
how = "INTERNAL"
v = [[[1.0, 2.0, 3.0]], [[4.0, 5.0, 6.0]], [[7.0, 8.0, 9.0]]]
value = ""
for a in v:
for b in a:
# TODO: MFArray expects this on separate line
value += f"{how}\n"
value += " ".join(str(x) for x in b)
value += "\n"
with open(fpth, "w") as f:
f.write(f"{name.upper()} LAYERED\n{value}")

with open(fpth, "r") as f:
array = MFArray.load(f, cwd=tmp_path, shape=(3, 1, 3))
assert array.name == name
assert np.allclose(array.value, np.array(v))
18 changes: 12 additions & 6 deletions test/test_block.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import numpy as np

from flopy4.array import MFArray
from flopy4.block import MFBlock, get_member_params
from flopy4.scalar import MFDouble, MFFilename, MFInteger, MFKeyword, MFString

Expand All @@ -10,12 +13,12 @@ class TestBlock(MFBlock):
d = MFDouble(description="double")
s = MFString(description="string", optional=False)
f = MFFilename(description="filename", optional=False)
# a = MFArray(description="array")
a = MFArray(array=[], shape=(3), description="array")


def test_params():
params = get_member_params(TestBlock)
assert len(params) == 5
assert len(params) == 6

k = params["k"]
assert isinstance(k, MFKeyword)
Expand All @@ -42,10 +45,10 @@ def test_params():
assert f.description == "filename"
assert not f.optional

# a = params["a"]
# assert isinstance(f, MFArray)
# assert f.description == "array"
# assert not f.optional
a = params["a"]
assert isinstance(a, MFArray)
assert a.description == "array"
assert a.optional


def test_load_write(tmp_path):
Expand All @@ -58,6 +61,7 @@ def test_load_write(tmp_path):
f.write(" D 1.0\n")
f.write(" S value\n")
f.write(f" F FILEIN {fpth}\n")
f.write(" A\n INTERNAL\n 1.0 2.0 3.0\n")
f.write(f"END {name.upper()}\n")

# test block load
Expand All @@ -77,6 +81,7 @@ def test_load_write(tmp_path):
assert block.d == 1.0
assert block.s == "value"
assert block.f == fpth
assert np.allclose(block.a, np.array([1.0, 2.0, 3.0]))

# test block write
fpth2 = tmp_path / f"{name}2.txt"
Expand All @@ -89,3 +94,4 @@ def test_load_write(tmp_path):
assert " D 1.0\n" in lines
assert " S value\n" in lines
assert f" F FILEIN {fpth}\n" in lines
#assert " A\n INTERNAL\n 1.0 2.0 3.0\n" in lines

0 comments on commit 4e8ba01

Please sign in to comment.