{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "efb1606299b8",
   "metadata": {},
   "source": [
    "# Reversing the force on a leaky dielectric sphere\n",
    "Place a small lossy sphere in a weakly nonuniform alternating electric field. Change the particle conductivity and decide whether high- and low-frequency forces point toward the same region."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e45cdc8f4e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "particle_er, medium_er = 60., 10.\n",
    "particle_sigma, medium_sigma = .002, .010    # S/m\n",
    "radius_um = 5.\n",
    "grad_rms_squared = 1e12                    # V²/m³\n",
    "assert min(particle_er,medium_er,particle_sigma,medium_sigma,radius_um)>0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b420159b5ae",
   "metadata": {},
   "source": [
    "## Match total normal current and potential\n",
    "The complex permittivity includes conduction for an exp(iωt) convention. The electric field is quoted as an rms amplitude. Keeping this convention avoids a factor of two in the time-averaged force."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12c23ba8030b",
   "metadata": {},
   "outputs": [],
   "source": [
    "eps0=8.8541878128e-12; ep,em=particle_er*eps0,medium_er*eps0\n",
    "low=(particle_sigma-medium_sigma)/(particle_sigma+2*medium_sigma)\n",
    "high=(ep-em)/(ep+2*em)\n",
    "tau=(ep+2*em)/(particle_sigma+2*medium_sigma)\n",
    "fc=np.sqrt(-low/high)/(2*np.pi*tau) if low*high<0 else np.nan\n",
    "freq=np.geomspace(.005/(2*np.pi*tau),200/(2*np.pi*tau),240)\n",
    "omega=2*np.pi*freq\n",
    "K=((ep-em)-1j*(particle_sigma-medium_sigma)/omega)/((ep+2*em)-1j*(particle_sigma+2*medium_sigma)/omega)\n",
    "F=2*np.pi*em*(radius_um*1e-6)**3*grad_rms_squared*K.real\n",
    "print(\"Low/high contrast\",low,high,\". Crossover frequency (Hz)\",fc)\n",
    "assert np.allclose(K.real,(low+high*(omega*tau)**2)/(1+(omega*tau)**2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5afc89f43f68",
   "metadata": {},
   "source": [
    "## Inspect a zero against both limiting contrasts\n",
    "A zero in the real part erases the leading gradient force even though the imaginary part and dissipation can remain finite. Change the medium conductivity until the root disappears."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95cd73741efd",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig,ax=plt.subplots(1,2,figsize=(9,3.9),layout=\"constrained\")\n",
    "ax[0].semilogx(freq/1e6,K.real,label=\"Real part\",color=\"#175868\");ax[0].semilogx(freq/1e6,K.imag,label=\"Imaginary part\",color=\"#a23d21\",ls=\"--\")\n",
    "ax[0].set(xlabel=\"Frequency (MHz)\",ylabel=\"Dipole contrast\",title=\"Electrical response\");ax[0].legend(fontsize=9)\n",
    "ax[1].semilogx(freq/1e6,F*1e15,color=\"#375b9a\");ax[1].set(xlabel=\"Frequency (MHz)\",ylabel=\"Force along positive gradient (fN)\",title=\"Time-averaged gradient force\")\n",
    "for a in ax:\n",
    "    a.axhline(0,color=\"grey\",lw=.8)\n",
    "    if np.isfinite(fc): a.axvline(fc/1e6,color=\"grey\",ls=\":\")\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
