Ufl error when defining variational formulations based on UserExpression

Hi everyone,

I am facing an issue with the update from the version 2017.2 to 2018.1 when using UserExpression in the definition of the variational formulation. Indeed, defining an expression through the following class

class DefineExpression(UserExpression):

def __init__(self, F,element, **kwargs):
	self.F = F
	self._ufl_element = element
	super().__init__(**kwargs)

    def eval(self, value, x):
	value[0] = self.F(x)	

def value_shape(self):
	return (1,)    

and defining the formulation L such that

        V = FunctionSpace(mesh,'P',order)
	u = TrialFunction(V)
	v = TestFunction(V)
	
	a = dot(grad(u), grad(v))*dx
	L = DefineExpression(function,element=V.ufl_element())*v*dx

the following error is triggered.

ufl.log.UFLException: Can only integrate scalar expressions. The integrand is a tensor expression with value shape (1,) and free indices with labels ().

A similar issue has been reported in
https://bitbucket.org/dolfin-adjoint/pyadjoint/issues/36/fenics_adjointtypesconstantconstant-cannot
but i have problem to figure out the workaround.

Would you know if there is a new syntax or a fix/workaround possible?
Thanks,
Bests.

You should be able to either return an empty tuple () from value_shape or just leave that method un-implemented (and get a warning about how the shape will be assumed scalar). See the following example:

from dolfin import *

class E(UserExpression):
    def eval(self, value, x):
        value[0] = 1.0
    def value_shape(self):
        #return (1,) # reproduces error
        return () # works

mesh = UnitSquareMesh(1,1)
print(assemble(E(degree=0)*dx(domain=mesh)))
1 Like

Thanks!

I was afraid to do so as it led at first to the differentiation error, caused in fact by a wrong version of sympy. ( ValueError: Since there are no variables in the expression [0, 0], it cannot be differentiated.)

Fixing both issues solved this problem, thank you.

Hello, what version of Sympy did you use to get it working? I am also having this problem with 2017.2 and Sympy 1.5.1.

EDIT: Looks like using Sympy 1.0 fixes this specific error.