Thank you David and Colin for the help.

I tried David's suggestion and that seemed to work well.

For this particular example when the function space is CG1, the functions looks perfect.  However, I've noticed, when I pick CG2 or CG3, the function actually dips below 0 at the origin.  I presume that if I choose a continuous function, this would not occur, but this happens because the function is discontinuous?

Cheers, Francis


------------------
Francis Poulin                    
Associate Professor
Department of Applied Mathematics
University of Waterloo

email:           fpoulin@uwaterloo.ca
Web:            https://uwaterloo.ca/poulin-research-group/
Telephone:  +1 519 888 4567 x32637


From: firedrake-bounces@imperial.ac.uk [firedrake-bounces@imperial.ac.uk] on behalf of David Ham [David.Ham@imperial.ac.uk]
Sent: Monday, February 27, 2017 6:49 AM
To: firedrake
Subject: Re: [firedrake] defining an expression piecewise

Hi Francis,

There are two things here. The first is the proximate cause of your exception. If f is a FunctionSpace then it doesn't have an interpolate method. Instead, you would call the interpolate function:

c = interpolate(MyExpression(), f).

However, creating a subclass of Expression is not the best way to solve this problem. Instead you should interpolate a UFL expression:

x = SpatialCoordinate(f.mesh())

c = interpolate(conditional(le(x[0], 0.5), 1.0, 0.0), f)


Regards,

David

On Sun, 26 Feb 2017 at 22:06 Francis Poulin <fpoulin@uwaterloo.ca> wrote:
Hello,

I am trying to define an expression piecewise and see that I need to do that by defining a class.    Below is something that I tried, building on what I read, but it has an error, see below.

class MyExpression(Expression):
    def eval(self, value, x):
        if x[0] <= 0.5:
            value[0] = 1.0
        else:
          value[0] = 0.0

    def value_shape(self):
        return (1,)

f.interpolate(MyExpression())

It fails with the following error:

AttributeError: 'FunctionSpace' object has no attribute 'interpolate'

Can someone maybe point me in the right direction on how to do tis properly?

Cheers, Francis

------------------
Francis Poulin                    
Associate Professor
Department of Applied Mathematics
University of Waterloo

email:           fpoulin@uwaterloo.ca
Web:            https://uwaterloo.ca/poulin-research-group/
Telephone:  +1 519 888 4567 x32637

--
Dr David Ham
Department of Mathematics
Imperial College London