Skip to content

Commit 615f714

Browse files
committed
Make tests more trivial and warn when uniform and double tanh
1 parent 43a7dcf commit 615f714

2 files changed

Lines changed: 59 additions & 29 deletions

File tree

pydomcfg/domzgr/zco.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def _compute_pp(
149149
if not all(_are_nemo_none((ppsur, ppa0, ppa1))):
150150
warnings.warn(
151151
"Uniform grid case (no stretching):"
152-
" ppsur, ppa0, and ppa1 are ignored because ppacr == ppkth == 0."
152+
" ppsur, ppa0 and ppa1 are ignored when ppacr == ppkth == 0"
153153
)
154154

155155
return (0, 0, 0)
@@ -283,9 +283,8 @@ def _analyt_e3(self) -> Tuple[DataArray, ...]:
283283

284284
return tuple(zco_e3)
285285

286-
@staticmethod
287286
def _get_ldbletanh_and_pp2(
288-
ldbletanh: Optional[bool], pp2: Tuple[Optional[float], ...]
287+
self, ldbletanh: Optional[bool], pp2: Tuple[Optional[float], ...]
289288
) -> Tuple[bool, Tuple[float, ...]]:
290289
"""
291290
If ldbletanh is None, its bool value is inferred from pp2.
@@ -294,17 +293,29 @@ def _get_ldbletanh_and_pp2(
294293

295294
pp_are_none = tuple(pp is None for pp in pp2)
296295
prefix_msg = "ppa2, ppkth2 and ppacr2"
296+
ldbletanh_out = ldbletanh if (ldbletanh is not None) else not any(pp_are_none)
297+
298+
# Warnings: Ignore double tanh coeffiecients
299+
if ldbletanh_out and self._is_uniform:
300+
# Uniform and double tanh
301+
warning_msg = (
302+
"Uniform grid case (no stretching):"
303+
f" {prefix_msg} are ignored when ppacr == ppkth == 0"
304+
)
305+
elif ldbletanh is False and not all(pp_are_none):
306+
# ldbletanh False and double tanh coefficients specified
307+
warning_msg = f"{prefix_msg} are ignored when ldbletanh is False"
308+
else:
309+
warning_msg = ""
310+
311+
if warning_msg:
312+
warnings.warn(warning_msg)
313+
return (False, (0, 0, 0))
297314

298-
# Errors
315+
# Errors: Wrong types
299316
if ldbletanh is True and any(pp_are_none):
300317
raise ValueError(f"{prefix_msg} MUST be all float when ldbletanh is True")
301318
if ldbletanh is None and (any(pp_are_none) and not all(pp_are_none)):
302319
raise ValueError(f"{prefix_msg} MUST be all None or float")
303320

304-
# Warning
305-
if ldbletanh is False and not all(pp_are_none):
306-
warnings.warn(f"{prefix_msg} are ignored when ldbletanh is False")
307-
308-
ldbletanh = ldbletanh if (ldbletanh is not None) else not any(pp_are_none)
309-
310-
return (ldbletanh, tuple(pp or 0 for pp in pp2))
321+
return (ldbletanh_out, tuple(pp or 0 for pp in pp2))

pydomcfg/tests/test_zco.py

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ def test_zco_uniform():
7272
zco = Zco(ds_bathy, jpk=51)
7373

7474
# Compare zco mesh with analytical VS finite difference e3
75-
expected, actual = (
76-
zco(**kwargs, ln_e3_dep=ln_e3_dep) for ln_e3_dep in (True, False)
77-
)
75+
expected = zco(**kwargs, ln_e3_dep=True)
76+
actual = zco(**kwargs, ln_e3_dep=False)
7877
eps = 1.0e-14 # truncation errors
7978
xr.testing.assert_allclose(expected, actual, rtol=eps, atol=0)
8079

@@ -98,34 +97,54 @@ def test_zco_errors():
9897
"""Make sure we raise informative errors"""
9998

10099
# Input parameters
101-
kwargs = dict(ppdzmin=10, pphmax=5.0e3)
100+
kwargs = dict(ppdzmin=10, pphmax=5.0e3, ppkth=1, ppacr=1)
102101

103102
# Generate test data
104103
ds_bathy = Bathymetry(1.0e3, 1.2e3, 1, 1).flat(5.0e3)
105104
zco = Zco(ds_bathy, jpk=10)
106105

107106
# Without ldbletanh flag, only allow all pps set or none of them
108-
with pytest.raises(ValueError, match="ppa2.*MUST be all None or float"):
109-
zco(**kwargs, ppa2=1)
107+
with pytest.raises(
108+
ValueError, match="ppa2, ppkth2 and ppacr2 MUST be all None or float"
109+
):
110+
zco(**kwargs, ppa2=1, ppkth2=1, ppacr2=None)
110111

111112
# When ldbletanh flag is True, all coefficients must be specified
112-
with pytest.raises(ValueError, match="ppa2.*MUST be all float"):
113-
zco(**kwargs, ldbletanh=True, ppa2=None)
113+
with pytest.raises(ValueError, match="ppa2, ppkth2 and ppacr2 MUST be all float"):
114+
zco(**kwargs, ldbletanh=True, ppa2=1, ppkth2=1, ppacr2=None)
114115

115116

116-
@pytest.mark.parametrize("ppname", ("ppa2", "ppsur"))
117-
def test_zco_warnings(ppname):
117+
def test_zco_warnings():
118118
"""Make sure we warn when arguments are ignored"""
119119

120-
# Input parameters
121-
kwargs = dict(ppdzmin=10, pphmax=5.0e3)
122-
123-
# Generate test data
120+
# Initialize test class
124121
ds_bathy = Bathymetry(1.0e3, 1.2e3, 1, 1).flat(5.0e3)
125122
zco = Zco(ds_bathy, jpk=10)
126123

127-
# Warnings (ignored arguments)
128-
expected = zco(**kwargs)
129-
with pytest.warns(UserWarning, match=f"{ppname}.*are ignored"):
130-
actual = zco(**kwargs, ldbletanh=False, **{ppname: 1})
124+
# Uniform: Ignore coefficients controlling stretching
125+
kwargs = dict(ppdzmin=10, pphmax=5.0e3, ppkth=0, ppacr=0)
126+
expected = zco(**kwargs, ppsur=None, ppa0=None, ppa1=None)
127+
with pytest.warns(
128+
UserWarning, match="ppsur, ppa0 and ppa1 are ignored when ppacr == ppkth == 0"
129+
):
130+
actual = zco(**kwargs, ppsur=2, ppa0=2, ppa1=2)
131+
xr.testing.assert_identical(expected, actual)
132+
133+
# ldbletanh = False : Ignore parameters controlling double tanh
134+
kwargs = dict(ppdzmin=10, pphmax=5.0e3, ldbletanh=False)
135+
expected = zco(**kwargs, ppa2=None, ppkth2=None, ppacr2=None)
136+
with pytest.warns(
137+
UserWarning, match="ppa2, ppkth2 and ppacr2 are ignored when ldbletanh is False"
138+
):
139+
actual = zco(**kwargs, ppa2=2, ppkth2=2, ppacr2=2)
140+
xr.testing.assert_identical(expected, actual)
141+
142+
# Uniform case: Ignore double tanh
143+
kwargs = dict(ppdzmin=10, pphmax=5.0e3, ppkth=0, ppacr=0)
144+
expected = zco(**kwargs, ppa2=None, ppkth2=None, ppacr2=None)
145+
with pytest.warns(
146+
UserWarning,
147+
match="ppa2, ppkth2 and ppacr2 are ignored when ppacr == ppkth == 0",
148+
):
149+
actual = zco(**kwargs, ppa2=2, ppkth2=2, ppacr2=2)
131150
xr.testing.assert_identical(expected, actual)

0 commit comments

Comments
 (0)