Skip to content

Commit 53dd0d8

Browse files
committed
Correctly write Oc package
1 parent 8b9b8e4 commit 53dd0d8

File tree

5 files changed

+68
-14
lines changed

5 files changed

+68
-14
lines changed

flopy4/mf6/filters.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ def fieldkind(field: Attribute) -> str:
1515
- 'dim' for integer fields describing a dimension's size
1616
- 'attr' for all other fields
1717
"""
18-
if meta := field.metadata is None:
18+
if (meta := field.metadata) is None:
1919
raise TypeError(f"Field {field.name} has no metadata")
20-
if xatmeta := meta.get("xattree", None) is None:
20+
if (xatmeta := meta.get("xattree", None)) is None:
2121
raise TypeError(f"Field {field.name} has no xattree metadata")
22-
if kind := xatmeta.get("kind", None) is None:
22+
if "kind" not in xatmeta:
2323
raise TypeError(f"Field {field.name} has no kind")
24-
return kind
24+
return xatmeta.get("kind", "attr")
2525

2626

2727
@pass_context
@@ -32,7 +32,9 @@ def fieldvalue(ctx, field: Attribute):
3232

3333
def arraydelayed(value: xr.DataArray):
3434
"""Yield chunks (lines) from a Dask array."""
35-
for chunk in value.data.to_delayed():
35+
# TODO: Determine a good chunk size,
36+
# because if the underlying array is only numpy, it will stay one block.
37+
for chunk in value.chunk():
3638
yield chunk.compute()
3739

3840

flopy4/mf6/templates/blocks.jinja

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{% import 'macros.jinja' as macros with context %}
2+
13
{% for block_name, block_ in blocks.items() %}
24
BEGIN {{ block_name }}
35
{% for field in block_.values() %}

flopy4/mf6/templates/macros.jinja

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,31 @@
22
{% set kind = field|fieldkind %}
33
{% if kind == 'attr' %}
44
{% if field.type|isdict %}
5-
{{ macros.record(field) }}
5+
{{ record(field) }}
66
{% else %}
7-
{{ macros.scalar(field) }}
7+
{{ scalar(field) }}
88
{% endif %}
99
{% elif kind in ['array', 'coord'] %}
10-
{{ macros.array(field) }}
10+
{{ array(field) }}
1111
{% elif kind == 'dim' %}
12-
{{ macros.scalar(field) }}
12+
{{ scalar(field) }}
1313
{% elif kind == 'child' %}
1414
{# TODO #}
1515
{% endif %}
1616
{% endmacro %}
1717

1818
{% macro scalar(field) %}
19-
{{ field.name }} {% endif %}{{ field.value }}
19+
{{ field.name }} {{ field.value }}
2020
{% endmacro %}
2121

2222
{% macro keystring(field) %} {# union #}
2323
{% for item in field.value.items() %}
24-
{{ macros.field(item) }}
24+
{{ field(item) }}
2525
{% endfor %}
2626
{% endmacro %}
2727

2828
{% macro record(field) %}
29-
{% for item in field.values() %}{% if item.tagged %}{{ item.name }} {% endif %}{{ macros.field(field) }}{%
29+
{% for item in field.values() %}{% if item.tagged %}{{ item.name }} {% endif %}{{ field(field) }}{%
3030
endfor
3131
%}
3232
{% endmacro %}
@@ -43,8 +43,9 @@ CONSTANT
4343
{% if layered %}
4444
{{ field.name }}
4545
{% for val in field.value %}{{ val }}{% endfor %}
46+
{% endif %}
4647
{% elif how == "internal" %}
47-
{{ field.name }} {{ macro.internal_array(field) }}
48+
{{ field.name }} {{ internal_array(field) }}
4849
{% elif how == "external" %}
4950
{{ field.name}} OPEN/CLOSE {{ field.value }}
5051
{% endif %}

flopy4/mf6/utils/cbc_reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from attrs import define
1313
from flopy.discretization import StructuredGrid
1414

15-
from flopy4.structured_grid import StructuredGridWrapper
15+
from flopy4.discretization.structured_grid import StructuredGridWrapper
1616

1717
from .grid_utils import get_coords
1818

test/test_jinja.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from pathlib import Path
2+
3+
from flopy.discretization import StructuredGrid
4+
from flopy.discretization.modeltime import ModelTime
5+
from jinja2 import Environment, PackageLoader
6+
7+
from flopy4.mf6 import filters
8+
from flopy4.mf6.gwf import Chd, Gwf, Npf, Oc
9+
from flopy4.mf6.ims import Ims
10+
from flopy4.mf6.simulation import Simulation
11+
from flopy4.mf6.spec import blocks_dict, fields_dict
12+
13+
14+
def test_simulation_to_jinja():
15+
name = "quickstart"
16+
workspace = Path(__file__).parent / name
17+
time = ModelTime(perlen=[1.0], nstp=[1])
18+
grid = StructuredGrid(nlay=1, nrow=10, ncol=10)
19+
sim = Simulation(name=name, path=workspace, tdis=time)
20+
ims = Ims(parent=sim)
21+
gwf_name = "mymodel"
22+
gwf = Gwf(parent=sim, name=gwf_name, save_flows=True, dis=grid)
23+
npf = Npf(parent=gwf, save_specific_discharge=True)
24+
chd = Chd(
25+
parent=gwf,
26+
head={"*": {(0, 0, 0): 1.0, (0, 9, 9): 0.0}},
27+
)
28+
oc = Oc(
29+
parent=gwf,
30+
budget_file=f"{gwf.name}.bud",
31+
head_file=f"{gwf.name}.hds",
32+
save_head={"*": "all"},
33+
save_budget={"*": "all"},
34+
)
35+
36+
env = Environment(
37+
loader=PackageLoader("flopy4.mf6"),
38+
trim_blocks=True,
39+
lstrip_blocks=True,
40+
)
41+
env.filters["fieldkind"] = filters.fieldkind
42+
env.filters["fieldvalue"] = filters.fieldvalue
43+
env.filters["arraydelayed"] = filters.arraydelayed
44+
env.filters["array2string"] = filters.array2string
45+
46+
fields = fields_dict(Oc)
47+
blocks = blocks_dict(Oc)
48+
result = env.get_template("blocks.jinja").render(fields=fields, blocks=blocks, data=oc.data)
49+
assert result != ""

0 commit comments

Comments
 (0)