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. 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!
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()