# How to define a UserExpression that depends on the solution?

**URL:** <https://fenicsproject.discourse.group/t/how-to-define-a-userexpression-that-depends-on-the-solution/347>\
**Category:** variational formulation\
**Created:** [March 10, 2019, 3:22pm UTC](https://fenicsproject.discourse.group/t/how-to-define-a-userexpression-that-depends-on-the-solution/347 "2019-03-10T15:22:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anton](https://avatars.discourse-cdn.com/v4/letter/a/d9b06d/32.png) [@Anton](https://fenicsproject.discourse.group/u/Anton)\
**Post date:** [March 10, 2019, 3:22pm UTC](https://fenicsproject.discourse.group/t/how-to-define-a-userexpression-that-depends-on-the-solution/347/1 "2019-03-10T15:22:52Z")

</div>

Hi all,

In [How to define a vector function depends on the solution?](https://fenicsproject.org/qa/10210/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:

```auto
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

```auto
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

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

```

doesn’t seem to work. Any ideas?

---

<div class="post-metadata">

**Author:** ![tormod](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/tormod/32/27_2.png) [@tormod](https://fenicsproject.discourse.group/u/tormod)\
**Post date:** [March 10, 2019, 7:00pm UTC](https://fenicsproject.discourse.group/t/how-to-define-a-userexpression-that-depends-on-the-solution/347/2 "2019-03-10T19:00:26Z")

</div>

In the linked example they have

```python
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.

---

<div class="post-metadata">

**Author:** ![Anton](https://avatars.discourse-cdn.com/v4/letter/a/d9b06d/32.png) [@Anton](https://fenicsproject.discourse.group/u/Anton)\
**Post date:** [March 10, 2019, 7:39pm UTC](https://fenicsproject.discourse.group/t/how-to-define-a-userexpression-that-depends-on-the-solution/347/3 "2019-03-10T19:39:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tormod](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/tormod/32/27_2.png) [@tormod](https://fenicsproject.discourse.group/u/tormod)\
**Post date:** [March 10, 2019, 7:47pm UTC](https://fenicsproject.discourse.group/t/how-to-define-a-userexpression-that-depends-on-the-solution/347/4 "2019-03-10T19:47:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Anton](https://avatars.discourse-cdn.com/v4/letter/a/d9b06d/32.png) [@Anton](https://fenicsproject.discourse.group/u/Anton)\
**Post date:** [March 10, 2019, 8:13pm UTC](https://fenicsproject.discourse.group/t/how-to-define-a-userexpression-that-depends-on-the-solution/347/5 "2019-03-10T20:13:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![siddhartha\_harsha\_om](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/siddhartha_harsha_om/32/93_2.png) [@siddhartha\_harsha\_om](https://fenicsproject.discourse.group/u/siddhartha_harsha_om)\
**Post date:** [March 21, 2019, 3:28pm UTC](https://fenicsproject.discourse.group/t/how-to-define-a-userexpression-that-depends-on-the-solution/347/6 "2019-03-21T15:28:47Z")

</div>

Hi Anton,

Did you try to find a solution?  
I am stuck with a similar issue ([link](https://fenicsproject.discourse.group/t/non-linear-coeffecients-one-needs-taking-power-of-the-solution-and-another-needs-integration-of-the-first-coeff/406?u=siddhartha_harsha_om)). I have non-linear coefficients that are dependent on the trial functions as well.

may Thanks,  
Sid
