How to define a UserExpression that depends on the solution?

Hi all,

In How to define a vector function depends on the solution?, it was suggested that one could define an Expression that depends on the solution, u, as, for example:

Expression(("1/(x[0]+u0)","1/(x[1]+u1)"),u0=u.sub(0),u1=u.sub(1))

My question is, how can we do the same with an UserExpression? We can overload

class Example(UserExpression):
    def eval(self, value, x):
        value[0] = something

but I don’t know how to pass a TrialFunction to my expression. Something like

class Example(UserExpression):
    def eval(self, value, x,u):
        value[0] = f(u(x))

doesn’t seem to work. Any ideas?

In the linked example they have

u  = Function(V)  # Displacement from previous iteration

So, the expression from the link does not reference the trial function, but a known function. If your expression uses the trial function I think you need to to express it directly in UFL. I believe expressions must evaluate to scalars, not arguments. Otherwise, you may be able to use iteration to solve your problem.

Thanks for the reply!

I see. In the problem I’m trying to solve, I have a non-linear source function, that is position and TrialFunction dependent, that relies on some scipy interpolations, which I can’t express directly in UFL. I guess there is no way to do it FENICS then?

If your problem is nonlinear you can either use the built-in nonlinear support in FEniCS (NewtonSolver) which uses iterations, but requires pure UFL (I believe, I have never used it), or you can implement Newton or Picard fixed point iteration yourself. Both using FEniCS, but with a loop that assembles and solves the problem until convergence

Ok, thanks a lot! I’ve tried to implement Picard iterations, which did not work, but I’ll try again.

Hi Anton,

Did you try to find a solution?
I am stuck with a similar issue (link). I have non-linear coefficients that are dependent on the trial functions as well.

may Thanks,
Sid