Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"cell_style": "center",
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# 3.3 Nonlinear problems\n",
"\n",
"In this unit we turn our attention to nonlinear PDE problems and the tools that `NGSolve` provides to simplify it."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## A simple scalar PDE\n",
"Let us start with a simple PDE with a nonlinearity: \n",
"\n",
"$$\n",
"- \\Delta u + \\frac13 u^3 = 10 \\text{ in } \\Omega\n",
"$$\n",
"\n",
"on the unit square $\\Omega = (0,1)^2$. \n",
"\n",
"We note that this PDE can also be formulated as a nonlinear minimization problem (cf. [3.4](../unit-3.4-nonlmin/nonlmin.ipynb))."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"code_folding": [
0
],
"run_control": {
"marked": false
},
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"# define geometry and generate mesh\n",
"from ngsolve import *\n",
"from ngsolve.webgui import *\n",
"from netgen.occ import *\n",
"\n",
"mesh = Mesh(unit_square.GenerateMesh(maxh=0.1))"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"In NGSolve we can solve the PDE conveniently using the *linearization* feature of `SymbolicBFI`.\n",
"\n",
"The `BilinearForm` (**which is not bilinear!**) needed in the weak formulation is\n",
"$$\n",
" A(u,v) = \\int_{\\Omega} \\nabla u \\nabla v + 1/3 u^3 v - 10 v ~ dx \\quad ( = 0 ~ \\forall~v \\in H^1_0)\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"run_control": {
"marked": false
},
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"V = H1(mesh, order=3, dirichlet=[1,2,3,4])\n",
"u,v = V.TnT()\n",
"a = BilinearForm(V)\n",
"a += (grad(u) * grad(v) + 1/3*u**3*v- 10 * v)*dx"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Newton's method\n",
"\n",
"In the preparation of the formulation of Newton's method we identify the semilinear form $A(\\cdot,\\cdot)$ with the corresponding operator $A: V \\to V'$ and write (assuming sufficient regularity)\n",
"\n",
" * $A(u)$ for the linear form $[A(u)](v) = A(u,v)$ and \n",
" * the derivative of $A$ at an evaluation point $u$ is the bilinear form $\\delta A(u)$ with\n",
" $$[\\delta A(u)] (w,v) = \\lim_{h \\to 0} \\frac{ A(u+h\\cdot w,v) - A(u,v)}{h}$$"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"\n",
"You could compute these linear and bilinear forms manually. However, `NGSolve` provides functions for both operations: \n",
"\n",
"* The linear form $A(u)$ for a given (vector) u is obtained from `a.Apply(vec,res)` (resulting in a vector `res` representing the linear form)\n",
"* The bilinear form (represented by the corresponding matrix) is stored in `a.mat` after a call of `a.AssembleLinearization(vec)`"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"Under the hood it uses its functionality to derive `CoefficientFunction`s symbolically. \n",
"\n",
"For example, `NGSolve` derives the bilinear form integrand $ \\frac13 u^3 v $ w.r.t. $u$ at $\\tilde{u}$ in direction $w$ resulting in the integrand $\\tilde{u}^2 w v$.\n",
"\n",
"This allows to form the corresponding bilinear form integrals automatically for you."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"To obtain a Newton algorithm we hence only need to translate the following pseudo-code formulation of Newton's method to `NGSolve`. The pseudo code is:\n",
"\n",
"`NewtonSolve`(Pseudo code):\n",
"\n",
"* Given an initial guess $u^0$\n",
"* loop over $i=0,..$ until convergence:\n",
" * Compute linearization: $A (u^i) + \\delta A(u^i) \\Delta u^{i} = 0$:\n",
" * $r^i = A (u^i)$ \n",
" * $B^i = \\delta A(u^i)$ \n",
" * Solve $B^i \\Delta u^i = -r^i$\n",
" * Update $u^{i+1} = u^i + \\Delta u^{i}$\n",
" * Evaluate stopping criteria\n",
"\n",
"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$."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"Now, here comes the same thing in `NGSolve` syntax:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"run_control": {
"marked": false
},
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"def SimpleNewtonSolve(gfu,a,tol=1e-13,maxits=10, callback=lambda gfu: None):\n",
" res = gfu.vec.CreateVector()\n",
" du = gfu.vec.CreateVector()\n",
" fes = gfu.space\n",
" callback(gfu)\n",
" for it in range(maxits):\n",
" print (\"Iteration {:3} \".format(it),end=\"\")\n",
" a.Apply(gfu.vec, res)\n",
" a.AssembleLinearization(gfu.vec)\n",
" du.data = a.mat.Inverse(fes.FreeDofs()) * res\n",
" gfu.vec.data -= du\n",
" callback(gfu)\n",
" #stopping criteria\n",
" stopcritval = sqrt(abs(InnerProduct(du,res)))\n",
" print (\"<A u\",it,\", A u\",it,\">_{-1}^0.5 = \", stopcritval)\n",
" if stopcritval < tol:\n",
" break"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"cell_style": "center",
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"Let's apply this to the previous PDE problem:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"run_control": {
"marked": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Iteration 0 <A u 0 , A u 0 >_{-1}^0.5 = 1.874663937977647\n",
"Iteration 1 <A u 1 , A u 1 >_{-1}^0.5 = 0.010379625485142853\n",
"Iteration 2 <A u 2 , A u 2 >_{-1}^0.5 = 1.1229365441454634e-06\n",
"Iteration 3 <A u 3 , A u 3 >_{-1}^0.5 = 1.3183469064796606e-14\n"
]
}
],
"source": [
"gfu = GridFunction(V)\n",
"gfu.Set((x*(1-x))**4*(y*(1-y))**4) # initial guess\n",
"gfu_it = GridFunction(gfu.space,multidim=0)\n",
"cb = lambda gfu : gfu_it.AddMultiDimComponent(gfu.vec) # store current state\n",
"SimpleNewtonSolve(gfu, a, callback = cb)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The iterations converge to a solution in a few steps."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"cell_style": "center",
"run_control": {
"marked": false
},
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "ec0abf5df70b402a9e735d79c6125aca",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"WebGuiWidget(layout=Layout(height='500px', width='100%'), value={'gui_settings': {}, 'ngsolve_version': '6.2.2…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"BaseWebGuiScene"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Draw(gfu_it,mesh,\"u\", deformation = True, animate = True, autoscale = True, min = 0, max = 0.73)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"cell_style": "center",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"Use the `multidim`-Slider to inspect the results after the iterations."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"There are also some solvers shipped with NGSolve now. The `ngsolve.solvers.Newton` method allows you to use static condensation and is overall more refined (e.g. with damping options). For this tutorial we will mostly stay with the simple hand-crafted `SimpleNewtonSolve`. \n",
"Here is the alternative demonstrated once, nevertheless:\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"run_control": {
"marked": false
},
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function Newton in module ngsolve.nonlinearsolvers:\n",
"\n",
"Newton(a, u, freedofs=None, maxit=100, maxerr=1e-11, inverse='', dirichletvalues=None, dampfactor=1, printing=True, callback=None)\n",
" Newton's method for solving non-linear problems of the form A(u)=0.\n",
"\n",
" Parameters\n",
" ----------\n",
" a : BilinearForm\n",
" The BilinearForm of the non-linear variational problem. It does not have to be assembled.\n",
"\n",
" u : GridFunction\n",
" The GridFunction where the solution is saved. The values are used as initial guess for Newton's method.\n",
"\n",
" freedofs : BitArray\n",
" The FreeDofs on which the assembled matrix is inverted. If argument is 'None' then the FreeDofs of the underlying FESpace is used.\n",
"\n",
" maxit : int\n",
" 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.\n",
"\n",
" maxerr : float\n",
" 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.\n",
"\n",
" inverse : string\n",
" A string of the sparse direct solver which should be solved for inverting the assembled Newton matrix.\n",
"\n",
" dampfactor : float\n",
" 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.\n",
"\n",
" printing : bool\n",
" Set if Newton's method should print informations about the actual iteration like the error.\n",
"\n",
" Returns\n",
" -------\n",
" (int, int)\n",
" 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.\n",
"\n"
]
}
],
"source": [
"from ngsolve.solvers import *\n",
"help(Newton)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Another example: Stationary Navier-Stokes:\n",
"\n",
"Next, we consider incompressible Navier-Stokes equations again. This time however stationary.\n",
"\n",
"Find $\\mathbf{u} \\in \\mathbf{V}$, $p \\in Q$, $\\lambda \\in \\mathbb{R}$ so that\n",
"\n",
"\\begin{align*}\n",
"\\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}, \\\\ \n",
"- \\int_{\\Omega} \\operatorname{div}(\\mathbf{u}) q & & \n",
"+ \\int_{\\Omega} \\lambda q\n",
"&= 0 && \\forall q \\in Q, \\\\\n",
"& \\int_{\\Omega} \\mu p & &= 0 && \\forall \\mu \\in \\mathbb{R}.\n",
"\\end{align*}\n",
"\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"The domain $\\Omega$ is still $(0,1)^2$ and we prescribe homogenuous Dirichlet bnd. conditions for the velocity, except for the top boundary where we prescribe a tangential velocity. This setup is known as \"driven cavity\".\n",
"\n",
"Note that we use a scalar constraint to fix the pressure level that is otherwise not controlled in the presence of pure Dirichlet conditions."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"We use a higher order Taylor-Hood discretization again:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"run_control": {
"marked": false
},
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"mesh = Mesh (unit_square.GenerateMesh(maxh=0.05)); \n",
"nu = Parameter(1)\n",
"\n",
"V = VectorH1(mesh,order=3,dirichlet=\"bottom|right|top|left\")\n",
"Q = H1(mesh,order=2); \n",
"N = NumberSpace(mesh); \n",
"X = V*Q*N\n",
"(u,p,lam), (v,q,mu) = X.TnT()\n",
"a = BilinearForm(X)\n",
"a += (nu*InnerProduct(grad(u),grad(v))+InnerProduct(grad(u)*u,v)\n",
" -div(u)*q-div(v)*p-lam*q-mu*p)*dx"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"The boundary condition:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"run_control": {
"marked": false
},
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"gfu = GridFunction(X)\n",
"gfu.components[0].Set(CF((4*x*(1-x),0)),\n",
" definedon=mesh.Boundaries(\"top\"))"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"Now, let's apply the Newton:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"code_folding": [
3,
11
],
"run_control": {
"marked": false
},
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"def SolveAndVisualize(multidim=True):\n",
" gfu.components[0].Set(CF((4*x*(1-x),0)),\n",
" definedon=mesh.Boundaries(\"top\"))\n",
" if multidim:\n",
" gfu_it = GridFunction(gfu.space,multidim=0)\n",
" cb = lambda gfu : gfu_it.AddMultiDimComponent(gfu.vec) # store current state\n",
" SimpleNewtonSolve(gfu, a, callback = cb)\n",
" else:\n",
" SimpleNewtonSolve(gfu, a)\n",
" Draw(gfu.components[0],mesh, vectors = {\"grid_size\" : 25})\n",
" print(\"above you see the solution after the Newton solve.\")\n",
" if multidim:\n",
" Draw(gfu_it.components[0], mesh, vectors = {\"grid_size\" : 25})\n",
" print(\"above you can inspect the results after each iteration of the Newton solve (use multidim-slider).\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"run_control": {
"marked": false
},
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Iteration 0 <A u 0 , A u 0 >_{-1}^0.5 = 2.7921394712043925\n",
"Iteration 1 <A u 1 , A u 1 >_{-1}^0.5 = 0.00793718884351596\n",
"Iteration 2 <A u 2 , A u 2 >_{-1}^0.5 = 6.509217402448266e-08\n",
"Iteration 3 <A u 3 , A u 3 >_{-1}^0.5 = 7.881632201927881e-16\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "05180a7eb8a94776aba958a2e75a6cbd",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"WebGuiWidget(layout=Layout(height='500px', width='100%'), value={'gui_settings': {}, 'ngsolve_version': '6.2.2…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"above you see the solution after the Newton solve.\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b0a4a82ef3e64e75b1776160d59f4c37",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"WebGuiWidget(layout=Layout(height='500px', width='100%'), value={'gui_settings': {}, 'ngsolve_version': '6.2.2…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"above you can inspect the results after each iteration of the Newton solve (use multidim-slider).\n"
]
}
],
"source": [
"SolveAndVisualize()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"The problem becomes more interesting if we decrease the viscosity $\\nu$ (`nu`), i.e. if we increase the Reynolds number *Re*. Let's consider the previous setup for decreasing values of `nu`"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"run_control": {
"marked": false
},
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Iteration 0 "
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"<A u 0 , A u 0 >_{-1}^0.5 = 1.0132061697631711\n",
"Iteration 1 <A u 1 , A u 1 >_{-1}^0.5 = 0.0680096252694748\n",
"Iteration 2 <A u 2 , A u 2 >_{-1}^0.5 = 0.005083383535941082\n",
"Iteration 3 <A u 3 , A u 3 >_{-1}^0.5 = 3.7138993291451534e-05\n",
"Iteration 4 <A u 4 , A u 4 >_{-1}^0.5 = 2.3705837866469634e-09\n",
"Iteration 5 <A u 5 , A u 5 >_{-1}^0.5 = 1.0569524681586126e-16\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b08ae848818a44af9d028c526b1af5b3",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"WebGuiWidget(layout=Layout(height='500px', width='100%'), value={'gui_settings': {}, 'ngsolve_version': '6.2.2…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"above you see the solution after the Newton solve.\n"
]
}
],
"source": [
"nu.Set(0.01)\n",
"SolveAndVisualize(multidim=False)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"run_control": {
"marked": false
},
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Iteration 0 <A u 0 , A u 0 >_{-1}^0.5 = 0.2963934033830182\n",
"Iteration 1 <A u 1 , A u 1 >_{-1}^0.5 = 0.06800962526947499\n",
"Iteration 2 <A u 2 , A u 2 >_{-1}^0.5 = 0.005083383535942459\n",
"Iteration 3 <A u 3 , A u 3 >_{-1}^0.5 = 3.713899329149202e-05\n",
"Iteration 4 <A u 4 , A u 4 >_{-1}^0.5 = 2.37058377393866e-09\n",
"Iteration 5 <A u 5 , A u 5 >_{-1}^0.5 = 1.0086823645114031e-16\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "a21e6af39b034abb9355c09383462a0c",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"WebGuiWidget(layout=Layout(height='500px', width='100%'), value={'gui_settings': {}, 'ngsolve_version': '6.2.2…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"above you see the solution after the Newton solve.\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b06f9f2094eb4ad7bd90cf30a32fd296",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"WebGuiWidget(layout=Layout(height='500px', width='100%'), value={'gui_settings': {}, 'ngsolve_version': '6.2.2…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"above you can inspect the results after each iteration of the Newton solve (use multidim-slider).\n"
]
}
],
"source": [
"nu.Set(0.01)\n",
"SolveAndVisualize()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"run_control": {
"marked": false
},
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Iteration 0 <A u 0 , A u 0 >_{-1}^0.5 = 0.1267547755116805\n",
"Iteration 1 <A u 1 , A u 1 >_{-1}^0.5 = 0.02758858633109622\n",
"Iteration 2 <A u 2 , A u 2 >_{-1}^0.5 = 0.1868025460774684\n",
"Iteration 3 <A u 3 , A u 3 >_{-1}^0.5 = 0.2614567190112457\n",
"Iteration 4 <A u 4 , A u 4 >_{-1}^0.5 = 0.07133935698047214\n",
"Iteration 5 <A u 5 , A u 5 >_{-1}^0.5 = 0.7750488029775008\n",
"Iteration 6 <A u 6 , A u 6 >_{-1}^0.5 = 0.7035527932949306\n",
"Iteration 7 <A u 7 , A u 7 >_{-1}^0.5 = 0.530301626732127\n",
"Iteration 8 <A u 8 , A u 8 >_{-1}^0.5 = 1.4961375522855465\n",
"Iteration 9 <A u 9 , A u 9 >_{-1}^0.5 = 14.244452561960456\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "4464f08e7d7648bea69eca51d6c36851",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"WebGuiWidget(layout=Layout(height='500px', width='100%'), value={'gui_settings': {}, 'ngsolve_version': '6.2.2…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"above you see the solution after the Newton solve.\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "7ef734b7e0ba4033adfd2654c9d66900",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"WebGuiWidget(layout=Layout(height='500px', width='100%'), value={'gui_settings': {}, 'ngsolve_version': '6.2.2…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"above you can inspect the results after each iteration of the Newton solve (use multidim-slider).\n"
]
}
],
"source": [
"nu.Set(0.001)\n",
"SolveAndVisualize()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"Now, viscosity is so small that Newton does not converge anymore.\n",
"In this case we can fix it using a small damping parameter with the `ngsolve.solvers.Newton`-solver:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"run_control": {
"marked": false
},
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Newton iteration 0\n",
"err = 3.626035844057475\n",
"Newton iteration 1\n",
"err = 3.4084699649576224\n",
"Newton iteration 2\n",
"err = 2.99945061517751\n",
"Newton iteration 3\n",
"err = 2.4595571865822423\n",
"Newton iteration 4\n",
"err = 1.8693450190661574\n",
"Newton iteration 5\n",
"err = 1.3085261919786284\n",
"Newton iteration 6\n",
"err = 0.837436815764426\n",
"Newton iteration 7\n",
"err = 0.4857088193714519\n",
"Newton iteration 8\n",
"err = 0.25215897978473867\n",
"Newton iteration 9\n",
"err = 0.1142470017520598\n",
"Newton iteration 10\n",
"err = 0.04940872266814323\n",
"Newton iteration 11\n",
"err = 0.016798803979527707\n",
"Newton iteration 12\n",
"err = 0.004703651089205825\n",
"Newton iteration 13\n",
"err = 0.0010348072124452574\n",
"Newton iteration 14\n",
"err = 0.0001655633041447728\n",
"Newton iteration 15\n",
"err = 1.655597976654527e-05\n",
"Newton iteration 16\n",
"err = 6.623350490884643e-07\n",
"Newton iteration 17\n",
"err = 1.0427988754409035e-12\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5121c029e2a24a9ba2f448f64610b43e",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"WebGuiWidget(layout=Layout(height='500px', width='100%'), value={'gui_settings': {}, 'ngsolve_version': '6.2.2…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"BaseWebGuiScene"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nu.Set(0.001)\n",
"gfu.components[0].Set(CF((4*x*(1-x),0)),definedon=mesh.Boundaries(\"top\"))\n",
"Newton(a,gfu,maxit=20,dampfactor=0.06)\n",
"Draw(gfu.components[0],mesh, vectors = {\"grid_size\" : 25})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Nonlinear minimization problems¶\n",
"\n",
"Sometimes we have to find minimizers of functionals of the form:\n",
"\n",
"\n",
"Find $u \\in V$ such that\n",
"\\begin{align*}\n",
"E(u)\\leq E(v) \\quad \\forall v \\in V.\n",
"\\end{align*}\n",
"\n",
"As an example consider the functional:\n",
"\n",
"\\begin{align*}\n",
"E(u) = \\int_{\\Omega} \\frac12 |\\nabla u|^2 + \\frac {1}{12} u^4 - 10 u \\, dx.\n",
"\\end{align*}\n",
"\n",
"The minimum is achived when the first variation of $E$ at $u$ is zero, i.e. when\n",
"\n",
"\\begin{align*}\n",
"\\delta E(u)[v] = 0 \\quad \\forall v \\in V.\n",
"\\end{align*}\n",
"or in other symbols\n",
"\\begin{align*}\n",
"-\\Delta u + \\frac13 u^3 = 10.\n",
"\\end{align*}\n",
"\n",
"The problem can be set up in the following way using the `Variation` keyword.\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"V = H1(mesh, order=4, dirichlet=\".*\")\n",
"u = V.TrialFunction()\n",
"\n",
"a = BilinearForm(V, symmetric=True)\n",
"a += Variation ((0.5*grad(u)*grad(u) + 1/12*u**4-10*u) * dx)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now the problem becomes equivalent to the one at the beginning of the jupiter notebook.\n",
"\n",
"NGSolve provides a `Newton`-solver for nonlinear minimization problems as well. It is called `NewtonMinimization`."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Newton iteration 0\n",
"Energy: -2.519485709075059e-05\n",
"err = 1.874666614698685\n",
"Newton iteration 1\n",
"Energy: -1.7526347043980903\n",
"err = 0.010379626788782924\n",
"Newton iteration 2\n",
"Energy: -1.752688576598051\n",
"err = 1.1229373270322439e-06\n",
"Newton iteration 3\n",
"Energy: -1.75268857659868\n",
"err = 1.330279474886181e-14\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "47d95d4bb94f4c6ab217e728f88ff1d2",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"WebGuiWidget(layout=Layout(height='500px', width='100%'), value={'gui_settings': {}, 'ngsolve_version': '6.2.2…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from ngsolve.solvers import *\n",
"\n",
"gfu = GridFunction(V)\n",
"\n",
"\n",
"gfu.Set((x*(1-x))**4*(y*(1-y))**4) # initial guess\n",
"NewtonMinimization(a, gfu)\n",
"\n",
"Draw(gfu, mesh, \"u\");"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
300fd526eThis file is pinned to the selected release. Download the original to open it in your design tool.