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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 3 additions & 1 deletion docs/dev/sdd.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,11 @@ The sparse input format used by MODFLOW 6 is in tension with an object model whe

The conversion layer (`flopy4.mf6.converter`) transforms between the product's `xarray`/`attrs`-based object model and plain Python data structures suitable for serialization. This layer is format-agnostic and handles structural transformations common across formats.

The generated classes are the only schema this layer reads. Codegen compiles the DFNs into each class's fields and their metadata, and no DFN is read at load or write: `flopy4.mf6.spec` finds a class's blocks (`file_blocks`) and how a block's rows find their fields (`block_keys`) from those fields. A test holds freshly generated classes to the DFNs they came from (`test/mf6/test_mf6_routing.py`), so what the converter needs from a DFN but a class doesn't carry is a codegen fix.

**Unstructuring (write time)**: `converter.egress.unstructure` converts components to a form suitable for serialization, handling transformations like:

- Grouping fields into blocks according to their `block` metadata from DFNs
- Grouping fields into blocks according to their `block` metadata
- Converting child components to binding tables for parent component name files
- Slicing time-varying arrays by stress period, returned in a period-indexed `dict`
- Converting `Path` objects to tuples (`<name>`, `FILEIN`/`FILEOUT`, `<path>`)
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/circle.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@
"sto = flopy4.mf6.gwf.Sto(\n",
" ss=1.0e-5,\n",
" sy=0.15,\n",
" stress_period_data={0: [(\"STEADY-STATE\",)]},\n",
" stress_period_data={0: \"steady-state\"},\n",
" iconvert=0,\n",
" dims=dims,\n",
")"
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/circle.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def plot_head_ugrid(head, cbc, workspace):
sto = flopy4.mf6.gwf.Sto(
ss=1.0e-5,
sy=0.15,
stress_period_data={0: [("STEADY-STATE",)]},
stress_period_data={0: "steady-state"},
iconvert=0,
dims=dims,
)
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/twri.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@
" storagecoefficient=False,\n",
" ss=1.0e-5,\n",
" sy=0.15,\n",
" stress_period_data={0: [(\"STEADY-STATE\",)]},\n",
" stress_period_data={0: \"steady-state\"},\n",
" iconvert=0,\n",
" dims=dims,\n",
")"
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/twri.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def plot_head(head, workspace):
storagecoefficient=False,
ss=1.0e-5,
sy=0.15,
stress_period_data={0: [("STEADY-STATE",)]},
stress_period_data={0: "steady-state"},
iconvert=0,
dims=dims,
)
Expand Down
4 changes: 2 additions & 2 deletions docs/migrating.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ you need to make anyway: flopy4's class names (`Sto`, not
`flopy.mf6.ModflowGwfsto`), and its constructors taking the parent as a
keyword (`parent=gwf`), not by position.

STO takes one entry per period, `STEADY-STATE` or `TRANSIENT`:
STO takes one word per period, `steady-state` or `transient`:

```python
# flopy3
sto = flopy.mf6.ModflowGwfsto(gwf, steady_state={0: True}, transient={1: True})

# flopy4
sto = Sto(parent=gwf, stress_period_data={0: [("STEADY-STATE",)], 1: [("TRANSIENT",)]})
sto = Sto(parent=gwf, stress_period_data={0: "steady-state", 1: "transient"})
```

OC takes each period's saves and prints in one list, each starting with
Expand Down
2 changes: 1 addition & 1 deletion flopy4/mf6/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def _write_mf6(component: Component, context=None, **kwargs) -> None:

# settle the arrays' and lists' files before opening (and emptying)
# the component's, which a strict write may refuse
data = annotate_arrays(unstructure(component), ctx)
data = annotate_arrays(unstructure(component), type(component), ctx)
where = (component.root, component.filename, component._name_or_class())
data = write_external_arrays(data, *where, ctx)
data = write_external_lists(data, *where, ctx)
Expand Down
7 changes: 6 additions & 1 deletion flopy4/mf6/chf/cdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from flopy4.mf6._types import TimeSeriesRef, to_array
from flopy4.mf6.item import Item
from flopy4.mf6.package import Package
from flopy4.mf6.spec import array, cellid_, child, double, integer, keyword, list_, string
from flopy4.mf6.spec import Header, array, cellid_, child, double, integer, keyword, list_, string
from flopy4.mf6.utl.obs import Obs


