This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Conditional expression: UFL condition as boolean

+1 vote

Hi all, I would like to define a function (call it Func) of my trial function 'u' in a way that depends on the value of a different function (call it epsilon) of the trial function u.
My (naive?) attempt gives an error (ufl.log.UFLException: UFL conditions cannot be evaluated as bool in a Python context.)

Here is a sketch of my attempt:

class F(Expression):
    def eval(self, value, x):
        if tr(epsilon) >0:
            value[0] = tr(epsilon)**2 
        else:
            value[0] = -tr(epsilon)**2 + tr(epsilon)**2

Vv = VectorFunctionSpace(mesh, "Lagrange", 1)
u = TrialFunction(Vv) 
epsilon = sym(nabla_grad(u))
Func = F()

Can someone tell me how this should actually be done? Do I need the class structure?
Thank you so much in advance!

asked Sep 21, 2015 by npmitchell FEniCS Novice (600 points)

1 Answer

+2 votes
 
Best answer

This functionality is already built in,

F = conditional(lt(0,epsilon),tr(epsilon)**2,-tr(epsilon)**2 + tr(epsilon)**2)

I doubt you will be able to solve the system with the built-in non-linear solver, i.e. you have to use a fixed point method. You could also make the system more smooth by transitioning continuously between the two expressions...

answered Sep 21, 2015 by KristianE FEniCS Expert (12,900 points)
selected Sep 21, 2015 by npmitchell
...