diff --git a/docs/changes.md b/docs/changes.md index 8acfc5a..e214ef8 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -7,6 +7,16 @@ rediscover why a thing is the way it is. Open gaps are in ## 2026-11 +- **2026-11-06: `hypot(a, b)` in the notation.** C5 step 2 (proteus runs a + split discretization from the spec and compares it bitwise with the deck): + the dam break's initial signed distance written `sqrt(max(x − 1.2, 0)² + + max(y − 0.6, 0)²) + …` rounds twice, and differs from + `damBreakLevelSet.py`'s `hypot` by one ulp at 2 of the 109 nodes of its + stored mesh, so no run from the spec could match the deck from t = 0. + `hypot` is sympy's `codegen.cfunctions.hypot` (differentiable, printed as + `numpy.hypot`); `hypot(a, 0)` is `|a|` exactly, so + `hypot(max(x − 1.2, 0), max(y − 0.6, 0)) + min(max(x − 1.2, y − 0.6), 0)` + is the deck's three branches to the last bit. - **2026-11-05: a mesh records its fingerprint; a discrete domain loads from a spec file.** cekees, 2026-11-05 (C7.1 round 2): store the mesh's fingerprint in the archive so a restart or a gauge can check the mesh diff --git a/docs/problem-specification.md b/docs/problem-specification.md index 13881da..48cec6e 100644 --- a/docs/problem-specification.md +++ b/docs/problem-specification.md @@ -356,6 +356,7 @@ solution_paths: | `x² π³ x^n x**n` | powers | | `2π²κt`, `κ ΔT`, `sin(πx)` | implicit multiplication | | `sin cos tan exp log sqrt sinh cosh tanh abs` | functions, with parentheses | +| `hypot(a, b)` | `sqrt(a² + b²)` rounded once (C99 `hypot`, `numpy.hypot` in code): a distance that matches code computing it with `hypot`, bit for bit | | `heaviside(φ)`, `sign(e)`, `min(a, b, ...)`, `max(a, b, ...)` | sharp functions; a solver needs `heaviside` and `sign` of an unknown regularized, since their derivatives are Dirac deltas | | `norm(v)`, `sym(∇v)` | `sqrt(v·v)`, and the symmetric part `(A + Aᵀ)/2` of a square tensor | | `grad div lap dt dx dy dz dot outer` | the operators spelled out, for ASCII-only text | diff --git a/src/ymf/names.py b/src/ymf/names.py index 24aa60a..0564266 100644 --- a/src/ymf/names.py +++ b/src/ymf/names.py @@ -97,7 +97,7 @@ def tokens(text: str) -> List[Tuple[str, str, int]]: #: the notation's function names (``ymf.symbolic.notation``'s ``_FUNCTIONS`` #: and the parser's own; a test checks the two agree) FUNCTION_NAMES = frozenset([ - "sin", "cos", "tan", "exp", "log", "sqrt", "sinh", "cosh", "tanh", "abs", + "sin", "cos", "tan", "exp", "log", "sqrt", "sinh", "cosh", "tanh", "abs", "hypot", "heaviside", "sign", "min", "max", "div", "grad", "lap", "dt", "dot", "outer", "vec", "norm", "sym", "dx", "dy", "dz", ]) diff --git a/src/ymf/symbolic/language.py b/src/ymf/symbolic/language.py index 7790adc..68b30ca 100644 --- a/src/ymf/symbolic/language.py +++ b/src/ymf/symbolic/language.py @@ -31,6 +31,7 @@ from typing import Dict, Iterable, List, Sequence import sympy +from sympy.codegen.cfunctions import hypot __all__ = [ "D", "Dt", "Space", "SymbolicError", "parse_expression", "parse_equation", @@ -212,7 +213,7 @@ def namespace(self) -> Dict[str, object]: "sin": sympy.sin, "cos": sympy.cos, "tan": sympy.tan, "exp": sympy.exp, "log": sympy.log, "sqrt": sympy.sqrt, "sinh": sympy.sinh, "cosh": sympy.cosh, "tanh": sympy.tanh, - "abs": sympy.Abs, "Abs": sympy.Abs, + "abs": sympy.Abs, "Abs": sympy.Abs, "hypot": hypot, "vec": lambda *xs: sympy.Matrix([sympy.sympify(v) for v in xs]), "t": self.t, } diff --git a/src/ymf/symbolic/notation.py b/src/ymf/symbolic/notation.py index 26bd426..69c3cf3 100644 --- a/src/ymf/symbolic/notation.py +++ b/src/ymf/symbolic/notation.py @@ -50,6 +50,7 @@ from typing import Any, Dict, List, Optional, Tuple import sympy +from sympy.codegen.cfunctions import hypot from ymf.names import _SUPERSCRIPTS, NotationError, tokens @@ -62,6 +63,9 @@ "sin": sympy.sin, "cos": sympy.cos, "tan": sympy.tan, "exp": sympy.exp, "log": sympy.log, "sqrt": sympy.sqrt, "sinh": sympy.sinh, "cosh": sympy.cosh, "tanh": sympy.tanh, "abs": sympy.Abs, + # sqrt(a² + b²) rounded once (C99 hypot), so that a formula can match code + # that computes a distance with it bit for bit + "hypot": hypot, # sharp functions: their derivatives are Dirac deltas, so a solver needs # them regularized (a discretization's business); min and max take any # number of arguments diff --git a/src/ymf/symbolic/printer.py b/src/ymf/symbolic/printer.py index f2844ca..9fa68dc 100644 --- a/src/ymf/symbolic/printer.py +++ b/src/ymf/symbolic/printer.py @@ -39,6 +39,7 @@ from typing import List import sympy +from sympy.codegen.cfunctions import hypot from ymf.symbolic.language import D, Dt from ymf.symbolic.notation import NotationError @@ -52,7 +53,7 @@ sympy.log: "log", sympy.sinh: "sinh", sympy.cosh: "cosh", sympy.tanh: "tanh", sympy.Abs: "abs", sympy.Heaviside: "heaviside", sympy.sign: "sign", } -_VARIADIC = {sympy.Min: "min", sympy.Max: "max"} +_VARIADIC = {sympy.Min: "min", sympy.Max: "max", hypot: "hypot"} # precedence of what a printed form is, for deciding on parentheses _ATOM, _POWER, _PRODUCT, _SUM = 4, 3, 2, 1 diff --git a/tests/fixtures/two_phase/dambreak.ymf b/tests/fixtures/two_phase/dambreak.ymf index 25596d9..8a47e10 100644 --- a/tests/fixtures/two_phase/dambreak.ymf +++ b/tests/fixtures/two_phase/dambreak.ymf @@ -19,7 +19,9 @@ model: - {field: p, type: constant, value: 0.0} - field: φ type: function - formula: "sqrt(max(x − 1.2, 0)² + max(y − 0.6, 0)²) + min(max(x − 1.2, y − 0.6), 0)" + # hypot, as damBreakLevelSet.py computes it (the sqrt of a sum of + # squares rounds twice: one ulp off the deck at some nodes) + formula: "hypot(max(x − 1.2, 0), max(y − 0.6, 0)) + min(max(x − 1.2, y − 0.6), 0)" units: m coefficients: ρw: {value: 998.2} diff --git a/tests/test_notation_functions.py b/tests/test_notation_functions.py index 1169f26..734284f 100644 --- a/tests/test_notation_functions.py +++ b/tests/test_notation_functions.py @@ -1,4 +1,4 @@ -"""heaviside, sign, min, max, norm and sym in the notation. +"""heaviside, sign, min, max, hypot, norm and sym in the notation. The two-phase level-set model writes χ = heaviside(φ); its initial signed distance is a min/max/sqrt formula; its momentum equation uses the strain @@ -29,7 +29,7 @@ def _evaluate(code, **names): @pytest.mark.parametrize("text", [ "heaviside(u)", "sign(u - 1)", "min(x, y)", "max(x, y, 0)", - "min(max(x - 1.2, y - 0.6), 0)", + "min(max(x - 1.2, y - 0.6), 0)", "hypot(x, y - 1)", ]) def test_the_new_functions_round_trip(text): space = _space() @@ -99,3 +99,34 @@ def test_a_sharp_function_of_the_coordinates_is_data(): reaction = adr_form(residuals, space)["equations"][0]["reaction"] assert reaction["depends_on"] == {"u": "linear"} assert "numpy.select" in reaction["r"] + + +def test_hypot_is_the_dam_break_column_bit_for_bit(): + """With hypot, φ0 is damBreakLevelSet.py's phi_init to the last bit, which the sqrt form is not + (it rounds twice: at 2 of the 109 nodes of the deck's stored mesh it differs by one ulp), so a run + from the spec can reproduce the deck bitwise. hypot(a, 0) is |a| exactly, so one formula covers the + three branches.""" + numpy = pytest.importorskip("numpy") + text = "hypot(max(x − 1.2, 0), max(y − 0.6, 0)) + min(max(x − 1.2, y − 0.6), 0)" + value = parse_expression(text, _space()) + code = to_code(value) + assert "numpy.hypot(" in code + assert parse_expression(to_notation(value), _space()) == value + + def phi_init(x, y): # test/TwoPhaseFlow/damBreakLevelSet.py + dx, dy = x - 1.2, y - 0.6 + if dx < 0 and dy < 0: + return max(dx, dy) + if dx > 0 and dy > 0: + return numpy.hypot(dx, dy) + return max(dx, dy) + + points = numpy.random.default_rng(0).uniform([0.0, 0.0], [3.22, 1.8], (20000, 2)) + found = _evaluate(code, x=points[:, 0], y=points[:, 1]) + assert numpy.array_equal(found, [phi_init(x, y) for x, y in points]) + + +def test_hypot_differentiates(): + space = _space() + value = parse_expression("hypot(x, y)", space) + assert sympy.simplify(sympy.diff(value, space.x[0]) - space.x[0] / value) == 0