Expand All @@ -19,6 +19,10 @@ class Cdb(Package):

multi_package: ClassVar[bool] = True

headers: ClassVar[dict[str, Header]] = {
"period": Header("integer", index=True),
}

observations: ClassVar[dict[str, tuple[tuple[str, ...], ...]]] = {
"cdb": (("cellid",), ("string",)),
"to-mvr": (("cellid",), ("string",)),
Expand Down Expand Up @@ -77,6 +81,7 @@ class StressPeriodData(Item):
repr=False,
block="period",
fill_forward=True,
tagged=False,
)

@property
Expand Down
7 changes: 6 additions & 1 deletion flopy4/mf6/chf/chd.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from flopy4.mf6._types import TimeSeriesRef, to_array
from flopy4.mf6.item import Item
from flopy4.mf6.package import Package
from flopy4.mf6.spec import array, cellid_, child, double, keyword, list_, string
from flopy4.mf6.spec import Header, array, cellid_, child, double, keyword, list_, string
from flopy4.mf6.utl.obs import Obs
from flopy4.mf6.utl.ts import Ts

Expand All @@ -20,6 +20,10 @@ class Chd(Package):

multi_package: ClassVar[bool] = True

headers: ClassVar[dict[str, Header]] = {
"period": Header("integer", index=True),
}

observations: ClassVar[dict[str, tuple[tuple[str, ...], ...]]] = {
"chd": (("cellid",), ("string",)),
}
Expand Down Expand Up @@ -85,6 +89,7 @@ class StressPeriodData(Item):
repr=False,
block="period",
fill_forward=True,
tagged=False,
)

@property
Expand Down
2 changes: 2 additions & 0 deletions flopy4/mf6/chf/cxs.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ class Crosssectiondata(Item):
packagedata: Optional[list[Packagedata]] = list_(
default=None,
block="packagedata",
tagged=False,
shape=("nsections",),
)
crosssectiondata: Optional[list[Crosssectiondata]] = list_(
default=None,
block="crosssectiondata",
tagged=False,
shape=("npoints",),
)

Expand Down
2 changes: 2 additions & 0 deletions flopy4/mf6/chf/disv1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,13 @@ class Cell1d(Item):
vertices: Optional[list[Vertices]] = list_(
default=None,
block="vertices",
tagged=False,
shape=("nvert",),
)
cell1d: Optional[list[Cell1d]] = list_(
default=None,
block="cell1d",
tagged=False,
shape=("nodes",),
)

Expand Down
7 changes: 6 additions & 1 deletion flopy4/mf6/chf/evp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from flopy4.mf6._types import TimeSeriesRef, to_array
from flopy4.mf6.item import Item
from flopy4.mf6.package import Package
from flopy4.mf6.spec import array, cellid_, child, double, keyword, list_, string
from flopy4.mf6.spec import Header, array, cellid_, child, double, keyword, list_, string
from flopy4.mf6.utl.obs import Obs
from flopy4.mf6.utl.ts import Ts

Expand All @@ -20,6 +20,10 @@ class Evp(Package):

multi_package: ClassVar[bool] = True

headers: ClassVar[dict[str, Header]] = {
"period": Header("integer", index=True),
}

observations: ClassVar[dict[str, tuple[tuple[str, ...], ...]]] = {
"evp": (("cellid",), ("string",)),
}
Expand Down Expand Up @@ -85,6 +89,7 @@ class StressPeriodData(Item):
repr=False,
block="period",
fill_forward=True,
tagged=False,
)

