Skip to content

Commit

Permalink
update notebooks with occ
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulSt committed Jul 9, 2023
1 parent a9be52e commit 93deb77
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 124 deletions.
2 changes: 1 addition & 1 deletion docs/notebooks/embTrefftz-adv.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"source": [
"from ngsolve import *\n",
"from ngstrefftz import *\n",
"from netgen.geom2d import unit_square"
"from netgen.occ import *"
]
},
{
Expand Down
150 changes: 123 additions & 27 deletions docs/notebooks/embTrefftz-helm.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,28 @@
"id": "a08f0db0",
"metadata": {},
"source": [
"# Embedded Trefftz-DG: Helmholtz\n",
"Standard polynomial Trefftz functions for the Helmholtz equation do not exist, to circumvent this problem, we weaken our condition in the Trefftz space. We introduce a projection $\\Pi$ that is yet to be defined and define the weak Trefftz space and the embedded weak Trefftz DG method:\n",
"# Embedded Trefftz-DG: Helmholtz"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d6d31794",
"metadata": {},
"outputs": [],
"source": [
"from ngsolve import *\n",
"from ngstrefftz import *\n",
"from netgen.occ import *\n",
"from ngsolve.webgui import Draw"
]
},
{
"cell_type": "markdown",
"id": "4e554177",
"metadata": {},
"source": [
"We consider the Helmholtz equation with Robin boundary conditions\n",
"\n",
"$$\n",
"\\newcommand{\\Th}{{\\mathcal{T}_h}} \n",
Expand All @@ -33,54 +53,114 @@
"\\newcommand{\\be}{\\mathbf{e}}\n",
"\\newcommand{\\bx}{{\\mathbf x}}\n",
"\\newcommand{\\inner}[1]{\\langle #1 \\rangle}\n",
"\\begin{align*}\n",
" \\begin{cases}\n",
" -\\Delta u - \\omega^2 u= 0 &\\text{ in } \\dom, \\\\\n",
" \\frac{\\partial u}{\\partial \\nx} + i u = g &\\text{ on } \\partial \\dom.\n",
" \\end{cases}\n",
"\\end{align*}\n",
"$$\n",
"\n",
"Standard *polynomial* Trefftz functions for the Helmholtz equation do not exist, to circumvent this problem, we weaken our condition in the Trefftz space. We introduce a projection $\\Pi$ that is yet to be defined and define the weak Trefftz space and the embedded weak Trefftz DG method:\n",
"\n",
"$$\n",
"\\begin{align}\n",
" \\text{Find }u_{\\IT}\\in \\IT^p(\\Th)&,~\\text{ s.t. }\n",
" a_h(u_{\\IT},v_{\\IT})=\\ell(v_{\\IT})\\qquad \\forall v_{\\IT}\\in \\IT^p(\\Th)\\quad \\text{ with } \\\\\n",
" \\IT^p(\\Th)&=\\{v\\in \\Vhp,\\ \\Pi \\calL v=0\\}. \\label{eq:weakTspace}\n",
" \\IT^p(\\Th)&=\\{v\\in V^k(\\mathcal T_h),\\ \\Pi \\calL v=0\\}. \\label{eq:weakTspace}\n",
" % \\\\ &\\IT^p(\\Th)=\\{v\\in L^2(\\dom) \\sst \\restr{v}{K}\\in\\IT(K),\\forall K\\in\\Th\\}\n",
"\\end{align}\n",
"$$\n",
"\n",
"This way, we can re-define the matrix $\\bW$ for the general case as \n",
"For the Helmholtz problem we choose $\\Pi:V^{k}(\\mathcal T_h)\\rightarrow V^{k-2}(\\mathcal T_h)$ the $L^2$ orthogonal projection. This way, we can define the matrix $\\bW$ as \n",
"\n",
"$$\n",
"\\begin{align} \\label{def:W3}\n",
" (\\bW)_{ij}&=\\inner{\\calL\\phi_j,\\tilde\\calL\\phi_i}_{0,h}. \n",
" (\\bW)_{ij}&=\\inner{\\calL\\phi_j,\\tilde\\phi_i}_{0,h}. \n",
"\\end{align}\n",
"$$\n",
"\n",
"with test functions $\\tilde\\phi_i\\in V^{k-2}(\\mathcal T_h)$ and $\\calL=-\\Delta u -\\omega^2 u$."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4855289d",
"metadata": {},
"outputs": [],
"source": [
"def TrefftzHelmholtzEmb(fes):\n",
" mesh = fes.mesh\n",
" k = fes.globalorder\n",
" u = fes.TrialFunction()\n",
" \n",
" Lap = lambda u : sum(Trace(u.Operator('hesse')))\n",
" fes2 = L2(mesh, order=order-2, dgjumps=True,complex=True)\n",
" v = fes2.TestFunction()\n",
" op = -u*v*dx - Lap(u)*v*dx\n",
" PP = TrefftzEmbedding(op,fes,test_fes=fes2)\n",
" return PP"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "263b8ff9",
"metadata": {},
"outputs": [],
"source": [
"def EmbeddedBasisFunctionsAsMultiDim(Emb, fes):\n",
" gfshow = GridFunction(fes, multidim=0)\n",
" gf = GridFunction(fes)\n",
" coefvec = Emb.CreateRowVector()\n",
" for i in range(Emb.width):\n",
" coefvec[:] = 0\n",
" coefvec[i] = 1\n",
" gf.vec.data = Emb * coefvec\n",
" gfshow.AddMultiDimComponent(gf.vec)\n",
" return gfshow\n",
"\n",
"For the Helmholtz equation with Robin boundary conditions\n",
"\n",
"$$\n",
"\\begin{align*}\n",
" \\begin{cases}\n",
" -\\Delta u - \\omega^2 u= 0 &\\text{ in } \\dom, \\\\\n",
" \\frac{\\partial u}{\\partial \\nx} + i u = g &\\text{ on } \\partial \\dom.\n",
" \\end{cases}\n",
"\\end{align*}\n",
"$$\n",
"order = 3\n",
"mesh = Mesh(unit_square.GenerateMesh(maxh=1))\n",
"fes = L2(mesh, order=order, dgjumps=True,complex=True)\n",
"PP = TrefftzHelmholtzEmb(fes)\n",
"gfshow = EmbeddedBasisFunctionsAsMultiDim(PP,fes)\n",
"Draw (gfshow, mesh, interpolate_multidim=False, animate=False, autoscale=True, settings={\"subdivision\":20})"
]
},
{
"cell_type": "markdown",
"id": "7ef6c524",
"metadata": {},
"source": [
"We compare this to the planewave space \n",
"\n",
"we choose the operators\n",
"\n",
"$$\n",
"\\begin{align*}\n",
"\\calL=-\\Delta u -\\omega^2 u && \\tilde\\calL=-\\Delta u\n",
" \\IT^p=\\{e^{-i\\omega(d_j\\cdot \\bx)},\\ j=-p,\\dots,p\\}.\n",
"\\end{align*}\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d6d31794",
"id": "9964431c",
"metadata": {},
"outputs": [],
"source": [
"from ngsolve import *\n",
"from ngstrefftz import *\n",
"from netgen.geom2d import unit_square\n",
"from netgen.csg import unit_cube"
"order = 3\n",
"mesh = Mesh(unit_square.GenerateMesh(maxh=1))\n",
"fes = trefftzfespace(mesh, order=order, eq=\"helmholtz\",dgjumps=True,complex=True)\n",
"gfshow = GridFunction(fes, multidim=0)\n",
"gf = GridFunction(fes)\n",
"for i in range(fes.ndof):\n",
" gf.vec[:] = 0\n",
" gf.vec[i] = 1\n",
" gfshow.AddMultiDimComponent(gf.vec)\n",
"Draw (gfshow, mesh, interpolate_multidim=False, animate=False, autoscale=False, min=0.8,max=1, settings={\"subdivision\":20})"
]
},
{
Expand All @@ -91,7 +171,8 @@
"outputs": [],
"source": [
"mesh = Mesh(unit_square.GenerateMesh(maxh=.3))\n",
"fes = L2(mesh, order=4, complex=True, dgjumps=True)\n",
"order = 4\n",
"fes = L2(mesh, order=order, complex=True, dgjumps=True)\n",
"u,v = fes.TnT()"
]
},
Expand All @@ -110,6 +191,24 @@
"eps = 10**-7"
]
},
{
"cell_type": "markdown",
"id": "6111befd",
"metadata": {},
"source": [
"We consider the DG-scheme given by\n",
"\n",
"$$\n",
"\\begin{align}\n",
" a_h(u,v) &= \\sum_{K\\in\\Th}\\int_K \\nabla u\\nabla v-\\omega^2 uv\\ dV\n",
" -\\int_{\\Fh^\\text{int}}\\left(\\avg{\\nabla u}\\jump{v}+\\jump{u} \\avg{\\overline{\\nabla v}} \\right) dS \\nonumber \\\\ \n",
" &\\qquad+\\int_{\\Fh^\\text{int}} \\left( i\\alpha \\omega\\jump{u}\\jump{\\overline{v}} - \\frac{\\beta}{i\\omega}\\jump{\\nabla u}\\jump{\\overline{\\nabla v}} \\right) dS -\\int_{\\Fh^\\text{bnd}}\\delta\\left(\\nx\\cdot\\nabla u \\overline{v}+u \\overline{\\nx\\cdot\\nabla v}\\right) dS\\\\ \\nonumber\n",
" &\\qquad+\\int_{\\Fh^\\text{bnd}} \\left( i(1-\\delta)\\omega{u}{\\overline{v}} - \\frac{\\delta}{i\\omega}{\\nabla u}{\\overline{\\nabla v}} \\right) dS \\\\ \n",
" \\ell(v) &= \\int_{\\Fh^\\text{bnd}}\\left( (1-\\delta)g\\overline{v} - \\frac{\\delta}{i\\omega}g\\overline{\\nx\\cdot\\nabla v}\\right) dS\n",
"\\end{align}\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -157,10 +256,7 @@
"metadata": {},
"outputs": [],
"source": [
"Lap = lambda u : sum(Trace(u.Operator('hesse')))\n",
"op = (-u)*(Lap(v))*dx + (-Lap(u))*(Lap(v))*dx\n",
"eps = 10**-8\n",
"PP = TrefftzEmbedding(op,fes,eps)\n",
"PP = TrefftzHelmholtzEmb(fes)\n",
"PPT = PP.CreateTranspose()\n",
"with TaskManager():\n",
" TA = [email protected]@PP\n",
Expand Down
114 changes: 60 additions & 54 deletions docs/notebooks/embTrefftz-poi.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,28 @@
"id": "0514a4c5",
"metadata": {},
"source": [
"# Embedded Trefftz-DG: Poisson \n",
"We consider the Poisson equation with Dirichlet boundary conditions\n",
"# Embedded Trefftz-DG: Poisson \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c2c9259a",
"metadata": {},
"outputs": [],
"source": [
"from ngsolve import *\n",
"from ngstrefftz import *\n",
"from netgen.occ import *\n",
"SetNumThreads(4)"
]
},
{
"cell_type": "markdown",
"id": "c1f05d62",
"metadata": {},
"source": [
"We consider the Poisson equation with Dirichlet boundary conditions.\n",
"\n",
"$$\n",
"\\newcommand{\\Th}{{\\mathcal{T}_h}} \n",
Expand Down Expand Up @@ -42,47 +62,7 @@
"\\end{align*}\n",
"$$\n",
"\n",
"For $u_{h,f}$ a particular solution, we are looking for a solution $u_h\\in\\IT^p(\\Th) + u_{h,f}$ so that\n",
"\n",
"$$\n",
"\\begin{equation} \\label{eq:inhom}\n",
" a_h(u_{h},v_{\\IT})\n",
" =\\ell(v_{\\IT}) ~~ \\forall~ v_{\\IT}\\in\\IT^p(\\Th).\n",
"\\end{equation}\n",
"$$\n",
"\n",
"After homogenization this means that we are looking for\n",
"$u_{\\IT}\\in\\IT^p(\\Th)$ that (uniquely) solves \n",
"\n",
"$$\n",
"\\begin{equation}\n",
" a_h(u_{\\IT},v_{\\IT})\n",
" =\\ell(v_{\\IT}) - a_h(u_{h,f},v_{\\IT}) ~~ \\forall~ v_{\\IT}\\in\\IT^p(\\Th).\n",
"\\end{equation}\n",
"$$\n",
"\n",
"This translates to the solution of the linear system \n",
"\n",
"$$\n",
"\\begin{align}\n",
" \\bT^T\\bA\\bT \\bu_{\\IT} = \\bT^T (\\bl-\\bA\\bu_f).\n",
"\\end{align}\n",
"$$\n",
"\n",
"To compute a (local) particular solution, on an element $K\\in \\mathcal{T}_h$ we assemble $(\\bw_K)_{i}=(f,\\calG(\\be_i))=(f,\\phi_i)$ and define $(\\bu_{f})_K=\\bW^\\dagger_K \\bw_K.$\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c2c9259a",
"metadata": {},
"outputs": [],
"source": [
"from ngsolve import *\n",
"from ngstrefftz import *\n",
"from netgen.csg import unit_cube\n",
"SetNumThreads(4)"
"We consider again the SIP-DG method"
]
},
{
Expand All @@ -97,24 +77,15 @@
"fes = L2(mesh, order=order, dgjumps=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e74507e1",
"metadata": {},
"outputs": [],
"source": [
"exact = sin(x)*sin(y)*sin(z)\n",
"rhs = 3*sin(x)*sin(y)*sin(z)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ff91edb0",
"metadata": {},
"outputs": [],
"source": [
"exact = sin(x)*sin(y)*sin(z)\n",
"rhs = 3*sin(x)*sin(y)*sin(z)\n",
"bndc=exact\n",
"alpha = 4\n",
"n = specialcf.normal(mesh.dim)\n",
Expand Down Expand Up @@ -143,6 +114,41 @@
" f.Assemble()"
]
},
{
"cell_type": "markdown",
"id": "83e7f61e",
"metadata": {},
"source": [
"For $u_{h,f}$ a particular solution, we are looking for a solution $u_h\\in\\IT^p(\\Th) + u_{h,f}$ so that\n",
"\n",
"$$\n",
"\\begin{equation} \\label{eq:inhom}\n",
" a_h(u_{h},v_{\\IT})\n",
" =\\ell(v_{\\IT}) ~~ \\forall~ v_{\\IT}\\in\\IT^p(\\Th).\n",
"\\end{equation}\n",
"$$\n",
"\n",
"After homogenization this means that we are looking for\n",
"$u_{\\IT}\\in\\IT^p(\\Th)$ that (uniquely) solves \n",
"\n",
"$$\n",
"\\begin{equation}\n",
" a_h(u_{\\IT},v_{\\IT})\n",
" =\\ell(v_{\\IT}) - a_h(u_{h,f},v_{\\IT}) ~~ \\forall~ v_{\\IT}\\in\\IT^p(\\Th).\n",
"\\end{equation}\n",
"$$\n",
"\n",
"This translates to the solution of the linear system \n",
"\n",
"$$\n",
"\\begin{align}\n",
" \\bT^T\\bA\\bT \\bu_{\\IT} = \\bT^T (\\bl-\\bA\\bu_f).\n",
"\\end{align}\n",
"$$\n",
"\n",
"To compute a (local) particular solution, on an element $K\\in \\mathcal{T}_h$ we assemble $(\\bw_K)_{i}=(f,\\calG(\\be_i))=(f,\\phi_i)$ and define $(\\bu_{f})_K=\\bW^\\dagger_K \\bw_K.$"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
2 changes: 1 addition & 1 deletion docs/notebooks/embTrefftz-stokes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"source": [
"from ngsolve import *\n",
"from ngstrefftz import *\n",
"from netgen.geom2d import unit_square\n",
"from netgen.occ import *\n",
"from ngsolve.webgui import Draw\n",
"SetNumThreads(4)\n",
"nu=1"
Expand Down
5 changes: 2 additions & 3 deletions docs/notebooks/embTrefftz-wave.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
"source": [
"from ngsolve import *\n",
"from ngstrefftz import *\n",
"from netgen.geom2d import unit_square\n",
"from netgen.occ import *\n",
"from ngsolve.TensorProductTools import MakeTensorProductMesh, SegMesh\n",
"from ngsolve import *\n",
"from ngsolve.webgui import Draw\n",
"import time"
"from ngsolve.webgui import Draw"
]
},
{
Expand Down
Loading

0 comments on commit 93deb77

Please sign in to comment.