# 3.7 Nonlinear problems¶

We want to solve a nonlinear PDE.

## A simple scalar PDE¶

We consider the simple PDE

$- \Delta u + 3 u^3 = 1 \text{ in } \Omega$

on the unit square $$\Omega = (0,1)^2$$.

We note that this PDE can also be formulated as a nonlinear minimization problem (cf. 3.8).

:

from ngsolve import *
import netgen.gui
from netgen.geom2d import unit_square

mesh = Mesh (unit_square.GenerateMesh(maxh=0.3))


In NGSolve we can solve the PDE conveniently using the linearization feature of SymbolicBFI.

The BilinearForm (which is not bilinear!) needed in the weak formulation is

$A(u,v) = \int_{\Omega} \nabla u \nabla v + 3 u^3 v - 1 v ~ dx \quad ( = 0 ~ \forall~v \in H^1_0)$
:

V = H1(mesh, order=3, dirichlet=[1,2,3,4])
u,v = V.TnT()
a = BilinearForm(V)


### Newton’s method¶

We use Newton’s method and make the loop:

• Given an initial guess $$u^0$$

• loop over $$i=0,..$$ until convergence:

• Compute linearization: $$A u^i + \delta A(u^i) \Delta u^{i} = 0$$:

• $$f^i = A u^i$$

• $$B^i = \delta A(u^i)$$

• Solve $$B^i \Delta u^i = -f^i$$

• Update $$u^{i+1} = u^i + \Delta u^{i}$$

• Evaluate stopping criteria

As a stopping criteria we take $$\langle A u^i,\Delta u^i \rangle = \langle A u^i, A u^i \rangle_{(B^i)^{-1}}< \varepsilon$$.

:

def SimpleNewtonSolve(gfu,a,tol=1e-13,maxits=25):
res = gfu.vec.CreateVector()
du = gfu.vec.CreateVector()
fes = gfu.space
for it in range(maxits):
print ("Iteration {:3}  ".format(it),end="")
a.Apply(gfu.vec, res)
a.AssembleLinearization(gfu.vec)
du.data = a.mat.Inverse(fes.FreeDofs()) * res
gfu.vec.data -= du

#stopping criteria
stopcritval = sqrt(abs(InnerProduct(du,res)))
print ("<A u",it,", A u",it,">_{-1}^0.5 = ", stopcritval)
if stopcritval < tol:
break

:

gfu = GridFunction(V)
Draw(gfu,mesh,"u")
SimpleNewtonSolve(gfu,a)

Iteration   0  <A u 0 , A u 0 >_{-1}^0.5 =  0.187435321076685
Iteration   1  <A u 1 , A u 1 >_{-1}^0.5 =  9.416472885501191e-05
Iteration   2  <A u 2 , A u 2 >_{-1}^0.5 =  8.538473326331901e-11
Iteration   3  <A u 3 , A u 3 >_{-1}^0.5 =  3.831924934146655e-17


There are also some solvers shipped with NGSolve now:

:

from ngsolve.solvers import *
help(Newton)

Help on function Newton in module ngsolve.nonlinearsolvers:

Newton(a, u, freedofs=None, maxit=100, maxerr=1e-11, inverse='umfpack', dirichletvalues=None, dampfactor=1, printing=True, callback=None)
Newton's method for solving non-linear problems of the form A(u)=0.

Parameters
----------
a : BilinearForm
The BilinearForm of the non-linear variational problem. It does not have to be assembled.

u : GridFunction
The GridFunction where the solution is saved. The values are used as initial guess for Newton's method.

freedofs : BitArray
The FreeDofs on which the assembled matrix is inverted. If argument is 'None' then the FreeDofs of the underlying FESpace is used.

maxit : int
Number of maximal iteration for Newton. If the maximal number is reached before the maximal error Newton might no converge and a warning is displayed.

maxerr : float
The maximal error which Newton should reach before it stops. The error is computed by the square root of the inner product of the residuum and the correction.

inverse : string
A string of the sparse direct solver which should be solved for inverting the assembled Newton matrix.

