Automatize Tutorials html, py files creation (#496)

* workflow to export tutorials

---------
This commit is contained in:
Dario Coscia
2025-03-15 11:01:19 +01:00
committed by FilippoOlivo
parent 8dfc9d19db
commit 3ff9f0c9a2
51 changed files with 140529 additions and 440 deletions

View File

@@ -26,12 +26,13 @@
"source": [
"## routine needed to run the notebook on Google Colab\n",
"try:\n",
" import google.colab\n",
" IN_COLAB = True\n",
" import google.colab\n",
"\n",
" IN_COLAB = True\n",
"except:\n",
" IN_COLAB = False\n",
" IN_COLAB = False\n",
"if IN_COLAB:\n",
" !pip install \"pina-mathlab\"\n",
" !pip install \"pina-mathlab\"\n",
"\n",
"import torch\n",
"import matplotlib.pyplot as plt\n",
@@ -47,7 +48,7 @@
"from pina.equation import Equation\n",
"from pina.callback import MetricTracker\n",
"\n",
"warnings.filterwarnings('ignore')"
"warnings.filterwarnings(\"ignore\")"
]
},
{
@@ -111,6 +112,7 @@
" def solution(self, pts):\n",
" return torch.sin(torch.pi * pts) * torch.cos(3.0 * torch.pi * pts)\n",
"\n",
"\n",
"problem = Helmholtz()\n",
"\n",
"# let's discretise the domain\n",
@@ -234,7 +236,7 @@
" pinn,\n",
" max_epochs=5000,\n",
" accelerator=\"cpu\",\n",
" enable_model_summary=False, \n",
" enable_model_summary=False,\n",
" callbacks=[MetricTracker()],\n",
" train_size=1.0,\n",
" val_size=0.0,\n",
@@ -266,9 +268,9 @@
" range(len(trainer_metrics[\"train_loss\"])), trainer_metrics[\"train_loss\"]\n",
")\n",
"# plotting\n",
"plt.xlabel('epoch')\n",
"plt.ylabel('loss')\n",
"plt.yscale('log') "
"plt.xlabel(\"epoch\")\n",
"plt.ylabel(\"loss\")\n",
"plt.yscale(\"log\")"
]
},
{
@@ -305,11 +307,11 @@
}
],
"source": [
"pts = pinn.problem.spatial_domain.sample(256, 'grid', variables='x')\n",
"predicted_output = pinn.forward(pts).extract('u').tensor.detach()\n",
"pts = pinn.problem.spatial_domain.sample(256, \"grid\", variables=\"x\")\n",
"predicted_output = pinn.forward(pts).extract(\"u\").tensor.detach()\n",
"true_output = pinn.problem.solution(pts)\n",
"plt.plot(pts.extract(['x']), predicted_output, label='Neural Network solution')\n",
"plt.plot(pts.extract(['x']), true_output, label='True solution')\n",
"plt.plot(pts.extract([\"x\"]), predicted_output, label=\"Neural Network solution\")\n",
"plt.plot(pts.extract([\"x\"]), true_output, label=\"True solution\")\n",
"plt.legend()"
]
},
@@ -340,21 +342,21 @@
"# plotting solution\n",
"with torch.no_grad():\n",
" # Notice here we put [-4, 4]!!!\n",
" new_domain = CartesianDomain({'x' : [0, 4]})\n",
" x = new_domain.sample(1000, mode='grid')\n",
" new_domain = CartesianDomain({\"x\": [0, 4]})\n",
" x = new_domain.sample(1000, mode=\"grid\")\n",
" fig, axes = plt.subplots(1, 3, figsize=(15, 5))\n",
" # Plot 1\n",
" axes[0].plot(x, problem.solution(x), label=r'$u(x)$', color='blue')\n",
" axes[0].set_title(r'True solution $u(x)$')\n",
" axes[0].plot(x, problem.solution(x), label=r\"$u(x)$\", color=\"blue\")\n",
" axes[0].set_title(r\"True solution $u(x)$\")\n",
" axes[0].legend(loc=\"upper right\")\n",
" # Plot 2\n",
" axes[1].plot(x, pinn(x), label=r'$u_{\\theta}(x)$', color='green')\n",
" axes[1].set_title(r'PINN solution $u_{\\theta}(x)$')\n",
" axes[1].plot(x, pinn(x), label=r\"$u_{\\theta}(x)$\", color=\"green\")\n",
" axes[1].set_title(r\"PINN solution $u_{\\theta}(x)$\")\n",
" axes[1].legend(loc=\"upper right\")\n",
" # Plot 3\n",
" diff = torch.abs(problem.solution(x) - pinn(x))\n",
" axes[2].plot(x, diff, label=r'$|u(x) - u_{\\theta}(x)|$', color='red')\n",
" axes[2].set_title(r'Absolute difference $|u(x) - u_{\\theta}(x)|$')\n",
" axes[2].plot(x, diff, label=r\"$|u(x) - u_{\\theta}(x)|$\", color=\"red\")\n",
" axes[2].set_title(r\"Absolute difference $|u(x) - u_{\\theta}(x)|$\")\n",
" axes[2].legend(loc=\"upper right\")\n",
" # Adjust layout\n",
" plt.tight_layout()\n",