[SOLVED] Conditional value of function

Asked by Michael Bruckner

Hello everybody,

I am a new user and I can't figure out how to define a function value conditionally.

Here is a minimal snippet illustrating the problem :

from dolfin import *

mesh = UnitSquare(32, 32)
Q = FunctionSpace(mesh, "DG", 1)
phi_exp=Expression("x[0]")
phi = project(phi_exp,Q)

step = 1. if phi > 0.5 else 1e-10
stepp = project(step,Q)
plot(stepp)
interactive()

The goal is to obtain a function where the value is 1 if phi >0.5, 0 elsewhere.
The code above gives 1 everywhere.

Can somebody tell me what is wrong here and how could I achieve a test on the function values ?

Thank you in advance,
Michael

Question information

Language:
English Edit question
Status:
Solved
For:
FEniCS Project Edit question
Assignee:
No assignee Edit question
Solved by:
Martin Sandve Alnæs
Solved:
Last query:
Last reply:
Revision history for this message
Best Martin Sandve Alnæs (martinal) said :
#1

Check out "conditional", its explained in the UFL chapter in the book.

Martin
Den 29. okt. 2012 19:31 skrev "Michael Bruckner" <
<email address hidden>> følgende:

> New question #212669 on FEniCS Project:
> https://answers.launchpad.net/fenics/+question/212669
>
> Hello everybody,
>
> I am a new user and I can't figure out how to define a function value
> conditionally.
>
> Here is a minimal snippet illustrating the problem :
>
> from dolfin import *
>
> mesh = UnitSquare(32, 32)
> Q = FunctionSpace(mesh, "DG", 1)
> phi_exp=Expression("x[0]")
> phi = project(phi_exp,Q)
>
> step = 1. if phi > 0.5 else 1e-10
> stepp = project(step,Q)
> plot(stepp)
> interactive()
>
> The goal is to obtain a function where the value is 1 if phi >0.5, 0
> elsewhere.
> The code above gives 1 everywhere.
>
> Can somebody tell me what is wrong here and how could I achieve a test on
> the function values ?
>
> Thank you in advance,
> Michael
>
> --
> You received this question notification because you are a member of
> FEniCS Team, which is an answer contact for FEniCS Project.
>

Revision history for this message
Michael Bruckner (bruckner-m) said :
#2

Thanks Martin Sandve Alnæs, that solved my question.

Revision history for this message
Michael Bruckner (bruckner-m) said :
#3

Thanks Martin for the quick answer.

One further thought : is there a way to define a function which would be able to handle some more intricate conditions ?
Writing becomes quite unhandy after a few combined ones.
Thanks,
Michael

Revision history for this message
Henrik Garde (1etr3k) said :
#4

Hi Michael, I have had luck using something like:

r = 1
C = 2
class fun(Expression):
    def eval(self, values, x):
        if numpy.sqrt((x[0]-cx)**2+(x[1]-cy)**2) < r:
            values[0] = 1+C
        else:
            values[0] = 1

Then fun() is an expression which equals 3 on the unit disk and 1 outside. Using interpolate(fun(),V), with V being a FunctionSpace you get the corresponding Function.

As you can see, you can simply define the fun() as you wish using the coordinates x[0],x[1],x[2] and define the values[0] as the output.

Revision history for this message
Michael Bruckner (bruckner-m) said :
#5

Hi Henrik ,
Thank you for answering.
I was actually looking for defining the values of a Function dependently from the values of another one, and not just from the coordinates. I don't know if there have been some significant advances lately but UFL manual from 2010 states :

TODO: This is rather limited, probably need the operations ”and” and ”or”
as well, the syntax will be rather convoluted... Can we improve? Low priority
though.
[Advanced] Because of details in the way Python behaves, we cannot over-
load the builtin comparison operators for this purpose, hence these named
operators.

Revision history for this message
Martin Sandve Alnæs (martinal) said :
#6

Check out the UFL chapter in the FEniCS book instead. It's available online
through fenicsproject.org somewhere.

On 31 October 2012 16:35, Michael Bruckner <
<email address hidden>> wrote:

> Question #212669 on FEniCS Project changed:
> https://answers.launchpad.net/fenics/+question/212669
>
> Michael Bruckner posted a new comment:
> Hi Henrik ,
> Thank you for answering.
> I was actually looking for defining the values of a Function dependently
> from the values of another one, and not just from the coordinates. I don't
> know if there have been some significant advances lately but UFL manual
> from 2010 states :
>
> TODO: This is rather limited, probably need the operations ”and” and ”or”
> as well, the syntax will be rather convoluted... Can we improve? Low
> priority
> though.
> [Advanced] Because of details in the way Python behaves, we cannot over-
> load the builtin comparison operators for this purpose, hence these named
> operators.
>
> --
> You received this question notification because you are a member of
> FEniCS Team, which is an answer contact for FEniCS Project.
>

Revision history for this message
Martin Sandve Alnæs (martinal) said :
#7

Example:
C syntax: (u < 0.5 && u > -0.5) ? 1.0: 0.0
UFL syntax: conditional( And( lt(u, 0.5), gt(u, -0.5) ), 1.0, 0.0 )

On 31 October 2012 16:56, Martin Sandve Alnæs <email address hidden> wrote:

> Check out the UFL chapter in the FEniCS book instead. It's available
> online through fenicsproject.org somewhere.
>
>
> On 31 October 2012 16:35, Michael Bruckner <
> <email address hidden>> wrote:
>
>> Question #212669 on FEniCS Project changed:
>> https://answers.launchpad.net/fenics/+question/212669
>>
>> Michael Bruckner posted a new comment:
>> Hi Henrik ,
>> Thank you for answering.
>> I was actually looking for defining the values of a Function dependently
>> from the values of another one, and not just from the coordinates. I don't
>> know if there have been some significant advances lately but UFL manual
>> from 2010 states :
>>
>> TODO: This is rather limited, probably need the operations ”and” and ”or”
>> as well, the syntax will be rather convoluted... Can we improve? Low
>> priority
>> though.
>> [Advanced] Because of details in the way Python behaves, we cannot over-
>> load the builtin comparison operators for this purpose, hence these named
>> operators.
>>
>> --
>> You received this question notification because you are a member of
>> FEniCS Team, which is an answer contact for FEniCS Project.
>>
>
>

Revision history for this message
Michael Bruckner (bruckner-m) said :
#8

Thanks Martin, this is how I got it working.