dampfactor : float
Set the damping factor for Newton's method. If dampfactor is 1 then no damping is done. If value is < 1 then the damping is done by the formula 'min(1,dampfactor*numit)' for the correction, where 'numit' denotes the Newton iteration.

printing : bool
Set if Newton's method should print informations about the actual iteration like the error.

Returns
-------
(int, int)
List of two integers. The first one is 0 if Newton's method did converge, -1 otherwise. The second one gives the number of Newton iterations needed.


:

gfu.vec[:]=0
Newton(a,gfu,freedofs=gfu.space.FreeDofs(),maxit=100,maxerr=1e-11,inverse="umfpack",dampfactor=1,printing=True)

Newton iteration  0
err =  0.18743532107668498
Newton iteration  1
err =  9.416472885497372e-05
Newton iteration  2
err =  8.538474393723481e-11
Newton iteration  3
err =  4.291958722351373e-17

:

(0, 4)


## A trivial problem:¶

$5 u^2 = 1, \qquad u \in \mathbb{R}.$
:

V = NumberSpace(mesh)
u,v = V.TnT()
a = BilinearForm(V)
a += ( 5*u*u*v - 1 * v)*dx
gfu = GridFunction(V)
gfu.vec[:] = 1
SimpleNewtonSolve(gfu,a)

print("\nscalar solution", gfu.vec, "(exact: ", sqrt(0.2), ")")

Iteration   0  <A u 0 , A u 0 >_{-1}^0.5 =  1.2649110640673515
Iteration   1  <A u 1 , A u 1 >_{-1}^0.5 =  0.32659863237109055
Iteration   2  <A u 2 , A u 2 >_{-1}^0.5 =  0.04114755998989123
Iteration   3  <A u 3 , A u 3 >_{-1}^0.5 =  0.0008574269268691781
Iteration   4  <A u 4 , A u 4 >_{-1}^0.5 =  3.8832745226099997e-07
Iteration   5  <A u 5 , A u 5 >_{-1}^0.5 =  7.979879233426059e-14

scalar solution 0.4472135954999579 (exact:  0.4472135954999579 )


## Another example: Stationary Navier-Stokes:¶

Find $$\mathbf{u} \in \mathbf{V}$$, $$p \in Q$$, $$\lambda \in \mathbb{R}$$ so that \begin{align} \int_{\Omega} \nu \nabla \mathbf{u} : \nabla \mathbf{v} + (\mathbf{u} \cdot \nabla) \mathbf{u} \cdot \mathbf{v}& - \int_{\Omega} \operatorname{div}(\mathbf{v}) p & &= \int \mathbf{f} \cdot \mathbf{v} && \forall \mathbf{v} \in \mathbf{V}, \\ - \int_{\Omega} \operatorname{div}(\mathbf{u}) q & & + \int_{\Omega} \lambda q &= 0 && \forall q \in Q, \\ & \int_{\Omega} \mu p & &= 0 && \forall \mu \in \mathbb{R}. \end{align}

:

mesh = Mesh (unit_square.GenerateMesh(maxh=0.05)); nu = Parameter(1)
V = VectorH1(mesh,order=3,dirichlet="bottom|right|top|left")
Q = H1(mesh,order=2);
N = NumberSpace(mesh);
X = FESpace([V,Q,N])
(u,p,lam), (v,q,mu) = X.TnT()
a = BilinearForm(X)
-div(u)*q-div(v)*p-lam*q-mu*p)*dx

:

gfu = GridFunction(X)
gfu.components.Set(CoefficientFunction((4*x*(1-x),0)),
definedon=mesh.Boundaries("top"))

:

SimpleNewtonSolve(gfu,a)
Draw(gfu.components,mesh,"p")
Draw(gfu.components,mesh,"u")

Iteration   0  <A u 0 , A u 0 >_{-1}^0.5 =  2.79692158555361
Iteration   1  <A u 1 , A u 1 >_{-1}^0.5 =  0.00793924075293565
Iteration   2  <A u 2 , A u 2 >_{-1}^0.5 =  6.512555490966166e-08
Iteration   3  <A u 3 , A u 3 >_{-1}^0.5 =  8.518191581624935e-16

:

