diff --git a/common/plotter.py b/common/plotter.py new file mode 100644 index 0000000..3f4a7f4 --- /dev/null +++ b/common/plotter.py @@ -0,0 +1,86 @@ +from typing import Union + +import numpy as np +import matplotlib.pyplot as plt + + +def plot_column_data(mpl_axes, output_file_name): + + array_in = np.loadtxt(output_file_name).transpose() + + mpl_axes.plot(array_in[0], array_in[1]) + plt.show() + + +def save_onto_white_background( + mpl_axes, output_file_name, save_format: Union["svg", "png", "pdf"] +): + #if save_format != "svg": + # mpl_axes.set_facecolor("white") + plt.savefig(f"{output_file_name}.{save_format}", transparent=True) + + +def set_geometry(left=0, c2=30, width=1280, height=1600): + plt.get_current_fig_manager().window.setGeometry(left, c2, width, height) + + +def customize_figure_size(left, right, bottom, top): + """ Set the figure size + + 0 < left < right < 1 + 0 < bottom < top < 1 + + -1 for no change + """ + + if left != -1: + plt.rcParams["figure.subplot.left"] = left + if right != -1: + plt.rcParams["figure.subplot.right"] = right + if top != -1: + plt.rcParams["figure.subplot.top"] = top + if bottom != -1: + plt.rcParams["figure.subplot.bottom"] = bottom + + +def compare_to_matlab(coo_matrix, output_file_name): + """ + __ Description __ + outputs the non zeros row, col, element values of a coo matrix + used to compare with the Matlab simulations + """ + + row_array = [] + col_array = [] + element_real_array = [] + element_imaginary_array = [] + + # 1 - collect element + for row, col, elm in zip(coo_matrix.row, coo_matrix.col, coo_matrix.data): + row_array.append(row) + col_array.append(col) + element_real_array.append(elm.real) + element_imaginary_array.append(elm.imag) + + row_array = np.array(row_array) + col_array = np.array(col_array) + element_real_array = np.array(element_real_array) + element_imaginary_array = np.array(element_imaginary_array) + + # 2 - sort array by the column + _sort_idx = np.argsort(col_array) + row_array = np.array(row_array)[_sort_idx] + col_array = np.array(col_array)[_sort_idx] + element_real_array = np.array(element_real_array)[_sort_idx] + element_imaginary_array = np.array(element_imaginary_array)[_sort_idx] + + with open(output_file_name, "w") as fout: + for row, col, element_real, element_imaginary in zip( + row_array, col_array, element_real_array, element_imaginary_array + ): + string_to_write = "{_row}\t{_col}\t{_real:.1f} {_im:.2f}\n".format( + _row=row + 1, _col=col + 1, _real=element_real, _im=element_imaginary + ) + + fout.write(string_to_write) + diff --git a/entrypoint_cuda.ipynb b/entrypoint_cuda.ipynb index 9709c1c..ff881dc 100644 --- a/entrypoint_cuda.ipynb +++ b/entrypoint_cuda.ipynb @@ -599,7 +599,7 @@ }, { "cell_type": "code", - "execution_count": 73, + "execution_count": 213, "metadata": { "scrolled": false }, @@ -1384,7 +1384,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -1396,41 +1396,81 @@ { "data": { "text/plain": [ - "" + "Text(0.5, 0, '$\\\\varphi_l/2\\\\pi$')" ] }, - "execution_count": 73, + "execution_count": 213, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%matplotlib notebook\n", - "ig, ax = plt.subplots(2, 2, sharex=True, sharey=True, figsize=(5, 5))\n", - "im0 = ax[0][0].imshow(result[\"potential\"],\n", + "fig, ax = plt.subplots(2, 2, sharex=True, sharey=True, figsize=(5, 5))\n", + "plt.subplots_adjust(left=0.1, bottom=None, right=0.85, top=None, wspace=None, hspace=None)\n", + "cb = defaultdict(lambda: defaultdict(int))\n", + "im = defaultdict(lambda: defaultdict(int))\n", + "\n", + "im[0][0] = ax[0][0].imshow(result[\"potential\"],\n", " extent = [LOWER, UPPER, LOWER, UPPER],\n", " origin='lower',\n", - " interpolation='bessel')\n", - "fig.colorbar(im0, ax=ax[0][0])\n", - "im1 = ax[0][1].imshow(result[\"phi02\"],\n", + " interpolation='bessel',\n", + " vmin=0, vmax=2.2)\n", + "ax[0][0].set_title(r\"$U_{min}/E_J$\", fontsize=14, loc=\"left\")\n", + "\n", + "im[0][1] = ax[0][1].imshow(result[\"phi02\"],\n", " extent = [LOWER, UPPER, LOWER, UPPER],\n", " origin='lower',\n", " cmap='RdGy', \n", - " interpolation='bessel')\n", - "fig.colorbar(im1, ax=ax[0][1])\n", - "im2 = ax[1][0].imshow(result[\"phi01\"] + result[\"phi01\"],\n", + " interpolation='bessel',\n", + " vmin=-0.5, vmax=0.5)\n", + "ax[0][1].set_title(r\"$\\varphi_{02}$\", fontsize=14)\n", + "\n", + "im[1][0] = ax[1][0].imshow(result[\"phi01\"] + result[\"phi01\"],\n", " extent = [LOWER, UPPER, LOWER, UPPER],\n", " origin='lower',\n", " cmap='RdGy',\n", - " interpolation='bessel')\n", - "fig.colorbar(im2, ax=ax[1][0])\n", - "im3 = ax[1][1].imshow(result[\"phi03\"] + result[\"phi03\"],\n", + " interpolation='bessel',\n", + " vmin=-0.5, vmax=0.5)\n", + "ax[1][0].set_title(r\"$\\varphi_{01} + \\varphi_{12}$\", fontsize=14)\n", + "\n", + "im[1][1] = ax[1][1].imshow(result[\"phi03\"] + result[\"phi03\"],\n", " extent = [LOWER, UPPER, LOWER, UPPER],\n", " origin='lower',\n", " cmap='RdGy',\n", - " interpolation='bessel'\n", - " )\n", - "fig.colorbar(im3, ax=ax[1][1])" + " interpolation='bessel',\n", + " vmin=-0.5, vmax=0.5)\n", + "ax[1][1].set_title(r\"$\\varphi_{03} + \\varphi_{32}$\", fontsize=14)\n", + "\n", + "# Colorbars\n", + "for (i, j) in list(itertools.product([0,1], [0,1])):\n", + " if i == 0 and j == 0:\n", + " cb[i][j] = fig.colorbar(im[i][j], ax=ax[i][j], fraction=0.046, pad=0.04)\n", + " cb[i][j].ax.set_ylabel(r\"$\\frac{U_{min}}{E_J}$\", fontsize=8, rotation=0, ha=\"left\", labelpad=-25, y=1.15)\n", + " cb[i][j].set_ticks([0, 0.5, 1, 1.5, 2])\n", + " else:\n", + " cb[i][j] = fig.colorbar(im[i][j], ax=ax[i][j], fraction=0.046, pad=0.04)\n", + " cb[i][j].ax.set_ylabel(r\"$\\frac{\\varphi}{2\\pi}$\", fontsize=8, rotation=0, ha=\"left\", labelpad=-32, y=1.11)\n", + " cb[i][j].set_ticks([-0.5, 0, 0.5])\n", + " cb[i][j].ax.tick_params(labelsize=8)\n", + "\n", + "# X ticks\n", + "ax[0][0].set_xticks([-0.5, 0, 0.5, 1, 1.5])\n", + "# Axes Labels\n", + "ax[0][0].set_ylabel(r\"$\\varphi_r/2\\pi$\", fontsize=14)\n", + "ax[1][0].set_xlabel(r\"$\\varphi_l/2\\pi$\", fontsize=14)\n", + "ax[1][0].set_ylabel(r\"$\\varphi_r/2\\pi$\", fontsize=14)\n", + "ax[1][1].set_xlabel(r\"$\\varphi_l/2\\pi$\", fontsize=14)" + ] + }, + { + "cell_type": "code", + "execution_count": 202, + "metadata": {}, + "outputs": [], + "source": [ + "from common import plotter\n", + "plotter.save_onto_white_background(ax, \"./output/potential_minimum_diamonds\", \"pdf\")" ] }, { @@ -2254,52 +2294,888 @@ ] }, { - "cell_type": "code", - "execution_count": 57, + "cell_type": "markdown", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "2.0" - ] - }, - "execution_count": 57, - "metadata": {}, - "output_type": "execute_result" - } - ], "source": [ - "from functions.potential import potential_function\n", - "potential_function([0, 0.5 * 2 * pi, 0], 0.5 * 2 * pi, 0.5 * 2 * pi, 1)" + "# Take slices of the potential\n", + "- Along $\\varphi_l == \\varphi_r$\n", + "- Along $\\frac{\\varphi_l + \\varphi_r}{2} = \\pi \\equiv \\varphi_+$ \n", + "- And also do the approximation:\n", + "$$\\varphi_{02} \\approx \\frac{\\varphi_l-\\varphi_r - 2\\pi(L-R)}{2(1+\\alpha)}$$\n", + "$$\\frac{U_\\text{min}}{E_J} \\approx \\left(\\frac{\\varphi_{02} - \\varphi_l + 2\\pi L}{2}\\right)^2 + \\left(\\frac{\\varphi_{02} + \\varphi_r - 2\\pi R}{2}\\right)^2 + \\frac{\\alpha}{2}\\varphi_{02}^2$$" ] }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 263, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "(1000, 1000)" - ] - }, - "execution_count": 60, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "result[\"potential\"].shape" + "def phi02_approximation(phi_l: float, phi_r: float, L: int, R: int, alpha: float):\n", + " return (phi_l - phi_r - 2 * pi * (L - R)) / (2 * (1 + alpha))\n", + "def potential_approximation(phi02: float, phi_l: float, phi_r: float, L: int, R: int, alpha: float):\n", + " return (phi02 - phi_l + 2 * pi * L) ** 2 / 4 + (phi02 + phi_r - 2 * pi * R) ** 2 / 4 + alpha * phi02 ** 2 / 2" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 292, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "# Along phi+ and approximation\n", + "result[\"potential_phi_plus\"] = []\n", + "result[\"potential_phi_approximation\"] = []\n", + "result[\"potential_phi_approximation_01\"] = []\n", + "result[\"potential_phi_approximation_10\"] = []\n", + "length_of_lr_array = len(lr_array)\n", + "alpha = 1\n", + "\n", + "for phi_l_idx, phi_l in enumerate(lr_array):\n", + " phi_r_idx = length_of_lr_array - phi_l_idx - 1\n", + " phi_r = lr_array[phi_r_idx]\n", + " \n", + " result[\"potential_phi_plus\"].append(\n", + " result[\"potential\"][phi_l_idx][phi_r_idx]\n", + " )\n", + " \n", + " min_potential = None\n", + " for (L, R) in itertools.permutations([-1, 0, 1], r=2):\n", + " phi02 = phi02_approximation(phi_l, phi_r, L, R, alpha)\n", + " potential = potential_approximation(phi02, phi_l, phi_r, L, R, alpha)\n", + " if min_potential is None or potential < min_potential:\n", + " min_potential = potential\n", + " if L == 0 and R == 1:\n", + " result[\"potential_phi_approximation_01\"].append(potential)\n", + " if L == 1 and R == 0:\n", + " result[\"potential_phi_approximation_10\"].append(potential)\n", + " \n", + " result[\"potential_phi_approximation\"].append(min_potential)" + ] + }, + { + "cell_type": "code", + "execution_count": 300, + "metadata": {}, + "outputs": [ + { + "data": { + "application/javascript": [ + "/* Put everything inside the global mpl namespace */\n", + "window.mpl = {};\n", + "\n", + "\n", + "mpl.get_websocket_type = function() {\n", + " if (typeof(WebSocket) !== 'undefined') {\n", + " return WebSocket;\n", + " } else if (typeof(MozWebSocket) !== 'undefined') {\n", + " return MozWebSocket;\n", + " } else {\n", + " alert('Your browser does not have WebSocket support. ' +\n", + " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", + " 'Firefox 4 and 5 are also supported but you ' +\n", + " 'have to enable WebSockets in about:config.');\n", + " };\n", + "}\n", + "\n", + "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", + " this.id = figure_id;\n", + "\n", + " this.ws = websocket;\n", + "\n", + " this.supports_binary = (this.ws.binaryType != undefined);\n", + "\n", + " if (!this.supports_binary) {\n", + " var warnings = document.getElementById(\"mpl-warnings\");\n", + " if (warnings) {\n", + " warnings.style.display = 'block';\n", + " warnings.textContent = (\n", + " \"This browser does not support binary websocket messages. \" +\n", + " \"Performance may be slow.\");\n", + " }\n", + " }\n", + "\n", + " this.imageObj = new Image();\n", + "\n", + " this.context = undefined;\n", + " this.message = undefined;\n", + " this.canvas = undefined;\n", + " this.rubberband_canvas = undefined;\n", + " this.rubberband_context = undefined;\n", + " this.format_dropdown = undefined;\n", + "\n", + " this.image_mode = 'full';\n", + "\n", + " this.root = $('
');\n", + " this._root_extra_style(this.root)\n", + " this.root.attr('style', 'display: inline-block');\n", + "\n", + " $(parent_element).append(this.root);\n", + "\n", + " this._init_header(this);\n", + " this._init_canvas(this);\n", + " this._init_toolbar(this);\n", + "\n", + " var fig = this;\n", + "\n", + " this.waiting = false;\n", + "\n", + " this.ws.onopen = function () {\n", + " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", + " fig.send_message(\"send_image_mode\", {});\n", + " if (mpl.ratio != 1) {\n", + " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", + " }\n", + " fig.send_message(\"refresh\", {});\n", + " }\n", + "\n", + " this.imageObj.onload = function() {\n", + " if (fig.image_mode == 'full') {\n", + " // Full images could contain transparency (where diff images\n", + " // almost always do), so we need to clear the canvas so that\n", + " // there is no ghosting.\n", + " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", + " }\n", + " fig.context.drawImage(fig.imageObj, 0, 0);\n", + " };\n", + "\n", + " this.imageObj.onunload = function() {\n", + " fig.ws.close();\n", + " }\n", + "\n", + " this.ws.onmessage = this._make_on_message_function(this);\n", + "\n", + " this.ondownload = ondownload;\n", + "}\n", + "\n", + "mpl.figure.prototype._init_header = function() {\n", + " var titlebar = $(\n", + " '
');\n", + " var titletext = $(\n", + " '
');\n", + " titlebar.append(titletext)\n", + " this.root.append(titlebar);\n", + " this.header = titletext[0];\n", + "}\n", + "\n", + "\n", + "\n", + "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", + "\n", + "}\n", + "\n", + "\n", + "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", + "\n", + "}\n", + "\n", + "mpl.figure.prototype._init_canvas = function() {\n", + " var fig = this;\n", + "\n", + " var canvas_div = $('
');\n", + "\n", + " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", + "\n", + " function canvas_keyboard_event(event) {\n", + " return fig.key_event(event, event['data']);\n", + " }\n", + "\n", + " canvas_div.keydown('key_press', canvas_keyboard_event);\n", + " canvas_div.keyup('key_release', canvas_keyboard_event);\n", + " this.canvas_div = canvas_div\n", + " this._canvas_extra_style(canvas_div)\n", + " this.root.append(canvas_div);\n", + "\n", + " var canvas = $('');\n", + " canvas.addClass('mpl-canvas');\n", + " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", + "\n", + " this.canvas = canvas[0];\n", + " this.context = canvas[0].getContext(\"2d\");\n", + "\n", + " var backingStore = this.context.backingStorePixelRatio ||\n", + "\tthis.context.webkitBackingStorePixelRatio ||\n", + "\tthis.context.mozBackingStorePixelRatio ||\n", + "\tthis.context.msBackingStorePixelRatio ||\n", + "\tthis.context.oBackingStorePixelRatio ||\n", + "\tthis.context.backingStorePixelRatio || 1;\n", + "\n", + " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", + "\n", + " var rubberband = $('');\n", + " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", + "\n", + " var pass_mouse_events = true;\n", + "\n", + " canvas_div.resizable({\n", + " start: function(event, ui) {\n", + " pass_mouse_events = false;\n", + " },\n", + " resize: function(event, ui) {\n", + " fig.request_resize(ui.size.width, ui.size.height);\n", + " },\n", + " stop: function(event, ui) {\n", + " pass_mouse_events = true;\n", + " fig.request_resize(ui.size.width, ui.size.height);\n", + " },\n", + " });\n", + "\n", + " function mouse_event_fn(event) {\n", + " if (pass_mouse_events)\n", + " return fig.mouse_event(event, event['data']);\n", + " }\n", + "\n", + " rubberband.mousedown('button_press', mouse_event_fn);\n", + " rubberband.mouseup('button_release', mouse_event_fn);\n", + " // Throttle sequential mouse events to 1 every 20ms.\n", + " rubberband.mousemove('motion_notify', mouse_event_fn);\n", + "\n", + " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", + " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", + "\n", + " canvas_div.on(\"wheel\", function (event) {\n", + " event = event.originalEvent;\n", + " event['data'] = 'scroll'\n", + " if (event.deltaY < 0) {\n", + " event.step = 1;\n", + " } else {\n", + " event.step = -1;\n", + " }\n", + " mouse_event_fn(event);\n", + " });\n", + "\n", + " canvas_div.append(canvas);\n", + " canvas_div.append(rubberband);\n", + "\n", + " this.rubberband = rubberband;\n", + " this.rubberband_canvas = rubberband[0];\n", + " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", + " this.rubberband_context.strokeStyle = \"#000000\";\n", + "\n", + " this._resize_canvas = function(width, height) {\n", + " // Keep the size of the canvas, canvas container, and rubber band\n", + " // canvas in synch.\n", + " canvas_div.css('width', width)\n", + " canvas_div.css('height', height)\n", + "\n", + " canvas.attr('width', width * mpl.ratio);\n", + " canvas.attr('height', height * mpl.ratio);\n", + " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", + "\n", + " rubberband.attr('width', width);\n", + " rubberband.attr('height', height);\n", + " }\n", + "\n", + " // Set the figure to an initial 600x600px, this will subsequently be updated\n", + " // upon first draw.\n", + " this._resize_canvas(600, 600);\n", + "\n", + " // Disable right mouse context menu.\n", + " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", + " return false;\n", + " });\n", + "\n", + " function set_focus () {\n", + " canvas.focus();\n", + " canvas_div.focus();\n", + " }\n", + "\n", + " window.setTimeout(set_focus, 100);\n", + "}\n", + "\n", + "mpl.figure.prototype._init_toolbar = function() {\n", + " var fig = this;\n", + "\n", + " var nav_element = $('
');\n", + " nav_element.attr('style', 'width: 100%');\n", + " this.root.append(nav_element);\n", + "\n", + " // Define a callback function for later on.\n", + " function toolbar_event(event) {\n", + " return fig.toolbar_button_onclick(event['data']);\n", + " }\n", + " function toolbar_mouse_event(event) {\n", + " return fig.toolbar_button_onmouseover(event['data']);\n", + " }\n", + "\n", + " for(var toolbar_ind in mpl.toolbar_items) {\n", + " var name = mpl.toolbar_items[toolbar_ind][0];\n", + " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", + " var image = mpl.toolbar_items[toolbar_ind][2];\n", + " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", + "\n", + " if (!name) {\n", + " // put a spacer in here.\n", + " continue;\n", + " }\n", + " var button = $('