3
$\begingroup$

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?

$\endgroup$
  • 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$ – Nico Schlömer May 22 '13 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$ – Christian Clason May 22 '13 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$ – Christian Clason May 22 '13 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$ – Jan Blechta May 22 '13 at 15:35
5
$\begingroup$

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)
solve(F==0,u,bc)
plot(u,interactive=True)

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.

$\endgroup$
  • 1
    $\begingroup$ You forgot minus from integration by parts. $\endgroup$ – Jan Blechta May 22 '13 at 15:40
  • $\begingroup$ @Jan - right, fixed now! $\endgroup$ – Christian Clason May 22 '13 at 15:55
  • $\begingroup$ Thanks for the quick response. I can't understand the lambda x as a boundary condition. $\endgroup$ – jessie May 23 '13 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$ – Christian Clason May 23 '13 at 16:44
  • $\begingroup$ @jessie: lambda is python keyword - see docs.python.org/2/reference/expressions.html#lambda for description. $\endgroup$ – Jan Blechta May 23 '13 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.