k0 = 20 # approximation space polynomial degree deg = 1 # number of elements in each direction of mesh n_elem = 128 mesh = UnitSquareMesh(MPI.comm_world, n_elem, n_elem) n = FacetNormal(mesh) # Incident plane wave theta = np.pi / 8 ui = Expression('exp(j*k0*(cos(theta)*x[0] + sin(theta)*x[1]))', k0=k0, theta=theta, degree=deg + 3, domain=mesh.ufl_domain()) # Test and trial function space V = FunctionSpace(mesh, "Lagrange", deg) # Define variational problem u = TrialFunction(V) v = TestFunction(V) g = dot(grad(ui), n) + 1j * k0 * ui a = inner(grad(u), grad(v)) * dx - k0**2 * inner(u, v) * dx + \ 1j * k0 * inner(u, v) * ds L = inner(g, v) * ds # Compute solution u = Function(V) solve(a == L, u, [])