{
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3"
  },
  "language_info": {
   "name": "python"
  },
  "fundamentals": {
   "title": "Weighing a small deposit with a piezoelectric resonator",
   "lead": "Two nearby frequencies appear in the electrical response of one mechanical mode. Derive the series and parallel resonances, then use their different sensitivities to recognise added mass and stray capacitance.",
   "unit_slug": "mass-loading-a-piezoelectric-resonator"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6ee3b6ad322f",
   "metadata": {},
   "source": [
    "# Weighing a small deposit with a piezoelectric resonator\n",
    "\n",
    "Two nearby frequencies appear in the electrical response of one mechanical mode. Derive the series and parallel resonances, then use their different sensitivities to recognise added mass and stray capacitance."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bfaaf47ac1ad",
   "metadata": {
    "fundamentals": {
     "section": "derive-the-model"
    }
   },
   "source": [
    "## Convert mechanical motion into a terminal admittance\n",
    "\n",
    "A piezoelectric plate converts terminal voltage into force and velocity into current. With a linear reciprocal coupling coefficient $n$, $F=nV$ and $I_m=nv$. The mechanical velocity impedance is $b+i\\omega m+k/(i\\omega)$. Dividing it by $n^2$ maps it to a series branch with $R_m=b/n^2$, $L_m=m/n^2$ and $C_m=n^2/k$. The electrodes also have a static capacitance $C_0$ in parallel. Thus\n",
    "\n",
    "$$Y(\\omega)=i\\omega C_0+\\left[R_m+i\\omega L_m+\\frac{1}{i\\omega C_m}\\right]^{-1}.$$\n",
    "\n",
    "For negligible loss the series branch impedance vanishes at $\\omega_s=1/\\sqrt{L_mC_m}$. Just above this frequency its susceptance is inductive and can cancel the parallel capacitance. Setting $Y=0$ gives\n",
    "\n",
    "$$\n",
    "\\omega_p=\\omega_s\\sqrt{1+C_m/C_0}.\n",
    "$$\n",
    "\n",
    "With finite damping a peak and a dip replace the singular limits. Their extrema need not coincide exactly with the lossless frequencies."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "992f3929367f",
   "metadata": {
    "fundamentals": {
     "section": "worked-prediction"
    }
   },
   "source": [
    "## Isolate the perturbation\n",
    "\n",
    "A thin rigid deposit that changes only the effective mass multiplies $L_m$ by $1+\\eta$. Both ideal frequencies scale as $(1+\\eta)^{-1/2}$; their ratio is unchanged. Additional cable capacitance increases $C_0$, changing the ratio while leaving the mechanical series frequency fixed. At a load fraction of 0.01 the expected series shift is about minus 0.496 percent. A viscoelastic deposit can also dissipate energy and alter stiffness, so the two-frequency test includes a physical assumption about the coating.\n",
    "For $f_s=100.000$ kHz, $C_m=0.25$ pF and $C_0=15$ pF, the unloaded parallel frequency is 100.830 kHz. A 1% increase in effective mass puts the series resonance at 99.504 kHz. If the same shift came from extra cable capacitance alone, the series frequency would remain at 100.000 kHz. At quality factor 600 the approximate unloaded resonance linewidth is $f_s/Q=167$ Hz. The predicted mass shift spans about three linewidths. The height of the peak by itself would confound damping and transduction.\n",
    "\n",
    "Conversely, fit the two frequencies and infer the capacitance ratio as $(f_p/f_s)^2-1$. A small random error in $f_p/f_s$ has a large relative effect on this estimate when the resonances nearly coincide. Zoom the frequency grid and compare the uncertainty from bin spacing with the shift under investigation."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4dd5b7efb0f",
   "metadata": {
    "fundamentals": {
     "section": "choose-inputs",
     "notebook_only": true
    }
   },
   "source": [
    "## Set the experimental inputs\n",
    "\n",
    "Edit these values, then run the calculation and both figures again."
   ]
  },
  {
   "cell_type": "code",
   "id": "a94fbdcaf346",
   "metadata": {},
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "teal, orange, blue = \"#19596a\", \"#aa462a\", \"#3868a0\"\n",
    "def axes(xlabel, ylabel):\n",
    "    fig, ax = plt.subplots(figsize=(7, 4), constrained_layout=True)\n",
    "    ax.set(xlabel=xlabel, ylabel=ylabel)\n",
    "    ax.grid(alpha=0.2)\n",
    "    return fig, ax"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "52a43b915706",
   "metadata": {},
   "source": [
    "series_hz = 100_000.\n",
    "motional_pf = .25\n",
    "parallel_pf = 15.\n",
    "quality = 600.\n",
    "mass_fraction = .01"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "bc59ed983381",
   "metadata": {},
   "source": [
    "cm,c0=motional_pf*1e-12,parallel_pf*1e-12\n",
    "assert min(cm,c0,series_hz,quality)>0 and mass_fraction>=0\n",
    "lm=1/((2*np.pi*series_hz)**2*cm)\n",
    "rm=2*np.pi*series_hz*lm/quality\n",
    "def response(f,eta,c):\n",
    "    w=2*np.pi*f\n",
    "    return 1j*w*c+1/(rm+1j*w*lm*(1+eta)+1/(1j*w*cm))\n",
    "fs=series_hz/np.sqrt(1+mass_fraction)\n",
    "fp=fs*np.sqrt(1+cm/c0)\n",
    "print(f\"Loaded lossless frequencies {fs:.2f} Hz and {fp:.2f} Hz\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "2e911dabb465",
   "metadata": {
    "fundamentals": {
     "figure": {
      "file": "electrodynamics-coupled-field-problems-1.png",
      "alt": "The two terminal-admittance features shift together when additional effective mass is attached.",
      "caption": "A terminal sweep contains the mechanical resonance and the electrical cancellation. Follow both features before inferring added mass."
     }
    }
   },
   "source": [
    "f=np.linspace(.975*min(fs,series_hz),1.025*max(fp,series_hz*np.sqrt(1+cm/c0)),1200)\n",
    "fig,ax=axes(\"Frequency (kHz)\",\"Terminal admittance magnitude (µS)\")\n",
    "for eta,colour in [(0,teal),(mass_fraction,orange)]:\n",
    "    ax.semilogy(f/1e3,abs(response(f,eta,c0))*1e6,label=f\"Added mass {100*eta:g}%\",color=colour)\n",
    "ax.legend()\n",
    "plt.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "6ceaee69361b",
   "metadata": {
    "fundamentals": {
     "notebook_only": true
    }
   },
   "source": [
    "A terminal sweep contains the mechanical resonance and the electrical cancellation. Follow both features before inferring added mass."
   ]
  },
  {
   "cell_type": "code",
   "id": "855f0b0b9c0b",
   "metadata": {
    "fundamentals": {
     "figure": {
      "file": "electrodynamics-coupled-field-problems-2.png",
      "alt": "A map of series and parallel resonance frequencies separates a mass perturbation from a capacitance perturbation.",
      "caption": "Changing cable capacitance displaces the parallel resonance at fixed series frequency. Mass loading moves both frequencies along a diagonal path."
     }
    }
   },
   "source": [
    "eta=np.linspace(0,.04,120)\n",
    "fseries=series_hz/np.sqrt(1+eta)\n",
    "fig,ax=axes(\"Series resonance (kHz)\",\"Parallel resonance (kHz)\")\n",
    "for scale,colour in [(.7,orange),(1,teal),(1.5,blue)]:\n",
    "    ax.plot(fseries/1e3,fseries*np.sqrt(1+cm/(scale*c0))/1e3,label=f\"C₀ × {scale:g}\",color=colour)\n",
    "ax.legend()\n",
    "plt.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "9066439df61d",
   "metadata": {
    "fundamentals": {
     "notebook_only": true
    }
   },
   "source": [
    "Changing cable capacitance displaces the parallel resonance at fixed series frequency. Mass loading moves both frequencies along a diagonal path."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14e4acba70ee",
   "metadata": {
    "fundamentals": {
     "section": "problem-1"
    }
   },
   "source": [
    "## Exercise · recover a capacitance ratio\n",
    "\n",
    "A nearly lossless device gives $f_p/f_s=1.01$. What ratio of motional to static capacitance is consistent with it?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a09c5cb9c890",
   "metadata": {
    "fundamentals": {
     "solution": true
    }
   },
   "source": [
    "Squaring and subtracting one gives $C_m/C_0=0.0201$. The difference between the squares matters when the shift is used for accurate capacitance inference."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ef7ab23db87",
   "metadata": {
    "fundamentals": {
     "section": "problem-2"
    }
   },
   "source": [
    "## Exercise · challenge a mass reading\n",
    "\n",
    "After changing a fixture the dip moves but the series frequency remains within the experimental uncertainty. Does that support a pure mass increase?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22b4a07a5d74",
   "metadata": {
    "fundamentals": {
     "solution": true
    }
   },
   "source": [
    "A purely inertial change shifts both resonance frequencies. The observed pattern is consistent with an increased shunt capacitance if the dip moved toward the series peak. A sweep of the linewidth and an independent capacitance check would test the model."
   ]
  }
 ]
}
