{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "# 11.1.3 Layer potentials\n",
    "\n",
    "Let $G(x,y) = \\frac{1}{4 \\pi} \\frac{\\exp (i k |x-y|)}{|x-y|}$ be Green's function for the Helmholtz equation. For a surface $\\Gamma$, and a scalar function $\\rho$ defined on $\\Gamma$, we define the single layer potential as\n",
    "\n",
    "$$\n",
    "SL (\\rho) (x) = \\int_{\\Gamma} G(x,y) \\rho(y) dy\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ngsolve import *\n",
    "from netgen.occ import *\n",
    "from ngsolve.webgui import Draw\n",
    "\n",
    "# the boundary Gamma\n",
    "face = WorkPlane(Axes((0,0,0), -Y, Z)).RectangleC(1,1).Face()\n",
    "mesh = Mesh(OCCGeometry(face).GenerateMesh(maxh=0.1))\n",
    "Draw (mesh);\n",
    "\n",
    "# the visualization mesh\n",
    "visplane = WorkPlane(Axes((0,0,0), Z, X)).RectangleC(5,5).Face()\n",
    "vismesh = Mesh(OCCGeometry(visplane).GenerateMesh(maxh=0.1))\n",
    "Draw (vismesh);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ngsolve.bem import SingularMLExpansionCF, RegularMLExpansionCF"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3",
   "metadata": {},
   "source": [
    "We evaluate the single layer integral using numerical integration on the surface mesh. Thus, we get a sum of many Green's functions, which is compressed using a multilevel-multipole. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "kappa = 3*pi\n",
    "S = SingularMLExpansionCF((0,0,0), r=3, kappa=kappa)\n",
    "\n",
    "ir = IntegrationRule(TRIG,20)\n",
    "pnts = mesh.MapToAllElements(ir, BND)\n",
    "# vals = (20*x)(pnts).flatten()\n",
    "vals = CF(1)(pnts).flatten()\n",
    "\n",
    "# find the integration weights:  sqrt(F^T F)*weight_ref\n",
    "F = specialcf.JacobianMatrix(3,2)\n",
    "weightCF = sqrt(Det (F.trans*F))\n",
    "weights = weightCF(pnts).flatten()\n",
    "for j in range(len(ir)):\n",
    "    weights[j::len(ir)] *= ir[j].weight\n",
    "print (\"number of source points: \", len(pnts))\n",
    "for p,vi,wi in zip(pnts, vals, weights):\n",
    "    S.expansion.AddCharge ((x(p), y(p), z(p)), vi*wi)\n",
    "\n",
    "S.expansion.Calc()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "Draw (S, vismesh, min=-0.02,max=0.02, animate_complex=True, order=2);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6",
   "metadata": {},
   "source": [
    "We see that the single layer potential is continuous across the surface $\\Gamma$, but has a kink at $\\Gamma$. This shows that the normal derivative is jumping (exactly by $\\rho$)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "R = RegularMLExpansionCF(S, (0,0,0.00),r=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "Draw (R, vismesh, min=-0.02,max=0.02, animate_complex=True, order=2);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "Draw (S.real-R.real, vismesh, min=-1e-5,max=1e-5, animate_complex=False, order=2);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10",
   "metadata": {},
   "source": [
    "## Double layer potential\n",
    "\n",
    "the double layer potential is\n",
    " \n",
    "$$\n",
    "DL (\\rho) (x) = \\int_{\\Gamma} n_y \\cdot \\frac{\\partial G}{\\partial y}(x,y) \\rho(y) dy\n",
    "$$ \n",
    "\n",
    "the name comes from adding a charge density $\\tfrac{1}{2 \\varepsilon} \\rho$ at the offset surface $\\Gamma + \\varepsilon n$, and a second charge density $\\tfrac{-1}{2 \\varepsilon} \\rho$ at $\\Gamma - \\varepsilon n$, \n",
    "and passing to the limit, i.e.\n",
    "\n",
    "$$\n",
    "DL (\\rho) (x) = \\lim_{\\varepsilon \\rightarrow 0} \\int_{\\Gamma} \\frac{1}{2 \\varepsilon } (G(x,y+\\varepsilon n) - G(x,y-\\varepsilon n) \\big) \\rho(y) dy,\n",
    "$$ \n",
    "This pair of charges defines a charge dipole in normal direction."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "kappa = pi\n",
    "S = SingularMLExpansionCF((0,0,0), 2, kappa)\n",
    "\n",
    "ir = IntegrationRule(TRIG,5)\n",
    "pnts = mesh.MapToAllElements(ir, BND)\n",
    "vals = (20*x)(pnts).flatten()\n",
    "\n",
    "F = specialcf.JacobianMatrix(3,2)\n",
    "weightCF = sqrt(Det (F.trans*F))\n",
    "weights = weightCF(pnts).flatten()\n",
    "for j in range(len(ir)):\n",
    "    weights[j::len(ir)] *= ir[j].weight\n",
    "\n",
    "for p,vi,wi in zip(pnts, vals, weights):\n",
    "    S.expansion.AddDipole ((x(p), y(p), z(p)), (0,1,0), vi*wi)\n",
    "S.expansion.Calc()\n",
    "\n",
    "Draw (S.real, vismesh, min=-1,max=1, animate_complex=True, order=2)\n",
    "Draw (S, vismesh, min=-1,max=1, animate_complex=True, order=2);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12",
   "metadata": {},
   "source": [
    "Now we see that the double layer potential is discontinuous (with jump exactly equal to $\\rho$), and the normal derivative is continuous."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13",
   "metadata": {},
   "source": [
    "These layer potentials are the foundation for the boundary element method, see \n",
    "[ngbem](https://weggler.github.io/ngbem/intro.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14",
   "metadata": {},
   "source": [
    "## Charge Density\n",
    "\n",
    "Instead of adding all charges by hand, we can directly add a charge density to the multipole. This performs "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15",
   "metadata": {},
   "outputs": [],
   "source": [
    "kappa = 3*pi\n",
    "S = SingularMLExpansionCF((0,0,0), 2, kappa)\n",
    "\n",
    "S.expansion.AddChargeDensity(50, mesh.Boundaries(\".*\"))\n",
    "S.expansion.Calc()\n",
    "R = RegularMLExpansionCF(S, (0,0,0.00),r=5)\n",
    "\n",
    "# Draw (mp.real, vismesh, min=-1,max=1, animate_complex=True, order=2);\n",
    "Draw (R, vismesh, min=-1,max=1, animate_complex=True, order=2);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16",
   "metadata": {},
   "source": [
    "## Solving for the charge density\n",
    "\n",
    "Next, we want to find a charge density, such that a certain potential $u_0$ is reached at the shield:\n",
    "\n",
    "$$\n",
    "\\int_{\\Gamma} (S \\rho) v \\, ds = \\int u_0 v \\, ds \\qquad \\forall \\, v \\in L_2(\\Gamma)\n",
    "$$\n",
    "\n",
    "Since the single layer potential is already defined by an integral, the left hand side is now a double integral. \n",
    "\n",
    "The left hand side is actually a bilineaer form, and plugging in all basis functions (for $\\rho$ and for $v$) leads to a discretization matrix.\n",
    "\n",
    "In contrast to fem, there a couple of difficulties:\n",
    "* the matrix is dense\n",
    "* the integrals are singular\n",
    "\n",
    "The ngsolve.bem class `SingleLayerPotentialOperator` provides this discretization matrix as linear operator. It uses Sauter-Schwab numerical integration for the singular integrals, and the fast multipole method for the far field interaction.\n",
    "\n",
    "The new syntax allows to define the `SingleLayerPotentialOperator` by means of the single-layer potential `LaplaceSL`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ngsolve.bem import SingleLayerPotentialOperator, LaplaceSL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18",
   "metadata": {},
   "outputs": [],
   "source": [
    "fesL2 = SurfaceL2(mesh, order=4, dual_mapping=True)\n",
    "u,v = fesL2.TnT()\n",
    "\n",
    "with TaskManager():\n",
    "    # V = SingleLayerPotentialOperator(fesL2, intorder=10)\n",
    "    V = LaplaceSL(u*ds)*v*ds\n",
    "    lf = LinearForm(1*v*ds).Assemble()\n",
    "\n",
    "    pre = BilinearForm(u*v*ds).Assemble().mat.Inverse(inverse=\"sparsecholesky\")\n",
    "\n",
    "    inv = solvers.CGSolver(V.mat, pre, tol=1e-6, printrates=False, plotrates=True)\n",
    "\n",
    "    gfurho = GridFunction(fesL2)\n",
    "    gfurho.vec.data = inv * lf.vec"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19",
   "metadata": {},
   "source": [
    "The solution shows strong singularities at the boundary of the shield:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20",
   "metadata": {},
   "outputs": [],
   "source": [
    "Draw (gfurho, min=-10, max=10);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21",
   "metadata": {},
   "outputs": [],
   "source": [
    "S = SingularMLExpansionCF((0,0,0), 2, 1e-10)\n",
    "\n",
    "S.expansion.AddChargeDensity(gfurho, mesh.Boundaries(\".*\"))\n",
    "S.expansion.Calc()\n",
    "R = RegularMLExpansionCF(S, (0,0,0.00),r=5)\n",
    "\n",
    "Draw (R.real*CF((0,0,1)), vismesh, min=-1,max=1, order=2);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23",
   "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.13.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
