{
 "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": 2,
   "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": 3,
   "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": 4,
   "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": 7,
   "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": 8,
   "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_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",
    "       \n",
    "    \n",
    "        layers[0] = int(4 * m)\n",
    "        self.layers = layers\n",
    "        self.n_batches = n_batches\n",
    "        self.batch_size = batch_size\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",
    "        self.optimizer_LW_uPDE = torch.optim.Adam(self.LW_uPDE.parameters(), lr = 5e-3)\n",
    "        self.optimizer_LW_vPDE = torch.optim.Adam(self.LW_vPDE.parameters(), lr = 5e-3)\n",
    "    \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",
    "    # update alpha\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",
    "        t_trans = torch.matmul(t, self.ms)\n",
    "        NN = self.dnn(torch.cat([torch.cos(x_trans), torch.sin(x_trans), torch.cos(t_trans), torch.sin(t_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(-t) + t * uNN\n",
    "        vnew = v0_torch*torch.exp(-t) + t * vNN\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",
    "        # compute PDE loss\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_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((self.LW_uPDE[0][batch_id*self.batch_size:(batch_id + 1)*self.batch_size, :]*pde1_pred )** 2) + torch.mean((self.LW_vPDE[0][batch_id*self.batch_size:(batch_id + 1)*self.batch_size, :]*pde2_pred )** 2)   \n",
    "         \n",
    "                # compute the total loss, it can be weighted\n",
    "                loss = loss_PDE\n",
    "                # Backward and optimize\n",
    "            \n",
    "                self.optimizer_Adam.zero_grad()\n",
    "                self.optimizer_LW_uPDE.zero_grad()\n",
    "                self.optimizer_LW_vPDE.zero_grad()\n",
    "            \n",
    "                loss.backward()\n",
    "                self.optimizer_Adam.step() \n",
    "            \n",
    "           \n",
    "            \n",
    "                self.LW_uPDE[0].grad.data = -self.LW_uPDE[0].grad.data\n",
    "            \n",
    "           \n",
    "                self.LW_vPDE[0].grad.data = -self.LW_vPDE[0].grad.data\n",
    "            \n",
    "           \n",
    "                self.optimizer_LW_uPDE.step()\n",
    "            \n",
    "        \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' % (epoch + 1, loss.item()))\n",
    "                print('PDE: %10.4e' % (loss_PDE.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 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",
    "                # change the learning rate\n",
    "                self.scheduler.step()\n",
    "       # self.optimizer_LBFGS.step(self.loss_func) \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",
    "\n",
    "\n",
    "## Configurations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Training"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Starting with Adam\n",
      "Iter  1000, Total: 7.1416e-01\n",
      "PDE: 7.1416e-01\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 1.1683e+01\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 1.3102e+01\n",
      "Iter  2000, Total: 8.5692e-01\n",
      "PDE: 8.5692e-01\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 2.2705e+01\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 2.4807e+01\n",
      "Iter  3000, Total: 5.5339e+00\n",
      "PDE: 5.5339e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 3.3809e+01\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 3.5867e+01\n",
      "Iter  4000, Total: 2.6813e+00\n",
      "PDE: 2.6813e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 4.3449e+01\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 4.4983e+01\n",
      "Iter  5000, Total: 4.5125e+00\n",
      "PDE: 4.5125e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 5.3979e+01\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 5.2897e+01\n",
      "Iter  6000, Total: 5.8790e+00\n",
      "PDE: 5.8790e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 6.4355e+01\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 6.1183e+01\n",
      "Iter  7000, Total: 1.1809e+00\n",
      "PDE: 1.1809e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 7.3356e+01\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 6.6542e+01\n",
      "Iter  8000, Total: 8.1199e-01\n",
      "PDE: 8.1199e-01\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 8.1069e+01\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 7.3402e+01\n",
      "Iter  9000, Total: 3.4448e+00\n",
      "PDE: 3.4448e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 9.0894e+01\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 8.2593e+01\n",
      "Iter 10000, Total: 2.2429e+00\n",
      "PDE: 2.2429e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 1.0056e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 9.1737e+01\n",
      "Iter 11000, Total: 2.4426e-01\n",
      "PDE: 2.4426e-01\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 1.1094e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 1.0131e+02\n",
      "Iter 12000, Total: 2.4143e+00\n",
      "PDE: 2.4143e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 1.2177e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 1.1012e+02\n",
      "Iter 13000, Total: 8.0812e+00\n",
      "PDE: 8.0812e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 1.3264e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 1.1891e+02\n",
      "Iter 14000, Total: 1.0557e+00\n",
      "PDE: 1.0557e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 1.4342e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 1.2769e+02\n",
      "Iter 15000, Total: 3.2947e+00\n",
      "PDE: 3.2947e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 1.5398e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 1.3681e+02\n",
      "Iter 16000, Total: 1.5947e+00\n",
      "PDE: 1.5947e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 1.6453e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 1.4699e+02\n",
      "Iter 17000, Total: 2.0397e+00\n",
      "PDE: 2.0397e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 1.7510e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 1.5742e+02\n",
      "Iter 18000, Total: 1.2514e+00\n",
      "PDE: 1.2514e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 1.8552e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 1.6750e+02\n",
      "Iter 19000, Total: 3.4221e+00\n",
      "PDE: 3.4221e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 1.9607e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 1.7741e+02\n",
      "Iter 20000, Total: 7.0879e+00\n",
      "PDE: 7.0879e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 2.0667e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 1.8793e+02\n",
      "Iter 21000, Total: 1.3638e+00\n",
      "PDE: 1.3638e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 2.1736e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 1.9853e+02\n",
      "Iter 22000, Total: 1.1696e+01\n",
      "PDE: 1.1696e+01\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 2.2787e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 2.0894e+02\n",
      "Iter 23000, Total: 4.1746e+00\n",
      "PDE: 4.1746e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 2.3848e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 2.1951e+02\n",
      "Iter 24000, Total: 4.4552e+00\n",
      "PDE: 4.4552e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 2.4907e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 2.3015e+02\n",
      "Iter 25000, Total: 5.0626e+00\n",
      "PDE: 5.0626e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 2.5956e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 2.4077e+02\n",
      "Iter 26000, Total: 9.1916e+00\n",
      "PDE: 9.1916e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 2.6985e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 2.5120e+02\n",
      "Iter 27000, Total: 1.0716e+01\n",
      "PDE: 1.0716e+01\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 2.8022e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 2.6182e+02\n",
      "Iter 28000, Total: 6.0444e+00\n",
      "PDE: 6.0444e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 2.9074e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 2.7267e+02\n",
      "Iter 29000, Total: 5.3661e+00\n",
      "PDE: 5.3661e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 3.0120e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 2.8354e+02\n",
      "Iter 30000, Total: 4.3434e+00\n",
      "PDE: 4.3434e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 3.1152e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 2.9428e+02\n",
      "Iter 31000, Total: 1.4229e+01\n",
      "PDE: 1.4229e+01\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 3.2167e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 3.0505e+02\n",
      "Iter 32000, Total: 8.3731e+00\n",
      "PDE: 8.3731e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 3.3177e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 3.1531e+02\n",
      "Iter 33000, Total: 1.5349e+00\n",
      "PDE: 1.5349e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 3.4201e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 3.2602e+02\n",
      "Iter 34000, Total: 5.1374e+00\n",
      "PDE: 5.1374e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 3.5209e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 3.3667e+02\n",
      "Iter 35000, Total: 7.1178e+00\n",
      "PDE: 7.1178e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 3.6217e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 3.4723e+02\n",
      "Iter 36000, Total: 8.2433e-01\n",
      "PDE: 8.2433e-01\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 3.7229e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 3.5777e+02\n",
      "Iter 37000, Total: 2.5993e+01\n",
      "PDE: 2.5993e+01\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 3.8234e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 3.6821e+02\n",
      "Iter 38000, Total: 5.9371e+00\n",
      "PDE: 5.9371e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 3.9236e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 3.7875e+02\n",
      "Iter 39000, Total: 3.6667e+00\n",
      "PDE: 3.6667e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 4.0235e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 3.8920e+02\n",
      "Iter 40000, Total: 6.1160e+00\n",
      "PDE: 6.1160e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 4.1248e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 3.9971e+02\n",
      "Iter 41000, Total: 3.3009e+00\n",
      "PDE: 3.3009e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 4.2270e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 4.1027e+02\n",
      "Iter 42000, Total: 4.2635e+00\n",
      "PDE: 4.2635e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 4.3286e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 4.2075e+02\n",
      "Iter 43000, Total: 4.6821e+00\n",
      "PDE: 4.6821e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 4.4310e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 4.3127e+02\n",
      "Iter 44000, Total: 8.9290e+00\n",
      "PDE: 8.9290e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 4.5342e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 4.4178e+02\n",
      "Iter 45000, Total: 1.2656e+01\n",
      "PDE: 1.2656e+01\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 4.6389e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 4.5238e+02\n",
      "Iter 46000, Total: 5.1880e+00\n",
      "PDE: 5.1880e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 4.7442e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 4.6297e+02\n",
      "Iter 47000, Total: 6.4348e+00\n",
      "PDE: 6.4348e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 4.8481e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 4.7343e+02\n",
      "Iter 48000, Total: 6.6416e+00\n",
      "PDE: 6.6416e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 4.9536e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 4.8397e+02\n",
      "Iter 49000, Total: 5.4317e+00\n",
      "PDE: 5.4317e+00\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 5.0603e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 4.9456e+02\n",
      "Iter 50000, Total: 1.1782e+01\n",
      "PDE: 1.1782e+01\n",
      "For uPDE, min uLW: 1.0000e+00, max uLW: 5.1653e+02\n",
      "For vPDE, min vLW: 1.0000e+00, max vLW: 5.0508e+02\n",
      "Starting with L-BFGS\n",
      "Iter  1000, Total: 1.6201e-01, PDE: 1.6201e-01\n",
      "Iter  2000, Total: 1.0841e-01, PDE: 1.0841e-01\n",
      "Iter  3000, Total: 7.6882e-02, PDE: 7.6882e-02\n",
      "Iter  4000, Total: 5.7227e-02, PDE: 5.7227e-02\n",
      "Iter  5000, Total: 4.4736e-02, PDE: 4.4736e-02\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": 13,
   "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": 5,
   "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\n",
    "u_pred = np.load(\"u_pred_BL+PBC+IC1+SA+minibatch.npy\")\n",
    "v_pred = np.load(\"v_pred_BL+PBC+IC1+SA+minibatch.npy\")\n",
    "t = np.linspace(tlo, thi, 201)\n",
    "x = np.linspace(xlo, xhi, 501)\n",
    "T, X = np.meshgrid(t, x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "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": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "l2: 7.39378139766507e-05\n",
      "l1: 6.520259304937266e-05\n",
      "linf: 0.0001108086869051428\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": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "l2: 0.00010071752192882938\n",
      "l1: 0.0001139090090241323\n",
      "linf: 0.00013938080055482137\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": 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.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
