### Conditional with pointwise condition

180

views

0

How can I define a function like this in FEniCS for a given function $f\in L^2\left(\Omega\right)$

\begin{align*}

v(x) = \begin{cases}

f(x), \quad \text{if } |f(x)|\leq g(x) \\

\frac{f(x)}{|f(x)|}g(x),\quad \text{if } |f(x)| > g(x)

\end{cases}

\end{align*}

I don't really think, that you can implement this using "conditional", because of this "pointwise" condition. Btw: I'm using Nedelec's edge elements. Thanks for your help

`ƒ`∈`L`^{2}(Ω) and $g\in L^{\infty}\left(\Omega\right)$`g`∈`L`^{∞}(Ω) :\begin{align*}

v(x) = \begin{cases}

f(x), \quad \text{if } |f(x)|\leq g(x) \\

\frac{f(x)}{|f(x)|}g(x),\quad \text{if } |f(x)| > g(x)

\end{cases}

\end{align*}

I don't really think, that you can implement this using "conditional", because of this "pointwise" condition. Btw: I'm using Nedelec's edge elements. Thanks for your help

Community: FEniCS Project

### 1 Answer

4

Hi there,

You could try

You could try

```
import numpy as np
class v_Expression(Expression):
def eval(self, value, x):
#your functions here
f = x[0]**2 + 2*x[0] + 1
g = -x[0]**3 + x[0] -2
if np.abs(f)<= g:
value[0] = f
else:
value[0] = (f/np.abs(f))*g
#to use the personalized function
v = v_Expression(degree = 3)
```

Good luck!!

1

This is certainly a valid way to do this for non-time dependent problems, but i believe he wants \(f(x)\) to be a "fenics::TrialFunction" as per the previous post

https://www.allanswered.com/post/eqxnv/#qvnao

here.

https://www.allanswered.com/post/eqxnv/#qvnao

here.

written
6 months ago by
pf4d

Thanks you both for your answers. Ruben answered my question! Regarding my last question, I made a mistake during the calculations and the function $f\left(x\right)$

`ƒ`(`x`) only depends on known functions. So it is basically just an other "data"-function and the pde stays linear. But your comment answers even my intended question. Thanks!!
written
6 months ago by
FFoDWindow

Please login to add an answer/comment or follow this question.

https://www.allanswered.com/post/eqxnv/#qvnao

you have introduced a non linearity into your pde and as such must solve using a nonlinear method. There are two ways to accomplish this: make \(f(x)\) a "fenics::Function", use a "fenics::conditional" specified with \(f(x)\) just like the cases you have mathed out above and solve via

Newton's method; or, solve viafixed-pointiterationand update "point-wise" coefficients each iteration depending on the current value of the "fenics::Function" \(f(x)\) which satisfies yourlinearpde as Ruben correctly suggested below. read, thanks to nate:https://fenicsproject.org/qa/10763/how-can-create-an-expression-is-dependent-on-function-value

Thanks, Nate!

also here:

https://fenicsproject.org/pub/tutorial/html/._ftut1007.html#ftut1:gallery:nonlinearpoisson

for your convenience.