@property
Expand Down
7 changes: 6 additions & 1 deletion flopy4/mf6/chf/flw.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from flopy4.mf6._types import TimeSeriesRef, to_array
from flopy4.mf6.item import Item
from flopy4.mf6.package import Package
from flopy4.mf6.spec import array, cellid_, child, double, keyword, list_, string
from flopy4.mf6.spec import Header, array, cellid_, child, double, keyword, list_, string
from flopy4.mf6.utl.obs import Obs
from flopy4.mf6.utl.ts import Ts

Expand All @@ -20,6 +20,10 @@ class Flw(Package):

multi_package: ClassVar[bool] = True

headers: ClassVar[dict[str, Header]] = {
"period": Header("integer", index=True),
}

observations: ClassVar[dict[str, tuple[tuple[str, ...], ...]]] = {
"flw": (("cellid",), ("string",)),
"to-mvr": (("cellid",), ("string",)),
Expand Down Expand Up @@ -86,6 +90,7 @@ class StressPeriodData(Item):
repr=False,
block="period",
fill_forward=True,
tagged=False,
)

@property
Expand Down
6 changes: 5 additions & 1 deletion flopy4/mf6/chf/oc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
from flopy4.mf6.legacy import Legacy, oc_period_data
from flopy4.mf6.package import Package
from flopy4.mf6.record import Record
from flopy4.mf6.spec import array, file, integer, list_, record, string, union
from flopy4.mf6.spec import Header, array, file, integer, list_, record, string, union


@attrs.define(kw_only=True, slots=False)
class Oc(Package):
dfn_name: ClassVar[str] = "chf-oc"
ftype: ClassVar[str] = "OC6"

headers: ClassVar[dict[str, Header]] = {
"period": Header("integer", index=True),
}

legacy_kwargs: ClassVar[dict[str, Legacy]] = {
"saverecord": Legacy("stress_period_data", oc_period_data),
"printrecord": Legacy("stress_period_data", oc_period_data),
Expand Down
7 changes: 6 additions & 1 deletion flopy4/mf6/chf/pcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from flopy4.mf6._types import TimeSeriesRef, to_array
from flopy4.mf6.item import Item
from flopy4.mf6.package import Package
from flopy4.mf6.spec import array, cellid_, child, double, keyword, list_, string
from flopy4.mf6.spec import Header, array, cellid_, child, double, keyword, list_, string
from flopy4.mf6.utl.obs import Obs
from flopy4.mf6.utl.ts import Ts

Expand All @@ -20,6 +20,10 @@ class Pcp(Package):

multi_package: ClassVar[bool] = True

headers: ClassVar[dict[str, Header]] = {
"period": Header("integer", index=True),
}

observations: ClassVar[dict[str, tuple[tuple[str, ...], ...]]] = {
"pcp": (("cellid",), ("string",)),
}
Expand Down Expand Up @@ -85,6 +89,7 @@ class StressPeriodData(Item):
repr=False,
block="period",
fill_forward=True,
tagged=False,
)

@property
Expand Down
22 changes: 9 additions & 13 deletions flopy4/mf6/chf/sto.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
# autogenerated file, do not modify
from typing import ClassVar, Optional
from typing import ClassVar, Literal, Optional

import attrs

from flopy4.mf6.item import Item
from flopy4.mf6.legacy import Legacy, sto_period_data
from flopy4.mf6.package import Package
from flopy4.mf6.spec import keyword, list_, string
from flopy4.mf6.spec import Header, keyword, string


@attrs.define(kw_only=True, slots=False)
class Sto(Package):
dfn_name: ClassVar[str] = "chf-sto"
ftype: ClassVar[str] = "STO6"

headers: ClassVar[dict[str, Header]] = {
"period": Header("integer", index=True),
}

legacy_kwargs: ClassVar[dict[str, Legacy]] = {
"steady_state": Legacy("stress_period_data", sto_period_data),
"transient": Legacy("stress_period_data", sto_period_data),
}

@attrs.define
class StressPeriodData(Item):
stress_period_data: str = string()

