"""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 = UnitSquare(32, 32) V = FunctionSpace(mesh, "CG", 1) # Define Dirichlet boundary (x = 0 or x = 1) def boundary(x): return x[0] < DOLFIN_EPS or x[0] > 1.0 - DOLFIN_EPS # Define boundary condition u0 = Constant(0.0) bc = DirichletBC(V, u0, boundary) # Define variational problem v = TestFunction(V) u = TrialFunction(V) f = Expression("10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)") g = Expression("sin(5*x[0])") a = inner(grad(v), grad(u))*dx L = v*f*dx - v*g*ds U = Function(V) # Assemble linear system A = assemble(a) b = assemble(L) x = U.vector() # Apply boundary conditions bc.apply(A, b) # Solve linear system solver = KrylovSolver() solver.solve(A, x, b) # Save solution in VTK format file = File("poisson.pvd") file << U # Plot solution plot(U, interactive=True)