{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Practical Sheet 11: Physics-Inspired Machine Learning\n",
    "\n",
   ],
   "id": "cell-00"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import copy, json, time\n",
    "from pathlib import Path\n",
    "import numpy as np\n",
    "import torch\n",
    "from torch import nn\n",
    "\n",
    "torch.set_num_threads(1)\n",
    "DTYPE = torch.float32\n",
    "T_OBS = torch.tensor([[0.0], [0.2], [0.4], [0.6], [0.8], [1.0]], dtype=DTYPE)\n",
    "NOISE = torch.tensor([[0.0], [0.02], [-0.03], [0.01], [-0.02], [0.02]], dtype=DTYPE)\n",
    "Y_OBS = torch.exp(-T_OBS) + NOISE\n",
    "T0 = torch.zeros((1, 1), dtype=DTYPE)\n",
    "\n",
    "def make_model(seed=7):\n",
    "    torch.manual_seed(seed)\n",
    "    return nn.Sequential(nn.Linear(1, 32), nn.Tanh(), nn.Linear(32, 32),\n",
    "                         nn.Tanh(), nn.Linear(32, 1))\n",
    "\n"
   ],
   "id": "cell-01"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Queries and shared weights"
   ],
   "id": "cell-02"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "initial = make_model(7)\n",
    "for n in [25, 250]:\n",
    "    tau = torch.linspace(0, 4, n).reshape(-1, 1)\n",
    "    with torch.no_grad():\n",
    "        plt.plot(tau.numpy(), initial(tau).numpy(), label=f\"{n} queries\")\n",
    "plt.xlabel(\"Time tau\"); plt.ylabel(\"Untrained network output\")\n",
    "plt.legend(); plt.show()\n",
    "print(\"Parameters:\", sum(p.numel() for p in initial.parameters()))\n",
    "with torch.no_grad():\n",
    "    print(initial(torch.tensor([[0.37], [1.45], [2.83]])))"
   ],
   "id": "cell-03"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Loss and evaluation helpers\n",
    "Complete TODO 1 before baseline training. Complete TODO 4 before reporting metrics."
   ],
   "id": "cell-04"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "def total_loss(data_loss, initial_loss, physics_loss, physics_weight):\n",
    "    raise NotImplementedError(\"TODO 1: weighted sum of losses\")\n"
   ],
   "id": "cell-05"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "def rmse(prediction, target):\n",
    "    raise NotImplementedError(\"TODO 4: root mean squared error\")\n"
   ],
   "id": "cell-06"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Coordinate derivative and physics residual\n",
    "Complete TODOs 2 and 3. This function is not called during baseline training."
   ],
   "id": "cell-07"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "def residual(model, tau, beta=1.0):\n",
    "    tau = tau.detach().clone().requires_grad_(True)\n",
    "    y = model(tau)\n",
    "    raise NotImplementedError(\"TODO 2: coordinate derivative, keeping its graph\")\n",
    "    dy = None\n",
    "    raise NotImplementedError(\"TODO 3: physics residual\")\n",
    "\n"
   ],
   "id": "cell-08"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "def train(initial, physics_weight=1.0, beta=1.0, epochs=2000):\n",
    "    model = copy.deepcopy(initial)\n",
    "    opt = torch.optim.Adam(model.parameters(), lr=1e-3)\n",
    "    tc = torch.linspace(0.0, 4.0, 64).reshape(-1, 1)\n",
    "    history = []\n",
    "    for step in range(epochs):\n",
    "        opt.zero_grad()\n",
    "        ld = ((model(T_OBS) - Y_OBS)**2).mean()\n",
    "        li = ((model(T0) - 1.0)**2).mean()\n",
    "        lp = residual(model, tc, beta).square().mean() if physics_weight else torch.tensor(0.)\n",
    "        loss = total_loss(ld, li, lp, physics_weight)\n",
    "        loss.backward()\n",
    "        opt.step()\n",
    "        if step % 100 == 0:\n",
    "            history.append([step, ld.item(), li.item(), lp.item()])\n",
    "    return model, history\n",
    "\n",
    "def evaluate(model):\n",
    "    out = {}\n",
    "    for name, lo, hi in [('data_region',0,1), ('physics_region',1,4), ('outside',4,5)]:\n",
    "        t = torch.linspace(lo, hi, 201).reshape(-1,1)\n",
    "        with torch.no_grad():\n",
    "            out[name+'_rmse'] = rmse(model(t), torch.exp(-t)).item()\n",
    "    # Offset grid avoids reusing the 64 collocation locations.\n",
    "    t = ((torch.arange(200,dtype=DTYPE)+0.5)*4.0/200).reshape(-1,1)\n",
    "    out['residual_rms'] = residual(model,t).square().mean().sqrt().item()\n",
    "    out['initial_error'] = abs(model(T0).item()-1.0)\n",
    "    return out\n",
    "\n"
   ],
   "id": "cell-09"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Baseline checkpoint\n",
    "Run this cell before completing the PINN if needed."
   ],
   "id": "cell-10"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "initial = make_model(7)\n",
    "baseline, baseline_history = train(initial, physics_weight=0)\n",
    "print(\"Baseline training complete\")"
   ],
   "id": "cell-11"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Residual unit checks"
   ],
   "id": "cell-12"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "class Reference(nn.Module):\n",
    "    def forward(self, t): return torch.exp(-t)\n",
    "class Zero(nn.Module):\n",
    "    def forward(self, t): return 0*t\n",
    "class Fast(nn.Module):\n",
    "    def forward(self, t): return torch.exp(-2*t)\n",
    "test_t = torch.tensor([[0.0], [0.5], [1.0]])\n",
    "assert torch.max(torch.abs(residual(Reference(), test_t))).item() < 1e-6\n",
    "assert torch.max(torch.abs(residual(Zero(), test_t))).item() < 1e-6\n",
    "assert torch.allclose(residual(Fast(), test_t), -torch.exp(-2*test_t))\n",
    "print(\"Residual checks passed. Which candidate meets y(0)=1?\")"
   ],
   "id": "cell-13"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Matched comparison"
   ],
   "id": "cell-14"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "initial = make_model(7)\n",
    "baseline, baseline_history = train(initial, physics_weight=0)\n",
    "# Execute after completing the residual TODOs.\n",
    "pinn, pinn_history = train(initial, physics_weight=1)\n",
    "print(\"baseline\", evaluate(baseline))\n",
    "print(\"PINN\", evaluate(pinn))"
   ],
   "id": "cell-15"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "tau = torch.linspace(0,5,301).reshape(-1,1)\n",
    "plt.figure(figsize=(9,4))\n",
    "with torch.no_grad():\n",
    "    plt.plot(tau, torch.exp(-tau), \"k--\", label=\"Reference\")\n",
    "    for name, model in [(\"Baseline\",baseline),(\"PINN\",pinn)]:\n",
    "        plt.plot(tau,model(tau),label=name)\n",
    "plt.scatter(T_OBS,Y_OBS,color=\"red\",label=\"Observations\")\n",
    "plt.axvline(1,color=\"grey\",linestyle=\":\");plt.axvline(4,color=\"grey\",linestyle=\":\")\n",
    "plt.xlabel(\"Time tau\");plt.ylabel(\"Normalised temperature\")\n",
    "plt.legend();plt.show()"
   ],
   "id": "cell-16"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Repeat the random initialisation"
   ],
   "id": "cell-17"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "initial17 = make_model(17)\n",
    "for name, weight in [(\"Baseline\",0),(\"PINN\",1)]:\n",
    "    model, _ = train(initial17, physics_weight=weight)\n",
    "    print(\"seed 17\",name,evaluate(model))"
   ],
   "id": "cell-18"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Incorrect physics"
   ],
   "id": "cell-19"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "wrong, _ = train(initial, physics_weight=1, beta=2)\n",
    "print(\"Wrong law, evaluated using true law:\", evaluate(wrong))\n",
    "tcheck=((torch.arange(200,dtype=DTYPE)+0.5)*4/200).reshape(-1,1)\n",
    "print(\"Imposed-law residual RMS:\",residual(wrong,tcheck,beta=2).square().mean().sqrt().item())\n",
    "plt.figure(figsize=(9,4))\n",
    "with torch.no_grad():\n",
    "    plt.plot(tau,torch.exp(-tau),\"k--\",label=\"Reference\")\n",
    "    for name, model in [(\"Baseline\",baseline),(\"PINN\",pinn),(\"Wrong law\",wrong)]:\n",
    "        plt.plot(tau,model(tau),label=name)\n",
    "plt.scatter(T_OBS,Y_OBS,color=\"red\",label=\"Observations\")\n",
    "plt.xlabel(\"Time tau\");plt.ylabel(\"Normalised temperature\")\n",
    "plt.legend();plt.show()"
   ],
   "id": "cell-20"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Your conclusions\n",
    "1. Does querying more points change the parameter count?\n",
    "2. Explain the role of collocation points.\n",
    "3. Compare the three evaluation regions.\n",
    "4. Discuss seed variation and wrong physics.\n",
    "5. Would a classical ODE solver be preferable here?"
   ],
   "id": "cell-21"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
