# UserExpression depending on solution

**URL:** <https://fenicsproject.discourse.group/t/userexpression-depending-on-solution/5011>\
**Category:** Uncategorized\
**Created:** [January 30, 2021, 7:23pm UTC](https://fenicsproject.discourse.group/t/userexpression-depending-on-solution/5011 "2021-01-30T19:23:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xanda7](https://avatars.discourse-cdn.com/v4/letter/x/94ad74/32.png) [@xanda7](https://fenicsproject.discourse.group/u/xanda7)\
**Post date:** [January 30, 2021, 7:23pm UTC](https://fenicsproject.discourse.group/t/userexpression-depending-on-solution/5011/1 "2021-01-30T19:23:14Z")

</div>

Hi,  
I’m new to Fenics and are trying to implement a poisson-solver where the prescribed function f depends on the solution u. The question is very similar to here [Previous Question](https://fenicsproject.org/qa/10210/how-to-define-a-vector-function-depends-on-the-solution/). However, I want to call another python library within the UserExpression to calculate the returned value. How can I get the current value of the solution u every time eval is called?  
Would be grateful for any help!

```auto
from fenics import *
from fdint import fd1h
import matplotlib.pyplot as plt

class charge(UserExpression):
    def __init__ (self, nu_s, **kwargs):
        super(). __init__ (**kwargs)
        self.nu_s = nu_s

    #Here is the function where the solution u should be used  
    def eval(self, value, x):
        p = self.nu_s * x * fd1h(u)
        value [0] = p

mesh = UnitIntervalMesh(10)
V = FunctionSpace(mesh, 'P', 1)
u_D = Expression('1 + x[0]*x[0]', degree=1)
def boundary(x, on_boundary):
    return on_boundary
bc = DirichletBC(V, u_D, boundary)
u = TrialFunction(V)
v = TestFunction(V)

#User-defined function
f = charge(nu_s=5,degree=0)

a = dot(grad(u), grad(v))*dx
L = f*v*dx

u = Function(V)
solve(a == L, u, bc)

plot(u)
plt.show()

```

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [January 31, 2021, 10:49pm UTC](https://fenicsproject.discourse.group/t/userexpression-depending-on-solution/5011/2 "2021-01-31T22:49:35Z")

</div>

Have you considered using the function(x)/Eval functionality? See for instance [Material property depending on solution - #2 by Maeggis](https://fenicsproject.discourse.group/t/material-property-depending-on-solution/549/2)

---

<div class="post-metadata">

**Author:** ![xanda7](https://avatars.discourse-cdn.com/v4/letter/x/94ad74/32.png) [@xanda7](https://fenicsproject.discourse.group/u/xanda7)\
**Post date:** [February 1, 2021, 8:54pm UTC](https://fenicsproject.discourse.group/t/userexpression-depending-on-solution/5011/3 "2021-02-01T20:54:04Z")

</div>

Thanks for your answer!  
I figured out how I can cast my equation into the form of the nonlinear Poisson equation (similar to Fenics Tutorial Volume 1 Ch. 3.2) . This solved the problem.
