Skip to content

Commit d66fca3

Browse files
authored
Merge pull request #59 from FEniCS/test_knook_simplification_ufl
Add native ufl test for constant complex division
2 parents f769103 + 5e3296b commit d66fca3

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

test/test_evaluate.py

+34-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import math
55

6+
import numpy as np
7+
68
from ufl import (
79
Argument,
810
Coefficient,
@@ -33,12 +35,28 @@
3335
tr,
3436
triangle,
3537
)
36-
from ufl.constantvalue import as_ufl
38+
from ufl.constantvalue import ConstantValue, as_ufl
3739
from ufl.finiteelement import FiniteElement
3840
from ufl.pullback import identity_pullback
3941
from ufl.sobolevspace import H1
4042

4143

44+
class CustomConstant(ConstantValue):
45+
def __init__(self, value):
46+
super().__init__()
47+
self._value = value
48+
49+
@property
50+
def ufl_shape(self):
51+
return ()
52+
53+
def evaluate(self, x, mapping, component, index_values):
54+
return self._value
55+
56+
def __repr__(self):
57+
return f"CustomConstant({self._value})"
58+
59+
4260
def testScalars():
4361
s = as_ufl(123)
4462
e = s((5, 7))
@@ -132,6 +150,21 @@ def testAlgebra():
132150
assert e == v
133151

134152

153+
def testConstant():
154+
"""Test that constant division doesn't discard the complex type in the case the value is
155+
a numpy complex type, not a native python complex type.
156+
"""
157+
_a = np.complex128(1 + 1j)
158+
_b = np.complex128(-3 + 2j)
159+
a = CustomConstant(_a)
160+
b = CustomConstant(_b)
161+
expr = a / b
162+
e = expr(())
163+
164+
expected = complex(_a) / complex(_b)
165+
assert e == expected
166+
167+
135168
def testIndexSum():
136169
cell = triangle
137170
domain = Mesh(FiniteElement("Lagrange", cell, 1, (2,), identity_pullback, H1))

0 commit comments

Comments
 (0)