{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import seeing\n",
    "from seeing import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rayleigh-Sommerfeld\n"
     ]
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\operatorname{E_{1}}{\\left(x_{1},y_{1},z_{1} \\right)} = \\int\\limits_{- a}^{a}\\int\\limits_{- a}^{a} - \\frac{i E_{0} z_{1} \\left(i \\lambda \\frac{1}{2 \\pi \\sqrt{z_{1}^{2} + \\left(- x_{0} + x_{1}\\right)^{2} + \\left(- y_{0} + y_{1}\\right)^{2}}} + 1\\right) e^{\\frac{2 i \\pi \\sqrt{z_{1}^{2} + \\left(- x_{0} + x_{1}\\right)^{2} + \\left(- y_{0} + y_{1}\\right)^{2}}}{\\lambda}}}{\\lambda \\left(z_{1}^{2} + \\left(- x_{0} + x_{1}\\right)^{2} + \\left(- y_{0} + y_{1}\\right)^{2}\\right)}\\, dx_{0}\\, dy_{0}$"
      ],
      "text/plain": [
       "Eq(E_1(x_1, y_1, z_1), Integral(-I*E_0*z_1*(I*lambda*(1/(2*pi*sqrt(z_1**2 + (-x_0 + x_1)**2 + (-y_0 + y_1)**2))) + 1)*exp(2*I*pi*sqrt(z_1**2 + (-x_0 + x_1)**2 + (-y_0 + y_1)**2)/lambda)/(lambda*(z_1**2 + (-x_0 + x_1)**2 + (-y_0 + y_1)**2)), (x_0, -a, a), (y_0, -a, a)))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Approximate Rayleigh-Sommerfeld\n"
     ]
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\operatorname{E_{1}}{\\left(x_{1},y_{1},z_{1} \\right)} = \\int\\limits_{- a}^{a}\\int\\limits_{- a}^{a} - \\frac{i E_{0} z_{1} e^{\\frac{2 i \\pi \\sqrt{z_{1}^{2} + \\left(- x_{0} + x_{1}\\right)^{2} + \\left(- y_{0} + y_{1}\\right)^{2}}}{\\lambda}}}{\\lambda \\left(z_{1}^{2} + \\left(- x_{0} + x_{1}\\right)^{2} + \\left(- y_{0} + y_{1}\\right)^{2}\\right)}\\, dx_{0}\\, dy_{0}$"
      ],
      "text/plain": [
       "Eq(E_1(x_1, y_1, z_1), Integral(-I*E_0*z_1*exp(2*I*pi*sqrt(z_1**2 + (-x_0 + x_1)**2 + (-y_0 + y_1)**2)/lambda)/(lambda*(z_1**2 + (-x_0 + x_1)**2 + (-y_0 + y_1)**2)), (x_0, -a, a), (y_0, -a, a)))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Near Fresnel\n"
     ]
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\operatorname{E_{1}}{\\left(x_{1},y_{1},z_{1} \\right)} = \\int\\limits_{- a}^{a}\\int\\limits_{- a}^{a} - \\frac{i E_{0} e^{\\frac{2 i \\pi z_{1}}{\\lambda}} e^{\\frac{i \\pi \\left(\\left(- x_{0} + x_{1}\\right)^{2} + \\left(- y_{0} + y_{1}\\right)^{2}\\right)}{\\lambda z_{1}}}}{\\lambda z_{1}}\\, dx_{0}\\, dy_{0}$"
      ],
      "text/plain": [
       "Eq(E_1(x_1, y_1, z_1), Integral(-I*E_0*exp(2*I*pi*z_1/lambda)*exp(I*pi*((-x_0 + x_1)**2 + (-y_0 + y_1)**2)/(lambda*z_1))/(lambda*z_1), (x_0, -a, a), (y_0, -a, a)))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Far Fresnel\n"
     ]
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\operatorname{E_{1}}{\\left(x_{1},y_{1},z_{1} \\right)} = \\int\\limits_{- a}^{a}\\int\\limits_{- a}^{a} - \\frac{i E_{0} e^{\\frac{2 i \\pi z_{1}}{\\lambda}} e^{\\frac{i \\pi \\left(x_{0}^{2} + y_{0}^{2}\\right)}{\\lambda z_{1}}} e^{\\frac{2 i \\pi \\left(x_{0} x_{1} + y_{0} y_{1}\\right)}{\\lambda z_{1}}}}{\\lambda z_{1}}\\, dx_{0}\\, dy_{0}$"
      ],
      "text/plain": [
       "Eq(E_1(x_1, y_1, z_1), Integral(-I*E_0*exp(2*I*pi*z_1/lambda)*exp(I*pi*(x_0**2 + y_0**2)/(lambda*z_1))*exp(2*I*pi*(x_0*x_1 + y_0*y_1)/(lambda*z_1))/(lambda*z_1), (x_0, -a, a), (y_0, -a, a)))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "propMethodsCartesian = Formulary.loadFromFile('Propagation100.frm')\n",
    "\n",
    "approximations = [\n",
    "    \"Rayleigh-Sommerfeld\",\n",
    "    \"Approximate Rayleigh-Sommerfeld\",\n",
    "    \"Near Fresnel\",\n",
    "    \"Far Fresnel\"]\n",
    "\n",
    "propMethodsCartesian.display(approximations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "waveLength = 10e-6\n",
    "apertureRadius =  waveLength*10\n",
    "mCalc = Calculator(cp, cp.float64, 'intensity')\n",
    "xyCircle = propMethodsCartesian.getFormula('xyCircle')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rayleigh-Sommerfeld Arg\n"
     ]
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - \\frac{i E_{0} z_{1} \\left(i \\lambda \\frac{1}{2 \\pi \\sqrt{z_{1}^{2} + \\left(- x_{0} + x_{1}\\right)^{2} + \\left(- y_{0} + y_{1}\\right)^{2}}} + 1\\right) e^{\\frac{2 i \\pi \\sqrt{z_{1}^{2} + \\left(- x_{0} + x_{1}\\right)^{2} + \\left(- y_{0} + y_{1}\\right)^{2}}}{\\lambda}}}{\\lambda \\left(z_{1}^{2} + \\left(- x_{0} + x_{1}\\right)^{2} + \\left(- y_{0} + y_{1}\\right)^{2}\\right)}$"
      ],
      "text/plain": [
       "-I*E_0*z_1*(I*lambda*(1/(2*pi*sqrt(z_1**2 + (-x_0 + x_1)**2 + (-y_0 + y_1)**2))) + 1)*exp(2*I*pi*sqrt(z_1**2 + (-x_0 + x_1)**2 + (-y_0 + y_1)**2)/lambda)/(lambda*(z_1**2 + (-x_0 + x_1)**2 + (-y_0 + y_1)**2))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 162396 characters]\n",
      "text/plain": [
       "<Figure size 504x504 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 35144 characters]\n",
      "text/plain": [
       "<Figure size 504x504 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "propMethodsCartesian.display([\"Rayleigh-Sommerfeld Arg\"])\n",
    "\n",
    "subdiv_points = 512\n",
    "#ez0_c =  subsParamsByName( xyCircle, {'a':apertureRadius, 'x_0': 0.1, 'y_0': apertureRadius} )\n",
    "subsDictC = {'E_0': 1, 'z_1': 2*apertureRadius, 'x_0': 0, 'y_0': apertureRadius, 'lambda': waveLength, 'a':apertureRadius}\n",
    "paramAndRanges = [( 'x_1', -apertureRadius, apertureRadius, subdiv_points, 'linear' ),\n",
    "                  ( 'y_1', -apertureRadius, apertureRadius, subdiv_points, 'linear' )]\n",
    "\n",
    "#xx, ez = propMethodsCartesian.evaluateFormula(\"Rayleigh-Sommerfeld Arg\", subsDictC, paramAndRanges, mCalc)\n",
    "#fig, ax = plt.subplots(figsize=(10,10))\n",
    "#ax.imshow( np.real(ez), cmap='hot' )\n",
    "\n",
    "propMethodsCartesian.plotFormula(\"Rayleigh-Sommerfeld Arg\", subsDictC, paramAndRanges, mCalc)\n",
    "\n",
    "paramAndRanges = [( 'x_1', -apertureRadius, apertureRadius, subdiv_points, 'linear' )]\n",
    "\n",
    "subsDictC = {'E_0': 1, 'z_1': 2*apertureRadius, 'y_1':0.0, 'x_0': 0, 'y_0': apertureRadius, 'lambda': waveLength, 'a':apertureRadius}\n",
    "propMethodsCartesian.plotFormula(\"Rayleigh-Sommerfeld Arg\", subsDictC, paramAndRanges, mCalc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 98388 characters]\n",
      "text/plain": [
       "<Figure size 720x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "subdiv_points = 256\n",
    "ez0_c =  subsParamsByName( xyCircle, {'a':apertureRadius} )\n",
    "subsDictC = {'E_0': ez0_c, 'x_1': 0, 'y_1': 0, 'lambda': waveLength, 'a':apertureRadius}\n",
    "paramAndRange = ( 'z_1', 0.01*apertureRadius, apertureRadius*100, subdiv_points, 'geometric' )\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(10,5))\n",
    "plt.xscale('log')\n",
    "plt.yscale('linear')\n",
    "\n",
    "for appr in reversed(approximations):\n",
    "    eeq = propMethodsCartesian.getFormula(appr)\n",
    "    xplot2, zplot2 = mCalc.IntegralEvalE(subsParamsByName(eeq, subsDictC), [paramAndRange], None, 'trap')\n",
    "    plt.plot(xplot2[0]/apertureRadius, np.clip(zplot2, -4.5, 4.5), label=appr)\n",
    "        \n",
    "plt.legend(loc=(1.0, 0.78))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7fd49a8c8f60>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 63952 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "subdiv_points = 256\n",
    "ez0_l =  subsParamsByName( xyCircle, {'a':apertureRadius} )\n",
    "subsDict = {'E_0': ez0_l, 'y_1': 0, 'lambda': waveLength, 'a':apertureRadius}\n",
    "paramsAndRanges = [ ('x_1', -1.5*apertureRadius, 1.5*apertureRadius, subdiv_points, 'linear'), ('z_1',  waveLength, 15*apertureRadius, subdiv_points, 'linear')]\n",
    "eeq = propMethodsCartesian.getFormula(\"Rayleigh-Sommerfeld\")\n",
    "\n",
    "xplot, fplot1 = mCalc.IntegralEvalE(subsParamsByName(eeq, subsDict), paramsAndRanges) \n",
    "\n",
    "fig, ax = plt.subplots(figsize=(10,10))\n",
    "ax.imshow( fplot1, cmap='hot' )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7fd490e7cfd0>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 41372 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "subsDict = {'E_0': ez0_l, 'z_1': 2*apertureRadius, 'lambda': waveLength, 'a':apertureRadius}\n",
    "paramsAndRanges = [ ('x_1', -2*apertureRadius, 2*apertureRadius, subdiv_points, 'linear'), ('y_1', -2*apertureRadius, 2*apertureRadius, subdiv_points, 'linear')]\n",
    "eeq = propMethodsCartesian.getFormula(\"Rayleigh-Sommerfeld\")\n",
    "\n",
    "xplot, fplot1 = mCalc.IntegralEvalE(subsParamsByName(eeq, subsDict), paramsAndRanges) \n",
    "\n",
    "fig, ax = plt.subplots(figsize=(10,10))\n",
    "ax.imshow( np.log(fplot1+1), cmap='hot' )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 72676 characters]\n",
      "text/plain": [
       "<Figure size 720x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "approximationsF = [\n",
    "    \"Rayleigh-Sommerfeld\",\n",
    "    \"Far Fresnel\"]\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(10,5))\n",
    "plt.xscale('log')\n",
    "plt.yscale('linear')\n",
    "\n",
    "paramAndRange = ( 'z_1', apertureRadius, apertureRadius*10, 500, 'geometric' )\n",
    "subsDictC = {'E_0': ez0_l, 'x_1': 0, 'y_1': 0, 'lambda': waveLength, 'a':apertureRadius}\n",
    "\n",
    "for appr in reversed(approximations):\n",
    "    eeq = propMethodsCartesian.getFormula(appr)\n",
    "    xplot2, zplot2 = mCalc.IntegralEvalE(subsParamsByName(eeq, subsDictC), [paramAndRange], [(subdiv_points, 'linear'), (subdiv_points, 'linear')], 'rect')\n",
    "    plt.plot(xplot2[0]/apertureRadius, np.clip(zplot2, -4.5, 4.5), label=appr)\n",
    "        \n",
    "plt.legend(loc=(1.0, 0.78))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\operatorname{W_{\\phi}}{\\left(\\rho \\right)} = \\int\\limits_{f_{min}}^{f_{max}} \\frac{0.0229 \\left(-1\\right)^{m_{k}} 2^{\\left(-0.5\\right) \\delta_{0 m_{j}} - 0.5 \\delta_{0 m_{k}} + 1} i^{n_{j} + n_{k}} \\sqrt{\\left(n_{j} + 1\\right) \\left(n_{k} + 1\\right)} \\left(i^{3 m_{j} + 3 m_{k}} \\cos{\\left(\\theta \\left(m_{j} + m_{k}\\right) + \\frac{\\pi \\left(\\left(1 - \\delta_{0 m_{j}}\\right) \\left(\\left(-1\\right)^{j} - 1\\right) + \\left(1 - \\delta_{0 m_{k}}\\right) \\left(\\left(-1\\right)^{k} - 1\\right)\\right)}{4} \\right)} J_{m_{j} + m_{k}}\\left(2 \\pi f h \\rho\\right) + i^{3 \\left|{m_{j} - m_{k}}\\right|} \\cos{\\left(\\theta \\left(m_{j} - m_{k}\\right) + \\frac{\\pi \\left(\\left(1 - \\delta_{0 m_{j}}\\right) \\left(\\left(-1\\right)^{j} - 1\\right) - \\left(1 - \\delta_{0 m_{k}}\\right) \\left(\\left(-1\\right)^{k} - 1\\right)\\right)}{4} \\right)} J_{\\left|{m_{j} - m_{k}}\\right|}\\left(2 \\pi f h \\rho\\right)\\right) J_{n_{j} + 1}\\left(2 \\pi R_{1} f\\right) J_{n_{j} + 1}\\left(2 \\pi R_{2} f\\right)}{\\pi R_{1} R_{2} f r_{0}^{\\frac{5}{3}} \\left(\\left(\\frac{1}{L_{0}}\\right)^{2} + f^{2}\\right)^{\\frac{11}{6}}}\\, df$"
      ],
      "text/plain": [
       "Eq(W_phi(rho), Integral(0.0229*(-1)**m_k*2**((-0.5)*KroneckerDelta(0, m_j) - 0.5*KroneckerDelta(0, m_k) + 1)*I**(n_j + n_k)*sqrt((n_j + 1)*(n_k + 1))*(I**(3*m_j + 3*m_k)*cos(theta*(m_j + m_k) + pi*((1 - KroneckerDelta(0, m_j))*((-1)**j - 1) + (1 - KroneckerDelta(0, m_k))*((-1)**k - 1))/4)*besselj(m_j + m_k, 2*pi*f*h*rho) + I**(3*Abs(m_j - m_k))*cos(theta*(m_j - m_k) + pi*((1 - KroneckerDelta(0, m_j))*((-1)**j - 1) - (1 - KroneckerDelta(0, m_k))*((-1)**k - 1))/4)*besselj(Abs(m_j - m_k), 2*pi*f*h*rho))*besselj(n_j + 1, 2*pi*R_1*f)*besselj(n_j + 1, 2*pi*R_2*f)/((pi*R_1*R_2*f*r_0**(5/3)*((1/L_0)**2 + f**2)**(11/6))), (f, f_min, f_max)))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\operatorname{W_{\\phi}}{\\left(\\rho \\right)} = \\int\\limits_{0.001}^{10} - \\frac{0.135193784634899 J^{2}_{2}\\left(8.0 \\pi f\\right) J_{2}\\left(20000 \\pi f \\rho\\right)}{\\pi f \\left(f^{2} + 0.0016\\right)^{\\frac{11}{6}}}\\, df$"
      ],
      "text/plain": [
       "Eq(W_phi(rho), Integral(-0.135193784634899*besselj(2, 8.0*pi*f)**2*besselj(2, 20000*pi*f*rho)/(pi*f*(f**2 + 0.0016)**(11/6)), (f, 0.001, 10)))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 16496 characters]\n",
      "text/plain": [
       "<Figure size 720x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from seeing import *\n",
    "from zernike import *\n",
    "\n",
    "#mf.writeToFile('Mavis.frm')\n",
    "mf = Formulary.loadFromFile('Mavis.frm')\n",
    "\n",
    "mIt = Integrator(cp, cp.float64, 'none')\n",
    "covIntegral = mf['ZernikeCovarianceI']\n",
    "display(covIntegral)\n",
    "\n",
    "r0_Value = 0.15 # [m]\n",
    "L0_Value = 25.0 # [m]\n",
    "TelescopeDiameter = 8.0 #[m]\n",
    "DM_height = 10000 # [m]\n",
    "\n",
    "paramsDict = {'f_min':0.001, 'f_max':10, 'theta': np.pi/4.0, 'L_0': L0_Value, \n",
    "              'r_0': r0_Value, 'R_1': TelescopeDiameter/2.0, 'R_2': TelescopeDiameter/2.0,\n",
    "              'h': DM_height }\n",
    "\n",
    "_integral1 = subsParamsByName( cov_expr_jk(covIntegral, 2, 3), paramsDict)\n",
    "display(_integral1)\n",
    "\n",
    "rho_max = 120.0/206265.0\n",
    "xplot1, zplot1 = mIt.IntegralEvalE(_integral1, [('rho',  0.0, rho_max, 1000, 'linear')], [(1024, 'linear')], method='rect')\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(10,5))\n",
    "ax.plot(xplot1[0], zplot1)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
