Hi, I’m trying to diagnose an issue with my own code using Fenics, but I’m running into the same problem with it. I need to solve the very elementary 1D diffusion equation
on the interval [5, 108] where k and q are piecewise constant functions whose values I have in a vector. The equation is subject to a Dirichlet boundary condition at x = 5 and the natural boundary condition on the left endpoint (i.e. k \frac{dT}{dx} = 0, which is the default if I understand correctly). Depending on whether I divide both sides by k, I get wildly different answers:
from fenics import *
mesh = IntervalMesh(n_elems, vec[0], vec[-1])
V = FunctionSpace(mesh, "P", 3)
def boundary_L(x, on_boundary):
r = on_boundary and np.abs(x - vec[0]) < 1e-4
return r
left_val = Constant(Ta)
bc = DirichletBC(V, left_val, boundary_L)
T = TrialFunction(V)
v = TestFunction(V)
class q_fen(UserExpression):
def eval(self, value, x):
value[0] = q(x[0])
class k_fen(UserExpression):
def eval(self, value, x):
value[0] = k(x[0])
class f(UserExpression):
def eval(self, value, x):
value[0] = q(x[0]) / k(x[0])
q_fen = q_fen(element = V.ufl_element())
k_fen = k_fen(element = V.ufl_element())
f = f(element = V.ufl_element())
a = k_fen*dot(grad(T), grad(v)) * dx
b = dot(grad(T), grad(v)) * dx
L1 = q_fen * v * dx
L2 = f * v * dx
T1 = Function(V)
T2 = Function(V)
solve(a == L1, T1, bc)
solve(b == L2, T2, bc)
I’ve been breaking my head trying to understand why. Any help would be welcome.