{
 "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": [
    "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_weight.pt')\n",
    "    def forward(self, x):\n",
    "        out = self.layers(x)\n",
    "        return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "class PhysicsInformedNN():\n",
    "    def __init__(self, period, X_BC,u_BC, v_BC, X_PDE, layers):\n",
    "       \n",
    "        # BC data point\n",
    "        self.t_BC = torch.tensor(X_BC[:, 0:1]).float().to(device)\n",
    "        self.x_BC = torch.tensor(X_BC[:, 1:2]).float().to(device)\n",
    "        self.u_BC = torch.tensor(u_BC).float().to(device)\n",
    "        self.v_BC = torch.tensor(v_BC).float().to(device)\n",
    "        N_BC = X_BC.shape[0]\n",
    "        self.LW_uBC = torch.nn.ParameterList([torch.nn.Parameter(torch.ones(N_BC, 1).float(), requires_grad=True).to(device)])        \n",
    "        self.LW_vBC = torch.nn.ParameterList([torch.nn.Parameter(torch.ones(N_BC, 1).float(), requires_grad=True).to(device)])        \n",
    " \n",
    "        self.period = torch.tensor(period).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_PDE = X_PDE.shape[0]\n",
    "        self.LW_uPDE = torch.nn.ParameterList([torch.nn.Parameter(torch.ones(N_PDE, 1).float(), requires_grad=True).to(device)])\n",
    "        self.LW_vPDE = torch.nn.ParameterList([torch.nn.Parameter(torch.ones(N_PDE, 1).float(), requires_grad=True).to(device)])\n",
    "\n",
    "        \n",
    "        # layers to build Neural Net\n",
    "        self.layers = layers\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",
    "        \n",
    "        self.optimizer_LW_uBC = torch.optim.Adam(self.LW_uBC.parameters(), lr = 5e-3)\n",
    "        self.optimizer_LW_uPDE = torch.optim.Adam(self.LW_uPDE.parameters(), lr = 5e-3)\n",
    "       \n",
    "        self.optimizer_LW_vBC = torch.optim.Adam(self.LW_vBC.parameters(), lr = 5e-3)\n",
    "        self.optimizer_LW_vPDE = torch.optim.Adam(self.LW_vPDE.parameters(), lr = 5e-3)\n",
    "\n",
    "       \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",
    "        self.iter = 0\n",
    "    # evaluater neural network, no transformation\n",
    "    def NN_eval(self, t, x): \n",
    "        NN = self.dnn(torch.cat([t, x], dim = 1)) \n",
    "        u = NN[:, 0][:, None]\n",
    "        v = NN[:, 1][:, None]\n",
    "        u0_torch = u0_ten(x)\n",
    "        v0_torch = v0_ten(x)\n",
    "        unew = u0_torch*torch.exp(-t) + t * u\n",
    "        vnew = v0_torch*torch.exp(-t) + t * u\n",
    "        return unew,vnew\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",
    "\n",
    "        \n",
    "        # compute PBC loss\n",
    "        uBC_pred_left, vBC_pred_left = self.NN_eval(self.t_BC, self.x_BC)\n",
    "        uBC_pred_right, vBC_pred_right = self.NN_eval(self.t_BC, self.x_BC+self.period)\n",
    "        loss_BC = torch.mean((self.LW_uBC[0]*(uBC_pred_left - uBC_pred_right))**2.0) + torch.mean((self.LW_vBC[0]*(vBC_pred_left - vBC_pred_right))**2.0)\n",
    "        \n",
    "        \n",
    "        pde1_pred, pde2_pred = self.pde_eval(self.t_PDE, self.x_PDE)\n",
    "        loss_PDE = torch.mean((self.LW_uPDE[0]*pde1_pred )** 2) + torch.mean((self.LW_vPDE[0]*pde2_pred )** 2)   \n",
    "\n",
    "        # compute the total loss, it can be weighted\n",
    "        loss =  loss_BC + 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",
    "            end_time = time.time()\n",
    "            print('Iter %5d, Total: %10.4e, Time: %.2f secs' % (self.iter, loss.item(), end_time - self.start_time))\n",
    "            print('PBC: %10.4e, PDE: %10.4e' % (loss_BC.item(), loss_PDE.item()))\n",
    "            self.start_time = end_time\n",
    "        return loss\n",
    "    #\n",
    "    def train(self, nIter):\n",
    "        # start the timer\n",
    "        start_time = time.time()        \n",
    "        # start the training with Adam first\n",
    "        self.dnn.train()\n",
    "        print('Starting with Adam')\n",
    "        for epoch in range(nIter):\n",
    "          \n",
    "            # compute PBC loss\n",
    "            uBC_pred_left, vBC_pred_left = self.NN_eval(self.t_BC, self.x_BC)\n",
    "            uBC_pred_right, vBC_pred_right = self.NN_eval(self.t_BC, self.x_BC+self.period)\n",
    "            loss_BC = torch.mean((self.LW_uBC[0]*(uBC_pred_left - uBC_pred_right))**2.0) + torch.mean((self.LW_vBC[0]*(vBC_pred_left - vBC_pred_right))**2.0)\n",
    "        \n",
    "        \n",
    "            pde1_pred, pde2_pred = self.pde_eval(self.t_PDE, self.x_PDE)\n",
    "            loss_PDE = torch.mean((self.LW_uPDE[0]*pde1_pred )** 2) + torch.mean((self.LW_vPDE[0]*pde2_pred )** 2)   \n",
    "         \n",
    "            # compute the total loss, it can be weighted\n",
    "            loss =  loss_BC + loss_PDE\n",
    "            # Backward and optimize\n",
    "            self.optimizer_Adam.zero_grad()\n",
    "            self.optimizer_LW_uBC.zero_grad()\n",
    "            self.optimizer_LW_uPDE.zero_grad()\n",
    "            \n",
    "            self.optimizer_LW_vBC.zero_grad()\n",
    "            self.optimizer_LW_vPDE.zero_grad()\n",
    "            loss.backward()\n",
    "            self.optimizer_Adam.step() \n",
    "            \n",
    "            self.LW_uBC[0].grad.data = -self.LW_uBC[0].grad.data\n",
    "            self.LW_uPDE[0].grad.data = -self.LW_uPDE[0].grad.data\n",
    "            \n",
    "            self.LW_vBC[0].grad.data = -self.LW_vBC[0].grad.data\n",
    "            self.LW_vPDE[0].grad.data = -self.LW_vPDE[0].grad.data\n",
    "            \n",
    "            self.optimizer_LW_uBC.step()\n",
    "            self.optimizer_LW_uPDE.step()\n",
    "            \n",
    "            self.optimizer_LW_vBC.step()\n",
    "            self.optimizer_LW_vPDE.step()\n",
    "            # output the progress\n",
    "            if (epoch + 1) % 1000 == 0:\n",
    "                end_time = time.time()\n",
    "                print('Iter %5d, Total: %10.4e, Time: %.2f secs' % (epoch + 1, loss.item(), end_time - start_time))\n",
    "                print('PBC: %10.4e, PDE: %10.4e' % (loss_BC.item(), loss_PDE.item()))\n",
    "                print('For uBC,  min uLW: %10.4e, max uLW: %10.4e' %(torch.min(self.LW_uBC[0]).item(), torch.max(self.LW_uBC[0]).item()))\n",
    "                print('For uPDE, min uLW: %10.4e, max uLW: %10.4e' %(torch.min(self.LW_uPDE[0]).item(), torch.max(self.LW_uPDE[0]).item()))\n",
    "                \n",
    "                print('For vBC,  min vLW: %10.4e, max vLW: %10.4e' %(torch.min(self.LW_vBC[0]).item(), torch.max(self.LW_vBC[0]).item()))\n",
    "                print('For vPDE, min vLW: %10.4e, max vLW: %10.4e' %(torch.min(self.LW_vPDE[0]).item(), torch.max(self.LW_vPDE[0]).item()))\n",
    "                \n",
    "                start_time = end_time\n",
    "                # change the learning rate\n",
    "                self.scheduler.step()\n",
    "                \n",
    "        print('Starting with L-BFGS')\n",
    "        self.start_time = time.time()\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"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Training"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Starting with Adam\n",
      "Iter  1000, Total: 1.8589e-01, Time: 55.71 secs\n",
      "PBC: 1.3067e-01, PDE: 5.5226e-02\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 1.0539e+00\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 1.0355e+00\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 1.0539e+00\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 7.1424e+00\n",
      "Iter  2000, Total: 1.8727e-01, Time: 55.42 secs\n",
      "PBC: 1.3228e-01, PDE: 5.4988e-02\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 1.0833e+00\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 1.0480e+00\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 1.0833e+00\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 1.4835e+01\n",
      "Iter  3000, Total: 1.8908e-01, Time: 55.80 secs\n",
      "PBC: 1.3452e-01, PDE: 5.4559e-02\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 1.1367e+00\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 1.0702e+00\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 1.1367e+00\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 2.1694e+01\n",
      "Iter  4000, Total: 1.9035e-01, Time: 56.04 secs\n",
      "PBC: 1.3663e-01, PDE: 5.3712e-02\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 1.2279e+00\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 1.1074e+00\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 1.2279e+00\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 2.8191e+01\n",
      "Iter  5000, Total: 1.8695e-01, Time: 55.76 secs\n",
      "PBC: 1.3485e-01, PDE: 5.2091e-02\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 1.3791e+00\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 1.1676e+00\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 1.3791e+00\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 3.4561e+01\n",
      "Iter  6000, Total: 1.5740e-01, Time: 56.22 secs\n",
      "PBC: 1.0775e-01, PDE: 4.9643e-02\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 1.6142e+00\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 1.2633e+00\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 1.6142e+00\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 4.1012e+01\n",
      "Iter  7000, Total: 9.3607e-02, Time: 56.61 secs\n",
      "PBC: 4.4388e-02, PDE: 4.9219e-02\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 1.8369e+00\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 1.4190e+00\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 1.8369e+00\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 4.7634e+01\n",
      "Iter  8000, Total: 5.9561e-02, Time: 57.54 secs\n",
      "PBC: 5.8879e-03, PDE: 5.3674e-02\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 1.9603e+00\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 1.6915e+00\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 1.9603e+00\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 5.4074e+01\n",
      "Iter  9000, Total: 6.3864e-02, Time: 55.43 secs\n",
      "PBC: 5.7317e-03, PDE: 5.8133e-02\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 1.9907e+00\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 2.2399e+00\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 1.9907e+00\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 5.9928e+01\n",
      "Iter 10000, Total: 7.1358e-02, Time: 56.55 secs\n",
      "PBC: 7.6903e-03, PDE: 6.3667e-02\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 2.0322e+00\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 3.5126e+00\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 2.0322e+00\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 6.5418e+01\n",
      "Iter 11000, Total: 7.4283e-02, Time: 56.67 secs\n",
      "PBC: 8.4902e-03, PDE: 6.5793e-02\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 2.1029e+00\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 7.0215e+00\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 2.1029e+00\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 7.0405e+01\n",
      "Iter 12000, Total: 9.3217e-02, Time: 56.14 secs\n",
      "PBC: 1.3585e-02, PDE: 7.9632e-02\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 3.1706e+00\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 1.4611e+01\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 3.1706e+00\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 7.4810e+01\n",
      "Iter 13000, Total: 1.2053e-01, Time: 55.49 secs\n",
      "PBC: 1.5792e-02, PDE: 1.0473e-01\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 4.9702e+00\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 2.1846e+01\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 4.9702e+00\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 7.9574e+01\n",
      "Iter 14000, Total: 1.5115e-01, Time: 55.61 secs\n",
      "PBC: 1.5916e-02, PDE: 1.3524e-01\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 6.6558e+00\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 2.8422e+01\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 6.6558e+00\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 8.4371e+01\n",
      "Iter 15000, Total: 1.8009e-01, Time: 56.08 secs\n",
      "PBC: 6.5323e-03, PDE: 1.7356e-01\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 8.2622e+00\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 3.4688e+01\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 8.2622e+00\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 8.9065e+01\n",
      "Iter 16000, Total: 2.0352e-01, Time: 56.33 secs\n",
      "PBC: 4.9500e-03, PDE: 1.9857e-01\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 9.0941e+00\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 4.0232e+01\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 9.0941e+00\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 9.3335e+01\n",
      "Iter 17000, Total: 2.5480e-01, Time: 56.53 secs\n",
      "PBC: 3.9308e-03, PDE: 2.5087e-01\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 1.5347e+01\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 4.4367e+01\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 1.5347e+01\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 9.7451e+01\n",
      "Iter 18000, Total: 3.2763e-01, Time: 56.24 secs\n",
      "PBC: 3.7079e-03, PDE: 3.2392e-01\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 1.6412e+01\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 5.0458e+01\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 1.6412e+01\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 1.0174e+02\n",
      "Iter 19000, Total: 4.1907e-01, Time: 56.60 secs\n",
      "PBC: 2.9417e-03, PDE: 4.1612e-01\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 1.9561e+01\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 5.5564e+01\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 1.9561e+01\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 1.0528e+02\n",
      "Iter 20000, Total: 5.3775e-01, Time: 56.67 secs\n",
      "PBC: 2.1671e-03, PDE: 5.3558e-01\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 2.4345e+01\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 6.0789e+01\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 2.4345e+01\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 1.0837e+02\n",
      "Iter 21000, Total: 6.7404e-01, Time: 54.91 secs\n",
      "PBC: 1.4434e-03, PDE: 6.7260e-01\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 2.8221e+01\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 6.6262e+01\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 2.8221e+01\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 1.1196e+02\n",
      "Iter 22000, Total: 8.2382e-01, Time: 55.01 secs\n",
      "PBC: 9.9985e-04, PDE: 8.2282e-01\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 2.8755e+01\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 7.2009e+01\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 2.8755e+01\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 1.1663e+02\n",
      "Iter 23000, Total: 9.8941e-01, Time: 55.92 secs\n",
      "PBC: 8.0897e-04, PDE: 9.8860e-01\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 2.8792e+01\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 7.7441e+01\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 2.8792e+01\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 1.2210e+02\n",
      "Iter 24000, Total: 1.1624e+00, Time: 55.96 secs\n",
      "PBC: 7.8315e-04, PDE: 1.1616e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 3.1220e+01\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 8.2754e+01\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 3.1220e+01\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 1.2776e+02\n",
      "Iter 25000, Total: 1.3574e+00, Time: 55.67 secs\n",
      "PBC: 5.5022e-04, PDE: 1.3568e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 3.4422e+01\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 8.7965e+01\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 3.4422e+01\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 1.3329e+02\n",
      "Iter 26000, Total: 1.5706e+00, Time: 56.04 secs\n",
      "PBC: 1.4584e-03, PDE: 1.5692e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 3.8293e+01\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 9.3251e+01\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 3.8293e+01\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 1.3879e+02\n",
      "Iter 27000, Total: 1.7973e+00, Time: 56.34 secs\n",
      "PBC: 4.4031e-04, PDE: 1.7969e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 4.2111e+01\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 9.8567e+01\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 4.2111e+01\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 1.4438e+02\n",
      "Iter 28000, Total: 2.0389e+00, Time: 56.25 secs\n",
      "PBC: 4.1123e-04, PDE: 2.0385e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 4.5654e+01\n",
      "For uPDE, min uLW: 1.0300e+00, max uLW: 1.0385e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 4.5654e+01\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 1.4992e+02\n",
      "Iter 29000, Total: 2.3019e+00, Time: 56.16 secs\n",
      "PBC: 6.7497e-03, PDE: 2.2952e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 4.9092e+01\n",
      "For uPDE, min uLW: 1.0301e+00, max uLW: 1.0912e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 4.9092e+01\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 1.5539e+02\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Iter 30000, Total: 2.5623e+00, Time: 56.42 secs\n",
      "PBC: 3.1126e-04, PDE: 2.5620e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 5.2160e+01\n",
      "For uPDE, min uLW: 1.0302e+00, max uLW: 1.1436e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 5.2160e+01\n",
      "For vPDE, min vLW: 1.0300e+00, max vLW: 1.6083e+02\n",
      "Iter 31000, Total: 2.8470e+00, Time: 56.66 secs\n",
      "PBC: 6.3939e-04, PDE: 2.8463e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 5.4948e+01\n",
      "For uPDE, min uLW: 1.0305e+00, max uLW: 1.1961e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 5.4948e+01\n",
      "For vPDE, min vLW: 1.0301e+00, max vLW: 1.6622e+02\n",
      "Iter 32000, Total: 3.1434e+00, Time: 56.97 secs\n",
      "PBC: 4.2665e-03, PDE: 3.1392e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 5.7221e+01\n",
      "For uPDE, min uLW: 1.0311e+00, max uLW: 1.2484e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 5.7221e+01\n",
      "For vPDE, min vLW: 1.0303e+00, max vLW: 1.7158e+02\n",
      "Iter 33000, Total: 3.4472e+00, Time: 55.13 secs\n",
      "PBC: 1.4531e-04, PDE: 3.4471e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 5.9142e+01\n",
      "For uPDE, min uLW: 1.0336e+00, max uLW: 1.3002e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 5.9142e+01\n",
      "For vPDE, min vLW: 1.0309e+00, max vLW: 1.7697e+02\n",
      "Iter 34000, Total: 3.7695e+00, Time: 55.04 secs\n",
      "PBC: 1.4452e-03, PDE: 3.7680e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 5.9441e+01\n",
      "For uPDE, min uLW: 1.0344e+00, max uLW: 1.3514e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 5.9441e+01\n",
      "For vPDE, min vLW: 1.0311e+00, max vLW: 1.8220e+02\n",
      "Iter 35000, Total: 4.1001e+00, Time: 55.32 secs\n",
      "PBC: 4.0313e-04, PDE: 4.0997e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 6.1378e+01\n",
      "For uPDE, min uLW: 1.0365e+00, max uLW: 1.4029e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 6.1378e+01\n",
      "For vPDE, min vLW: 1.0315e+00, max vLW: 1.8732e+02\n",
      "Iter 36000, Total: 4.4613e+00, Time: 55.53 secs\n",
      "PBC: 1.4179e-02, PDE: 4.4472e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 6.4926e+01\n",
      "For uPDE, min uLW: 1.0400e+00, max uLW: 1.4567e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 6.4926e+01\n",
      "For vPDE, min vLW: 1.0321e+00, max vLW: 1.9238e+02\n",
      "Iter 37000, Total: 4.7987e+00, Time: 55.28 secs\n",
      "PBC: 6.1115e-04, PDE: 4.7981e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 6.8952e+01\n",
      "For uPDE, min uLW: 1.0513e+00, max uLW: 1.5104e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 6.8952e+01\n",
      "For vPDE, min vLW: 1.0339e+00, max vLW: 1.9722e+02\n",
      "Iter 38000, Total: 5.1669e+00, Time: 55.92 secs\n",
      "PBC: 8.9164e-04, PDE: 5.1660e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 7.2193e+01\n",
      "For uPDE, min uLW: 1.0618e+00, max uLW: 1.5630e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 7.2193e+01\n",
      "For vPDE, min vLW: 1.0360e+00, max vLW: 2.0214e+02\n",
      "Iter 39000, Total: 5.5761e+00, Time: 55.55 secs\n",
      "PBC: 1.2742e-03, PDE: 5.5749e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 7.3800e+01\n",
      "For uPDE, min uLW: 1.4380e+00, max uLW: 1.6155e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 7.3800e+01\n",
      "For vPDE, min vLW: 1.3940e+00, max vLW: 2.0818e+02\n",
      "Iter 40000, Total: 5.9534e+00, Time: 55.56 secs\n",
      "PBC: 9.0458e-04, PDE: 5.9525e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 7.3802e+01\n",
      "For uPDE, min uLW: 1.4384e+00, max uLW: 1.6663e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 7.3802e+01\n",
      "For vPDE, min vLW: 1.3946e+00, max vLW: 2.1306e+02\n",
      "Iter 41000, Total: 6.3499e+00, Time: 55.20 secs\n",
      "PBC: 8.1005e-04, PDE: 6.3491e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 7.3819e+01\n",
      "For uPDE, min uLW: 1.4402e+00, max uLW: 1.7208e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 7.3819e+01\n",
      "For vPDE, min vLW: 1.3953e+00, max vLW: 2.1795e+02\n",
      "Iter 42000, Total: 6.7515e+00, Time: 55.73 secs\n",
      "PBC: 3.9959e-04, PDE: 6.7511e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 7.3870e+01\n",
      "For uPDE, min uLW: 1.4458e+00, max uLW: 1.7799e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 7.3870e+01\n",
      "For vPDE, min vLW: 1.3962e+00, max vLW: 2.2297e+02\n",
      "Iter 43000, Total: 7.1604e+00, Time: 55.94 secs\n",
      "PBC: 1.0437e-03, PDE: 7.1593e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 7.4030e+01\n",
      "For uPDE, min uLW: 1.4549e+00, max uLW: 1.8337e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 7.4030e+01\n",
      "For vPDE, min vLW: 1.3968e+00, max vLW: 2.2804e+02\n",
      "Iter 44000, Total: 7.4265e+00, Time: 56.02 secs\n",
      "PBC: 3.0891e-02, PDE: 7.3956e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 8.3756e+01\n",
      "For uPDE, min uLW: 1.4612e+00, max uLW: 1.8831e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 8.3756e+01\n",
      "For vPDE, min vLW: 1.3977e+00, max vLW: 2.3291e+02\n",
      "Iter 45000, Total: 8.1200e+00, Time: 54.59 secs\n",
      "PBC: 4.4807e-03, PDE: 8.1155e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 9.0359e+01\n",
      "For uPDE, min uLW: 1.8610e+00, max uLW: 1.9321e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 9.0359e+01\n",
      "For vPDE, min vLW: 1.6037e+00, max vLW: 2.3738e+02\n",
      "Iter 46000, Total: 7.9004e+00, Time: 54.99 secs\n",
      "PBC: 3.7293e-02, PDE: 7.8631e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 9.3042e+01\n",
      "For uPDE, min uLW: 1.9346e+00, max uLW: 1.9844e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 9.3042e+01\n",
      "For vPDE, min vLW: 1.6156e+00, max vLW: 2.4218e+02\n",
      "Iter 47000, Total: 7.1290e+00, Time: 54.81 secs\n",
      "PBC: 2.5221e-02, PDE: 7.1038e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 9.9392e+01\n",
      "For uPDE, min uLW: 1.9619e+00, max uLW: 2.0376e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 9.9392e+01\n",
      "For vPDE, min vLW: 1.6211e+00, max vLW: 2.4457e+02\n",
      "Iter 48000, Total: 7.4099e+00, Time: 54.85 secs\n",
      "PBC: 1.7083e-02, PDE: 7.3928e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 1.0305e+02\n",
      "For uPDE, min uLW: 2.0026e+00, max uLW: 2.0883e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 1.0305e+02\n",
      "For vPDE, min vLW: 1.6255e+00, max vLW: 2.4983e+02\n",
      "Iter 49000, Total: 7.7578e+00, Time: 54.64 secs\n",
      "PBC: 3.5599e-02, PDE: 7.7222e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 1.0634e+02\n",
      "For uPDE, min uLW: 2.0800e+00, max uLW: 2.1395e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 1.0634e+02\n",
      "For vPDE, min vLW: 1.6329e+00, max vLW: 2.5488e+02\n",
      "Iter 50000, Total: 8.0390e+00, Time: 55.29 secs\n",
      "PBC: 1.3994e-02, PDE: 8.0250e+00\n",
      "For uBC,  min uLW: 1.0000e+00, max uLW: 1.0941e+02\n",
      "For uPDE, min uLW: 2.1792e+00, max uLW: 2.1898e+02\n",
      "For vBC,  min vLW: 1.0000e+00, max vLW: 1.0941e+02\n",
      "For vPDE, min vLW: 1.6687e+00, max vLW: 2.5984e+02\n",
      "Starting with L-BFGS\n",
      "Iter  1000, Total: 7.8699e+00, Time: 88.72 secs\n",
      "PBC: 7.0769e-03, PDE: 7.8628e+00\n"
     ]
    }
   ],
   "source": [
    "layers = [2, 32, 32, 32, 32, 32, 32, 32, 2]\n",
    "N_IC = 96\n",
    "ptsIC=np.load('ptsIC.npy')\n",
    "ptsBC=np.load('ptsBC.npy')\n",
    "ptsPDE=np.load('random_data.npy')\n",
    "ptsPDE = np.vstack((ptsPDE,ptsIC))\n",
    "x_IC = np.expand_dims(np.linspace(xlo, xhi, N_IC), axis = 1)\n",
    "uIC = u0(x_IC)\n",
    "vIC = v0(x_IC)\n",
    "uBC = np.load('uBC.npy')\n",
    "vBC = np.load('vBC.npy')\n",
    "model = PhysicsInformedNN(period,  ptsBC, uBC, vBC, ptsPDE, layers)\n",
    "model.train(50000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "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": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = scipy.io.loadmat('/Users/abraham/Desktop/PINN/New_Data/NLS_1D_t2.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": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 203180 characters]",
      "text/plain": [
       "<Figure size 1100x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(11, 5))\n",
    "ax = fig.add_subplot(1, 1, 1)\n",
    "#\n",
    "ax.plot(ptsPDE[:, 0], ptsPDE[:, 1], \n",
    "    'rd', label = 'PDE Data (%d points)' % (ptsPDE.shape[0]), \n",
    "    markersize = 4,  # marker size doubled\n",
    "    clip_on = False,\n",
    "    alpha=1.0\n",
    ")\n",
    "\n",
    "ax.plot(ptsIC[:, 0], ptsIC[:, 1], \n",
    "    'bd', label = 'IC Data (%d points)' % (ptsIC.shape[0]), \n",
    "    markersize = 4,  # marker size doubled\n",
    "    clip_on = False,\n",
    "    alpha=1.0\n",
    ")\n",
    "\n",
    "\n",
    "ax.plot(ptsBC[:, 0], ptsBC[:, 1], \n",
    "    'gd', label = 'BC Data (%d points)' % (ptsBC.shape[0]), \n",
    "    markersize = 4,  # marker size doubled\n",
    "    clip_on = False,\n",
    "    alpha=1.0\n",
    ")\n",
    "\n",
    "ax.set_xlabel('$t$', size=15)\n",
    "ax.set_ylabel('$x$', size=15)\n",
    "ax.legend(\n",
    "    loc='upper center', \n",
    "    bbox_to_anchor=(0.9, -0.05), \n",
    "    ncol=5, \n",
    "    frameon=False, \n",
    "    prop={'size': 15}\n",
    ")\n",
    "ax.legend()\n",
    "ax.set_title('Points', fontsize = 15) # font size doubled\n",
    "ax.tick_params(labelsize=12)\n",
    "#\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 115280 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+IC1+SA.png',format='png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 112012 characters]",
      "text/plain": [
       "<Figure size 1800x600 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "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+IC1+SA.png',format='png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "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": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "l2: 0.4503890867287142\n",
      "l1: 0.37678667259224197\n",
      "linf: 0.761013870783781\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": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "l2: 1.1100689972085234\n",
      "l1: 1.4504506258572485\n",
      "linf: 0.8219035710122826\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": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 285588 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+IC1+SA.png',format='png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 302748 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",
    "\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",
    "\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+IC1+SA.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
}