nu.Set(0.01)
SimpleNewtonSolve(gfu,a)
Redraw()

Iteration   0  <A u 0 , A u 0 >_{-1}^0.5 =  0.0881192861307836
Iteration   1  <A u 1 , A u 1 >_{-1}^0.5 =  0.008271027658133112
Iteration   2  <A u 2 , A u 2 >_{-1}^0.5 =  0.00010638957558584126
Iteration   3  <A u 3 , A u 3 >_{-1}^0.5 =  1.940167469891874e-08
Iteration   4  <A u 4 , A u 4 >_{-1}^0.5 =  5.859334658448172e-16

:

nu.Set(0.001)
SimpleNewtonSolve(gfu,a)
Redraw()

Iteration   0  <A u 0 , A u 0 >_{-1}^0.5 =  0.07337567458977313
Iteration   1  <A u 1 , A u 1 >_{-1}^0.5 =  0.05991431939670181
Iteration   2  <A u 2 , A u 2 >_{-1}^0.5 =  0.03627604097500266
Iteration   3  <A u 3 , A u 3 >_{-1}^0.5 =  0.03186287812555933
Iteration   4  <A u 4 , A u 4 >_{-1}^0.5 =  0.030209204128244117
Iteration   5  <A u 5 , A u 5 >_{-1}^0.5 =  0.024352540358544214
Iteration   6  <A u 6 , A u 6 >_{-1}^0.5 =  0.050941929805131216
Iteration   7  <A u 7 , A u 7 >_{-1}^0.5 =  0.10649691679264128
Iteration   8  <A u 8 , A u 8 >_{-1}^0.5 =  0.23022761859098245
Iteration   9  <A u 9 , A u 9 >_{-1}^0.5 =  0.473433563758806
Iteration  10  <A u 10 , A u 10 >_{-1}^0.5 =  1.2003785078689346
Iteration  11  <A u 11 , A u 11 >_{-1}^0.5 =  1.0453238217885021
Iteration  12  <A u 12 , A u 12 >_{-1}^0.5 =  6.958618234504686
Iteration  13  <A u 13 , A u 13 >_{-1}^0.5 =  71.43296697518673
Iteration  14  <A u 14 , A u 14 >_{-1}^0.5 =  100.52323155897903
Iteration  15  <A u 15 , A u 15 >_{-1}^0.5 =  243.30245498293093
Iteration  16  <A u 16 , A u 16 >_{-1}^0.5 =  326.2459624388071
Iteration  17  <A u 17 , A u 17 >_{-1}^0.5 =  319.1595971237783
Iteration  18  <A u 18 , A u 18 >_{-1}^0.5 =  174.91946132272724
Iteration  19  <A u 19 , A u 19 >_{-1}^0.5 =  87.46976128712504
Iteration  20  <A u 20 , A u 20 >_{-1}^0.5 =  651.0315611724214
Iteration  21  <A u 21 , A u 21 >_{-1}^0.5 =  323.842138958704
Iteration  22  <A u 22 , A u 22 >_{-1}^0.5 =  1049.976198385085
Iteration  23  <A u 23 , A u 23 >_{-1}^0.5 =  422.00537867095727
Iteration  24  <A u 24 , A u 24 >_{-1}^0.5 =  610.6824165159913

:

nu.Set(0.001)
gfu.components.Set(CoefficientFunction((4*x*(1-x),0)),definedon=mesh.Boundaries("top"))
Newton(a,gfu,maxit=20,dampfactor=0.1)
Redraw()

Newton iteration  0
err =  24.256317605662897
Newton iteration  1
err =  21.830686715647616
Newton iteration  2
err =  17.46453982047947
Newton iteration  3
err =  12.225149204381285
Newton iteration  4
err =  7.335021996308872
Newton iteration  5
err =  3.667302978175474
Newton iteration  6
err =  1.466640564583541
Newton iteration  7
err =  0.4399358386479375
Newton iteration  8
err =  0.087927846546468
Newton iteration  9
err =  0.008776125580023072
Newton iteration  10
err =  1.2643123656000818e-05
Newton iteration  11
err =  6.72982114126984e-09
Newton iteration  12
err =  1.579834353325483e-15