This page was generated from unit-2.14-globalspaces/planewavecoupling.ipynb.

2.14 Coupling of FEM with plane waves

In this tutorial we define global basis functions via Python. As an example, we choose outgoing plane waves in a waveguide.

[1]:
from ngsolve import *
from netgen.occ import *
from ngsolve.webgui import Draw
[2]:
r1 = Rectangle(1,1).Face()
r1.edges.Min(Y).name = "dir"
r1.edges.Max(Y).name = "dir"
r1.edges.Min(X).name = "dir"
r1.edges.Max(X).name = "coupling"
r1.faces.name = "fem"
circ = Circle((0.4,0.7),0.02).Face()
circ.faces.name = "femsource"

circ2 = Circle((0.4,0.3),0.05).Face()
circ2.faces.name = "femobstacle"


r2 = MoveTo(1,0).Rectangle(3,1).Face()
r2.edges.Min(Y).name = "dir"
r2.edges.Max(Y).name = "dir"
r2.faces.name = "waves"
shape = Glue([r1-circ-circ2, circ,circ2,r2])

The left side is discretized by finite elements. On the right side we use (propagating and evanescent) plane waves. On the interface both fields are coupled by an ultra-weak variational formulation.

[3]:
Draw (shape);
[4]:
mesh = shape.GenerateMesh(dim=2, maxh=0.2).Curve(3)
Draw (mesh);

Outgoing plane waves:

\[\sin (k_{y,j} y) e^{i k_{x,j} x}\]

with \begin{eqnarray*} k_{y,j} & = & j \pi \\ k_{x,j} & = & \sqrt{\omega^2 - k_{y,j}^2} \end{eqnarray*}

For \(k_{y,j} < \omega\) the wave is propagating, otherwise \(k_{x,j}\) is imaginary, and the wave is evanescent.

Define them as a multi-dimensional CoefficientFunction:

[5]:
omega = 3.5*pi * (1+0j)

ky = [j*pi for j in range(1,5)]
kx = [sqrt(omega**2-kyi**2) for kyi in ky]
shapes = CF(tuple(sin(kyi*y)*exp(1j*kxi*x) for kyi,kxi in zip(ky,kx)))
dshapesx = shapes.Diff(x)
[6]:
Draw (shapes[2], mesh, animate_complex=True );

The GlobalSpace is a finite element space spanned by the given global basis functions. We add the normal derivative on the coupling boundary as an additional operator. The finite element space is defined on the left sub-domains only:

[7]:
from ngsolve.comp import GlobalSpace

fesfem = H1(mesh, order=4, definedon="fem.*", dirichlet="dir", complex=True)
feswaves = GlobalSpace (mesh, definedon="waves", basis = shapes)
feswaves.AddOperator("dn", BND, dshapesx)
fes = fesfem*feswaves
[8]:
fes.ndof, fesfem.ndof, feswaves.ndof
[8]:
(3093, 3089, 4)

Bilinear form:

\[\int_{\Omega_{FEM}} \nabla u \nabla v - \omega^2 \varepsilon u v dx + \int_{\gamma_{coupling}} - u \partial_n v_w - v \partial_n u_w + u_w \partial_n v_w\]
[9]:
(uf,uw), (vf,vw) = fes.TnT()
uwdx = uw.Operator("dn")
vwdx = vw.Operator("dn")

epsilon = mesh.MaterialCF( { "femobstacle" : 10 }, default=1)

a = BilinearForm(fes)
a += (grad(uf))*grad(vf)*dx("fem.*") - omega**2*epsilon*uf*vf*dx("fem.*")
a += (-uf*vwdx-vf*uwdx+uw.Trace()*vwdx)*ds("coupling")
a.Assemble()

f = LinearForm(1*vf*dx("femsource")).Assemble()
[10]:
gfu = GridFunction(fes)
gfu.vec.data = a.mat.Inverse(fes.FreeDofs()) * f.vec

gfu2 = mesh.MaterialCF( { "fem.*" : gfu.components[0], "waves" : gfu.components[1] })
Draw (gfu2, mesh, animate_complex=True );

The coefficients of the second component are the amplitudes of the outgoing waves:

[11]:
print (gfu.components[1].vec)
 (-0.000139805,6.85573e-05)
 (0.000144542,5.30687e-05)
 (9.0466e-05,-1.84194e-05)
 (0.00159941,0.000670395)