{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2018-12-02T06:48:38.961Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "19 1.5631319609718588\n", "39 1.5631319609718588\n", "59 1.5631319609718588\n", "79 1.5631319609718588\n", "99 1.5631319609718588\n", "119 1.5631319609718588\n", "139 1.5631319609718588\n", "159 1.5631319609718588\n", "179 1.5631319609718588\n", "199 1.5631319609718588\n", "219 1.5631319609718588\n", "239 1.5631319609718588\n", "259 1.5631319609718588\n", "279 1.5631319609718588\n", "299 1.5631319609718588\n", "319 1.5631319609718588\n", "339 1.5631319609718588\n", "359 1.5631319609718588\n", "379 1.5631319609718588\n", "399 1.5631319609718588\n", "419 1.5631319609718588\n" ] } ], "source": [ "import warnings\n", "warnings.filterwarnings(\"ignore\",category =RuntimeWarning)\n", "import numpy as np\n", "import makeqx as mkq\n", "import de2 as de\n", "import multiprocessing as mp\n", " \n", "# def arc_0(qx, lam_min=400, lam_max=800, npoints=50):\n", "# cthick = qx[0]\n", "# q_x = qx[1:]\n", "# Rs0, Rp0 = mkq.tmm_lam_parallel(q_x, cthick, 0, n_par=12, lam_low=lam_min, lam_high=lam_max, lam_pts=npoints, n_subs=1.52)\n", "# return np.mean(Rs0)\n", " \n", "# def arc_dig_0(qx, lam_min=400, lam_max=800, npoints=50):\n", "# cthick = qx[0]\n", "# q_x = mkq.digitize_qx(qx[1:], dlevels=2)\n", "# Rs0, Rp0 = mkq.tmm_lam_parallel(q_x, cthick, 0, n_par=12, lam_low=lam_min, lam_high=lam_max, lam_pts=npoints, n_subs=1.52)\n", "# return np.mean(Rs0)\n", "\n", "\n", "def tmm_wrapper2(arg):\n", " args, kwargs = arg\n", " return mkq.tmm_eval_wsweep(*args, **kwargs)\n", "\n", "\n", "def arc_par(pop, **kwargs):\n", " jobs = []\n", " pool=mp.Pool(60)\n", " for indiv in pop:\n", " indivi = mkq.digitize_qx(indiv[1:], dlevels=2)\n", " jobs.append((indivi, indiv[0], 0))\n", " arg = [(j, kwargs) for j in jobs]\n", " answ = np.array(pool.map(tmm_wrapper2, arg))\n", " pool.close()\n", " return np.mean(answ, axis=1)\n", "\n", " \n", "num_layers = 35 \n", "bnds = [(0, 1)]*num_layers\n", "bnds = [(300, 400)] + bnds\n", "its_first = 0\n", "psnew = 40\n", "its_second = 500\n", "\n", "pop = np.ones((psnew, 1+ num_layers))\n", "for indiv in range(psnew):\n", " pop[indiv,1:] = mkq.make_qx(num_layers)\n", " pop[indiv,0] = np.random.uniform( bnds[0][0], bnds[0][1], 1 )\n", " \n", "b, c, hstry = de.de_cp(fobj=arc_par, bounds=bnds, pop=pop, history=[], it_start=its_first, popsize=psnew, its=its_second, lam_low=400, lam_high=800, lam_pts=50)\n", "\n", "\n", "#m1 = mkq.tmm_eval2(qx=pop[0][1:], cthick=pop[0][0]) \n", "#m2 = mkq.tmm_eval(qx=pop[0][1:], cthick=pop[0][0]) \n", "\n", "#arc_par(pop, lam_low=400, lam_high=800, lam_pts=50)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-11-25T10:33:20.269270Z", "start_time": "2018-11-25T10:32:36.651677Z" } }, "outputs": [], "source": [ "import warnings\n", "warnings.filterwarnings(\"ignore\",category =RuntimeWarning)\n", "import numpy as np\n", "import makeqx as mkq\n", "import de2 as de\n", "import multiprocessing as mp\n", "import qxnew as qn \n", "# def arc_0(qx, lam_min=400, lam_max=800, npoints=50):\n", "# cthick = qx[0]\n", "# q_x = qx[1:]\n", "# Rs0, Rp0 = mkq.tmm_lam_parallel(q_x, cthick, 0, n_par=12, lam_low=lam_min, lam_high=lam_max, lam_pts=npoints, n_subs=1.52)\n", "# return np.mean(Rs0)\n", " \n", "# def arc_dig_0(qx, lam_min=400, lam_max=800, npoints=50):\n", "# cthick = qx[0]\n", "# q_x = mkq.digitize_qx(qx[1:], dlevels=2)\n", "# Rs0, Rp0 = mkq.tmm_lam_parallel(q_x, cthick, 0, n_par=12, lam_low=lam_min, lam_high=lam_max, lam_pts=npoints, n_subs=1.52)\n", "# return np.mean(Rs0)\n", "\n", "\n", "def tmm_wrapper2(arg):\n", " args, kwargs = arg\n", " return qn.tmm_eval_wsweep(*args, **kwargs)\n", "\n", "\n", "def arc_par(pop, **kwargs):\n", " jobs = []\n", " pool=mp.Pool(90)\n", " for indiv in pop:\n", " indiv = indiv.reshape((int(indiv.size/2), 2))\n", " indiv[:,1] = mkq.digitize_qx(indiv[:,1], dlevels=2)\n", " indiv = indiv.flatten()\n", " jobs.append((indiv, 0.5, 8 , 0))\n", " arg = [(j, kwargs) for j in jobs]\n", " answ = np.array(pool.map(tmm_wrapper2, arg))\n", " pool.close()\n", " return np.mean(answ, axis=1)\n", "\n", " \n", "num_layers = 30\n", "d_min = 0.5\n", "d_max = 8\n", "\n", "bnds = [(d_min, d_max), (0,1) ]*num_layers\n", "#bnds = [(300, 400)] + bnds\n", "its_first = 0\n", "psnew = 160\n", "its_second = 500\n", "\n", "pop = np.ones((psnew, 2*num_layers))\n", "for indiv in range(psnew):\n", " pop[indiv] = qn.make_qxn(num_layers, dlevels=2)\n", " #pop[indiv][::2] = (d_max - d_min)*pop[indiv][::2] + d_min\n", " pop[indiv,0] = np.random.uniform( bnds[0][0], bnds[0][1], 1 )\n", " \n", "b, c, hstry = de.de_cp(fobj=arc_par, bounds=bnds, pop=pop, history=[], it_start=its_first, popsize=psnew, its=its_second, lam_low=400, lam_high=800, lam_pts=50)\n", "\n", "np.sum(b[::2]), b[::2], b[1::2], c\n", "\n", "#m1 = mkq.tmm_eval2(qx=pop[0][1:], cthick=pop[0][0]) \n", "#m2 = mkq.tmm_eval(qx=pop[0][1:], cthick=pop[0][0]) \n", "\n", "#" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-11-27T02:07:24.693765Z", "start_time": "2018-11-26T12:51:52.517421Z" } }, "outputs": [], "source": [ "%reload_ext autoreload\n", "%autoreload 2\n", "import warnings\n", "warnings.filterwarnings(\"ignore\",category =RuntimeWarning)\n", "import numpy as np\n", "import de2 as de\n", "import multiprocessing as mp\n", "import makeqx as mkq\n", "import qxnew as qn \n", "\n", "def tmm_wrapper2(arg):\n", " args, kwargs = arg\n", " return qn.tmm_eval_wsweep(*args, **kwargs)\n", "\n", "def arc_par(pop, **kwargs):\n", " jobs = []\n", " pool=mp.Pool(90)\n", " for indiv in pop:\n", " indiv = indiv.reshape((int(indiv.size/2), 2))\n", " indiv[:,1] = mkq.digitize_qx(indiv[:,1], dlevels=2)\n", " indiv = indiv.flatten()\n", " jobs.append((indiv, 0, 1, 0))\n", " arg = [(j, kwargs) for j in jobs]\n", " answ = np.array(pool.map(tmm_wrapper2, arg))\n", " pool.close()\n", " return np.mean(answ, axis=1)\n", "\n", " \n", "num_layers = 50\n", "d_min = 0.5\n", "d_max = 15\n", "\n", "bnds = [(d_min, d_max), (0,1)]*num_layers\n", "#bnds = [(300, 400)] + bnds\n", "its_first = 0\n", "psnew = 40\n", "islands = 10 \n", "its_second = 500\n", "numgens = 10 \n", "\n", "isl = np.ones((psnew*islands, 2*num_layers))\n", "for indiv in range(psnew*islands):\n", " isl[indiv] = qn.make_qxn(num_layers, dlevels=2)\n", "poplist = np.split(isl, islands)\n", "\n", "bids = np.zeros(islands, dtype=int)\n", "bfits = np.zeros(islands)\n", "bests = isl[0:islands]\n", "\n", "\n", "for gen in range(numgens):\n", " print(\"==============================\")\n", " print(\"Epoch #:\" + str(gen + 1))\n", " print(\"==============================\")\n", " for isln in range(islands):\n", " print(\"----------------- I-\" + str(isln + 1)+ \"---------------------\")\n", " poplist[isln], bids[isln], bfits[isln], bests[isln] = de.de_cp(\n", " fobj=arc_par, \n", " bounds=bnds, \n", " pop=poplist[isln], \n", " history=[], \n", " it_start=its_first, \n", " popsize=psnew, \n", " its=its_second, \n", " lam_low=400, \n", " lam_high=800, \n", " lam_pts=50)\n", " print(np.sum(bests[isln][::2]))\n", " if gen < (numgens - 1):\n", " print(\"Round robin best migration\")\n", " stmp = poplist[islands-1][bids[islands-1]] \n", " for isln in range(1, islands):\n", " poplist[isln][bids[isln]] = poplist[isln-1][bids[isln-1]]\n", " poplist[0][bids[0]] = stmp \n", "\n", "print(bfits) \n", "print(bests[np.argmin(bfits)][::2])\n", "#np.sum(b[::2]), b[::2], b[1::2], c\n", "\n", "#m1 = mkq.tmm_eval2(qx=pop[0][1:], cthick=pop[0][0]) \n", "#m2 = mkq.tmm_eval(qx=pop[0][1:], cthick=pop[0][0]) " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-11-27T04:39:19.806762Z", "start_time": "2018-11-27T04:39:19.740844Z" } }, "outputs": [], "source": [ "print(np.sum(bests[np.argmin(bfits)][::2]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-11-25T08:46:16.654004Z", "start_time": "2018-11-25T08:46:15.567261Z" } }, "outputs": [], "source": [ "isl = np.split(pop,8)\n", "isl[0].shape\n", "arc_par(pop)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-11-26T10:52:44.968511Z", "start_time": "2018-11-26T10:52:44.892890Z" } }, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2\n", "import qxnew as qn\n", "import makeqx as mkq\n", "\n", "qn.make_qxn(10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-11-26T10:52:28.147240Z", "start_time": "2018-11-26T10:52:28.076907Z" } }, "outputs": [], "source": [ "qx = qn.make_qxn(40,5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-11-24T09:33:50.326789Z", "start_time": "2018-11-24T09:33:50.307053Z" } }, "outputs": [], "source": [ "qx" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-11-24T07:07:44.202457Z", "start_time": "2018-11-24T07:07:44.180095Z" } }, "outputs": [], "source": [ "qn.make_nxdx(qx, d_min=2, d_max=10, wavelen=550, n_substrate=1.52)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-11-24T07:07:45.345103Z", "start_time": "2018-11-24T07:07:45.322470Z" } }, "outputs": [], "source": [ "qn.tmm_eval_wbk(qx, 2, 10, 0, 550, 1.52)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-11-24T07:07:47.293093Z", "start_time": "2018-11-24T07:07:47.155314Z" } }, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from jupyterthemes import jtplot\n", "# currently installed theme will be used to\n", "# set plot style if no arguments provided\n", "jtplot.style()\n", "Rs = qn.tmm_eval_wsweep(qx, d_min=2 , d_max=10, inc_ang=0, lam_low=400, lam_high=800, lam_pts=50, n_subs=1.52) # in nm\n", "lams = np.linspace(400, 800, endpoint=True, num=50)\n", "plt.plot(lams, Rs )\n", "plt.xlabel('Wavelength (nm)')\n", "plt.ylabel('Reflectance (%)')\n", "plt.ylim([0,100])\n", "plt.show()" ] }, { "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.0" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false } }, "nbformat": 4, "nbformat_minor": 2 }