Hello everyone!
I am curious how to solve the following problem using Fenics
\dfrac{d^2 u}{d x^2} - u = 0, \quad x \in [0,1],
with the following BCs
\int_0^1 u\; dx = 1, \quad \dfrac{du}{dx}=u |_{x=1}
It’s obvious that it has a solution u_{ex} = \dfrac{1}{e-1}exp(x), but i’am struggling with variational formulation for this.
I’ve seen approach for pure Neumann conditions with Lagrange multipliers to include integral condition.
However, here it’s slightly different, due to the fact that we have Robin type BC only on one part of the boundary.
I came up with smt like this, but it gives wrong results
u v |_{x=1} - \dfrac{du}{dx} v |_{x=0} - \dfrac{du}{dx} \dfrac{dv}{dx} dx - u v \; dx + \lambda v \; dx + test(\lambda)(u-1) \; dx = 0,
where \lambda is a Lagrange multiplier.
Here is the code
from dolfin import *
import math as mt
# definig mesh
R_out = 1
R_in = 0
h = R_out-R_in
mesh = IntervalMesh(20, R_in, R_out)
tol = 1E-14
class Left(SubDomain):
def inside(self, x, on_boundary):
return near(x[0], R_in)
class Right(SubDomain):
def inside(self, x, on_boundary):
return near(x[0], R_out)
r_ = Expression('x[0]',degree=2)
left = Left()
right = Right()
boundaries = MeshFunction("size_t", mesh, mesh.topology().dim()-1, 0)
boundaries.set_all(0)
left.mark(boundaries, 1)
right.mark(boundaries, 2)
ds = Measure("ds", subdomain_data=boundaries)
P2 = FiniteElement("Lagrange", interval, 1)
R = FiniteElement("R", interval, 0)
element = MixedElement([P2, R])
V0 = FunctionSpace(mesh, element)
U_r = TrialFunction(V0)
V_r = TestFunction(V0)
trial_Ur, trial_lam = split(U_r)
test_Vr, test_lam = split(V_r)
# the bilinear part
a = ( -trial_Ur.dx(0)*test_Vr.dx(0) - trial_Ur*test_Vr )*dx
a += ( test_lam*(trial_Ur) + trial_lam*test_Vr)*dx
a += - trial_Ur.dx(0)*test_Vr*ds(1) + trial_Ur*test_Vr*ds(2)
# the linear part
L= (test_lam/h)*dx
u = Function(V0)
# solve the system
solve(a == L, u)