save_flows: bool = keyword(
default=False,
block="options",
Expand All @@ -35,13 +34,10 @@ class StressPeriodData(Item):
optional=True,
longname="export array variables to layered ascii files.",
)
_stress_period_data: Optional[dict[int, list[StressPeriodData]]] = list_(
alias="stress_period_data",
stress_period_data: Optional[dict[int, Literal["steady-state", "transient"]]] = string(
default=None,
repr=False,
block="period",
fill_forward=True,
valid=("steady-state", "transient"),
tagged=False,
)


StoStressPeriodData = Sto.StressPeriodData
7 changes: 6 additions & 1 deletion flopy4/mf6/chf/zdg.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from flopy4.mf6._types import TimeSeriesRef, to_array
from flopy4.mf6.item import Item
from flopy4.mf6.package import Package
from flopy4.mf6.spec import array, cellid_, child, double, integer, keyword, list_, string
from flopy4.mf6.spec import Header, array, cellid_, child, double, integer, keyword, list_, string
from flopy4.mf6.utl.obs import Obs
from flopy4.mf6.utl.ts import Ts

Expand All @@ -20,6 +20,10 @@ class Zdg(Package):

multi_package: ClassVar[bool] = True

headers: ClassVar[dict[str, Header]] = {
"period": Header("integer", index=True),
}

observations: ClassVar[dict[str, tuple[tuple[str, ...], ...]]] = {
"to-mvr": (("cellid",), ("string",)),
"zdg": (("cellid",), ("string",)),
Expand Down Expand Up @@ -83,6 +87,7 @@ class StressPeriodData(Item):
repr=False,
block="period",
fill_forward=True,
tagged=False,
)

@property
Expand Down
16 changes: 15 additions & 1 deletion flopy4/mf6/codec/reader/transformer/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,27 @@ def start(self, items: list[Any]) -> dict[str, Any]:
return blocks

def block(self, items: list[Any]) -> dict[str, Any]:
# BEGIN name line... END name
# BEGIN name line... END name. The grammar takes a BEGIN inside a
# block as a row and any name after END, so a block missing its END
# would swallow the next one; like MF6, stop on both.
begin, name, end_name = items[0], items[1], items[-1]

def no_end(found: str) -> ValueError:
return ValueError(
f"{name.upper()} block, opened on line {begin.line}, has no END: "
f"found {found.upper()}"
)

body: list[Any] = []
for item in items[2:-2]:
if isinstance(item, _RecordRows):
body.extend(item)
else:
if isinstance(item, list) and item and item[0].upper() == "BEGIN":
raise no_end(" ".join(item))
body.append(item)
if end_name.split()[0].lower() != name.split()[0].lower():
raise no_end(f"END {end_name}")
if len(body) == 1 and isinstance(body[0], NumberRows):
return {items[1]: body[0]}
if any(isinstance(item, NumberRows) for item in body):
Expand Down
5 changes: 4 additions & 1 deletion flopy4/mf6/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from flopy4.mf6.constants import MF6
from flopy4.mf6.external import in_write_session, write_session
from flopy4.mf6.legacy import Legacy, LegacyKwargs
from flopy4.mf6.spec import fields_dict
from flopy4.mf6.spec import Header, fields_dict
from flopy4.mf6.write_context import WriteContext
from flopy4.spec import is_reference, reference_names, referenced, resolve, retarget
from flopy4.uio import IO, Loader, Writer
Expand Down Expand Up @@ -254,6 +254,9 @@ class Component(DimensionResolverMixin, ABC, MutableMapping, metaclass=LegacyKwa
"""The token naming the component's file type in name files (e.g.
'GWF6', 'CHD6', 'GWF6-GWT6'), from its DFN. Array-based variants share
their base's ('Chdg' is 'CHD6'). Empty for the simulation."""
headers: ClassVar[dict[str, Header]] = {}
"""The header of each of the component's repeating blocks, by block
name, set by codegen from the DFN (see `Header`)."""

_load = IO(Loader) # type: ignore
_write = IO(Writer) # type: ignore
Expand Down
Loading
Loading