From 68f1c26bcf3ee330ff609c178234607043a0bb99 Mon Sep 17 00:00:00 2001 From: nbouziani Date: Tue, 26 Mar 2024 11:58:13 +0000 Subject: [PATCH] Fix BaseForm.__add__ simplification of Zero --- test/test_duals.py | 8 ++++++++ ufl/form.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/test/test_duals.py b/test/test_duals.py index 53e230f3f..c0e81830f 100644 --- a/test/test_duals.py +++ b/test/test_duals.py @@ -101,6 +101,9 @@ def test_addition(): V = FunctionSpace(domain_2d, f_2d) V_dual = V.dual() + fvector_2d = FiniteElement("Lagrange", triangle, 1, (2, ), identity_pullback, H1) + W = FunctionSpace(domain_2d, fvector_2d) + u = TrialFunction(V) v = TestFunction(V) @@ -132,6 +135,11 @@ def test_addition(): res -= ZeroBaseForm((v,)) assert res == L + # Simplification with respect to ufl.Zero + a_W = Matrix(W, W) + res = a_W + Zero(W.ufl_element().value_shape) + assert res == a_W + def test_scalar_mult(): domain_2d = Mesh(FiniteElement("Lagrange", triangle, 1, (2, ), identity_pullback, H1)) diff --git a/ufl/form.py b/ufl/form.py index 3f85b709d..d9504e4a6 100644 --- a/ufl/form.py +++ b/ufl/form.py @@ -137,7 +137,7 @@ def __add__(self, other): if isinstance(other, (int, float)) and other == 0: # Allow adding 0 or 0.0 as a no-op, needed for sum([a,b]) return self - elif isinstance(other, Zero) and not (other.ufl_shape or other.ufl_free_indices): + elif isinstance(other, Zero): # Allow adding ufl Zero as a no-op, needed for sum([a,b]) return self