firedrakeproject / firedrake

Firedrake is an automated system for the portable solution of partial differential equations using the finite element method (FEM)
https://firedrakeproject.org
Other
498 stars 157 forks source link

ufl does not like numpy floats #949

Open tkarna opened 7 years ago

tkarna commented 7 years ago

numpy.float64*Function evaluation fails in some cases. Example:

from firedrake import *
import numpy

mesh = UnitSquareMesh(5, 5)

V = VectorFunctionSpace(mesh, 'DG', 1)
# V = FunctionSpace(mesh, 'DG', 1)  # NOTE works

c = numpy.array([0.5, 1.0])

f = Function(V)
one = Function(V).assign(1.0)
two = Function(V).assign(2.0)
f.assign(c[0]*one + c[1]*two)
# f.assign(float(c[0])*one + float(c[1])*two)  # NOTE works
# f.assign(one*c[0] + two*c[1])  # NOTE works

print f.dat.data.min()

dies with

Traceback (most recent call last):
  File "test_float64.py", line 18, in <module>
    f.assign(c[0]*one + c[1]*two)
  File "<decorator-gen-271>", line 2, in assign
  File "/home/tuomas_master/sources/firedrake/src/firedrake/firedrake/utils.py", line 62, in wrapper
    return f(*args, **kwargs)
  File "/home/tuomas_master/sources/firedrake/src/firedrake/firedrake/function.py", line 356, in assign
    assemble_expressions.Assign(self, expr), subset)
  File "/home/tuomas_master/sources/firedrake/src/firedrake/firedrake/assemble_expressions.py", line 107, in __init__
    operands = map(ufl.as_ufl, (lhs, rhs))
  File "/home/tuomas_master/sources/firedrake/src/ufl/ufl/constantvalue.py", line 417, in as_ufl
    " to any UFL type." % str(expression))
  File "/home/tuomas_master/sources/firedrake/local/lib/python2.7/site-packages/numpy/core/numeric.py", line 1869, in array_str
    return array2string(a, max_line_width, precision, suppress_small, ' ', "", str)
  File "/home/tuomas_master/sources/firedrake/local/lib/python2.7/site-packages/numpy/core/arrayprint.py", line 447, in array2string
    separator, prefix, formatter=formatter)
  File "/home/tuomas_master/sources/firedrake/local/lib/python2.7/site-packages/numpy/core/arrayprint.py", line 260, in _array2string
    'int': IntegerFormat(data),
  File "/home/tuomas_master/sources/firedrake/local/lib/python2.7/site-packages/numpy/core/arrayprint.py", line 637, in __init__
    max_str_len = max(len(str(maximum.reduce(data))),
  File "/home/tuomas_master/sources/firedrake/src/ufl/ufl/conditional.py", line 46, in __bool__
    error("UFL conditions cannot be evaluated as bool in a Python context.")
  File "/home/tuomas_master/sources/firedrake/src/ufl/ufl/log.py", line 171, in error
    raise self._exception_type(self._format_raw(*message))
ufl.log.UFLException: UFL conditions cannot be evaluated as bool in a Python context.

So apparently this only occurs with VectorFunctionSpace, and if you do scalar*Function multiplication.

jrmaddison commented 4 years ago

I've seen this appear, e.g.

from firedrake import *
import numpy as np

mesh = UnitIntervalMesh(10)
space = VectorFunctionSpace(mesh, "Lagrange", 1)

F = Function(space, name="F")

v = np.float64(1.0)

G = interpolate(v * F, space)

leads to an error, but in Firedrake

Traceback (most recent call last):
  File "test.py", line 14, in <module>
    G = interpolate(v * F, space)
  File "[...]/build/firedrake/firedrake/src/firedrake/firedrake/interpolation.py", line 37, in interpolate
    return Interpolator(expr, V, subset=subset, access=access).interpolate()
  File "[...]/build/firedrake/firedrake/src/firedrake/firedrake/interpolation.py", line 57, in __init__
    self.callable = make_interpolator(expr, V, subset, access)
  File "[...]/build/firedrake/firedrake/src/firedrake/firedrake/interpolation.py", line 68, in make_interpolator
    assert isinstance(expr, ufl.classes.Expr)
AssertionError

Applying a BC

G = Function(space, name="G")
DirichletBC(space, v * F, "on_boundary").apply(G)

also leads to an error.