[firedrake] defining an expression piecewise

David Ham David.Ham at imperial.ac.uk
Mon Feb 27 11:49:50 GMT 2017


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 at 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 at uwaterloo.ca
> Web:            https://uwaterloo.ca/poulin-research-group/
> Telephone:  +1 519 888 4567 x32637 <(519)%20888-4567>
>
> --
Dr David Ham
Department of Mathematics
Imperial College London
-------------- next part --------------
HTML attachment scrubbed and removed


More information about the firedrake mailing list