### Conditional with pointwise condition

120
views
0
3 months ago by
How can I define a function like this in FEniCS for a given function  $f\in L^2\left(\Omega\right)$ƒ L2(Ω)  and  $g\in L^{\infty}\left(\Omega\right)$gL(Ω) :

\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
if your function $f(x)$ represents the unknown of your pde like you describe

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 via fixed-point iteration and update "point-wise" coefficients each iteration depending on the current value of the "fenics::Function" $f(x)$ which satisfies your linear pde 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

written 3 months ago by pf4d

4
3 months ago by
Hi there,

You could try
import numpy as np
class v_Expression(Expression):
def eval(self, value, x):
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

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!!