From 65a8a030f22c6da14966c1921c56479c119b641b Mon Sep 17 00:00:00 2001 From: jorgensd Date: Tue, 4 Feb 2025 19:16:57 +0000 Subject: [PATCH 1/2] Add test of knook fix --- test/test_evaluate.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/test/test_evaluate.py b/test/test_evaluate.py index da01d452b..6673a5d26 100755 --- a/test/test_evaluate.py +++ b/test/test_evaluate.py @@ -2,7 +2,8 @@ __date__ = "2009-02-13 -- 2009-02-13" import math - +import numpy as np +from ufl.constantvalue import ConstantValue from ufl import ( Argument, Coefficient, @@ -39,6 +40,22 @@ from ufl.sobolevspace import H1 +class CustomConstant(ConstantValue): + def __init__(self, value): + super().__init__() + self._value = value + + @property + def ufl_shape(self): + return () + + def evaluate(self, x, mapping, component, index_values): + return self._value + + def __repr__(self): + return f"CustomConstant({self._value})" + + def testScalars(): s = as_ufl(123) e = s((5, 7)) @@ -132,6 +149,18 @@ def testAlgebra(): assert e == v +def testConstant(): + _a = np.complex128(1 + 1j) + _b = np.complex128(-3 + 2j) + a = CustomConstant(_a) + b = CustomConstant(_b) + expr = a / b + e = expr(()) + + expected = complex(_a) / complex(_b) + assert e == expected + + def testIndexSum(): cell = triangle domain = Mesh(FiniteElement("Lagrange", cell, 1, (2,), identity_pullback, H1)) From 5e3296bbbc9d26a2b28cda243a45a80654d37dfd Mon Sep 17 00:00:00 2001 From: jorgensd Date: Tue, 4 Feb 2025 19:22:54 +0000 Subject: [PATCH 2/2] Add documentation --- test/test_evaluate.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/test_evaluate.py b/test/test_evaluate.py index 6673a5d26..c808b30ce 100755 --- a/test/test_evaluate.py +++ b/test/test_evaluate.py @@ -2,8 +2,9 @@ __date__ = "2009-02-13 -- 2009-02-13" import math + import numpy as np -from ufl.constantvalue import ConstantValue + from ufl import ( Argument, Coefficient, @@ -34,7 +35,7 @@ tr, triangle, ) -from ufl.constantvalue import as_ufl +from ufl.constantvalue import ConstantValue, as_ufl from ufl.finiteelement import FiniteElement from ufl.pullback import identity_pullback from ufl.sobolevspace import H1 @@ -150,6 +151,9 @@ def testAlgebra(): def testConstant(): + """Test that constant division doesn't discard the complex type in the case the value is + a numpy complex type, not a native python complex type. + """ _a = np.complex128(1 + 1j) _b = np.complex128(-3 + 2j) a = CustomConstant(_a)