Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2.8 Discontinuous Galerkin Methods\n",
"\n",
"* Use discontinuous finite element spaces to solve PDEs. \n",
"* Allows upwind-stabilization for convection-dominated problems\n",
"* Requires additional jump terms for consistency \n",
"\n",
"Interior penalty DG form for $-\\Delta u$:\n",
"\n",
"$$\n",
"A(u,v) = \\sum_T \\int_T \\nabla u \\nabla v\n",
"- \\sum_F \\int_F \\{ n \\nabla u \\} [v] \n",
"- \\sum_F \\int_F \\{ n \\nabla v \\} [u] \n",
"+ \\frac{\\alpha p^2}{h} \\sum_F \\int_F [u][v]\n",
"$$\n",
"\n",
"with jump-term over facets:\n",
"$$\n",
"[u] = u_{left} - u_{right}\n",
"$$\n",
"\n",
"and averaging operator\n",
"$$\n",
"\\{ n \\nabla u \\} = \\tfrac{1}{2} (n_{left} \\nabla u_{left} + n_{left} \\nabla u_{right})\n",
"$$\n",
"\n",
"DG form for $\\text{div} (b u)$, where $b$ is the given wind:\n",
"\n",
"$$\n",
"B(u,v) = -\\sum_T b u \\nabla v + \\sum_F \\int_F b\\cdot n u^{upwind} v \n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "21d6df00e17944a39bc259b947c415e1",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"WebGuiWidget(layout=Layout(height='500px', width='100%'), value={'ngsolve_version': 'Netgen x.x', 'mesh_dim': …"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from ngsolve import *\n",
"from ngsolve.webgui import Draw\n",
"from netgen.occ import *\n",
"\n",
"def AtomicShape(maxh = 0.1):\n",
" ax2d = gp_Ax2d(gp_Pnt2d(0.5, 0.5), gp_Dir2d(1, 0))\n",
" outeli = Ellipse( ax2d, 0.5, 0.1)\n",
" ineli = Ellipse( ax2d, 0.3, 0.06)\n",
"\n",
" shape = outeli.Face() - ineli.Face() \n",
"\n",
"\n",
" shape += shape.Rotate(gp_Ax1(gp_Pnt(0.5, 0.5, 0), gp_Dir(0,0,1)), 120)\n",
" shape += shape.Rotate(gp_Ax1(gp_Pnt(0.5, 0.5, 0), gp_Dir(0,0,1)), 240)\n",
" shape += Circle((0.5,0.5),0.2).Face() \n",
"\n",
" Draw(shape)\n",
" geo = OCCGeometry(shape, dim = 2)\n",
" return Mesh(geo.GenerateMesh(maxh = maxh)).Curve(3)\n",
"\n",
"mesh = AtomicShape()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The space is responsible for allocating the matrix graph. Tell it that it should reserve entries for the coupling terms:"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [],
"source": [
"order=4\n",
"fes = L2(mesh, order=order, dgjumps=True)\n",
"u,v = fes.TnT()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Every facet has a master element. The value from the other element is referred to via the\n",
"`Other()` operator:"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
"jump_u = u-u.Other()\n",
"jump_v = v-v.Other()\n",
"n = specialcf.normal(2)\n",
"mean_dudn = 0.5*n * (grad(u)+grad(u.Other()))\n",
"mean_dvdn = 0.5*n * (grad(v)+grad(v.Other()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Integrals on facets are computed by setting `skeleton=True`. \n",
"* `dx(skeleton=True)` iterates over all internal faces\n",
"* `ds(skeleton=True)` iterates over all boundary faces"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
"alpha = 4\n",
"h = specialcf.mesh_size\n",
"diffusion = grad(u)*grad(v) * dx \\\n",
" +alpha*order**2/h*jump_u*jump_v * dx(skeleton=True) \\\n",
" +(-mean_dudn*jump_v-mean_dvdn*jump_u) * dx(skeleton=True) \\\n",
" +alpha*order**2/h*u*v * ds(skeleton=True) \\\n",
" +(-n*grad(u)*v-n*grad(v)*u)* ds(skeleton=True)\n",
"\n",
"a = BilinearForm(diffusion).Assemble()"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [],
"source": [
"r_centr = (x-0.5)**2+(y-0.5)**2\n",
"\n",
"f = LinearForm(100*exp(10*r_centr)*v*dx).Assemble()"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "f272e575118747fb990b0fcbff217a42",
"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": [
"gfu = GridFunction(fes, name=\"uDG\")\n",
"gfu.vec.data = a.mat.Inverse() * f.vec\n",
"Draw (gfu);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"DG requires a lot of additional matrix entries:"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"DG-matrix nze: 718425\n",
"L2-matrix nze: 191925\n"
]
}
],
"source": [
"fes2 = L2(mesh, order=order)\n",
"ul2,vl2 = fes2.TnT()\n",
"a2 = BilinearForm(ul2*vl2*dx).Assemble()\n",
"print (\"DG-matrix nze:\", a.mat.nze)\n",
"print (\"L2-matrix nze:\", a2.mat.nze)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next we are solving a convection-diffusion problem:"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [],
"source": [
"alpha = 4\n",
"h = specialcf.mesh_size"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `IfPos` checks whether the first argument is positive. Then it returns the second one, else the third one. This is used to define the upwind flux. The check is performed in every integration-point on the skeleton:"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [],
"source": [
"b = CF( (20,5) )\n",
"uup = IfPos(b*n, u, u.Other())\n",
"\n",
"convection = -b * u * grad(v)*dx + b*n*uup*jump_v * dx(skeleton=True)\n",
"\n",
"acd = BilinearForm(diffusion + convection).Assemble()"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5825626fe18f497fbd65913d258c75e2",
"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": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gfu = GridFunction(fes)\n",
"gfu.vec.data = acd.mat.Inverse(freedofs=fes.FreeDofs(),inverse=\"pardiso\") * f.vec\n",
"Draw (gfu)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.