{
 "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": [
    "# Laser heating of a rotating silicon wafer\n",
    "\n",
    "In rapid thermal processing a laser heats a wafer locally. Here a wafer of 100 mm diameter and\n",
    "0.5 mm thickness turns at 6 revolutions per minute while the laser moves radially between 10 and\n",
    "40 mm from the centre and back within the 10 s of the run. Seen from the wafer, the spot\n",
    "travels on a spiral:\n",
    "\n",
    "$$\\mathbf p(t)=r_l(t)\\,\\big(\\cos\\omega t,\\,-\\sin\\omega t\\big),\\qquad\n",
    "  r_l(t)=25\\ \\mathrm{mm}-15\\ \\mathrm{mm}\\cdot\\cos\\frac{2\\pi t}{10\\ \\mathrm s},\\qquad \\omega=0.2\\pi\\ \\mathrm{1/s}.$$\n",
    "\n",
    "The absorbed laser power $P=50$ W arrives as a Gaussian surface flux of radius $w=3$ mm,\n",
    "\n",
    "$$q(\\mathbf x,t)=\\frac{P}{\\pi w^2}\\,e^{-|\\mathbf x-\\mathbf p(t)|^2/w^2},\\qquad\n",
    "  \\rho c\\,\\frac{\\partial T}{\\partial t}=\\nabla\\cdot(k\\nabla T),$$\n",
    "\n",
    "with silicon $k=148$ W/(m K), $\\rho=2330$ kg/m³, $c=712$ J/(kg K).\n",
    "\n",
    "| face | condition |\n",
    "|---|---|\n",
    "| `top` | laser flux $q(\\mathbf x,t)$ |\n",
    "| `insulated`: bottom and rim | insulated (short run in vacuum, radiation neglected) |\n",
    "| initial state | 20 °C |\n",
    "\n",
    "The wafer comes from the CAD file `laser_heating_rotating_wafer.step`; the script\n",
    "`laser_heating_rotating_wafer_geometry.py` builds it with the CAD objects of OneSimulation.\n",
    "\n",
    "**Reference.** With all losses neglected the energy balance is exact: the heat stored in the\n",
    "wafer equals the laser energy $Pt$, and the mean temperature rises linearly as $Pt/(\\rho cV)$.\n",
    "The spot stays well inside the wafer, so no part of the Gaussian is cut off."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "import numpy as np\n",
    "from onesim.cad.exchange import load\n",
    "from onesim.materials import IsotropicSolid\n",
    "from onesim.meshing import Geometric, disc_blocks, extrude, merge_blocks\n",
    "from onesim.numerics import Numerics\n",
    "from onesim.numerics.fem import FiniteElement, Lagrange\n",
    "from onesim.numerics.linear import SparseDirect\n",
    "from onesim.numerics.time import BDF, BackwardEuler\n",
    "from onesim.physics.heat_transfer import HeatConduction, HeatFlux\n",
    "from onesim.simulation import Problem, Simulation\n",
    "from onesim.simulation.initial_conditions import InitialValue\n",
    "from onesim.visualization import plots, theme\n",
    "\n",
    "theme.set()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Geometry and mesh\n",
    "\n",
    "`load` returns the wafer with its named faces. `disc_blocks` meshes its face with a square core\n",
    "and a ring of quadrilaterals, about 2.5 mm in size; one layer of quadratic hexahedra spans the\n",
    "thickness, through which the temperature hardly changes."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "wafer = load(\"laser_heating_rotating_wafer.step\")\n",
    "faces = wafer.boundaries\n",
    "radius, thickness = wafer.size[0] / 2, wafer.size[2]\n",
    "mesh = extrude(\n",
    "    merge_blocks(disc_blocks((0, 0), (radius,), 24, (14,), core=0.5), order=2),\n",
    "    thickness,\n",
    "    Geometric(1, ratio=1.0),\n",
    "    geometry=wafer,\n",
    ")\n",
    "\n",
    "view = plots.Camera(elevation=40, azimuth=-60)\n",
    "fig = plots.figure(1, 2, size=(14, 5.5))\n",
    "plots.geometry(\n",
    "    wafer,\n",
    "    selection={\"top, under the laser\": faces.top, \"insulated\": faces.insulated},\n",
    "    labels=False,\n",
    "    camera=view,\n",
    "    title=\"Wafer with its named faces\",\n",
    "    ax=fig[0],\n",
    ")\n",
    "plots.mesh(\n",
    "    mesh,\n",
    "    groups=(\"all\",),\n",
    "    legend=False,\n",
    "    camera=view,\n",
    "    title=f\"{mesh.cell_count} quadratic hexahedra\",\n",
    "    ax=fig[1],\n",
    ")\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model\n",
    "\n",
    "The laser is a `HeatFlux` whose value depends on position and time (a positive flux leaves the\n",
    "wafer, so the laser enters with a negative sign). Second-order BDF steps of 0.1 s move the spot\n",
    "by at most 2.6 mm, less than its radius."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "power, spot, initial, duration = 50.0, 0.003, 293.15, 10.0\n",
    "path_radius = lambda t: 0.025 - 0.015 * np.cos(2 * np.pi * t / duration)\n",
    "path = lambda t: (\n",
    "    path_radius(t) * np.cos(0.2 * np.pi * t),\n",
    "    -path_radius(t) * np.sin(0.2 * np.pi * t),\n",
    ")\n",
    "silicon = IsotropicSolid(\n",
    "    density=2330.0,\n",
    "    specific_heat_capacity=712.0,\n",
    "    thermal_conductivity=148.0,\n",
    "    youngs_modulus=1.3e11,\n",
    "    poisson_ratio=0.28,\n",
    "    thermal_expansion_coefficient=2.6e-6,\n",
    ")\n",
    "model = HeatConduction(domain=wafer.volume, material=silicon, heating=0.0)\n",
    "problem = Problem(\n",
    "    name=\"Laser on a rotating wafer\",\n",
    "    geometry=wafer,\n",
    "    physics=(model,),\n",
    "    boundary_conditions=(\n",
    "        HeatFlux(\n",
    "            boundary=faces.top,\n",
    "            physics=model,\n",
    "            value=lambda x, y, z, t: -power\n",
    "            / (np.pi * spot**2)\n",
    "            * np.exp(-((x - path(t)[0]) ** 2 + (y - path(t)[1]) ** 2) / spot**2),\n",
    "        ),\n",
    "        HeatFlux(boundary=faces.insulated, physics=model, value=0.0),\n",
    "    ),\n",
    "    initial_conditions=(InitialValue(model.temperature, initial),),\n",
    ")\n",
    "numerics = Numerics(\n",
    "    spatial=FiniteElement(element=Lagrange(order=2)),\n",
    "    linear_solver=SparseDirect(),\n",
    "    time_integrator=BDF(order=2, startup=BackwardEuler()),\n",
    ")\n",
    "temperature = (\n",
    "    Simulation(problem=problem, mesh=mesh, numerics=numerics)\n",
    "    .run(start_time=0.0, end_time=duration, time_step=0.1)\n",
    "    .field(model.temperature)\n",
    ")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Results\n",
    "\n",
    "Stored heat and mean temperature against the laser energy, and the temperature under the spot."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "capacity, volume = (\n",
    "    silicon.density * silicon.specific_heat_capacity,\n",
    "    np.pi * radius**2 * thickness,\n",
    ")\n",
    "times = temperature.times\n",
    "under_spot = np.array(\n",
    "    [np.ravel(temperature.evaluate([[*path(t), thickness]], time=t))[0] for t in times]\n",
    ")\n",
    "print(\n",
    "    \"time [s]   stored heat [J]   laser energy [J]   mean T [°C]   expected [°C]   T under the spot [°C]\"\n",
    ")\n",
    "for t in (1.0, 2.5, 5.0, 7.5, 10.0):\n",
    "    index = np.argmin(np.abs(times - t))\n",
    "    stored = capacity * (\n",
    "        temperature.integrate(time=times[index]).value - initial * volume\n",
    "    )\n",
    "    print(\n",
    "        f\"{times[index]:8.2f} {stored:17.3f} {power * times[index]:18.3f} {stored / (capacity * volume) + initial - 273.15:13.3f} \"\n",
    "        f\"{power * times[index] / (capacity * volume) + initial - 273.15:15.3f} {under_spot[index] - 273.15:21.2f}\"\n",
    "    )"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Figure\n",
    "\n",
    "The temperature of the top face at three instants, and the\n",
    "temperature under the spot and the mean temperature over time."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "fig = plots.figure(2, 2, size=(15, 12))\n",
    "for panel, instant in zip(range(3), (2.5, 5.0, 10.0)):\n",
    "    plots.surface(\n",
    "        temperature,\n",
    "        time=instant,\n",
    "        boundary=faces.top,\n",
    "        component=lambda T, position: T - 273.15,\n",
    "        limits=(20, 300),\n",
    "        contours=8,\n",
    "        subdivisions=2,\n",
    "        xlabel=\"x [m]\",\n",
    "        ylabel=\"y [m]\",\n",
    "        label=\"T [°C]\",\n",
    "        title=f\"Top face after {instant:.1f} s\",\n",
    "        ax=fig[panel],\n",
    "    )\n",
    "plots.line(\n",
    "    y={\n",
    "        \"under the moving spot\": (times, under_spot - 273.15),\n",
    "        \"mean of the wafer\": (\n",
    "            times,\n",
    "            np.array([temperature.integrate(time=t).value / volume for t in times])\n",
    "            - 273.15,\n",
    "        ),\n",
    "    },\n",
    "    reference={\n",
    "        \"mean expected from P t / (ρ c V)\": (\n",
    "            times,\n",
    "            power * times / (capacity * volume) + initial - 273.15,\n",
    "        )\n",
    "    },\n",
    "    xlabel=\"time [s]\",\n",
    "    ylabel=\"T [°C]\",\n",
    "    title=\"Temperature under the spot and mean temperature\",\n",
    "    legend=\"upper left\",\n",
    "    ax=fig[3],\n",
    ")\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Going further\n",
    "\n",
    "Add the radiation of both faces (`RadiativeHeatFlux`), spin the wafer faster and compare the\n",
    "uniformity of the heating, or replace the spiral by a raster scan of the laser."
   ]
  }
 ]
}
