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
513 stars 160 forks source link

BUG: Error with product of single numpy entry times VectorFunction #3184

Open pbrubeck opened 1 year ago

pbrubeck commented 1 year ago

Describe the bug An invalid type conversion error ocurs when we try to rescale a VectorFunction by pre-multiplying by a single entry of a numpy.array.

Steps to Reproduce

import numpy
from firedrake import *

mesh = UnitSquareMesh(1, 1)
V = VectorFunctionSpace(mesh, "CG", 1)
# V = FunctionSpace(mesh, "CG", 1)

b = Function(V)
b.assign(1)

weights = numpy.ones((1,))
# weights = [1]

# this woks with scalars and vectors
b.assign(b * weights[0])
print(b.dat.data, flush=True)

# this woks with scalars and vectors
b.assign(float(weights[0]) * b)
print(b.dat.data, flush=True)

# this works only with scalars
b.assign(weights[0] * b)
print(b.dat.data, flush=True)

Expected behavior I would expect no error from all three cases.

Error message

Traceback (most recent call last):
  File "/home/pbrubeck/git/dphil_thesis/static_condensation/snippets/bug_function_assign.py", line 23, in <module>
    b.assign(weights[0] * b)
  File "petsc4py/PETSc/Log.pyx", line 115, in petsc4py.PETSc.Log.EventDecorator.decorator.wrapped_func
  File "petsc4py/PETSc/Log.pyx", line 116, in petsc4py.PETSc.Log.EventDecorator.decorator.wrapped_func
  File "/home/pbrubeck/firedrake-dev-20221228-mpich/src/firedrake/firedrake/adjoint_utils/function.py", line 118, in wrapper
    ret = assign(self, other, *args, **kwargs)
  File "/home/pbrubeck/firedrake-dev-20221228-mpich/src/firedrake/firedrake/function.py", line 465, in assign
    Assigner(self, expr, subset).assign()
  File "/home/pbrubeck/firedrake-dev-20221228-mpich/src/firedrake/firedrake/assign.py", line 147, in __init__
    expression = as_ufl(expression)
  File "/home/pbrubeck/firedrake-dev-20221228-mpich/src/ufl/ufl/constantvalue.py", line 501, in as_ufl
    raise ValueError(
ValueError: Invalid type conversion: [Indexed(Coefficient(WithGeometry(FunctionSpace(<firedrake.mesh.MeshTopology object at 0x149c1e869b40>, VectorElement(FiniteElement('Lagrange', triangle, 1), dim=2), name=None), Mesh(VectorElement(FiniteElement('Lagrange', triangle, 1), dim=2), 1)), 2), MultiIndex((FixedIndex(0),)))
 Indexed(Coefficient(WithGeometry(FunctionSpace(<firedrake.mesh.MeshTopology object at 0x149c1e869b40>, VectorElement(FiniteElement('Lagrange', triangle, 1), dim=2), name=None), Mesh(VectorElement(FiniteElement('Lagrange', triangle, 1), dim=2), 1)), 2), MultiIndex((FixedIndex(1),)))] can not be converted to any UFL type.
connorjward commented 1 year ago

I think this is really a UFL bug. as_ufl doesn't know what to do with this number type.

dham commented 1 year ago

I agree. See: https://github.com/FEniCS/ufl/issues/228