| 1 |
|
| 2 |
.. _demo_nonlinear_poisson: |
| 3 |
|
| 4 |
Nonlinear Poisson equation |
| 5 |
========================== |
| 6 |
|
| 7 |
This demo is implemented in a single Python file, |
| 8 |
:download:`demo_nonlinear-poisson.py`, which contains both the |
| 9 |
variational form and the solver. |
| 10 |
|
| 11 |
This demo illustrates how to: |
| 12 |
|
| 13 |
* Solve a nonlinear partial differential equation (in this case a |
| 14 |
nonlinear variant of Poisson's equation) |
| 15 |
* Create and apply Dirichlet boundary conditions |
| 16 |
* Define an :py:class:`Expression <dolfin.cpp.function.Expression>` |
| 17 |
* Define a :py:class:`FunctionSpace <dolfin.cpp.function.FunctionSpace>` |
| 18 |
* Create a :py:class:`SubDomain <dolfin.cpp.mesh.SubDomain>` |
| 19 |
|
| 20 |
The solution for :math:`u` in this demo will look as follows: |
| 21 |
|
| 22 |
.. image:: plot_u.png |
| 23 |
:scale: 75 % |
| 24 |
|
| 25 |
and the gradient of :math:`u` will look like this: |
| 26 |
|
| 27 |
.. image:: plot_u_gradient.png |
| 28 |
:scale: 75 % |
| 29 |
|
| 30 |
Equation and problem definition |
| 31 |
------------------------------- |
| 32 |
|
| 33 |
For a domain :math:`\Omega \subset \mathbb{R}^N` with boundary |
| 34 |
:math:`\partial \Omega = \Gamma_{D} \cup \Gamma_{N}`, we consider the |
| 35 |
following nonlinear Poisson equation with given boundary conditions: |
| 36 |
|
| 37 |
.. math:: |
| 38 |
|
| 39 |
- \nabla\cdot((1 + u^2) \nabla u) &= f \quad {\rm in}\, \Omega,\\ |
| 40 |
u &= 1 \quad {\rm on}\, \Gamma_D,\\ |
| 41 |
\nabla u\cdot n &= 0 \quad {\rm on}\, \Gamma_N. |
| 42 |
|
| 43 |
Here :math:`f` is input data and :math:`n` denotes the outward |
| 44 |
directed boundary normal. The nonlinear variational form can be |
| 45 |
written in the following canonical form: find :math:`u \in V` such |
| 46 |
that |
| 47 |
|
| 48 |
.. math:: |
| 49 |
|
| 50 |
F(u;v) = 0 \quad \forall \, v \in \hat{V} |
| 51 |
|
| 52 |
Here :math:`F:V\times\hat{V}\rightarrow\mathbb{R}` is a semilinear |
| 53 |
form, linear in the argument subsequent to the semicolon, and |
| 54 |
:math:`V` is some suitable function space. The semilinear form is |
| 55 |
defined as follows: |
| 56 |
|
| 57 |
.. math:: |
| 58 |
|
| 59 |
F(u;v) = \int_\Omega (1 + u^2)\cdot\nabla u \cdot \nabla v - f v \,{\rm dx} = 0. |
| 60 |
|
| 61 |
To solve the nonlinear system :math:`b(U) = 0` by Newton's method we |
| 62 |
compute the Jacobian :math:`A = b'`, where :math:`U` is the |
| 63 |
coefficients of the linear combination in the finite element solution |
| 64 |
:math:`u_h = \sum_{j=1}^{N}U_j\phi_j, \; |
| 65 |
b:\mathbb{R}^N\rightarrow\mathbb{R}^N` and |
| 66 |
|
| 67 |
.. math:: |
| 68 |
|
| 69 |
b_i(U) = F(u_h;\hat{\phi}_i),\quad i = 1,2,\dotsc,N. |
| 70 |
|
| 71 |
Linearizing the semilinear form :math:`F` around :math:`u = u_h`, we obtain |
| 72 |
|
| 73 |
.. math:: |
| 74 |
|
| 75 |
F'(u_h;\delta u,v) = \int_\Omega [(2 u_h \delta u\nabla u_h)\cdot\nabla v + ((1+u_h^2)\nabla\delta u)\nabla v] \,{\rm dx} |
| 76 |
|
| 77 |
We note that for each fixed :math:`u_h`, :math:`a = |
| 78 |
F'(u_h;\,\cdot\,,\,\cdot\,)` is a bilinear form and :math:`L = |
| 79 |
F(u_h;\,\cdot\,,\,\cdot\,)` is a linear form. In each Newton |
| 80 |
iteration, we thus solve a linear variational problem of the canonical |
| 81 |
form: find :math:`\delta u \in V_{h,0}` such that |
| 82 |
|
| 83 |
.. math:: |
| 84 |
|
| 85 |
F'(u_h;\delta u,v) = -F(u_h;v)\quad\forall\,v\in\hat{V}_h. |
| 86 |
|
| 87 |
|
| 88 |
In this demo, we shall consider the following definitions of the input |
| 89 |
function, the domain, and the boundaries: |
| 90 |
|
| 91 |
* :math:`\Omega = [0,1] \times [0,1]\,\,\,` (a unit square) |
| 92 |
* :math:`\Gamma_{D} = \{(1, y) \subset \partial \Omega\}\,\,\,` (Dirichlet boundary) |
| 93 |
* :math:`\Gamma_{N} = \{(x, 0) \cup (x, 1) \cup (0, y) \subset \partial \Omega\}\,\,\,` (Neumann boundary) |
| 94 |
* :math:`f(x, y) = x\sin(y)\,\,\,` (source term) |
| 95 |
|
| 96 |
Implementation |
| 97 |
-------------- |
| 98 |
|
| 99 |
This description goes through the implementation (in |
| 100 |
:download:`demo_nonlinear-poisson.py`) of a solver for the above |
| 101 |
described nonlinear Poisson equation step-by-step. |
| 102 |
|
| 103 |
First, the matplotlib and :py:mod:`dolfin` modules are imported:: |
| 104 |
|
| 105 |
import matplotlib.pyplot as plt |
| 106 |
from dolfin import * |
| 107 |
|
| 108 |
Next, we want to consider the Dirichlet boundary condition. A simple |
| 109 |
Python function, returning a boolean, can be used to define the |
| 110 |
subdomain for the Dirichlet boundary condition (:math:`\Gamma_D`). The |
| 111 |
function should return True for those points inside the subdomain and |
| 112 |
False for the points outside. In our case, we want to say that the |
| 113 |
points :math:`(x, y)` such that :math:`x = 1` are inside on the inside |
| 114 |
of :math:`\Gamma_D`. (Note that because of rounding-off errors, it is |
| 115 |
often wise to instead specify :math:`|x - 1| < \epsilon`, where |
| 116 |
:math:`\epsilon` is a small number (such as machine precision).):: |
| 117 |
|
| 118 |
# Sub domain for Dirichlet boundary condition |
| 119 |
class DirichletBoundary(SubDomain): |
| 120 |
def inside(self, x, on_boundary): |
| 121 |
return abs(x[0] - 1.0) < DOLFIN_EPS and on_boundary |
| 122 |
|
| 123 |
We then define a mesh of the domain and a finite element function |
| 124 |
space V relative to this mesh. We use the built-in mesh provided by |
| 125 |
the class :py:class:`UnitSquareMesh |
| 126 |
<dolfin.cpp.mesh.UnitSquareMesh>`. In order to create a mesh |
| 127 |
consisting of :math:`32 \times 32` squares with each square divided |
| 128 |
into two triangles, we do as follows:: |
| 129 |
|
| 130 |
# Create mesh and define function space |
| 131 |
mesh = UnitSquareMesh(32, 32) |
| 132 |
File("mesh.pvd") << mesh |
| 133 |
|
| 134 |
V = FunctionSpace(mesh, "CG", 1) |
| 135 |
|
| 136 |
The second argument to :py:class:`FunctionSpace |
| 137 |
<dolfin.cpp.function.FunctionSpace>` is the finite element family, |
| 138 |
while the third argument specifies the polynomial degree. Thus, in |
| 139 |
this case, we use 'CG', for Continuous Galerkin, as a synonym for |
| 140 |
'Lagrange'. With degree 1, we simply get the standard linear Lagrange |
| 141 |
element, which is a triangle with nodes at the three vertices (or in |
| 142 |
other words, continuous piecewise linear polynomials). |
| 143 |
|
| 144 |
The Dirichlet boundary condition can be created using the class |
| 145 |
:py:class:`DirichletBC <dolfin.cpp.fem.DirichletBC>`. A |
| 146 |
:py:class:`DirichletBC <dolfin.cpp.fem.DirichletBC>` takes three |
| 147 |
arguments: the function space the boundary condition applies to, the |
| 148 |
value of the boundary condition, and the part of the boundary on which |
| 149 |
the condition applies. In our example, the function space is V, the |
| 150 |
value of the boundary condition (1.0) can be represented using a |
| 151 |
Constant and the Dirichlet boundary is defined above. The definition |
| 152 |
of the Dirichlet boundary condition then looks as follows:: |
| 153 |
|
| 154 |
# Define boundary condition |
| 155 |
g = Constant(1.0) |
| 156 |
bc = DirichletBC(V, g, DirichletBoundary()) |
| 157 |
|
| 158 |
Next, we want to express the variational problem. First, we need to |
| 159 |
specify the function u which represents the solution. Upon |
| 160 |
initialization, it is simply set to the zero function, which will |
| 161 |
represent the initial guess :math:`u_0`. A :py:class:`Function |
| 162 |
<dolfin.cpp.function.Function>` represents a function living in a |
| 163 |
finite element function space. The test function :math:`v` is |
| 164 |
specified, also living in the function space :math:`V`. We do this by |
| 165 |
defining a :py:class:`Function <dolfin.cpp.function.Function>` and a |
| 166 |
:py:class:`TestFunction <dolfin.functions.function.TestFunction>` on |
| 167 |
the previously defined :py:class:`FunctionSpace |
| 168 |
<dolfin.cpp.function.FunctionSpace>` V. |
| 169 |
|
| 170 |
Further, the source :math:`f` is involved in the variational forms, |
| 171 |
and hence we must specify this. We have :math:`f` given by a simple |
| 172 |
mathematical formula, which can be easily declared using the |
| 173 |
:py:class:`Expression <dolfin.cpp.function.Expression>` class. Note |
| 174 |
that the strings defining f use C++ syntax since, for efficiency, |
| 175 |
DOLFIN will generate and compile C++ code for this expression at |
| 176 |
run-time. |
| 177 |
|
| 178 |
By defining the function in this step and omitting the trial function |
| 179 |
we tell FEniCS that the problem is nonlinear. With these ingredients, |
| 180 |
we can write down the semilinear form F (using UFL operators). In |
| 181 |
summary, this reads:: |
| 182 |
|
| 183 |
# Define variational problem |
| 184 |
u = Function(V) |
| 185 |
v = TestFunction(V) |
| 186 |
f = Expression("x[0]*sin(x[1])", degree=2) |
| 187 |
F = inner((1 + u**2)*grad(u), grad(v))*dx - f*v*dx |
| 188 |
|
| 189 |
Now, we have specified the variational forms and can consider the |
| 190 |
solution of the variational problem. Next, we can call the solve |
| 191 |
function with the arguments F == 0, u, bc and solver parameters as |
| 192 |
follows:: |
| 193 |
|
| 194 |
# Compute solution |
| 195 |
solve(F == 0, u, bc, |
| 196 |
solver_parameters={"newton_solver":{"relative_tolerance":1e-6}}) |
| 197 |
|
| 198 |
The Newton procedure is considered to have converged when the residual |
| 199 |
:math:`r_n` at iteration :math:`n` is less than the absolute tolerance |
| 200 |
or the relative residual :math:`\frac{r_n}{r_0}` is less than the |
| 201 |
relative tolerance. |
| 202 |
|
| 203 |
A :py:class:`Function <dolfin.cpp.function.Function>` can be |
| 204 |
manipulated in various ways, in particular, it can be plotted and |
| 205 |
saved to file. Here, we output the solution to a VTK file (using the |
| 206 |
suffix .pvd) for later visualization and also plot it using the plot |
| 207 |
command:: |
| 208 |
|
| 209 |
# Plot solution and solution gradient |
| 210 |
plt.figure() |
| 211 |
plot(u, title="Solution") |
| 212 |
|
| 213 |
plt.figure() |
| 214 |
plot(grad(u), title="Solution gradient") |
| 215 |
|
| 216 |
plt.show() |
| 217 |
|
| 218 |
# Save solution in VTK format |
| 219 |
file = File("nonlinear_poisson.pvd") |
| 220 |
file << u |