"""This demo program solves Poisson's equation - div grad u(x, y) = f(x, y) on the unit square with source f given by f(x, y) = 10*exp(-((x - 0.5)^2 + (y - 0.5)^2) / 0.02) and boundary conditions given by u(x, y) = 0 for x = 0 or x = 1 du/dn(x, y) = sin(5*x) for y = 0 or y = 1 """ from dolfin import * # Create mesh and define function space mesh = UnitInterval(32) V = FunctionSpace(mesh, "CG", 1) # Define boundary condition gD = Constant(0.0) class Kappa(Expression): def eval(self, value, x): if x[0] < DOLFIN_EPS: value[0] = 1.0e30 else: value[0] = 0.0 kappa = Kappa() # Define variational problem v = TestFunction(V) u = TrialFunction(V) f = Expression("1.0") gN = Expression("-1.0") a = inner(grad(v), grad(u))*dx - kappa*u*v*ds L = v*f*dx - kappa*gD*v*ds + v*gN*ds U = Function(V) # Assemble linear system A = assemble(a) b = assemble(L) x = U.vector() # Solve linear system solver = LUSolver() solver.solve(A, x, b) # Save solution in VTK format file = File("poisson.pvd") file << U # Plot solution plot(U, interactive=True)