{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9fc467a22537",
   "metadata": {},
   "source": [
    "# When an unresolved binary looks stationary\n",
    "Change the companion's mass and brightness independently. The detector sees the centre of light in a projected circular orbit; the centre of mass remains fixed. Decide in advance when the astrometric signal will vanish."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb6d4f1878d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "mass_fraction = .30       # M2 / (M1 + M2)\n",
    "light_fraction = .10      # L2 / (L1 + L2)\n",
    "separation_au = 1.0\n",
    "distance_pc = 20.\n",
    "inclination_deg = 55.\n",
    "assert 0<mass_fraction<1 and 0<=light_fraction<=1 and distance_pc>0 and separation_au>0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bd404b8f3cb",
   "metadata": {},
   "source": [
    "## Construct the sky projection\n",
    "One astronomical unit at one parsec subtends one arcsecond. Use milliarcseconds (mas). Inclination foreshortens the minor axis but preserves the major semiaxis of a projected circular orbit."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1daf2ddd35d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "phase=np.linspace(0,2*np.pi,181); theta=1000*separation_au/distance_pc\n",
    "relative=theta*np.array([np.cos(phase),np.cos(np.deg2rad(inclination_deg))*np.sin(phase)])\n",
    "p1=-mass_fraction*relative; p2=(1-mass_fraction)*relative\n",
    "centroid=(1-light_fraction)*p1+light_fraction*p2\n",
    "print(\"Centre-of-light semimajor axis (mas):\",theta*abs(light_fraction-mass_fraction))\n",
    "assert np.allclose((1-mass_fraction)*p1+mass_fraction*p2,0)\n",
    "assert np.allclose(centroid,(light_fraction-mass_fraction)*relative)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "caea030e61fe",
   "metadata": {},
   "source": [
    "## Observe the sign flip as the brightness ratio changes\n",
    "A detector resolving only the photocentre cannot recover component separation without information about the component fractions. The second panel keeps an orientation sign that an amplitude alone would discard."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b7986be3886",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig,ax=plt.subplots(1,2,figsize=(9,3.9),layout=\"constrained\")\n",
    "for position,name,colour in [(p1,\"Primary\",\"#375b9a\"),(p2,\"Companion\",\"#a23d21\"),(centroid,\"Centre of light\",\"#175868\")]:\n",
    "    ax[0].plot(*position,label=name,color=colour);ax[0].plot(*position[:,0],\"o\",ms=4,color=colour)\n",
    "ax[0].set(xlabel=\"Sky x (mas)\",ylabel=\"Sky y (mas)\",aspect=\"equal\",title=\"Projected circular orbits\");ax[0].legend(fontsize=8)\n",
    "light=np.linspace(0,1,120);ax[1].plot(light,theta*(light-mass_fraction),color=\"#175868\");ax[1].axhline(0,color=\"grey\",lw=.8)\n",
    "ax[1].plot(light_fraction,theta*(light_fraction-mass_fraction),\"o\",color=\"#a23d21\")\n",
    "ax[1].set(xlabel=\"Companion light fraction\",ylabel=\"Signed centroid semiaxis (mas)\",title=\"Bias caused by unresolved light\")\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
