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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/problem-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
2 changes: 1 addition & 1 deletion src/ymf/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
])
Expand Down
3 changes: 2 additions & 1 deletion src/ymf/symbolic/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
}
Expand Down
4 changes: 4 additions & 0 deletions src/ymf/symbolic/notation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/ymf/symbolic/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tests/fixtures/two_phase/dambreak.ymf
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
35 changes: 33 additions & 2 deletions tests/test_notation_functions.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Loading