{
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "display_name": "OneSimulation (venv)",
   "language": "python",
   "name": "onesim"
  },
  "language_info": {
   "name": "python"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Monoconical antenna on a finite ground plane\n",
    "\n",
    "A cone standing with its tip on a ground plane is a broadband antenna: the cone and the plane form a\n",
    "conical transmission line that guides a spherical TEM wave outward from the feed with the\n",
    "characteristic impedance $Z_c=60\\ln\\cot(\\theta_0/2)$ for the half angle $\\theta_0$. A half angle of\n",
    "47° gives $Z_c\\approx50\\ \\Omega$, so the antenna matches a 50 Ω generator once the cone is long\n",
    "compared with the wavelength. A finite cone and ground disc reflect part of the wave and tilt the\n",
    "radiation pattern.\n",
    "\n",
    "Here the cone has a slant length of 100 mm and a flat top; the ground disc has a radius of 150 mm.\n",
    "The generator drives the gap of 2 mm between the ground and the cone tip, across a thin feed wire.\n",
    "Everything is symmetric about the axis, and the TEM wave has only $E_r$, $E_z$ and $H_\\varphi$:\n",
    "OneSimulation solves the meridian plane with `polarization=\"Erphiz\"` and `azimuthal_mode=0`, and\n",
    "`LumpedPort` feeds the gap with 1 W from a 50 Ω generator.\n",
    "\n",
    "| boundary | condition |\n",
    "|---|---|\n",
    "| `port` (feed wire surface) | lumped port, 50 Ω, 1 W available |\n",
    "| `metal` (cone, ground disc) | perfect conductor |\n",
    "| `outer` (box walls) | perfect conductor behind a spherical PML from 350 to 450 mm |\n",
    "\n",
    "The air comes from `conical_monopole_antenna.step`, written by `conical_monopole_antenna_geometry.py`\n",
    "with the CAD objects of OneSimulation.\n",
    "\n",
    "**Reference.** (i) The characteristic impedance of the infinite conical line over an infinite\n",
    "plane, $Z_c=60\\ln\\cot(\\theta_0/2)=50.0\\ \\Omega$ (Schelkunoff, *Electromagnetic Waves*, 1943; Balanis,\n",
    "*Antenna Theory*, 4th ed., §9.3); the impedance of the finite antenna oscillates about it, less and\n",
    "less as the cone grows longer than the wavelength. (ii) Energy balance: the power radiated through\n",
    "the far field, $P_\\mathrm{rad}=\\frac{1}{2\\eta_0}\\oint|\\mathbf F|^2\\,\\mathrm d\\Omega$, equals the\n",
    "power the antenna accepts from the generator, $P_\\mathrm{acc}=(1-|S_{11}|^2)\\cdot1$ W."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "import numpy as np\n",
    "from onesim.cad.exchange import load\n",
    "from onesim.materials import VACUUM_PERMEABILITY, VACUUM_PERMITTIVITY\n",
    "from onesim.materials.electromagnetic import (\n",
    "    IsotropicElectromagneticMaterial,\n",
    "    spherical_pml,\n",
    ")\n",
    "from onesim.meshing import TriangularMesher\n",
    "from onesim.numerics import Numerics\n",
    "from onesim.numerics.fem import FiniteElement, Nedelec, RevolvedH1SpaceFactory\n",
    "from onesim.numerics.linear import ComplexSparseDirect\n",
    "from onesim.physics.electromagnetics import (\n",
    "    FrequencyDomainMaxwell,\n",
    "    LumpedPort,\n",
    "    PerfectElectricConductor,\n",
    "    axisymmetric_far_field,\n",
    "    input_impedance,\n",
    ")\n",
    "from onesim.simulation import Problem, Simulation\n",
    "from onesim.visualization import plots, theme\n",
    "\n",
    "theme.set()\n",
    "\n",
    "light = 1 / np.sqrt(VACUUM_PERMEABILITY * VACUUM_PERMITTIVITY)\n",
    "eta = np.sqrt(VACUUM_PERMEABILITY / VACUUM_PERMITTIVITY)"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Geometry and mesh\n",
    "\n",
    "`section` with the plane $y=0$ gives the meridian plane ($x=r$). Quadratic triangles of 7 mm\n",
    "(eight per wavelength at 5 GHz), refined to 0.7 mm at the feed gap and 2 mm on the cone."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "air = load(\"conical_monopole_antenna.step\")\n",
    "meridian = air.section((0, 1, 0))\n",
    "faces = meridian.boundaries\n",
    "mesh = TriangularMesher(\n",
    "    target_size=0.007, local_sizes={\"port\": 0.0007, \"metal\": 0.002}, grading=0.25\n",
    ").generate(meridian)\n",
    "\n",
    "fig = plots.figure(1, 2, size=(14, 6.5))\n",
    "plots.geometry(\n",
    "    air,\n",
    "    selection={\"feed\": air.boundaries.port, \"cone and ground\": air.boundaries.metal},\n",
    "    labels=False,\n",
    "    camera=plots.Camera(elevation=20, azimuth=-60),\n",
    "    title=\"Air around the antenna, half (STEP)\",\n",
    "    ax=fig[0],\n",
    ")\n",
    "plots.mesh(\n",
    "    mesh,\n",
    "    groups=(\"all\",),\n",
    "    legend=False,\n",
    "    title=f\"Meridian plane: {mesh.cell_count} quadratic triangles\",\n",
    "    ax=fig[1],\n",
    ")\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model\n",
    "\n",
    "One solve per frequency from 1 to 5 GHz in steps of 0.25 GHz."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "model = lambda frequency: FrequencyDomainMaxwell(\n",
    "    domain=meridian.domain(),\n",
    "    frequency=frequency,\n",
    "    material=spherical_pml(IsotropicElectromagneticMaterial(), 0.35, 0.45),\n",
    "    polarization=\"Erphiz\",\n",
    "    azimuthal_mode=0,\n",
    "    phasor_convention=\"exp(-i*omega*t)\",\n",
    "    amplitude_convention=\"peak\",\n",
    ")\n",
    "numerics = Numerics(\n",
    "    spatial=FiniteElement(element=Nedelec(order=2), provider=RevolvedH1SpaceFactory()),\n",
    "    linear_solver=ComplexSparseDirect(),\n",
    ")\n",
    "solve = lambda m: Simulation(\n",
    "    problem=Problem(\n",
    "        name=\"Monocone\",\n",
    "        geometry=meridian,\n",
    "        physics=(m,),\n",
    "        boundary_conditions=(\n",
    "            LumpedPort(\n",
    "                physics=m, boundary=faces.port, impedance=50.0, incident_power=1.0\n",
    "            ),\n",
    "            PerfectElectricConductor(physics=m, boundary=faces.metal | faces.outer),\n",
    "        ),\n",
    "    ),\n",
    "    mesh=mesh,\n",
    "    numerics=numerics,\n",
    ").solve_stationary()\n",
    "frequencies = np.arange(1.0e9, 5.01e9, 0.25e9)\n",
    "runs = {f: (solve(m := model(f)), m) for f in frequencies}"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Results\n",
    "\n",
    "Input impedance (in the engineering convention $e^{+j\\omega t}$, the conjugate of the solver's\n",
    "phasors), reflection coefficient against 50 Ω, and the energy balance from the far field."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "impedance = np.array([np.conj(input_impedance(*runs[f])) for f in frequencies])\n",
    "reflection = np.abs((impedance - 50.0) / (impedance + 50.0))\n",
    "theta = np.linspace(0, np.pi, 181)\n",
    "radiated = np.array(\n",
    "    [\n",
    "        np.pi\n",
    "        / eta\n",
    "        * np.trapezoid(\n",
    "            np.sum(\n",
    "                np.abs(axisymmetric_far_field(*runs[f], theta, 0.0, radius=0.3)) ** 2,\n",
    "                axis=1,\n",
    "            )\n",
    "            * np.sin(theta),\n",
    "            theta,\n",
    "        )\n",
    "        for f in frequencies\n",
    "    ]\n",
    ")\n",
    "characteristic = 60 * np.log(1 / np.tan(np.radians(47.0) / 2))\n",
    "print(f\"characteristic impedance of the conical line: {characteristic:.2f} ohm\")\n",
    "print(\n",
    "    \"f [GHz]   slant / wavelength   Z_in [ohm]            |S11|     P_rad [W]   P_acc [W]   difference\"\n",
    ")\n",
    "for f, z, s, p in zip(frequencies, impedance, reflection, radiated):\n",
    "    print(\n",
    "        f\"{f / 1e9:7.2f} {0.1 * f / light:18.2f}   {z.real:7.2f} {z.imag:+8.2f}j {s:10.4f} {p:11.4f} {1 - s**2:11.4f} {p / (1 - s**2) - 1:+11.1e}\"\n",
    "    )\n",
    "long = frequencies >= 3e9\n",
    "print(\n",
    "    f\"above 3 GHz (cone longer than one wavelength): mean resistance {impedance[long].real.mean():.1f} ohm, mean reactance {impedance[long].imag.mean():+.1f} ohm, \"\n",
    "    f\"largest |S11| {reflection[long].max():.3f}\"\n",
    ")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Figure\n",
    "\n",
    "Left: the input impedance against frequency with the characteristic impedance of the conical line.\n",
    "Right: the radiation pattern $|\\mathbf F|^2$ in the elevation plane at 1.5, 3 and 5 GHz, normalized\n",
    "to its maximum; the finite ground plane lets part of the power radiate below the horizon."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "fig = plots.figure(1, 2, size=(16, 6))\n",
    "plots.line(\n",
    "    frequencies / 1e9,\n",
    "    {\"resistance\": impedance.real, \"reactance\": impedance.imag},\n",
    "    markers=True,\n",
    "    hlines={\"Z_c of the conical line\": characteristic},\n",
    "    xlabel=\"frequency [GHz]\",\n",
    "    ylabel=\"input impedance [ohm]\",\n",
    "    title=\"Input impedance\",\n",
    "    legend=\"lower right\",\n",
    "    ax=fig[0],\n",
    ")\n",
    "plots.line(\n",
    "    90 - np.degrees(theta),\n",
    "    {\n",
    "        f\"{f / 1e9:.1f} GHz\": (lambda p: p / p.max())(\n",
    "            np.sum(\n",
    "                np.abs(axisymmetric_far_field(*runs[f], theta, 0.0, radius=0.3)) ** 2,\n",
    "                axis=1,\n",
    "            )\n",
    "        )\n",
    "        for f in (1.5e9, 3.0e9, 5.0e9)\n",
    "    },\n",
    "    xlabel=\"elevation above the ground plane [°]\",\n",
    "    ylabel=\"|F|^2 / max\",\n",
    "    title=\"Radiation pattern\",\n",
    "    legend=\"upper left\",\n",
    "    ax=fig[1],\n",
    ")\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Going further\n",
    "\n",
    "Change the half angle in `conical_monopole_antenna_geometry.py`: 30° raises the characteristic\n",
    "impedance to about 79 Ω. A larger ground disc pulls the main lobe down towards the horizon."
   ]
  }
 ]
}
