Trouble Creating Piecewise UFL Expression

Hi everyone,

I’m trying to create a UFL Expression object (in FEniCS 2019.1.0) to represent the following function

\phi(x,y) =\begin{cases} \frac{1}{h^2}\biggl(1 - \frac{|x-x_0|}{h}\biggr)\biggl(1 - \frac{|y-y_0|}{h} \biggr), \quad \max(|x-x_0|,|y-y_0|) \le h \\ 0, \quad \mathrm{else}, \end{cases}

using the code below:

from dolfin import *
import matplotlib.pyplot as plt

mesh = UnitSquareMesh(20, 20)

h=1/20
x0 = 0.5
y0 = 0.5
g = Expression('(abs(x[0] -x0)< h ) & abs(x[1]-y0) < h'\
               +' ? 1/pow(h,2)*(1 - abs(x[0]-x0)/h)*(1-abs(x[1]-y)/h): 0',degree=2,x0 =x0,y0=y0,h=h )

V = FunctionSpace(mesh, 'CG', 1)
g = interpolate(g, V)

plot(g, title='g')
plt.show()

However, when I run this I keep getting the error message

RuntimeError: Unable to compile C++ code with dijitso

Any suggestions?

You have not defined y, I guess you mean y0, i.e.

from dolfin import *
import matplotlib.pyplot as plt

mesh = UnitSquareMesh(20, 20)

h = 1/20
x0 = 0.5
y0 = 0.5

expr = '(abs(x[0] -x0)< h ) & abs(x[1]-y0) < h ? 1/pow(h,2)*(1 - abs(x[0]-x0)/h)*(1-abs(x[1]-y0)/h): 0'
g = Expression(expr, degree=2, x0=x0, y0=y0, h=h)

V = FunctionSpace(mesh, 'CG', 1)
g = interpolate(g, V)

plot(g, title='g')
plt.show()