{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#This code generates the figures used in the paper ''Diamagnetic mechanism of critical current non-reciprocity in multilayered superconductors''''\n",
    "#Copyright (c) 2021-2023 Jukka Vayrynen\n",
    "\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "font = {'family': 'serif',\n",
    "        #'color':  'darkred',\n",
    "        'weight': 'normal',\n",
    "        'size': 16,\n",
    "        }\n",
    "\n",
    "def n(B):\n",
    "    return np.round(B/Bperiod)\n",
    "\n",
    "def eta(B,Bparallel):\n",
    "    if (1- np.abs( np.sqrt((B/Bc)**2 + (Bparallel/Bc)**2 )) ) > 0:\n",
    "        return (1- np.abs( np.sqrt((B/Bc)**2 + (Bparallel/Bc)**2 )) )\n",
    "    else: return 0\n",
    "    \n",
    "# Matrix that includes avoided crossings:     \n",
    "def h(B,Ej):\n",
    "    size = 20\n",
    "    h = np.zeros( (size, size) )\n",
    "    for i in range(size):\n",
    "        h[i,i] = (1/2) * (i -(size/2) - B)**2\n",
    "        if i < size-1: \n",
    "            h[i,i+1] = Ej \n",
    "            h[i+1,i] = Ej \n",
    "    return h\n",
    "\n",
    "# Hamiltonian with Phi in units of Phi0\n",
    "def h2(Phi,Ej, c):\n",
    "    size = 20\n",
    "    h = np.zeros( (size, size) )\n",
    "    for i in range(size):\n",
    "        h[i,i] = np.abs( i -(size/2) ) * (1/2) + (i -(size/2) - c * Phi)**2\n",
    "        if i < size-1: \n",
    "            h[i,i+1] = Ej \n",
    "            h[i+1,i] = Ej \n",
    "    return h\n",
    "\n",
    "def energy(B,Ej):\n",
    "    evals, evecs = np.linalg.eigh(h(B,Ej))\n",
    "    return evals[0]\n",
    "\n",
    "def energy2(B,Ej,c):\n",
    "    evals, evecs = np.linalg.eigh(h2(B,Ej,c))\n",
    "    return evals[0]\n",
    "\n",
    "def energies2(B,Ej,c):\n",
    "    evals, evecs = np.linalg.eigh(h2(B,Ej,c))\n",
    "    return evals\n",
    "\n",
    "def averageH(B,T):\n",
    "    evals, evecs = np.linalg.eigh(h(B,0))   \n",
    "    weights = [np.exp(-e/T) for e in evals]\n",
    "    z = np.sum( weights )\n",
    "    return np.sum( evals*weights ) / z\n",
    "\n",
    "'''Theta functions from mpmath:'''\n",
    "\n",
    "import mpmath\n",
    "\n",
    "#Define the theta function\n",
    "def theta(z,tau, derivative = 0):\n",
    "    '''Jacobi Theta function'''\n",
    "    q = np.exp(np.pi * 1j * tau)\n",
    "    return mpmath.jtheta(3, z*np.pi, q, derivative = derivative) * (np.pi)**derivative\n",
    "\n",
    "def averageN(z, T):\n",
    "    t = 2 * np.pi * T\n",
    "    return z + (1/2)*(T**2) * (  theta(z,1j*t)* theta(z,1j*t,3) -  theta(z,1j*t,2)*theta(z,1j*t,1)    ) / theta(z,1j*t)**2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Smoothening by quantum fluctuations; Josephson vortex; Figures for paper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "Bc = 0.3 * np.pi**2 / 3\n",
    "Bperiod = np.pi**2 / 3\n",
    "c =  (1/2) * np.pi**2\n",
    "\n",
    "numpoints = 300\n",
    "Bvals = np.linspace(-Bc, Bc, numpoints)\n",
    "BvalsSparse = np.linspace(-Bc, Bc, 30)\n",
    "\n",
    "def DeltaI(Bparallel):\n",
    "    return [ (n(B) - (B/Bperiod)) * eta(B,Bparallel)   for B in Bvals ]\n",
    "#print(DeltaI)\n",
    "plt.figure(figsize=(12,6))\n",
    "\n",
    "def expn(Bparallel, Ej):\n",
    "    # Current is equal to (pi/2 - 1 ) pi Phi/Phi0  - (Phi0 / pi^2 ) (dE / d Phi) / E0\n",
    "    grad = [(0.5 * np.pi) * np.pi *(B/Bperiod) for B in Bvals] -  (1 / np.pi**2) * np.gradient([energy2(B/Bperiod,Ej,c) for B in Bvals],  (Bvals[-1] - Bvals[0])/numpoints )\n",
    "    return grad #* [ eta(B,Bparallel)   for B in Bvals ]\n",
    "    \n",
    "for Ej in [0.0, 0.2]:\n",
    "    #plt.plot(Bvals, DeltaI(Bp) )\n",
    "    #plt.plot(Bvals, expn(0, Ej))\n",
    "    #plt.plot(Bvals, [ 0.2* np.round(B*np.pi**2 /3 ) for B in Bvals] )\n",
    "    #plt.plot(Bvals, [ B * (np.pi**2 / 3) - 0.1 * np.sin(2*np.pi *  B * (np.pi**2 / 3) )  for B in Bvals] )\n",
    "    plt.plot(Bvals,  [energies2(B/Bperiod,Ej,c)[[0]] for B in Bvals] , label=r'$E_J$ = %.2f' % (Ej) )\n",
    "    plt.plot(Bvals,  [energies2(B/Bperiod,Ej,c)[[1]] for B in Bvals] , label=r'$E_J$ = %.2f' % (Ej) )\n",
    "    \n",
    "    #plt.plot(Bvals,  [energy2(B/Bperiod,Ej,c) for B in Bvals] )\n",
    "    \n",
    "#plt.plot(Bvals, [energy(B/Bperiod,Ej) for B in Bvals] )\n",
    "#plt.plot(Bvals, [ B * (np.pi**2 / 3) - 0.1 * np.sin(2*np.pi *  B * (np.pi**2 / 3) )  for B in Bvals] )\n",
    "\n",
    "plt.vlines(3 / (2 * np.pi**2), 0,1.4)\n",
    "\n",
    "# plt.plot(Bvals, [ 0.15*eta(B,0)   for B in Bvals ]  )\n",
    "# plt.plot(Bvals, [ -0.15*eta(B,0)   for B in Bvals ]  )\n",
    "plt.ylim((-0.1,1.5))\n",
    "plt.legend()\n",
    "#plt.title('$\\Delta I(B_y)$', fontdict=font)\n",
    "plt.axvline(color=\"grey\")\n",
    "plt.axhline(color=\"grey\")\n",
    "\n",
    "plt.xlabel(r'$\\Phi/ \\Phi_0$', fontdict=font)\n",
    "plt.ylabel(r'$E_{\\mathrm{Vortex}}$', fontdict=font)\n",
    "\n",
    "image_format = 'svg' # e.g .png, .svg, etc.\n",
    "image_name = 'VxEnergyvsB_final.svg'\n",
    "\n",
    "plt.savefig(image_name, format=image_format, dpi=1200)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Bc = 0.3#* np.pi**2 / 3\n",
    "Bperiod = 1#* np.pi**2 / 3\n",
    "c =  (1/2) * np.pi**2\n",
    "\n",
    "numpoints = 300\n",
    "Bvals = np.linspace(-Bc, Bc, numpoints)\n",
    "BvalsSparse = np.linspace(-Bc, Bc, 40)\n",
    "\n",
    "def DeltaI(Bparallel):\n",
    "    return [ (n(B) - (B/Bperiod)) * eta(B,Bparallel)   for B in Bvals ]\n",
    "#print(DeltaI)\n",
    "plt.figure(figsize=(12,6))\n",
    "\n",
    "def smoothDeltaI(Bparallel, Ej):\n",
    "    # Current is equal to (pi/2 - 1 ) pi Phi/Phi0  - (Phi0 / pi^2 ) (dE / d Phi) / E0\n",
    "    grad = [(c - np.pi) * (B/Bperiod) for B in Bvals] -  (1 / np.pi**2) * np.gradient([energy2(B/Bperiod,Ej,c) for B in Bvals],  (Bvals[-1] - Bvals[0])/numpoints )\n",
    "    return grad * [ eta(B/Bperiod,Bparallel/Bperiod)   for B in Bvals ]\n",
    "\n",
    "def expn(Bparallel):\n",
    "    # Current is equal to (pi/2 - 1 ) pi Phi/Phi0  - (Phi0 / pi^2 ) (dE / d Phi) / E0\n",
    "    grad = [(0.5 * np.pi) * np.pi *(B/Bperiod) for B in Bvals] -  (1 / np.pi**2) * np.gradient([energy2(B/Bperiod,Ej,c) for B in Bvals],  (Bvals[-1] - Bvals[0])/numpoints )\n",
    "    return grad #* [ eta(B,Bparallel)   for B in Bvals ]\n",
    "\n",
    "Ej = 0.01\n",
    "    \n",
    "for Ej in [0.01, 0.1 ,0.2,0.4,0.6,0.8]:\n",
    "    #plt.plot(Bvals, DeltaI(Bp) )\n",
    "    #plt.plot(Bvals, expn(Bp))\n",
    "    plt.plot((np.pi**2 / 3) *Bvals, smoothDeltaI(0, Ej)  , label=r'$E_J$ = %.2f' % (Ej) )\n",
    "\n",
    "    \n",
    "    #plt.plot(BvalsSparse, [- 0.07 * eta(B,0) * np.sin(2*np.pi*B/Bperiod*3.14 )  for B in BvalsSparse ], 'b+',  label='approximation' )\n",
    " \n",
    "\n",
    "plt.plot((np.pi**2 / 3) *BvalsSparse, [ ( 2* (np.pi**2 / 3) *B   - 2*np.pi * (np.pi**2 / 3) *B * (3 / np.pi**2) - 0.13* np.sin(2*np.pi *  (np.pi**2 / 3) *B  )  ) * ( eta(B,0)) for B in BvalsSparse ], 'b+',  label='approximation' )\n",
    "# plt.plot(Bvals, [ 0.15*eta(B,0)   for B in Bvals ]  )\n",
    "# plt.plot(Bvals, [ -0.15*eta(B,0)   for B in Bvals ]  )\n",
    "#pyplot.xlim((0,2))\n",
    "\n",
    "plt.legend()\n",
    "#plt.title('$\\Delta I(B_y)$', fontdict=font)\n",
    "plt.axvline(color=\"grey\")\n",
    "plt.axhline(color=\"grey\")\n",
    "\n",
    "plt.xlabel(r'$\\Phi/ \\Phi_0$', fontdict=font)\n",
    "plt.ylabel(r'$\\Delta I(\\Phi)$', fontdict=font)\n",
    "\n",
    "image_format = 'svg' # e.g .png, .svg, etc.\n",
    "image_name = 'VxSawtoothInsetFin.svg'\n",
    "\n",
    "plt.savefig(image_name, format=image_format, dpi=120)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Bc = 0.3\n",
    "Bperiod = 1\n",
    "c =  (1/2) * np.pi**2\n",
    "\n",
    "numpoints = 300\n",
    "Bvals = np.linspace(-Bc, Bc, numpoints)\n",
    "BvalsSparse = np.linspace(-Bc, Bc, 50)\n",
    "\n",
    "def DeltaI(Bparallel):\n",
    "    return [ (n(B) - (B/Bperiod)) * eta(B,Bparallel)   for B in Bvals ]\n",
    "#print(DeltaI)\n",
    "plt.figure(figsize=(12,6))\n",
    "\n",
    "def smoothDeltaI(Bparallel, Ej):\n",
    "    # Current is equal to (pi/2 - 1 ) pi Phi/Phi0  - (Phi0 / pi^2 ) (dE / d Phi) / E0\n",
    "    grad = [(0.5 * np.pi - 1) * np.pi *(B/Bperiod) for B in Bvals] -  (1 / np.pi**2) * np.gradient([energy2(B/Bperiod,Ej,c) for B in Bvals],  (Bvals[-1] - Bvals[0])/numpoints )\n",
    "    return grad * [ eta(B,Bparallel)   for B in Bvals ]\n",
    "\n",
    "def expn(Bparallel):\n",
    "    # Current is equal to (pi/2 - 1 ) pi Phi/Phi0  - (Phi0 / pi^2 ) (dE / d Phi) / E0\n",
    "    grad = [(0.5 * np.pi) * np.pi *(B/Bperiod) for B in Bvals] -  (1 / np.pi**2) * np.gradient([energy2(B/Bperiod,Ej,c) for B in Bvals],  (Bvals[-1] - Bvals[0])/numpoints )\n",
    "    return grad #* [ eta(B,Bparallel)   for B in Bvals ]\n",
    "\n",
    "Ej = 0.4\n",
    "\n",
    "for Bp in  [0,0.2,0.4,0.6,0.8]:\n",
    "    Bp = Bp * Bc\n",
    "    #plt.plot(Bvals, DeltaI(Bp) )\n",
    "    #plt.plot(Bvals, expn(Bp))\n",
    "    plt.plot(  (np.pi**2 / 3) *Bvals, smoothDeltaI(Bp, Ej)  , label=r'$B_{\\parallel}/B_{\\parallel,c}$ = %.2f' % (Bp/Bc) )\n",
    "    \n",
    "\n",
    "plt.plot(  (np.pi**2 / 3) *BvalsSparse, [ ( ( (np.pi / 3)  - 1) * np.pi  * 2* B  - 0.13* np.sin(2*np.pi *  B * (np.pi**2 / 3) )  ) * ( eta(B,0)) for B in BvalsSparse ], 'b+',  label='approximation' )\n",
    "#plt.plot(BvalsSparse, [ ( 2* B * (np.pi**2 / 3)  - 2*np.pi * B - 0.07* np.sin(2*np.pi *  B * (np.pi**2 / 3) )  ) * ( eta(B,0)) for B in BvalsSparse ], 'b+',  label='approximation' )\n",
    "\n",
    "\n",
    "\n",
    "plt.legend()\n",
    "#plt.title('$\\Delta I(B_y)$', fontdict=font)\n",
    "plt.axvline(color=\"grey\")\n",
    "plt.axhline(color=\"grey\")\n",
    "\n",
    "plt.xlabel(r'$\\Phi/ \\Phi_0$', fontdict=font)\n",
    "plt.ylabel(r'$\\Delta I(\\Phi)$', fontdict=font)\n",
    "\n",
    "image_format = 'svg' # e.g .png, .svg, etc.\n",
    "image_name = 'VxDeltaI_fin.svg'\n",
    "\n",
    "plt.savefig(image_name, format=image_format, dpi=1200)"
   ]
  }
 ],
 "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.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
