Commit 29ab5fe1 by Antoine RAVETTA

### removing annoying checkpoints

parent 87ffc984
 { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Crank-Nicholson scheme" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Cylindrical Diffraction Term" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "\"\"\"\n", "### Adapted crank Nicholson class.\n", "\n", "* Prepare half-step method.\n", "* Prepare non linear term for one of the half-step.\n", "* Use \$V\$ advection to add first radial derivative.\n", "\n", "\$V\$ is a function of \$r\$\n", "\$\$\n", "V(r) = \\dfrac{i}{2 k_0} \\dfrac{1}{r}\n", "\$\$\n", "and \$V(0) = 0\$.\n", "**Warning**: do not use \$V(0)=0\$ for any calculation.\n", "\n", "**Warning**: Test scheme matrices separately.\n", "\"\"\"\n", "\n", "import numpy as np\n", "import scipy.sparse\n", "import scipy.linalg as la\n", "\n", "class CrankNicolson:\n", " \"\"\"A class that solves dE/dz = D*d2E/dr2 + V(r)*dE/dr\"\"\"\n", " \n", " # Cylindrical grid\n", " def set_grid(self, r_max, n_r, z_min, z_max, n_z):\n", "\n", " self.r_max, self.n_r = r_max, n_r\n", " self.z_min, self.z_max, self.n_z = z_min, z_max, n_z\n", " self.r_pts, self.delta_r = np.linspace(0, r_max, n_r, retstep=True, endpoint=False)\n", " self.z_pts, self.delta_z = np.linspace(z_min, z_max, n_z, retstep=True, endpoint=False)\n", " \n", " # Parameters of the scheme\n", " def set_parameters(self, D, V):\n", " \n", " # V has to be vectorised\n", " self.D, self.V = D, V\n", " \n", " \n", " \n", " # One solving step for r dependency\n", " def solve(self, E_init):\n", " \n", " # Coefficient of matrices\n", " sig = self.D * self.delta_z / 2. / self.delta_r**2\n", " nu = lambda x: - self.delta_z / 4. / self.delta_r * self.V(x) # minus sign for V convention\n", " \n", " # Empty solution matrix\n", " self.E_matrix = np.zeros([self.n_z, self.n_r], dtype=complex)\n", " \n", " # Sparse solver\n", " A = self._fillA_sp(sig, nu, self.n_r)\n", " B = self._fillB_sp(sig, nu, self.n_r)\n", " \n", " # Set boundary conditions \n", " # Dirichlet at infinity\n", " A[1,-1] = 1.0\n", " A[2,-2] = 0.0\n", " B[-1,-1] = 0.0\n", " B[-1,-2] = 0.0\n", " # Neumann at r=0\n", " A[0,1] = -2*sig\n", " B[0,1] = 2*sig\n", " \n", " # Propagate\n", " E = E_init.copy()\n", " for n in range(self.n_z):\n", " self.E_matrix[n,:] = E\n", " \n", " E = la.solve_banded((1,1),A, B.dot(E), check_finite=False)\n", " \n", " \n", " \n", " # Deep copy of results for export\n", " # np.savetxt ?\n", " def get_E(self):\n", " \n", " return self.E_matrix.copy()\n", " \n", " # Diagonal packing for banded\n", " def _fillA_sp(self, sig, nu, n):\n", " \"\"\"Returns a tridiagonal matrix in compact form ab[1+i-j,j]=a[i,j]\"\"\"\n", " \n", " A = np.zeros([3,n], dtype=complex) # A has three diagonals and size n\n", " # Superdiagonal\n", " A[0,1:] = -(sig - nu(self.r_pts[:-1]))\n", " # Diagonal\n", " A[1] = 1+2*sig\n", " # Subdiagonal\n", " A[2,:-1] = -(sig + nu(self.r_pts[1:]))\n", " \n", " return A\n", " \n", " # Sparse tridiagonal storage\n", " def _fillB_sp(self, sig, nu, n):\n", " \"\"\"Returns a tridiagonal sparse matrix in csr-form\"\"\"\n", " \n", " _o = np.ones(n, dtype=complex)\n", " supdiag = (sig - nu(self.r_pts[:-1]))\n", " diag = (1-2*sig)*_o\n", " subdiag = (sig + nu(self.r_pts[1:]))\n", " \n", " return scipy.sparse.diags([supdiag, diag, subdiag], [1,0,-1], (n,n), format=\"csr\")\n", " \n", " " ] }, { "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.3" } }, "nbformat": 4, "nbformat_minor": 2 }
 { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "DATA PRODUCTION\n", "\n", "We define some constants for the whole problem, as well as some functions :\n", "\n", "- wavevector\n", "- potential\n", "- initialisation of E" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'CrankNicolson'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mCrankNicolson\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mlambd\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0.7e-6\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'CrankNicolson'" ] } ], "source": [ "import numpy as np\n", "import CrankNicolson\n", "\n", "lambd = 0.7e-6\n", "\n", "k = 2 * np.pi / lambd\n", "\n", "w0 = 1e-3\n", "Pin = 1\n", "\n", "diff_coeff = 1j*1/(2 * k)\n", "#diff_coeff = 0\n", "#diff_coeff = 1e-4\n", "\n", "print('diff :', diff_coeff)\n", "\n", "\n", "def potential(r):\n", " try:\n", " return diff_coeff / r\n", " except ZeroDivisionError:\n", " return 0\n", "\n", "\n", "def gaussian(r, r0=0, w0=1, Pin=1):\n", " return np.sqrt(2*Pin/(np.pi*w0**2)) * np.exp(-(r-r0)** 2/(w0**2))\n", "\n", "\n", "def initial_enveloppe(r_pts, w0, Pin):\n", " return np.array([gaussian(r_pts[i], w0=w0, Pin=Pin) for i in range(len(r_pts))])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Instanciation of the CN Class" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'CrankNicolson' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mcrank\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mCrankNicolson\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mcrank\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mset_grid\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mr_max\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1e-2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mn_r\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m100\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mz_min\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mz_max\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mn_z\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m200\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mcrank\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mset_parameters\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mD\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdiff_coeff\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mV\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mpotential\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mNameError\u001b[0m: name 'CrankNicolson' is not defined" ] } ], "source": [ "crank = CrankNicolson()\n", "\n", "crank.set_grid(r_max=1e-2, n_r=100, z_min=0, z_max=10, n_z=200)\n", "crank.set_parameters(D=diff_coeff, V=potential)\n", "\n", "crank.solve(initial_enveloppe(crank.r_pts, w0, Pin))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save the result for later analysis" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.savetxt(\"../CN_cylindric_complex_E.dat\", np.abs(crank.E_matrix))\n", "np.savetxt(\"../CN_cylindric_complex_r_pts.dat\", crank.r_pts)\n", "np.savetxt(\"../CN_cylindric_complex_z_pts.dat\", crank.z_pts)" ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 2 }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!