How to interpolate a conditional function?

Hi, all. I have a simple question. I’m trying to interpolate (not project as I want to get exact values) the following u into a UniteSquare FunctionSpace V:
image
and following is my MWE

from dolfin import *

#define Function u
def u(h):
    return conditional(lt(h,0),h**2+1,10)

#define function space V
mesh=UnitSquareMesh.create(1,10,CellType.Type.quadrilateral)
V=FunctionSpace(mesh,'CG',1)

#define h and u
h=Expression('x[0]<0.9?-1:0',degree=0)
u=u(h)

#project u to V
u_proj=project(u,V)
out1=File('u_proj.pvd')
out1 << u_proj

#interpolate u to V
u_intp=interpolate(u,V)
out2=File('u_intp.pvd')
out2 << u_intp

The project Function works well when I use the conditional for projection directly, but the interploate fails with

AttributeError: 'Conditional' object has no attribute '_cpp_object'

So how can I interpolate the exact u into the FunctionSpace? Should I modify the definition of u or do something else? Thanks!

I guess I have figured it out, just by defining u as
u=Expression(h<0?pow(h,2)+1:10,degree=0,h=h)

1 Like