Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions flopy4/mf6/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
from attrs import NOTHING
from xattree import array as xattree_array
from xattree import coord as xattree_coord
from xattree import dim as xattree_dim
from xattree import field as xattree_field


def field(
default=NOTHING,
validator=None,
converter=None,
repr=True,
eq=True,
init=True,
metadata=None,
block: str | None = None,
):
"""Create a field."""
if block:
metadata = metadata or {}
metadata["block"] = block
return xattree_field(
default=default,
validator=validator,
converter=converter,
repr=repr,
eq=eq,
init=init,
metadata=metadata,
)


def dim(
scope=None,
coord: bool | str = True,
default=NOTHING,
repr=True,
eq=True,
init=True,
metadata=None,
block: str | None = None,
):
"""Create a dimension field."""
if block:
metadata = metadata or {}
metadata["block"] = block
return xattree_dim(
scope=scope,
coord=coord,
default=default,
repr=repr,
eq=eq,
init=init,
metadata=metadata,
)


def coord(
scope=None,
default=NOTHING,
repr=True,
eq=True,
metadata=None,
block: str | None = None,
):
"""Create a coordinate field."""
if block:
metadata = metadata or {}
metadata["block"] = block
return xattree_coord(
scope=scope,
default=default,
repr=repr,
eq=eq,
metadata=metadata,
)


def array(
cls=None,
dims=None,
default=NOTHING,
validator=None,
converter=None,
repr=True,
eq=None,
metadata=None,
block: str | None = None,
):
"""Create an array field."""
if block:
metadata = metadata or {}
metadata["block"] = block
return xattree_array(
cls=cls,
dims=dims,
default=default,
validator=validator,
converter=converter,
repr=repr,
eq=eq,
metadata=metadata,
)
27 changes: 10 additions & 17 deletions flopy4/mf6/gwf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import xarray as xr
from attrs import define
from flopy.discretization.grid import Grid
from xattree import field, xattree
from xattree import xattree

from flopy4.mf6.decorators import field
from flopy4.mf6.gwf.chd import Chd
from flopy4.mf6.gwf.dis import Dis
from flopy4.mf6.gwf.ic import Ic
Expand Down Expand Up @@ -64,22 +65,14 @@ class NewtonOptions:
newton: bool = field()
under_relaxation: bool = field()

list: Optional[str] = field(default=None, metadata={"block": "options"})
print_input: bool = field(default=False, metadata={"block": "options"})
print_flows: bool = field(default=False, metadata={"block": "options"})
save_flows: bool = field(default=False, metadata={"block": "options"})
newtonoptions: Optional[NewtonOptions] = field(
default=None, metadata={"block": "options"}
)
nc_mesh2d_filerecord: Optional[Path] = field(
default=None, metadata={"block": "options"}
)
nc_structured_filerecord: Optional[Path] = field(
default=None, metadata={"block": "options"}
)
nc_filerecord: Optional[Path] = field(
default=None, metadata={"block": "options"}
)
list: Optional[str] = field(block="options", default=None)
print_input: bool = field(block="options", default=False)
print_flows: bool = field(block="options", default=False)
save_flows: bool = field(block="options", default=False)
newtonoptions: Optional[NewtonOptions] = field(block="options", default=None)
nc_mesh2d_filerecord: Optional[Path] = field(block="options", default=None)
nc_structured_filerecord: Optional[Path] = field(block="options", default=None)
nc_filerecord: Optional[Path] = field(block="options", default=None)

@property
def grid(self) -> Grid:
Expand Down
39 changes: 15 additions & 24 deletions flopy4/mf6/gwf/chd.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import numpy as np
from attrs import Converter, define
from numpy.typing import NDArray
from xattree import array, field, xattree
from xattree import xattree

from flopy4.mf6.converters import convert_array
from flopy4.mf6.decorators import array, field
from flopy4.mf6.package import Package


Expand All @@ -20,57 +21,47 @@ class Steps:
steps: list[int] = field()
frequency: int = field()

