[fdmath] widen curl calculations and sparse reciprocals before arithmetic

This commit is contained in:
Forgejo Actions 2026-10-28 18:00:34 -07:00
commit b9aa06355b
4 changed files with 48 additions and 6 deletions

View file

@ -91,6 +91,22 @@ def test_vec_cross_matches_pointwise_cross_product() -> None:
assert_close(matrix_result, expected)
@pytest.mark.parametrize('name', ['deriv_forward', 'deriv_back'])
@pytest.mark.parametrize('complex_widths', [False, True])
def test_sparse_derivatives_widen_reciprocals_before_overflow(name, complex_widths) -> None:
dtype = numpy.complex64 if complex_widths else numpy.float32
width = numpy.array(1e-39 * (1 + 1j) if complex_widths else 1e-39, dtype=dtype)
widths = [numpy.full(2, width, dtype=dtype), numpy.ones(1), numpy.ones(1)]
wide_widths = [dx.astype(numpy.complex128 if complex_widths else numpy.float64) for dx in widths]
field = numpy.array([0, 1e-39])
with numpy.errstate(over='raise', invalid='raise'):
matrix = getattr(operators, name)(widths)[0]
result = matrix @ field
assert numpy.isfinite(matrix.data).all()
expected = getattr(operators, name)(wide_widths)[0] @ field
assert_close(result, expected, atol=1e-12, rtol=1e-12)
def test_avg_forward_matches_half_sum_with_forward_neighbor() -> None:
matrix_result = _apply_scalar_matrix(operators.avg_forward(1, SHAPE))
expected = 0.5 * (SCALAR_FIELD + numpy.roll(SCALAR_FIELD, -1, axis=1))