diff --git a/flopy4/mf6/chf/oc.py b/flopy4/mf6/chf/oc.py index e03e88a99..624bf345c 100644 --- a/flopy4/mf6/chf/oc.py +++ b/flopy4/mf6/chf/oc.py @@ -28,10 +28,10 @@ class Oc(Package): @attrs.define class Format(Record): _keyword: ClassVar[str] = "" - format_: str = string() columns: Optional[int] = integer(default=None, tagged=True, optional=True) width: Optional[int] = integer(default=None, tagged=True, optional=True) digits: Optional[int] = integer(default=None, tagged=True, optional=True) + format_: Optional[str] = string(default=None, optional=True) @attrs.define class Qoutflowprint(Record): diff --git a/flopy4/mf6/codec/reader/grammar/__init__.py b/flopy4/mf6/codec/reader/grammar/__init__.py index 845b5d7fe..b2235fd8a 100644 --- a/flopy4/mf6/codec/reader/grammar/__init__.py +++ b/flopy4/mf6/codec/reader/grammar/__init__.py @@ -16,6 +16,7 @@ from flopy4.mf6.codec.reader.grammar import filters from flopy4.mf6.codec.reader.grammar.names import rule_name, to_rule_name +from flopy4.mf6.utils.codegen.dfn_fixes import fix_component def _get_env(): @@ -128,7 +129,7 @@ def make_grammar(component: Component, outdir: PathLike): env = _get_env() template = env.get_template("component.lark.jinja") target_path = outdir / f"{component.name}.lark" - blocks, fields = _to_context(component.blocks or {}) + blocks, fields = _to_context(fix_component(component).blocks or {}) text = template.render(name=component.name, blocks=blocks, fields=fields) # one trailing newline, however the template's last rule ends target_path.write_text(text.rstrip("\n") + "\n") diff --git a/flopy4/mf6/codec/reader/grammar/generated/chf-oc.lark b/flopy4/mf6/codec/reader/grammar/generated/chf-oc.lark index b03b6684b..edcc29ecd 100644 --- a/flopy4/mf6/codec/reader/grammar/generated/chf-oc.lark +++ b/flopy4/mf6/codec/reader/grammar/generated/chf-oc.lark @@ -44,7 +44,7 @@ saverecord__rec__elem: "save"i saverecord__rtype__val ocsetting__union [_remark] saverecord__rtype__val: word printrecord__rec__elem: "print"i printrecord__rtype__val ocsetting__union [_remark] _NL printrecord__rtype__val: word -formatrecord__rec: [formatrecord__columns__val] [formatrecord__width__val] [formatrecord__digits__val] formatrecord__format__val +formatrecord__rec: [formatrecord__columns__val] [formatrecord__width__val] [formatrecord__digits__val] [formatrecord__format__val] formatrecord__columns__val: "columns"i integer formatrecord__width__val: "width"i integer formatrecord__digits__val: "digits"i integer diff --git a/flopy4/mf6/codec/reader/grammar/generated/gwe-oc.lark b/flopy4/mf6/codec/reader/grammar/generated/gwe-oc.lark index 2770ce2b7..0577551aa 100644 --- a/flopy4/mf6/codec/reader/grammar/generated/gwe-oc.lark +++ b/flopy4/mf6/codec/reader/grammar/generated/gwe-oc.lark @@ -42,7 +42,7 @@ saverecord__rec__elem: "save"i saverecord__rtype__val ocsetting__union [_remark] saverecord__rtype__val: word printrecord__rec__elem: "print"i printrecord__rtype__val ocsetting__union [_remark] _NL printrecord__rtype__val: word -formatrecord__rec: [formatrecord__columns__val] [formatrecord__width__val] [formatrecord__digits__val] formatrecord__format__val +formatrecord__rec: [formatrecord__columns__val] [formatrecord__width__val] [formatrecord__digits__val] [formatrecord__format__val] formatrecord__columns__val: "columns"i integer formatrecord__width__val: "width"i integer formatrecord__digits__val: "digits"i integer diff --git a/flopy4/mf6/codec/reader/grammar/generated/gwf-oc.lark b/flopy4/mf6/codec/reader/grammar/generated/gwf-oc.lark index 6fbe522e8..d6c17890c 100644 --- a/flopy4/mf6/codec/reader/grammar/generated/gwf-oc.lark +++ b/flopy4/mf6/codec/reader/grammar/generated/gwf-oc.lark @@ -42,7 +42,7 @@ saverecord__rec__elem: "save"i saverecord__rtype__val ocsetting__union [_remark] saverecord__rtype__val: word printrecord__rec__elem: "print"i printrecord__rtype__val ocsetting__union [_remark] _NL printrecord__rtype__val: word -formatrecord__rec: [formatrecord__columns__val] [formatrecord__width__val] [formatrecord__digits__val] formatrecord__format__val +formatrecord__rec: [formatrecord__columns__val] [formatrecord__width__val] [formatrecord__digits__val] [formatrecord__format__val] formatrecord__columns__val: "columns"i integer formatrecord__width__val: "width"i integer formatrecord__digits__val: "digits"i integer diff --git a/flopy4/mf6/codec/reader/grammar/generated/gwt-ist.lark b/flopy4/mf6/codec/reader/grammar/generated/gwt-ist.lark index 5ecf8b20d..520bc5b7c 100644 --- a/flopy4/mf6/codec/reader/grammar/generated/gwt-ist.lark +++ b/flopy4/mf6/codec/reader/grammar/generated/gwt-ist.lark @@ -59,7 +59,7 @@ decay_sorbed__val: "decay_sorbed"i [_remark] [_NL] array bulk_density__val: "bulk_density"i [_remark] [_NL] array distcoef__val: "distcoef"i [_remark] [_NL] array sp2__val: "sp2"i [_remark] [_NL] array -formatrecord__rec: [formatrecord__columns__val] [formatrecord__width__val] [formatrecord__digits__val] formatrecord__format__val +formatrecord__rec: [formatrecord__columns__val] [formatrecord__width__val] [formatrecord__digits__val] [formatrecord__format__val] formatrecord__columns__val: "columns"i integer formatrecord__width__val: "width"i integer formatrecord__digits__val: "digits"i integer diff --git a/flopy4/mf6/codec/reader/grammar/generated/gwt-oc.lark b/flopy4/mf6/codec/reader/grammar/generated/gwt-oc.lark index 47d03e51c..1f86f0bcc 100644 --- a/flopy4/mf6/codec/reader/grammar/generated/gwt-oc.lark +++ b/flopy4/mf6/codec/reader/grammar/generated/gwt-oc.lark @@ -42,7 +42,7 @@ saverecord__rec__elem: "save"i saverecord__rtype__val ocsetting__union [_remark] saverecord__rtype__val: word printrecord__rec__elem: "print"i printrecord__rtype__val ocsetting__union [_remark] _NL printrecord__rtype__val: word -formatrecord__rec: [formatrecord__columns__val] [formatrecord__width__val] [formatrecord__digits__val] formatrecord__format__val +formatrecord__rec: [formatrecord__columns__val] [formatrecord__width__val] [formatrecord__digits__val] [formatrecord__format__val] formatrecord__columns__val: "columns"i integer formatrecord__width__val: "width"i integer formatrecord__digits__val: "digits"i integer diff --git a/flopy4/mf6/codec/reader/grammar/generated/olf-oc.lark b/flopy4/mf6/codec/reader/grammar/generated/olf-oc.lark index b8dbbb25d..5cc2e5b29 100644 --- a/flopy4/mf6/codec/reader/grammar/generated/olf-oc.lark +++ b/flopy4/mf6/codec/reader/grammar/generated/olf-oc.lark @@ -44,7 +44,7 @@ saverecord__rec__elem: "save"i saverecord__rtype__val ocsetting__union [_remark] saverecord__rtype__val: word printrecord__rec__elem: "print"i printrecord__rtype__val ocsetting__union [_remark] _NL printrecord__rtype__val: word -formatrecord__rec: [formatrecord__columns__val] [formatrecord__width__val] [formatrecord__digits__val] formatrecord__format__val +formatrecord__rec: [formatrecord__columns__val] [formatrecord__width__val] [formatrecord__digits__val] [formatrecord__format__val] formatrecord__columns__val: "columns"i integer formatrecord__width__val: "width"i integer formatrecord__digits__val: "digits"i integer diff --git a/flopy4/mf6/gwe/oc.py b/flopy4/mf6/gwe/oc.py index 79d54f2d9..ff503cc22 100644 --- a/flopy4/mf6/gwe/oc.py +++ b/flopy4/mf6/gwe/oc.py @@ -28,10 +28,10 @@ class Oc(Package): @attrs.define class Format(Record): _keyword: ClassVar[str] = "" - format_: str = string() columns: Optional[int] = integer(default=None, tagged=True, optional=True) width: Optional[int] = integer(default=None, tagged=True, optional=True) digits: Optional[int] = integer(default=None, tagged=True, optional=True) + format_: Optional[str] = string(default=None, optional=True) @attrs.define class Temperatureprint(Record): diff --git a/flopy4/mf6/gwf/oc.py b/flopy4/mf6/gwf/oc.py index 42b816950..57877cee0 100644 --- a/flopy4/mf6/gwf/oc.py +++ b/flopy4/mf6/gwf/oc.py @@ -28,10 +28,10 @@ class Oc(Package): @attrs.define class Format(Record): _keyword: ClassVar[str] = "" - format_: str = string() columns: Optional[int] = integer(default=None, tagged=True, optional=True) width: Optional[int] = integer(default=None, tagged=True, optional=True) digits: Optional[int] = integer(default=None, tagged=True, optional=True) + format_: Optional[str] = string(default=None, optional=True) @attrs.define class Headprint(Record): diff --git a/flopy4/mf6/gwt/ist.py b/flopy4/mf6/gwt/ist.py index b214d4976..ac19f28f0 100644 --- a/flopy4/mf6/gwt/ist.py +++ b/flopy4/mf6/gwt/ist.py @@ -20,10 +20,10 @@ class Ist(Package): @attrs.define class Format(Record): _keyword: ClassVar[str] = "" - format_: str = string() columns: Optional[int] = integer(default=None, tagged=True, optional=True) width: Optional[int] = integer(default=None, tagged=True, optional=True) digits: Optional[int] = integer(default=None, tagged=True, optional=True) + format_: Optional[str] = string(default=None, optional=True) @attrs.define class Cimprint(Record): diff --git a/flopy4/mf6/gwt/oc.py b/flopy4/mf6/gwt/oc.py index c1d271884..db15b5a35 100644 --- a/flopy4/mf6/gwt/oc.py +++ b/flopy4/mf6/gwt/oc.py @@ -28,10 +28,10 @@ class Oc(Package): @attrs.define class Format(Record): _keyword: ClassVar[str] = "" - format_: str = string() columns: Optional[int] = integer(default=None, tagged=True, optional=True) width: Optional[int] = integer(default=None, tagged=True, optional=True) digits: Optional[int] = integer(default=None, tagged=True, optional=True) + format_: Optional[str] = string(default=None, optional=True) @attrs.define class Concentrationprint(Record): diff --git a/flopy4/mf6/olf/oc.py b/flopy4/mf6/olf/oc.py index 75e94a594..09e46dffe 100644 --- a/flopy4/mf6/olf/oc.py +++ b/flopy4/mf6/olf/oc.py @@ -28,10 +28,10 @@ class Oc(Package): @attrs.define class Format(Record): _keyword: ClassVar[str] = "" - format_: str = string() columns: Optional[int] = integer(default=None, tagged=True, optional=True) width: Optional[int] = integer(default=None, tagged=True, optional=True) digits: Optional[int] = integer(default=None, tagged=True, optional=True) + format_: Optional[str] = string(default=None, optional=True) @attrs.define class Qoutflowprint(Record): diff --git a/flopy4/mf6/utils/codegen/dfn_fixes.py b/flopy4/mf6/utils/codegen/dfn_fixes.py new file mode 100644 index 000000000..3db7d01ce --- /dev/null +++ b/flopy4/mf6/utils/codegen/dfn_fixes.py @@ -0,0 +1,39 @@ +"""Corrections for where MF6 accepts input its DFNs say it doesn't. + +Classes and grammars are generated from whichever release's DFNs ``flopy4 mf6 +sync`` reads, so a correction has to be made during generation to survive a +sync. One should be removed when no supported release's DFNs need it. +""" + +from typing import Any + +from modflow_devtools.dfns.schema import Component, Record + +# Record parts MF6 doesn't require but the DFNs mark `optional false`. +# +# formatrecord's format: MF6's `print_format` (src/Utilities/InputOutput.f90) +# reads whatever words follow DIGITS, and without any keeps the caller's edit +# descriptor. mf6/test/test060_gms_ets relies on this. +_OPTIONAL_PARTS: dict[str, frozenset[str]] = {"formatrecord": frozenset({"format"})} + + +def _fix_field(field: Any) -> Any: + if not isinstance(field, Record): + return field + optional = _OPTIONAL_PARTS.get(field.name, frozenset()) + fixed = {} + for name, part in field.fields.items(): + part = _fix_field(part) + fixed[name] = part.model_copy(update={"optional": True}) if name in optional else part + return field.model_copy(update={"fields": fixed}) + + +def fix_component(component: Component) -> Component: + """A copy of the component, with the DFN corrections applied.""" + blocks = { + name: block.model_copy( + update={"fields": {k: _fix_field(f) for k, f in block.fields.items()}} + ) + for name, block in (component.blocks or {}).items() + } + return component.model_copy(update={"blocks": blocks or component.blocks}) diff --git a/flopy4/mf6/utils/codegen/make.py b/flopy4/mf6/utils/codegen/make.py index 1810f0f0f..6e2aefe0e 100644 --- a/flopy4/mf6/utils/codegen/make.py +++ b/flopy4/mf6/utils/codegen/make.py @@ -41,6 +41,7 @@ ) from . import filters +from .dfn_fixes import fix_component from .filters import ColumnSpec, FieldV3, ItemColumn, _dq, item_class, pascal_name, python_repr # Pre-computed context dataclasses @@ -2253,7 +2254,9 @@ def make_modules( for name, component in dfns.items(): if name in skip: continue - spec = build_component_spec(component, root=outdir, developmode=developmode, dfns=dfns) + spec = build_component_spec( + fix_component(component), root=outdir, developmode=developmode, dfns=dfns + ) if existing_only: if not spec.outpath.exists(): if verbose: diff --git a/test/mf6/__snapshots__/test_mf6_io_roundtrip/test_roundtrip[mf6testtest060_gms_ets].json b/test/mf6/__snapshots__/test_mf6_io_roundtrip/test_roundtrip[mf6testtest060_gms_ets].json new file mode 100644 index 000000000..4c6d10127 --- /dev/null +++ b/test/mf6/__snapshots__/test_mf6_io_roundtrip/test_roundtrip[mf6testtest060_gms_ets].json @@ -0,0 +1,272 @@ +{ + "ets3_dis.chd": [ + "BEGIN OPTIONS", + " SAVE_FLOWS", + "END OPTIONS", + "", + "BEGIN DIMENSIONS", + " MAXBOUND 30", + "END DIMENSIONS", + "", + "BEGIN PERIOD 1", + "<30 data lines bd4bbce9b8276f4b>", + "END PERIOD 1" + ], + "ets3_dis.dis": [ + "BEGIN OPTIONS", + " LENGTH_UNITS METERS", + " NOGRB", + "END OPTIONS", + "", + "BEGIN DIMENSIONS", + " NLAY 3", + " NROW 15", + " NCOL 15", + "END DIMENSIONS", + "", + "BEGIN GRIDDATA", + " DELR", + " CONSTANT 1524.0", + "", + " DELC", + " CONSTANT 1524.0", + "", + " TOP", + " CONSTANT 60.0", + "", + " BOTM LAYERED", + "", + " CONSTANT -45.0", + " CONSTANT -120.0", + " CONSTANT -215.0", + " IDOMAIN LAYERED", + "", + " CONSTANT 1", + " CONSTANT 1", + " CONSTANT 1", + "END GRIDDATA" + ], + "ets3_dis.drn": [ + "BEGIN OPTIONS", + " AUXILIARY IFACE CONDFACT CELLGRP", + " SAVE_FLOWS", + "END OPTIONS", + "", + "BEGIN DIMENSIONS", + " MAXBOUND 9", + "END DIMENSIONS", + "", + "BEGIN PERIOD 1", + "<9 data lines 819fb6b43045c52b>", + "END PERIOD 1" + ], + "ets3_dis.evt": [ + "BEGIN OPTIONS", + " SAVE_FLOWS", + "END OPTIONS", + "", + "BEGIN DIMENSIONS", + " MAXBOUND 225", + " NSEG 3", + "END DIMENSIONS", + "", + "BEGIN PERIOD 1", + "<225 data lines 0790bd28ac5695b1>", + "END PERIOD 1", + "", + "BEGIN PERIOD 2", + "<225 data lines a42a9711757353b5>", + "END PERIOD 2", + "", + "BEGIN PERIOD 3", + "<225 data lines 90092b8965b86c00>", + "END PERIOD 3" + ], + "ets3_dis.ic": [ + "BEGIN GRIDDATA", + " STRT", + " CONSTANT 0.0", + "", + "END GRIDDATA" + ], + "ets3_dis.ims": [ + "BEGIN OPTIONS", + " PRINT_OPTION summary", + " COMPLEXITY moderate", + "END OPTIONS", + "", + "BEGIN NONLINEAR", + " OUTER_DVCLOSE 1e-06", + "END NONLINEAR", + "", + "BEGIN LINEAR", + " INNER_DVCLOSE 1e-05", + " INNER_RCLOSE 10000.0", + "END LINEAR" + ], + "ets3_dis.nam": [ + "BEGIN OPTIONS", + " LIST ets3_dis.out", + " PRINT_INPUT", + " PRINT_FLOWS", + " SAVE_FLOWS", + "END OPTIONS", + "", + "BEGIN PACKAGES", + " DIS6 ets3_dis.dis dis", + " CHD6 ets3_dis.chd chd-1", + " DRN6 ets3_dis.drn drn-1", + " EVT6 ets3_dis.evt evt-1", + " IC6 ets3_dis.ic ic", + " NPF6 ets3_dis.npf npf", + " OC6 ets3_dis.oc oc", + " RCH6 ets3_dis.rch rch-1", + " STO6 ets3_dis.sto sto", + " WEL6 ets3_dis.wel wel-1", + "END PACKAGES" + ], + "ets3_dis.npf": [ + "BEGIN OPTIONS", + " SAVE_FLOWS", + " VARIABLECV DEWATERED", + " PERCHED", + " REWET WETFCT 1.0 IWETIT 1 IHDWET 0", + "END OPTIONS", + "", + "BEGIN GRIDDATA", + " ICELLTYPE", + " CONSTANT 1", + "", + " K", + " INTERNAL", + "<2926 chars befdf43cbe6a4242>", + " K33", + " INTERNAL", + "<2926 chars ac8913717ec72222>", + " WETDRY", + " CONSTANT 0.01", + "", + "END GRIDDATA" + ], + "ets3_dis.oc": [ + "BEGIN OPTIONS", + " BUDGET FILEOUT ets3_dis.ccf", + " HEAD FILEOUT ets3_dis.hed", + " HEAD PRINT_FORMAT COLUMNS 10 WIDTH 11 DIGITS 4", + "END OPTIONS", + "", + "BEGIN PERIOD 1", + " PRINT BUDGET FIRST", + " SAVE HEAD FIRST", + " SAVE BUDGET FIRST", + "END PERIOD 1", + "", + "BEGIN PERIOD 2", + " PRINT BUDGET FIRST", + " SAVE HEAD FIRST", + " SAVE BUDGET FIRST", + "END PERIOD 2", + "", + "BEGIN PERIOD 3", + " PRINT BUDGET FIRST", + " SAVE HEAD FIRST", + " SAVE BUDGET FIRST", + " PRINT HEAD ALL", + "END PERIOD 3" + ], + "ets3_dis.rch": [ + "BEGIN OPTIONS", + " READASARRAYS", + "END OPTIONS", + "", + "BEGIN PERIOD 1", + " RECHARGE", + " CONSTANT 0.0009", + "", + "END PERIOD 1", + "", + "BEGIN PERIOD 2", + " RECHARGE", + " CONSTANT 0.0009", + "", + "END PERIOD 2" + ], + "ets3_dis.sto": [ + "BEGIN OPTIONS", + " SAVE_FLOWS", + "END OPTIONS", + "", + "BEGIN GRIDDATA", + " ICONVERT", + " CONSTANT 1", + "", + " SS", + " CONSTANT 0.0", + "", + " SY", + " CONSTANT 0.0", + "", + "END GRIDDATA", + "", + "BEGIN PERIOD 1", + " STEADY-STATE", + "END PERIOD 1", + "", + "BEGIN PERIOD 2", + " TRANSIENT", + "END PERIOD 2", + "", + "BEGIN PERIOD 3", + " TRANSIENT", + "END PERIOD 3" + ], + "ets3_dis.tdis": [ + "BEGIN OPTIONS", + " TIME_UNITS days", + "END OPTIONS", + "", + "BEGIN DIMENSIONS", + " NPER 3", + "END DIMENSIONS", + "", + "BEGIN PERIODDATA", + " 1.0 1 1.0", + " 1.0 1 1.0", + " 1.0 1 1.0", + "END PERIODDATA" + ], + "ets3_dis.wel": [ + "BEGIN OPTIONS", + " AUXILIARY IFACE QFACT CELLGRP", + " SAVE_FLOWS", + "END OPTIONS", + "", + "BEGIN DIMENSIONS", + " MAXBOUND 15", + "END DIMENSIONS", + "", + "BEGIN PERIOD 1", + "<15 data lines f5fce17a1bc30b36>", + "END PERIOD 1" + ], + "mfsim.nam": [ + "BEGIN OPTIONS", + "END OPTIONS", + "", + "BEGIN TIMING", + " TDIS6 ets3_dis.tdis", + "END TIMING", + "", + "BEGIN MODELS", + " GWF6 ets3_dis.nam GWF_Model", + "END MODELS", + "", + "BEGIN EXCHANGES", + "END EXCHANGES", + "", + "BEGIN SOLUTIONGROUP 1", + " MXITER 1", + " IMS6 ets3_dis.ims GWF_Model", + "END SOLUTIONGROUP 1" + ] +} diff --git a/test/mf6/__snapshots__/test_mf6_load_all_models/test_load_simulation[mf6testtest060_gms_ets].json b/test/mf6/__snapshots__/test_mf6_load_all_models/test_load_simulation[mf6testtest060_gms_ets].json new file mode 100644 index 000000000..d847aac4c --- /dev/null +++ b/test/mf6/__snapshots__/test_mf6_load_all_models/test_load_simulation[mf6testtest060_gms_ets].json @@ -0,0 +1,513 @@ +{ + "simulation": { + "__type__": "Simulation", + "continue_": false, + "dims": {}, + "exchanges": [], + "filename": "mfsim.nam", + "models": [ + "" + ], + "mxiter": 1, + "nocheck": false, + "print_input": false, + "solutiongroup": [ + "" + ], + "tdis": "", + "workspace": "" + }, + "simulation/GWF_Model": { + "__type__": "Gwf", + "api": [], + "chd": [ + "" + ], + "dims": {}, + "dis": "", + "drn": [ + "" + ], + "evt": [ + "" + ], + "filename": "ets3_dis.nam", + "ghb": [], + "ic": "", + "lak": [], + "list_": "ets3_dis.out", + "maw": [], + "name": "GWF_Model", + "npf": "", + "oc": "", + "print_flows": true, + "print_input": true, + "rch": [ + "" + ], + "riv": [], + "save_flows": true, + "sfr": [], + "sto": "", + "uzf": [], + "wel": [ + "" + ], + "workspace": "" + }, + "simulation/GWF_Model/chd-1": { + "__type__": "Chd", + "_stress_period_data": { + "0": { + "__table__": "StressPeriodData", + "aux": "float64[30, 0] da39a3ee5e6b4b0d", + "boundname": [ + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null + ], + "cellid": "int64[30, 3] 85024be68c8b8bbb", + "head": "float64[30] de0e7c9e496fdd65", + "n": 30 + } + }, + "boundnames": false, + "dims": { + "ncelldim": 3, + "ncol": 15, + "ncpl": 225, + "nlay": 3, + "nodes": 675, + "nrow": 15 + }, + "filename": "ets3_dis.chd", + "name": "chd-1", + "print_flows": false, + "print_input": false, + "save_flows": true, + "ts": [] + }, + "simulation/GWF_Model/dis": { + "__type__": "Dis", + "botm": "float64[675] 88293fed49993361", + "delc": "float64[15] 08d3965f358bb62c", + "delr": "float64[15] 08d3965f358bb62c", + "dims": {}, + "export_array_ascii": false, + "export_array_netcdf": false, + "filename": "ets3_dis.dis", + "idomain": "int64[675] 51bb844a15459418", + "length_units": "METERS", + "name": "dis", + "ncol": 15, + "nlay": 3, + "nogrb": true, + "nrow": 15, + "top": "float64[225] 302a32eb86e5e362" + }, + "simulation/GWF_Model/drn-1": { + "__type__": "Drn", + "_stress_period_data": { + "0": { + "__table__": "StressPeriodData", + "aux": "float64[9, 3] be3a62cda200e2bf", + "boundname": [ + null, + null, + null, + null, + null, + null, + null, + null, + null + ], + "cellid": "int64[9, 3] a739c4e81e7fcc75", + "cond": "float64[9] 813468f3c3db1312", + "elev": "float64[9] 741275df2fd911db", + "n": 9 + } + }, + "auxiliary": [ + "IFACE", + "CONDFACT", + "CELLGRP" + ], + "boundnames": false, + "dims": { + "ncelldim": 3, + "ncol": 15, + "ncpl": 225, + "nlay": 3, + "nodes": 675, + "nrow": 15 + }, + "filename": "ets3_dis.drn", + "mover": false, + "name": "drn-1", + "print_flows": false, + "print_input": false, + "save_flows": true, + "ts": [] + }, + "simulation/GWF_Model/evt-1": { + "__type__": "Evt", + "_stress_period_data": { + "0": { + "__table__": "StressPeriodData", + "aux": "float64[225, 0] da39a3ee5e6b4b0d", + "boundname": "1350 chars e6be834cd4fec514", + "cellid": "int64[225, 3] 69e7be7018829203", + "depth": "float64[225] 29c3fc253524f2b4", + "n": 225, + "petm": "float64[225, 2] 7130d0fe9316b0c2", + "petm0": "1350 chars e6be834cd4fec514", + "pxdp": "float64[225, 2] 3dedb833f4136175", + "rate": "float64[225] 6a44b5eccf16b6aa", + "surface": "float64[225] 9d1cbc096a295737" + }, + "1": { + "__table__": "StressPeriodData", + "aux": "float64[225, 0] da39a3ee5e6b4b0d", + "boundname": "1350 chars e6be834cd4fec514", + "cellid": "int64[225, 3] 0f208be05ba59abb", + "depth": "float64[225] 29c3fc253524f2b4", + "n": 225, + "petm": "float64[225, 2] 7130d0fe9316b0c2", + "petm0": "1350 chars e6be834cd4fec514", + "pxdp": "float64[225, 2] 3dedb833f4136175", + "rate": "float64[225] 6a44b5eccf16b6aa", + "surface": "float64[225] 0e5aee74e26c5146" + }, + "2": { + "__table__": "StressPeriodData", + "aux": "float64[225, 0] da39a3ee5e6b4b0d", + "boundname": "1350 chars e6be834cd4fec514", + "cellid": "int64[225, 3] 0f208be05ba59abb", + "depth": "float64[225] 354b08e86e976fc4", + "n": 225, + "petm": "float64[225, 2] a78eba2ca799330e", + "petm0": "1350 chars e6be834cd4fec514", + "pxdp": "float64[225, 2] b4c9f651488c82bd", + "rate": "float64[225] 8e125d03c1e9710d", + "surface": "float64[225] 0e5aee74e26c5146" + } + }, + "boundnames": false, + "dims": { + "ncelldim": 3, + "ncol": 15, + "ncpl": 225, + "nlay": 3, + "nodes": 675, + "nrow": 15 + }, + "filename": "ets3_dis.evt", + "fixed_cell": false, + "name": "evt-1", + "nseg": 3, + "print_flows": false, + "print_input": false, + "save_flows": true, + "surf_rate_specified": false, + "ts": [] + }, + "simulation/GWF_Model/ic": { + "__type__": "Ic", + "dims": { + "ncelldim": 3, + "ncol": 15, + "ncpl": 225, + "nlay": 3, + "nodes": 675, + "nrow": 15 + }, + "export_array_ascii": false, + "export_array_netcdf": false, + "filename": "ets3_dis.ic", + "name": "ic", + "strt": "float64[675] 4b6a77fd2118c2a0" + }, + "simulation/GWF_Model/npf": { + "__type__": "Npf", + "cvoptions": { + "__type__": "Cvoptions", + "dewatered": true + }, + "dims": { + "ncelldim": 3, + "ncol": 15, + "ncpl": 225, + "nlay": 3, + "nodes": 675, + "nrow": 15 + }, + "export_array_ascii": false, + "export_array_netcdf": false, + "filename": "ets3_dis.npf", + "highest_cell_saturation": false, + "icelltype": "int64[675] 51bb844a15459418", + "k": "float64[675] 4a0a83c295c5796c", + "k22overk": false, + "k33": "float64[675] c926c4ed15e78979", + "k33overk": false, + "name": "npf", + "perched": true, + "print_flows": false, + "rewet": { + "__type__": "Rewet", + "ihdwet": 0, + "iwetit": 1, + "wetfct": "1.0" + }, + "save_flows": true, + "save_saturation": false, + "save_specific_discharge": false, + "thickstrt": false, + "wetdry": "float64[675] 8523721a2130adfe" + }, + "simulation/GWF_Model/oc": { + "__type__": "Oc", + "_stress_period_data": { + "0": [ + { + "__type__": "Print", + "ocsetting": "FIRST", + "rtype": "BUDGET" + }, + { + "__type__": "Save", + "ocsetting": "FIRST", + "rtype": "HEAD" + }, + { + "__type__": "Save", + "ocsetting": "FIRST", + "rtype": "BUDGET" + } + ], + "1": [ + { + "__type__": "Print", + "ocsetting": "FIRST", + "rtype": "BUDGET" + }, + { + "__type__": "Save", + "ocsetting": "FIRST", + "rtype": "HEAD" + }, + { + "__type__": "Save", + "ocsetting": "FIRST", + "rtype": "BUDGET" + } + ], + "2": [ + { + "__type__": "Print", + "ocsetting": "FIRST", + "rtype": "BUDGET" + }, + { + "__type__": "Save", + "ocsetting": "FIRST", + "rtype": "HEAD" + }, + { + "__type__": "Save", + "ocsetting": "FIRST", + "rtype": "BUDGET" + }, + { + "__type__": "Print", + "ocsetting": "ALL", + "rtype": "HEAD" + } + ] + }, + "budget_file": "ets3_dis.ccf", + "dims": { + "ncelldim": 3, + "ncol": 15, + "ncpl": 225, + "nlay": 3, + "nodes": 675, + "nrow": 15 + }, + "filename": "ets3_dis.oc", + "head_file": "ets3_dis.hed", + "headprint": { + "__type__": "Headprint", + "formatrecord": { + "__type__": "Format", + "columns": 10, + "digits": 4, + "format_": null, + "width": 11 + } + }, + "name": "oc" + }, + "simulation/GWF_Model/rch-1": { + "__type__": "Rcha", + "dims": { + "ncelldim": 3, + "ncol": 15, + "ncpl": 225, + "nlay": 3, + "nodes": 675, + "nrow": 15 + }, + "export_array_netcdf": false, + "filename": "ets3_dis.rch", + "fixed_cell": false, + "name": "rch-1", + "print_flows": false, + "print_input": false, + "readasarrays": true, + "recharge": { + "0": "float64[675] b93e59bb6a62703c", + "1": "float64[675] b93e59bb6a62703c" + }, + "save_flows": false, + "tas": [] + }, + "simulation/GWF_Model/sto": { + "__type__": "Sto", + "dims": { + "ncelldim": 3, + "ncol": 15, + "ncpl": 225, + "nlay": 3, + "nodes": 675, + "nrow": 15 + }, + "export_array_ascii": false, + "export_array_netcdf": false, + "filename": "ets3_dis.sto", + "iconvert": "int64[675] 51bb844a15459418", + "name": "sto", + "save_flows": true, + "ss": "float64[675] 4b6a77fd2118c2a0", + "ss_confined_only": false, + "storagecoefficient": false, + "stress_period_data": { + "0": "steady-state", + "1": "transient", + "2": "transient" + }, + "sy": "float64[675] 4b6a77fd2118c2a0" + }, + "simulation/GWF_Model/wel-1": { + "__type__": "Wel", + "_stress_period_data": { + "0": { + "__table__": "StressPeriodData", + "aux": "float64[15, 3] 490166baf7cb3a9a", + "boundname": [ + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null + ], + "cellid": "int64[15, 3] 6a0f93accc30c380", + "n": 15, + "q": "float64[15] df3c4269264dfabf" + } + }, + "auxiliary": [ + "IFACE", + "QFACT", + "CELLGRP" + ], + "boundnames": false, + "dims": { + "ncelldim": 3, + "ncol": 15, + "ncpl": 225, + "nlay": 3, + "nodes": 675, + "nrow": 15 + }, + "filename": "ets3_dis.wel", + "flow_reduction_length": false, + "mover": false, + "name": "wel-1", + "print_flows": false, + "print_input": false, + "save_flows": true, + "ts": [] + }, + "simulation/ims-1": { + "__type__": "Ims", + "complexity": "moderate", + "dims": {}, + "filename": "ets3_dis.ims", + "inner_dvclose": "1e-05", + "name": "ims-1", + "outer_dvclose": "1e-06", + "print_option": "summary", + "rclose": { + "__type__": "Rclose", + "inner_rclose": "10000.0", + "rclose_option": null + }, + "slnmnames": [ + "GWF_Model" + ] + }, + "simulation/tdis": { + "__type__": "Tdis", + "dims": {}, + "filename": "ets3_dis.tdis", + "name": "tdis", + "nper": 3, + "perioddata": { + "__table__": "Perioddata", + "n": 3, + "nstp": "int64[3] 8aaab8ea3dc5e112", + "perlen": "float64[3] 12a72e95677fc01d", + "tsmult": "float64[3] 12a72e95677fc01d" + }, + "time_units": "days" + } +} diff --git a/test/mf6/test_mf6_codec.py b/test/mf6/test_mf6_codec.py index 21a62a576..728fc9238 100644 --- a/test/mf6/test_mf6_codec.py +++ b/test/mf6/test_mf6_codec.py @@ -976,7 +976,7 @@ def test_series_attributes_from_values(): ("gwf.Npf", "xt3doptions", True, ("Xt3doptions", ())), ("gwf.Npf", "xt3doptions", {"rhs": True}, ("Xt3doptions", (True,))), ("gwf.Oc", "headprint", "COLUMNS 10 WIDTH 12 DIGITS 6 GENERAL", ("Headprint", None)), - ("gwt.Oc", "concentrationprint", ("GENERAL", 10, 12, 6), ("Concentrationprint", None)), + ("gwt.Oc", "concentrationprint", (10, 12, 6, "GENERAL"), ("Concentrationprint", None)), ( "gwe.Oc", "temperatureprint", @@ -1001,7 +1001,7 @@ def test_record_fields_from_values(cls_path, name, value, record): cls = getattr(importlib.import_module(f"flopy4.mf6.{module}"), cls_name) rec_name, args = record if args is None: # a print format, one nested Format - args = (cls.Format("GENERAL", columns=10, width=12, digits=6),) + args = (cls.Format(columns=10, width=12, digits=6, format_="GENERAL"),) expected = getattr(cls, rec_name)(*args) component = cls(**{name: value}) assert getattr(component, name) == expected @@ -1401,6 +1401,51 @@ def test_oc_head_file_and_print_format(): assert "STEPS 1 5 10\n" in out +def test_oc_print_format_without_format_word(): + """MF6 doesn't need the format word, though the DFN says it does. Left + out, it stays out when written, rather than being written as None.""" + from flopy4.mf6.converter.egress.unstructure import unstructure_component + from flopy4.mf6.converter.ingress.structure import structure_component + from flopy4.mf6.gwf import Oc + + text = """BEGIN OPTIONS + HEAD PRINT_FORMAT COLUMNS 10 WIDTH 11 DIGITS 4 +END OPTIONS +""" + oc = structure_component(loads(text), Oc) + assert oc.headprint.formatrecord == Oc.Format(columns=10, width=11, digits=4) + assert oc.headprint.formatrecord.format_ is None + assert " HEAD PRINT_FORMAT COLUMNS 10 WIDTH 11 DIGITS 4\n" in dumps(unstructure_component(oc)) + + +@pytest.mark.parametrize( + "dfn_name, word", + [ + ("gwf-oc", "HEAD"), + ("gwt-oc", "CONCENTRATION"), + ("gwe-oc", "TEMPERATURE"), + ("chf-oc", "QOUTFLOW"), + ("olf-oc", "QOUTFLOW"), + ("gwt-ist", "CIM"), + ], +) +@pytest.mark.parametrize("format_", ["", " GENERAL"]) +def test_typed_print_format_word_optional(dfn_name, word, format_): + """Every component with a print format parses it with or without the + format word, and a word that's there is still the format.""" + from flopy4.mf6.codec.reader import loads_typed + + text = ( + f"BEGIN OPTIONS\n {word} PRINT_FORMAT COLUMNS 10 WIDTH 11 DIGITS 4{format_}\nEND OPTIONS\n" + ) + records = loads_typed(text, dfn_name)["options"] + assert len(records) == 1 + expected = {"columns": 10, "width": 11, "digits": 4} + if format_: + expected["format"] = format_.strip() + assert next(iter(records.values())) == {"formatrecord": expected} + + def test_ims_rclose_and_unknown_block(): from flopy4.mf6.converter.egress.unstructure import unstructure_component from flopy4.mf6.converter.ingress.structure import structure_component @@ -3473,7 +3518,7 @@ def test_headprint_from_tokens_no_prefix(): def test_headprint_from_tokens_format_only(): - """A single untagged token populates the required positional field.""" + """A single untagged token populates the positional field.""" from flopy4.mf6.gwf.oc import Oc hp = Oc.Headprint.from_tokens("exponential") @@ -3535,10 +3580,10 @@ def test_rclose_from_tokens_with_option(): def test_from_tokens_missing_required_raises(): """attrs raises TypeError when a required field has no value.""" - from flopy4.mf6.gwf.oc import Oc + from flopy4.mf6.gwf import Npf with pytest.raises(TypeError): - Oc.Headprint.from_tokens("") + Npf.Rewet.from_tokens("") # --------------------------------------------------------------------------- diff --git a/test/mf6/test_mf6_load_all_models.py b/test/mf6/test_mf6_load_all_models.py index 126c8542b..fb81d11be 100644 --- a/test/mf6/test_mf6_load_all_models.py +++ b/test/mf6/test_mf6_load_all_models.py @@ -339,6 +339,7 @@ "mf6/test/test056_mt3dms_usgs_gwtex_dev", "mf6/test/test057_transientchd", "mf6/test/test059_mvlake_lak_ss", + "mf6/test/test060_gms_ets", "mf6/test/test061_csub_holly", "mf6/test/test061_csub_jacob", "mf6/test/test100_ss_ic", diff --git a/test/mf6/test_mf6_sync.py b/test/mf6/test_mf6_sync.py index 949be7cb4..af87509c0 100644 --- a/test/mf6/test_mf6_sync.py +++ b/test/mf6/test_mf6_sync.py @@ -341,6 +341,12 @@ def test_grammars_generated(dfn_path, outdir): parser.parse("BEGIN OPTIONS\n PRINT_INPUT\nEND OPTIONS\n") +def test_dfn_fixes_generated(outdir): + """A sync keeps the corrections to the DFNs, in the classes and grammars.""" + assert "format_: Optional[str] = string(default=None" in (outdir / "gwf" / "oc.py").read_text() + assert "[formatrecord__format__val]" in _grammars(outdir)["gwf-oc.lark"] + + def test_stale_grammars_removed(dfn_path, outdir): """A grammar for a component the DFNs no longer have is removed.""" stale = outdir / _sync._GRAMMARS / "swf-zzz.lark"