auxiliary: Optional[list[str]] = array(
default=None, metadata={"block": "options"}
)
auxmultname: Optional[str] = field(
default=None, metadata={"block": "options"}
)
boundnames: bool = field(default=False, metadata={"block": "options"})
print_input: bool = field(default=False, metadata={"block": "options"})
print_flows: bool = field(default=False, metadata={"block": "options"})
save_flows: bool = field(default=False, metadata={"block": "options"})
ts_filerecord: Optional[Path] = field(
default=None, metadata={"block": "options"}
)
obs_filerecord: Optional[Path] = field(
default=None, metadata={"block": "options"}
)
auxiliary: Optional[list[str]] = array(block="options", default=None)
auxmultname: Optional[str] = field(block="options", default=None)
boundnames: bool = field(block="options", default=False)
print_input: bool = field(block="options", default=False)
print_flows: bool = field(block="options", default=False)
save_flows: bool = field(block="options", default=False)
ts_filerecord: Optional[Path] = field(block="options", default=None)
obs_filerecord: Optional[Path] = field(block="options", default=None)
dev_no_newton: bool = field(default=False, metadata={"block": "options"})
maxbound: Optional[int] = field(
default=None, metadata={"block": "dimensions"}
)
maxbound: Optional[int] = field(block="dimensions", default=None)
head: Optional[NDArray[np.floating]] = array(
block="period",
dims=(
"nper",
"nnodes",
),
default=None,
metadata={"block": "period"},
converter=Converter(convert_array, takes_self=True, takes_field=True),
)
aux: Optional[NDArray[np.floating]] = array(
block="period",
dims=(
"nper",
"nnodes",
),
default=None,
metadata={"block": "period"},
converter=Converter(convert_array, takes_self=True, takes_field=True),
)
boundname: Optional[NDArray[np.str_]] = array(
block="period",
dims=(
"nper",
"nnodes",
),
default=None,
metadata={"block": "period"},
converter=Converter(convert_array, takes_self=True, takes_field=True),
)
steps: Optional[NDArray[np.object_]] = array(
Steps,
block="period",
dims=("nper", "nnodes"),
default=None,
metadata={"block": "period"},
converter=Converter(convert_array, takes_self=True, takes_field=True),
)
37 changes: 15 additions & 22 deletions flopy4/mf6/gwf/dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
from attrs import Converter
from flopy.discretization.structuredgrid import StructuredGrid
from numpy.typing import NDArray
from xattree import array, dim, field, xattree
from xattree import xattree

from flopy4.mf6.converters import convert_array
from flopy4.mf6.decorators import array, dim, field
from flopy4.mf6.package import Package


Expand All @@ -14,65 +15,57 @@ class Dis(Package):
default=None,
metadata={"block": "options"},
)
nogrb: bool = field(default=False, metadata={"block": "options"})
xorigin: float = field(default=None, metadata={"block": "options"})
yorigin: float = field(default=None, metadata={"block": "options"})
angrot: float = field(default=None, metadata={"block": "options"})
export_array_netcdf: bool = field(
default=False, metadata={"block": "options"}
)
nogrb: bool = field(block="options", default=False)
xorigin: float = field(block="options", default=None)
yorigin: float = field(block="options", default=None)
angrot: float = field(block="options", default=None)
export_array_netcdf: bool = field(block="options", default=False)
nlay: int = dim(
block="dimensions",
coord="lay",
scope="gwf",
default=1,
metadata={
"block": "dimensions",
},
)
ncol: int = dim(
block="dimensions",
coord="col",
scope="gwf",
default=2,
metadata={
"block": "dimensions",
},
)
nrow: int = dim(
block="dimensions",
coord="row",
scope="gwf",
default=2,
metadata={
"block": "dimensions",
},
)
delr: NDArray[np.floating] = array(
block="griddata",
default=1.0,
dims=("ncol",),
metadata={"block": "griddata"},
converter=Converter(convert_array, takes_self=True, takes_field=True),
)
delc: NDArray[np.floating] = array(
block="griddata",
default=1.0,
dims=("nrow",),
metadata={"block": "griddata"},
converter=Converter(convert_array, takes_self=True, takes_field=True),
)
top: NDArray[np.floating] = array(
block="griddata",
default=1.0,
dims=("ncol", "nrow"),
metadata={"block": "griddata"},
converter=Converter(convert_array, takes_self=True, takes_field=True),
)
botm: NDArray[np.floating] = array(
block="griddata",
default=0.0,
dims=("ncol", "nrow", "nlay"),
metadata={"block": "griddata"},
converter=Converter(convert_array, takes_self=True, takes_field=True),
)
idomain: NDArray[np.integer] = array(
block="griddata",
default=1,
dims=("ncol", "nrow", "nlay"),
metadata={"block": "griddata"},
converter=Converter(convert_array, takes_self=True, takes_field=True),
)
nnodes: int = dim(
Expand Down
14 changes: 5 additions & 9 deletions flopy4/mf6/gwf/ic.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import numpy as np
from attrs import Converter
from numpy.typing import NDArray
from xattree import array, field, xattree
from xattree import xattree

from flopy4.mf6.converters import convert_array
from flopy4.mf6.decorators import array, field
from flopy4.mf6.package import Package


@xattree
class Ic(Package):
strt: NDArray[np.floating] = array(
block="packagedata",
dims=("nnodes",),
default=1.0,
metadata={"block": "packagedata"},
converter=Converter(convert_array, takes_self=True, takes_field=True),
)
export_array_ascii: bool = field(
default=False, metadata={"block": "options"}
)
export_array_netcdf: bool = field(
default=False,
metadata={"block": "options"},
)
export_array_ascii: bool = field(block="options", default=False)
export_array_netcdf: bool = field(block="options", default=False)
Loading
Loading