Is there a code for the equation $$ \frac{\partial^2u}{\partial x^2}+\frac{\partial^2u}{\partial y^2} = \sin(u) $$ or for the sine gordon equation in two dimensions because I want to change some boundary values to see the results?

  • 3
    $\begingroup$ Hi @jessie, welcome to scicomp! The $\theta$-notation is a little weird, but other than that this looks very much like the Poisson equation -- the basic starting example for many numerical methods. Check out fenicsproject.org/documentation/dolfin/1.2.0/python/demo/pde/…. $\endgroup$ May 22, 2013 at 11:59
  • $\begingroup$ I've taken the liberty to change the $\theta$ to $\partial$ according to standard notation, because I assumed that was just to avoid MathJax. Feel free to change back if that was incorrect. $\endgroup$ May 22, 2013 at 14:11
  • 1
    $\begingroup$ Actually, it's not the Poisson equation since it's not linear in $u$ (it appears in the $\sin$ on the right-hand side). $\endgroup$ May 22, 2013 at 14:12
  • 1
    $\begingroup$ @ChristianClason: Yes, but corresponding fixed point iteration $\Delta u_{n+1} = \sin(u_n)$ is Poisson equation - I don't say whether it converges or not. Similiarly applying Newton method would result in reaction-diffusion equation. $\endgroup$ May 22, 2013 at 15:35

1 Answer 1


That's actually very easy to do in Dolfin:

from dolfin import *
# define mesh, function space (piecewise linear)
mesh = UnitSquareMesh(64,64)
V = FunctionSpace(mesh,'CG',1)
# inhomogeneous boundary conditions (otherwise the solution is trivial)
bc = DirichletBC(V, Constant(1.0), lambda x,on_boundary: on_boundary)
# define bi(non)linear form
# note that nonlinear problems need u to be a Function, not TestFunction
u = Function(V)
v = TestFunction(V)
F = inner(grad(u),grad(v))*dx + sin(u)*v*dx
# solve using Newton method (Jacobian is computed automatically)

There is also a nonlinear-poisson demo in the demo/undocumented directory, which also shows how to enter define more complicated nonlinear forms and thow to control the solver parameters.

  • 1
    $\begingroup$ You forgot minus from integration by parts. $\endgroup$ May 22, 2013 at 15:40
  • $\begingroup$ @Jan - right, fixed now! $\endgroup$ May 22, 2013 at 15:55
  • $\begingroup$ Thanks for the quick response. I can't understand the lambda x as a boundary condition. $\endgroup$
    – jessie
    May 23, 2013 at 15:47
  • $\begingroup$ That just defines an anonymous function that returns True if the point x lies on the boundary; such a function (or a class implementing this function) is how FEniCS expects the part of the boundary where the condition should be imposed to be specified. $\endgroup$ May 23, 2013 at 16:44
  • $\begingroup$ @jessie: lambda is python keyword - see docs.python.org/2/reference/expressions.html#lambda for description. $\endgroup$ May 23, 2013 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.