 # How to implement the absolute value function in python for fenics?

Hello everyone,

The program below function well.

If I replace, in the expression of the constraint (last line), the trace of the deformation tr(eps (u) by its absolute value abs(tr(eps (u));

The code does not work anymore.

nu = Constant(0.3)
def eps(u):

def sigma_0(u):
mu = E/(2.0*(1.0 + nu))
lmbda = Enu/(1.0 - nu**2)
return 2.0
mu*(eps(u)) + lmbda*tr(eps(u))*Identity(ndim)

If the surrounding code is attempting to solve a linear problem for the displacement, then I’d expect it to fail, because the absolute value function is not linear.

it’s for a non-linear problem

Or is there another way of doing it by integrating the absolute value for the definition of the constraint?

Is it that the code runs, but just diverges? I would expect adding an absolute value to the bulk term to become unstable when `tr(eps(u))` is negative, since there will be a pressure that just accelerates that deformation instead of resisting it.

The code does not work by integrating the absolute value.

I fully agree on your behind reaction in the sense that I tried to play on the sign of the trace in a loop if (tr (eps (u))> = 0), it does not recognize the condition.

I’m doing a simple pull on a bar.

Thank you

Are you maybe trying to implement a ramp function instead of absolute value? You can use the UFL `conditional` function to implement if/then/else logic, for example:

``````def ramp(x):
return conditional(gt(x,0), x, Constant(0.0))
``````

Okay
I’ll get back to you right away
Thank you

I correct my last message
One moment I correct my last message

he does not recognize the expression.
Indeed, here is what I programmed,

``````def sigma(u):
mu    = E/(2.0*(1.0 + nu))
lmbda = E*nu/(1.0 - nu**2)
stress = conditional(tr(eps(u))>=0,2.0*mu*(eps(u)) + lmbda*tr(eps(u))*Identity(ndim),2.0*mu*(eps(u)) - lmbda*tr(eps(u))*Identity(ndim))
return stress
``````

[/quote]

Excuse me for my multiple mistakes. I am new on the forum.

For the condition in a UFL `Conditional`, you need to specify it using the special `gt` or `lt` functions, (since, for technical reasons, it would not work to overload Python’s comparison operators in this context). However, even with that correction, it looks like what you’ve programmed is equivalent to the use of `abs` and I would still expect it to suffer from instability when `tr(eps(u))` goes negative.

Thank you for trying to help me.

1 Like

this too does not work. that’s what you suggested.

``````def sigma(u):
mu    = E/(2.0*(1.0 + nu))
lmbda = E*nu/(1.0 - nu**2)
stress = conditional(ge(tr(eps(u)),0),2.0*mu*(eps(u)) + lmbda*tr(eps(u))*Identity(ndim),2.0*mu*(eps(u)) - lmbda*tr(eps(u))*Identity(ndim))
return stress
``````

Hello everyone,
I will bounce on my concern,
maybe someone can help me by sharing my pain.

I am redesigning my problem:

The program below function well.

If I replace, in the expression of the constraint (last line), the trace of the deformation tr(eps (u) by its absolute value abs(tr(eps (u));

The code does not work anymore.

``````nu = Constant(0.3)
def eps(u):