{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PINN Solution of the Nonlinear Shroedinger PDE\n",
    "\n",
    "This PyTorch code demonstrates the application of physically-informed neural networks (PINN) in the solution of a well-known Nonlinear Shroedinger PDE with periodic boundary condition\n",
    "\\begin{aligned}\n",
    "  &\\mathbf{u}_t = i * \\mathbf{u}_{xx} + i * |\\mathbf{u}|^2 * \\mathbf{u} \\in [0, T]\\times[-L, L]\\\\\n",
    "  &\\mathbf{u}(0, x) = \\mathbf{u}_0(x), \\quad \\forall x \\in [-L, L] \\\\\n",
    "  &\\mathbf{u}(t, -L) = \\mathbf{u}(t, L), \\quad \\forall t \\in [0, T]\n",
    "\\end{aligned}\n",
    "where $i^2 = -1$, and $[-L, L]$ covers one full period, i.e. $T = 2L$.  If we let $\\mathbf{u} = u + i * v$, then we obtain two PDEs\n",
    "\\begin{aligned}\n",
    "u_t &= -v_{xx} - (u^2 + v^2) * v \\\\\n",
    "v_t &= u_{xx} + (u^2 + v^2) * u\n",
    "\\end{aligned}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Libraries and Dependencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from itertools import chain\n",
    "from collections import OrderedDict\n",
    "import time\n",
    "import numpy as np\n",
    "import scipy as sp\n",
    "import scipy.io\n",
    "from scipy.interpolate import griddata\n",
    "from pyDOE import lhs\n",
    "import torch\n",
    "import torch.optim\n",
    "import torch.optim.lr_scheduler as lr_scheduler\n",
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "from mpl_toolkits.axes_grid1 import make_axes_locatable\n",
    "import matplotlib.gridspec as gridspec\n",
    "np.random.seed(1234)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Working on mps\n"
     ]
    }
   ],
   "source": [
    "# MPS or CUDA or CPU\n",
    "if torch.backends.mps.is_available():\n",
    "    device = torch.device('mps')\n",
    "elif torch.cuda.is_available():\n",
    "    device = torch.device('cuda')\n",
    "else:\n",
    "    device = torch.device('cpu')\n",
    "#\n",
    "print(f\"Working on {device}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "L = np.pi\n",
    "xlo = -L\n",
    "xhi = L\n",
    "period = xhi - xlo\n",
    "tlo = 0.0\n",
    "thi = 2.0\n",
    "sqrt2 = torch.tensor(np.sqrt(2)).float().to(device)\n",
    "# taking A = 1 and B = 1\n",
    "u0_ten = lambda x: 2.0/(2.0 - sqrt2 * torch.cos(x)) - 1.0\n",
    "v0_ten = lambda x: torch.zeros_like(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Physics-informed Neural Networks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# the deep neural network\n",
    "class DNN(torch.nn.Module):\n",
    "    def __init__(self, layers):\n",
    "        super(DNN, self).__init__()\n",
    "        # parameters\n",
    "        self.depth = len(layers) - 1\n",
    "        # set up layer order dict\n",
    "        self.activation = torch.nn.Tanh\n",
    "        layer_list = list()\n",
    "        for i in range(self.depth - 1): \n",
    "            layer_list.append(\n",
    "                ('layer_%d' % i, torch.nn.Linear(layers[i], layers[i+1]))\n",
    "            )\n",
    "            layer_list.append(('activation_%d' % i, self.activation()))\n",
    "            \n",
    "        layer_list.append(\n",
    "            ('layer_%d' % (self.depth - 1), torch.nn.Linear(layers[-2], layers[-1]))\n",
    "        )\n",
    "        layerDict = OrderedDict(layer_list)\n",
    "        # deploy layers\n",
    "        self.layers = torch.nn.Sequential(layerDict)\n",
    "        self.layers[0].weight = torch.load('initial_weights_PBC.pt')\n",
    "    def forward(self, x):\n",
    "        out = self.layers(x)\n",
    "        return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "class PhysicsInformedNN():\n",
    "    def __init__(self, period, m, X_PDE, layers,n_batches, batch_size):\n",
    "        # Prepare the periodic layer\n",
    "        m_vec = np.expand_dims(np.arange(1, m + 1), axis = 0)\n",
    "        self.ms = torch.tensor(2.0 * np.pi/period * m_vec).float().to(device)\n",
    "        # PDE data, gradients will be computed on these points so requires_grad = True\n",
    "        self.t_PDE = torch.tensor(X_PDE[:, 0:1], requires_grad=True).float().to(device)\n",
    "        self.x_PDE = torch.tensor(X_PDE[:, 1:2], requires_grad=True).float().to(device)\n",
    "        \n",
    "       \n",
    "    \n",
    "        layers[0] = int(2 * m + 2)\n",
    "        self.layers = layers\n",
    "        self.n_batches = n_batches\n",
    "        self.batch_size = batch_size\n",
    "\n",
    "       \n",
    "        \n",
    "        # deep neural networks\n",
    "        self.dnn = DNN(layers).to(device)    \n",
    "        # prepare the optimizer\n",
    "        self.optimizer_Adam = torch.optim.Adam(self.dnn.parameters(), lr = 1e-3)\n",
    "        # add a learning rate scheduler\n",
    "        self.optimizer_LBFGS = torch.optim.LBFGS(\n",
    "            self.dnn.parameters(), \n",
    "            lr=1.0, \n",
    "            max_iter=10000, \n",
    "            max_eval=5000, \n",
    "            history_size=50,\n",
    "            tolerance_grad=1e-7, \n",
    "            tolerance_change=1.0 * np.finfo(float).eps,\n",
    "            line_search_fn=\"strong_wolfe\"       # can be \"strong_wolfe\"\n",
    "        )        \n",
    "        self.scheduler = lr_scheduler.ExponentialLR(self.optimizer_Adam, gamma=0.99)\n",
    "       \n",
    "        self.iter = 0\n",
    "    \n",
    "    def NN_reset_iter(self):\n",
    "        self.iter = 0\n",
    "    # evaluater neural network\n",
    "    # with transformation to include the initial condition\n",
    "    def NN_eval(self, t, x):  \n",
    "        x_trans = torch.matmul(x, self.ms)\n",
    "        NN = self.dnn(torch.cat([t, torch.ones_like(x), torch.cos(x_trans), torch.sin(x_trans)], dim = 1))\n",
    "        uNN = NN[:, 0][:, None]\n",
    "        vNN = NN[:, 1][:, None]\n",
    "        u0_torch = u0_ten(x)\n",
    "        v0_torch = v0_ten(x)\n",
    "        unew = u0_torch*torch.exp(-0.1*t) + t * uNN\n",
    "        vnew = v0_torch*torch.exp(-0.1*t) + t * vNN\n",
    "        return unew,vnew\n",
    "\n",
    "    # compute the PDE\n",
    "    def pde_eval(self, t, x):\n",
    "        \"\"\" The pytorch autograd version of calculating residual \"\"\"\n",
    "        u, v = self.NN_eval(t, x)\n",
    "        # compute the derivatives for u\n",
    "        u_t    = torch.autograd.grad(u,     t, grad_outputs = torch.ones_like(u), retain_graph = True, create_graph=True)[0]\n",
    "        u_x    = torch.autograd.grad(u,     x, grad_outputs = torch.ones_like(u), retain_graph = True, create_graph=True)[0]\n",
    "        u_xx   = torch.autograd.grad(u_x,   x, grad_outputs = torch.ones_like(u), retain_graph = True, create_graph=True)[0]\n",
    "        v_t    = torch.autograd.grad(v,     t, grad_outputs = torch.ones_like(u), retain_graph = True, create_graph=True)[0]\n",
    "        v_x    = torch.autograd.grad(v,     x, grad_outputs = torch.ones_like(u), retain_graph = True, create_graph=True)[0]\n",
    "        v_xx   = torch.autograd.grad(v_x,   x, grad_outputs = torch.ones_like(u), retain_graph = True, create_graph=True)[0]   \n",
    "        # pre-compute the norm of \\mathbf{u}, so that it can be re-used\n",
    "        U_norm = torch.pow(u, 2.0) + torch.pow(v, 2.0)\n",
    "        Eq1    = u_t + v_xx + U_norm * v\n",
    "        Eq2    = v_t - u_xx - U_norm * u\n",
    "        return Eq1, Eq2\n",
    "    # compute the total loss for the second-order optimizer\n",
    "    def loss_func(self):\n",
    "        # reset the gradient\n",
    "        self.optimizer_LBFGS.zero_grad()\n",
    "        # compute PDE loss\n",
    "        pde1_pred, pde2_pred = self.pde_eval(self.t_PDE, self.x_PDE)\n",
    "        loss_PDE = torch.mean(pde1_pred ** 2) + torch.mean(pde2_pred ** 2) \n",
    "        \n",
    "        # compute the total loss, it can be weighted\n",
    "        loss = loss_PDE\n",
    "        # backward propagation\n",
    "        loss.backward()\n",
    "        # increase the iteration counter\n",
    "        self.iter += 1\n",
    "        # output\n",
    "        # output the progress\n",
    "        if self.iter % 1000 == 0:\n",
    "            print('Iter %5d, Total: %10.4e, PDE: %10.4e' % (self.iter, loss.item(), loss_PDE.item()))\n",
    "        return loss\n",
    "    #\n",
    "    def train(self, nIter):\n",
    "        # start the training with Adam first\n",
    "        self.dnn.train()\n",
    "        print('Starting with Adam')\n",
    "        for epoch in range(nIter):\n",
    "            for batch_id in range(self.n_batches):\n",
    "           \n",
    "                # compute PDE loss\n",
    "                pde1_pred, pde2_pred = self.pde_eval(self.t_PDE[batch_id*self.batch_size:(batch_id + 1)*self.batch_size, :], self.x_PDE[batch_id*self.batch_size:(batch_id + 1)*self.batch_size, :])\n",
    "                loss_PDE = torch.mean(pde1_pred ** 2) + torch.mean(pde2_pred ** 2)   \n",
    "                # compute the total loss, it can be weighted\n",
    "                loss = loss_PDE\n",
    "                # Backward and optimize\n",
    "                self.optimizer_Adam.zero_grad()\n",
    "                loss.backward()\n",
    "                self.optimizer_Adam.step() \n",
    "                # output the progress\n",
    "            if (epoch + 1) % 1000 == 0:\n",
    "                print('Iter %5d,  PDE: %10.4e' % (epoch + 1, loss_PDE.item()))\n",
    "                # change the learning rate\n",
    "                self.scheduler.step()\n",
    "       # self.optimizer_LBFGS.step(self.loss_func)   \n",
    "        self.optimizer_LBFGS.step(self.loss_func)      \n",
    "        \n",
    "    def predict(self, X):\n",
    "        t = torch.tensor(X[:, 0:1], requires_grad=True).float().to(device)\n",
    "        x = torch.tensor(X[:, 1:2], requires_grad=True).float().to(device)\n",
    "        self.dnn.eval()\n",
    "        u, v = self.NN_eval(t, x)\n",
    "        u = u.detach().cpu().numpy()\n",
    "        v = v.detach().cpu().numpy()\n",
    "        return u,v\n",
    "\n",
    "## Configurations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Training"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Starting with Adam\n",
      "Iter  1000,  PDE: 1.0961e+00\n",
      "Iter  2000,  PDE: 3.6645e-01\n",
      "Iter  3000,  PDE: 1.1938e+00\n",
      "Iter  4000,  PDE: 3.2840e-01\n",
      "Iter  5000,  PDE: 1.2833e+00\n",
      "Iter  6000,  PDE: 4.4857e-01\n",
      "Iter  7000,  PDE: 4.8462e-01\n",
      "Iter  8000,  PDE: 8.3074e-01\n",
      "Iter  9000,  PDE: 1.8005e+00\n",
      "Iter 10000,  PDE: 1.2745e+00\n",
      "Iter 11000,  PDE: 5.3999e-01\n",
      "Iter 12000,  PDE: 8.9092e-01\n",
      "Iter 13000,  PDE: 1.7856e+00\n",
      "Iter 14000,  PDE: 1.2231e-01\n",
      "Iter 15000,  PDE: 8.0523e-01\n",
      "Iter 16000,  PDE: 1.7989e+00\n",
      "Iter 17000,  PDE: 8.0212e-02\n",
      "Iter 18000,  PDE: 1.1251e+00\n",
      "Iter 19000,  PDE: 1.8699e+00\n",
      "Iter 20000,  PDE: 8.4341e-01\n",
      "Iter 21000,  PDE: 9.0676e-01\n",
      "Iter 22000,  PDE: 2.4990e-01\n",
      "Iter 23000,  PDE: 5.5662e-01\n",
      "Iter 24000,  PDE: 7.3756e-01\n",
      "Iter 25000,  PDE: 6.2240e-02\n",
      "Iter 26000,  PDE: 4.3442e-01\n",
      "Iter 27000,  PDE: 1.6558e+00\n",
      "Iter 28000,  PDE: 7.9399e-01\n",
      "Iter 29000,  PDE: 1.8684e+00\n",
      "Iter 30000,  PDE: 5.9371e-01\n",
      "Iter 31000,  PDE: 1.9707e-01\n",
      "Iter 32000,  PDE: 8.4345e-01\n",
      "Iter 33000,  PDE: 5.0218e-02\n",
      "Iter 34000,  PDE: 2.4026e+00\n",
      "Iter 35000,  PDE: 8.8810e-01\n",
      "Iter 36000,  PDE: 9.2035e-02\n",
      "Iter 37000,  PDE: 3.0857e-02\n",
      "Iter 38000,  PDE: 4.9284e-01\n",
      "Iter 39000,  PDE: 8.0387e-02\n",
      "Iter 40000,  PDE: 5.7319e-01\n",
      "Iter 41000,  PDE: 4.2410e-02\n",
      "Iter 42000,  PDE: 1.9178e+00\n",
      "Iter 43000,  PDE: 1.5314e+00\n",
      "Iter 44000,  PDE: 5.2254e-01\n",
      "Iter 45000,  PDE: 5.6005e-01\n",
      "Iter 46000,  PDE: 4.7729e-02\n",
      "Iter 47000,  PDE: 1.6376e+00\n",
      "Iter 48000,  PDE: 6.9165e-01\n",
      "Iter 49000,  PDE: 2.1929e-01\n",
      "Iter 50000,  PDE: 7.3890e-02\n",
      "Iter  1000, Total: 4.4392e-03, PDE: 4.4392e-03\n"
     ]
    }
   ],
   "source": [
    "layers = [2, 32, 32, 32, 32, 32, 32, 32, 2]\n",
    "m = 16\n",
    "n_batches = 5\n",
    "batch_size = 3280\n",
    "ptsIC=np.load('ptsIC.npy')\n",
    "ptsBC=np.load('ptsBC.npy')\n",
    "ptsPDE=np.load('random_data.npy')\n",
    "ptsPDE = np.vstack((ptsIC,ptsPDE))\n",
    "ptsPDE = np.vstack((ptsBC,ptsPDE))\n",
    "\n",
    "model = PhysicsInformedNN(period,m,ptsPDE,layers,n_batches, batch_size)\n",
    "model.train(50000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# apply PINN to the same grid as the quadrature solution for comparison\n",
    "t = np.linspace(tlo, thi, 201)\n",
    "x = np.linspace(xlo, xhi, 501)\n",
    "T, X = np.meshgrid(t, x)\n",
    "pts_flat = np.hstack((T.flatten()[:, None], X.flatten()[:, None]))\n",
    "u_pred, v_pred = model.predict(pts_flat)           \n",
    "u_pred = griddata(pts_flat, u_pred.flatten(), (T, X), method='cubic')\n",
    "v_pred = griddata(pts_flat, v_pred.flatten(), (T, X), method='cubic')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualizations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = scipy.io.loadmat('Data/NLS.mat')\n",
    "t = data['t'].flatten()[:,None]\n",
    "x2 = data['x'].flatten()[:,None]\n",
    "u_sol = np.real(data['Exact']).T\n",
    "v_sol = np.imag(data['Exact']).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 119756 characters]",
      "text/plain": [
       "<Figure size 1800x600 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "####### Row 1: u(t,x) slices ################## \n",
    "\n",
    "\"\"\" The aesthetic setting has changed. \"\"\"\n",
    "\n",
    "fig = plt.figure(figsize=(18, 6))\n",
    "#\n",
    "ax = plt.subplot(1, 3, 1)\n",
    "ax.plot(x2, u_sol[0,:], 'bo-', linewidth = 2, label = 'Exact')       \n",
    "ax.plot(x, u_pred[:, 0], 'rx--', linewidth = 2, label = 'u')\n",
    "ax.set_xlabel('$x$')\n",
    "ax.set_ylabel('$u(t,x)$')    \n",
    "ax.set_title('$t = %.1f$' %(t[0]), fontsize = 24)\n",
    "\n",
    "\n",
    "plt.locator_params(axis = 'y', nbins = 5)\n",
    "plt.locator_params(axis = 'x', nbins = 5)\n",
    "for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +\n",
    "             ax.get_xticklabels() + ax.get_yticklabels()):\n",
    "    item.set_fontsize(24)\n",
    "\n",
    "ax = plt.subplot(1, 3, 2)\n",
    "ax.plot(x,u_sol[100,:], 'bo-', linewidth = 2, label = 'Exact')       \n",
    "ax.plot(x, u_pred[:, 100], 'rx--', linewidth = 2, label = 'u')\n",
    "\n",
    "ax.set_xlabel('x')\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "ax.set_title('$t = %.1f$' %(t[100]), fontsize = 24)\n",
    "ax.legend(\n",
    "    loc='upper center', \n",
    "    bbox_to_anchor=(0.5, -0.15), \n",
    "    ncol=5, \n",
    "    frameon=False, \n",
    "    prop={'size': 24}\n",
    ")\n",
    "plt.locator_params(axis = 'y', nbins = 5)\n",
    "plt.locator_params(axis = 'x', nbins = 5)\n",
    "for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +\n",
    "             ax.get_xticklabels() + ax.get_yticklabels()):\n",
    "    item.set_fontsize(24)\n",
    "\n",
    "ax = plt.subplot(1, 3, 3)\n",
    "ax.plot(x,u_sol[200,:], 'bo-', linewidth = 2, label = 'Exact')       \n",
    "ax.plot(x, u_pred[:, 200], 'rx--', linewidth = 2, label = 'u')\n",
    "\n",
    "ax.set_xlabel('$x$')\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "ax.set_title('$t = %.1f$' %(t[200]), fontsize = 24)\n",
    "plt.locator_params(axis = 'y', nbins = 5)\n",
    "plt.locator_params(axis = 'x', nbins = 5)\n",
    "for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +\n",
    "             ax.get_xticklabels() + ax.get_yticklabels()):\n",
    "    item.set_fontsize(24)\n",
    "\n",
    "plt.show()\n",
    "fig.savefig('NLSu_1D_BL+PBC+IC2+minibatch.png',format='png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 105000 characters]",
      "text/plain": [
       "<Figure size 1800x600 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(18, 6))\n",
    "#\n",
    "ax = plt.subplot(1, 3, 1)\n",
    "ax.plot(x2,v_sol[0,:], 'bo-', linewidth = 2, label = 'Exact')       \n",
    "ax.plot(x, v_pred[:, 0], 'rx--', linewidth = 2, label = 'u')\n",
    "ax.set_xlabel('$x$')\n",
    "ax.set_ylabel('$v(t,x)$')    \n",
    "ax.set_title('$t = %.1f$' %(t[0]), fontsize = 24)\n",
    "\n",
    "\n",
    "plt.locator_params(axis = 'y', nbins = 5)\n",
    "plt.locator_params(axis = 'x', nbins = 5)\n",
    "for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +\n",
    "             ax.get_xticklabels() + ax.get_yticklabels()):\n",
    "    item.set_fontsize(24)\n",
    "\n",
    "ax = plt.subplot(1, 3, 2)\n",
    "ax.plot(x,-v_sol[100,:], 'bo-', linewidth = 2, label = 'Exact')       \n",
    "ax.plot(x, v_pred[:, 100], 'rx--', linewidth = 2, label = 'u')\n",
    "\n",
    "ax.set_xlabel('x')\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "ax.set_title('$t = %.1f$' %(t[100]), fontsize = 24)\n",
    "ax.legend(\n",
    "    loc='upper center', \n",
    "    bbox_to_anchor=(0.5, -0.15), \n",
    "    ncol=5, \n",
    "    frameon=False, \n",
    "    prop={'size': 24}\n",
    ")\n",
    "plt.locator_params(axis = 'y', nbins = 5)\n",
    "plt.locator_params(axis = 'x', nbins = 5)\n",
    "for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +\n",
    "             ax.get_xticklabels() + ax.get_yticklabels()):\n",
    "    item.set_fontsize(24)\n",
    "\n",
    "ax = plt.subplot(1, 3, 3)\n",
    "ax.plot(x,-v_sol[200,:], 'bo-', linewidth = 2, label = 'Exact')       \n",
    "ax.plot(x, v_pred[:, 200], 'rx--', linewidth = 2, label = 'u')\n",
    "\n",
    "ax.set_xlabel('$x$')\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "ax.set_title('$t = %.1f$' %(t[200]), fontsize = 24)\n",
    "plt.locator_params(axis = 'y', nbins = 5)\n",
    "plt.locator_params(axis = 'x', nbins = 5)\n",
    "for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +\n",
    "             ax.get_xticklabels() + ax.get_yticklabels()):\n",
    "    item.set_fontsize(24)\n",
    "\n",
    "plt.show()\n",
    "fig.savefig('NLSv_1D_BL+PBC+IC2+minibatch.png',format='png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def relative_error_l2(pred,exact):\n",
    "    error_l2 = np.sqrt(np.sum(np.power(pred - exact,2)))\n",
    "    relative = error_l2/np.sqrt(np.sum(np.power(exact,2)))\n",
    "    return relative\n",
    "def relative_error_l1(pred,exact):\n",
    "    error_l1 = np.sum(np.abs(pred-exact))\n",
    "    relative = error_l1/np.sum(np.abs(exact))\n",
    "    return relative\n",
    "def relative_error_linf(pred,exact):\n",
    "    error_linf = np.max(np.abs(pred-exact))\n",
    "    relative = error_linf/np.max(np.abs(exact))\n",
    "    return relative"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "l2: 0.08317160580870941\n",
      "l1: 0.06398385198591437\n",
      "linf: 0.07478112571861988\n"
     ]
    }
   ],
   "source": [
    "print(f'l2: {relative_error_l2(u_pred.T,u_sol)}')\n",
    "print(f'l1: {relative_error_l1(u_pred.T,u_sol)}')\n",
    "print(f'linf: {relative_error_linf(u_pred.T,u_sol)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "l2: 0.1390645799666827\n",
      "l1: 0.12444597159039944\n",
      "linf: 0.15850632627130876\n"
     ]
    }
   ],
   "source": [
    "print(f'l2: {relative_error_l2(-v_pred.T,v_sol)}')\n",
    "print(f'l1: {relative_error_l1(-v_pred.T,v_sol)}')\n",
    "print(f'linf: {relative_error_linf(-v_pred.T,v_sol)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 239592 characters]",
      "text/plain": [
       "<Figure size 2400x600 with 6 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(24, 6))\n",
    "\n",
    "ax = plt.subplot(1, 3, 1)\n",
    "h = ax.imshow(u_sol.T, interpolation='nearest', cmap='rainbow', \n",
    "              extent=[tlo, thi, x.min(), x.max()], \n",
    "              origin='lower', aspect='auto')\n",
    "divider = make_axes_locatable(ax)\n",
    "cax = divider.append_axes(\"right\", size=\"5%\", pad=0.10)\n",
    "cbar = fig.colorbar(h, cax=cax)\n",
    "cbar.ax.tick_params(labelsize=24) \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "ax.set_xlabel('$t$', size=24)\n",
    "ax.set_ylabel('$x$', size=24)\n",
    "\n",
    "ax.set_title('$NLS: u_{exact}(t,x)$', fontsize = 24) # font size doubled\n",
    "ax.tick_params(labelsize=24)\n",
    "\n",
    "ax = plt.subplot(1, 3, 2)\n",
    "h = ax.imshow(u_pred, interpolation='nearest', cmap='rainbow', \n",
    "              extent=[tlo, thi, x.min(), x.max()], \n",
    "              origin='lower', aspect='auto')\n",
    "divider = make_axes_locatable(ax)\n",
    "cax = divider.append_axes(\"right\", size=\"5%\", pad=0.10)\n",
    "cbar = fig.colorbar(h, cax=cax)\n",
    "cbar.ax.tick_params(labelsize=24) \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "ax.set_xlabel('$t$', size=24)\n",
    "\n",
    "\n",
    "ax.set_title('$NLS: u_{pred}(t,x)$', fontsize = 24) # font size doubled\n",
    "ax.tick_params(labelsize=24)\n",
    "\n",
    "ax = plt.subplot(1, 3, 3)\n",
    "h = ax.imshow(np.abs(u_pred-u_sol.T), interpolation='nearest', cmap='rainbow', \n",
    "              extent=[tlo, thi, x.min(), x.max()], \n",
    "              origin='lower', aspect='auto')\n",
    "divider = make_axes_locatable(ax)\n",
    "cax = divider.append_axes(\"right\", size=\"5%\", pad=0.10)\n",
    "cbar = fig.colorbar(h, cax=cax)\n",
    "cbar.ax.tick_params(labelsize=24) \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "ax.set_xlabel('$t$', size=24)\n",
    "\n",
    "\n",
    "ax.set_title('$NLS(Error): u(t,x)$', fontsize = 24) # font size doubled\n",
    "ax.tick_params(labelsize=24)\n",
    "\n",
    "\n",
    "plt.show()\n",
    "fig.savefig('NLS3u_1D_BL+PBC+IC2+minibatch.png',format='png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 277336 characters]",
      "text/plain": [
       "<Figure size 2400x600 with 6 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(24, 6))\n",
    "\n",
    "ax = plt.subplot(1, 3, 1)\n",
    "h = ax.imshow(-v_sol.T, interpolation='nearest', cmap='rainbow', \n",
    "              extent=[tlo, thi, x.min(), x.max()], \n",
    "              origin='lower', aspect='auto')\n",
    "divider = make_axes_locatable(ax)\n",
    "cax = divider.append_axes(\"right\", size=\"5%\", pad=0.10)\n",
    "cbar = fig.colorbar(h, cax=cax)\n",
    "cbar.ax.tick_params(labelsize=24) \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "ax.set_xlabel('$t$', size=24)\n",
    "ax.set_ylabel('$x$', size=24)\n",
    "\n",
    "ax.set_title('$NLS: v_{exact}(t,x)$', fontsize = 24) # font size doubled\n",
    "ax.tick_params(labelsize=24)\n",
    "\n",
    "ax = plt.subplot(1, 3, 2)\n",
    "h = ax.imshow(v_pred, interpolation='nearest', cmap='rainbow', \n",
    "              extent=[tlo, thi, x.min(), x.max()], \n",
    "              origin='lower', aspect='auto')\n",
    "divider = make_axes_locatable(ax)\n",
    "cax = divider.append_axes(\"right\", size=\"5%\", pad=0.10)\n",
    "cbar = fig.colorbar(h, cax=cax)\n",
    "cbar.ax.tick_params(labelsize=24) \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "ax.set_xlabel('$t$', size=24)\n",
    "\n",
    "ax.set_xticks([0 ,0.5 ,1])\n",
    "ax.set_yticks([-1 ,0 ,1])\n",
    "ax.set_title('$NLS: v_{pred}(t,x)$', fontsize = 24) # font size doubled\n",
    "ax.tick_params(labelsize=24)\n",
    "\n",
    "ax = plt.subplot(1, 3, 3)\n",
    "h = ax.imshow(np.abs(v_pred+v_sol.T), interpolation='nearest', cmap='rainbow', \n",
    "              extent=[tlo, thi, x.min(), x.max()], \n",
    "              origin='lower', aspect='auto')\n",
    "divider = make_axes_locatable(ax)\n",
    "cax = divider.append_axes(\"right\", size=\"5%\", pad=0.10)\n",
    "cbar = fig.colorbar(h, cax=cax)\n",
    "cbar.ax.tick_params(labelsize=24) \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "ax.set_xlabel('$t$', size=24)\n",
    "\n",
    "ax.set_xticks([0 ,0.5 ,1])\n",
    "ax.set_yticks([-1 ,0 ,1])\n",
    "ax.set_title('$NLS(Error): v(t,x)$', fontsize = 24) # font size doubled\n",
    "ax.tick_params(labelsize=24)\n",
    "\n",
    "\n",
    "plt.show()\n",
    "fig.savefig('NLS3v_1D_BL+PBC+IC2+minibatch.png',format='png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
