This demo illustrates how to:

* Solve a nonlinear partial differential equation (in this case a nonlinear variant of Poisson's equation)
* Create and apply Dirichlet boundary conditions
* Define an :py:class:`Expression <dolfin.cpp.function.Expression>`
* Define a :py:class:`FunctionSpace <dolfin.cpp.function.FunctionSpace>`
* Create a :py:class:`SubDomain <dolfin.cpp.mesh.SubDomain>`

The solution for :math:`u` in this demo will look as follows:

.. image:: ../plot_u.png
    :scale: 75 %

and the gradient of :math:`u` will look like this:

.. image:: ../plot_u_gradient.png
    :scale: 75 %

Equation and problem definition
-------------------------------

For a domain :math:`\Omega \subset \mathbb{R}^N` with boundary
:math:`\partial \Omega = \Gamma_{D} \cup \Gamma_{N}`, we consider the
following nonlinear Poisson equation with particular boundary
conditions reads:

.. math::

    - \nabla\cdot((1 + u^2) \nabla u) &= f \quad {\rm in}\, \Omega,\\
    u &= 1  \quad  {\rm on}\, \Gamma_D,\\
    \nabla u\cdot n &= 0 \quad  {\rm on}\, \Gamma_N.

Here :math:`f` is input data and :math:`n` denotes the outward
directed boundary normal. The nonlinear variational form can be
written in the following canonical form: find :math:`u \in V` such that

.. math::

	F(u;v)=0\quad \forall\,v\in\hat{V}

Here :math:`F:V\times\hat{V}\rightarrow\mathbb{R}` is a semilinear
form, linear in the argument subsequent to the semicolon, and
:math:`V` is some suitable function space. The semilinear form is
defined as follows:

.. math::

	 F(u;v) = \int_\Omega (1 + u^2)\cdot\nabla(u)\cdot \nabla(v) - f v \,{\rm dx} = 0.

To solve the nonlinear system :math:`b(U) = 0` by Newton's method we
compute the Jacobian :math:`A = b'`, where :math:`U` is the
coefficients of the linear combination in the finite element solution
:math:`u_h = \sum_{j=1}^{N}U_j\phi_j, \;
b:\mathbb{R}^N\rightarrow\mathbb{R}^N` and

.. math::

	b_i(U) = F(u_h;\hat{\phi}_i),\quad i = 1,2,\dotsc,N.

Linearizing the semilinear form :math:`F` around :math:`u = u_h`, we obtain

.. math::

	F'(u_h;\delta u,v) = \int_\Omega [(\delta u\nabla u_h)\cdot\nabla v + ((1+u_h)\nabla\delta u)\nabla v] \,{\rm dx}

We note that for each fixed :math:`u_h`, :math:`a = F'(u_h;\,\cdot\,,\,\cdot\,)` is a bilinear form and :math:`L = F(u_h;\,\cdot\,,\,\cdot\,)` is a linear form. In each Newton iteration, we thus solve a linear variational problem of the canonical form:
find :math:`\delta u \in V_{h,0}` such that

.. math::


	F'(u_h;\delta u,v) = F(u_h;v)\quad\forall\,v\in\hat{V}_h.


In this demo, we shall consider the following definitions of the input function, the domain, and the boundaries:

* :math:`\Omega = [0,1] \times [0,1]\,\,\,` (a unit square)
* :math:`\Gamma_{D} = \{(1, y) \subset \partial \Omega\}\,\,\,` (Dirichlet boundary)
* :math:`\Gamma_{N} = \{(x, 0) \cup (x, 1) \cup (0, y) \subset \partial \Omega\}\,\,\,` (Neumann boundary)
* :math:`f(x, y) = x\sin(y)\,\,\,` (source term)



