Automatize Tutorials html, py files creation (#496)
* workflow to export tutorials ---------
This commit is contained in:
committed by
FilippoOlivo
parent
8dfc9d19db
commit
3ff9f0c9a2
188
tutorials/tutorial14/tutorial.ipynb
vendored
188
tutorials/tutorial14/tutorial.ipynb
vendored
@@ -33,12 +33,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",
|
||||
"%matplotlib inline\n",
|
||||
"\n",
|
||||
@@ -50,7 +51,7 @@
|
||||
"from pina.model.block import PODBlock, RBFBlock\n",
|
||||
"from pina import LabelTensor\n",
|
||||
"\n",
|
||||
"warnings.filterwarnings('ignore')"
|
||||
"warnings.filterwarnings(\"ignore\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -70,6 +71,7 @@
|
||||
"source": [
|
||||
"import smithers\n",
|
||||
"from smithers.dataset import LidCavity\n",
|
||||
"\n",
|
||||
"dataset = LidCavity()"
|
||||
]
|
||||
},
|
||||
@@ -108,13 +110,13 @@
|
||||
],
|
||||
"source": [
|
||||
"fig, axs = plt.subplots(1, 3, figsize=(14, 3))\n",
|
||||
"for ax, par, u in zip(axs, dataset.params[:3], dataset.snapshots['mag(v)'][:3]):\n",
|
||||
"for ax, par, u in zip(axs, dataset.params[:3], dataset.snapshots[\"mag(v)\"][:3]):\n",
|
||||
" ax.tricontourf(dataset.triang, u, levels=16)\n",
|
||||
" ax.set_title(f'$u$ field for $\\mu$ = {par[0]:.4f}')\n",
|
||||
" ax.set_title(f\"$u$ field for $\\mu$ = {par[0]:.4f}\")\n",
|
||||
"fig, axs = plt.subplots(1, 3, figsize=(14, 3))\n",
|
||||
"for ax, par, u in zip(axs, dataset.params[:3], dataset.snapshots['p'][:3]):\n",
|
||||
"for ax, par, u in zip(axs, dataset.params[:3], dataset.snapshots[\"p\"][:3]):\n",
|
||||
" ax.tricontourf(dataset.triang, u, levels=16)\n",
|
||||
" ax.set_title(f'$p$ field for $\\mu$ = {par[0]:.4f}')"
|
||||
" ax.set_title(f\"$p$ field for $\\mu$ = {par[0]:.4f}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -130,15 +132,16 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"'''velocity magnitude data, 5041 for each snapshot'''\n",
|
||||
"u=torch.tensor(dataset.snapshots['mag(v)']).float() \n",
|
||||
"u = LabelTensor(u, labels=[f's{i}' for i in range(u.shape[1])])\n",
|
||||
"'''pressure data, 5041 for each snapshot'''\n",
|
||||
"p=torch.tensor(dataset.snapshots['p']).float()\n",
|
||||
"p = LabelTensor(p, labels=[f's{i}' for i in range(p.shape[1])])\n",
|
||||
"'''mu corresponding to each snapshot'''\n",
|
||||
"mu=torch.tensor(dataset.params).float()\n",
|
||||
"mu = LabelTensor(mu, labels=['mu'])\n"
|
||||
"\"\"\"velocity magnitude data, 5041 for each snapshot\"\"\"\n",
|
||||
"\n",
|
||||
"u = torch.tensor(dataset.snapshots[\"mag(v)\"]).float()\n",
|
||||
"u = LabelTensor(u, labels=[f\"s{i}\" for i in range(u.shape[1])])\n",
|
||||
"\"\"\"pressure data, 5041 for each snapshot\"\"\"\n",
|
||||
"p = torch.tensor(dataset.snapshots[\"p\"]).float()\n",
|
||||
"p = LabelTensor(p, labels=[f\"s{i}\" for i in range(p.shape[1])])\n",
|
||||
"\"\"\"mu corresponding to each snapshot\"\"\"\n",
|
||||
"mu = torch.tensor(dataset.params).float()\n",
|
||||
"mu = LabelTensor(mu, labels=[\"mu\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -154,15 +157,16 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"'''number of snapshots'''\n",
|
||||
"\"\"\"number of snapshots\"\"\"\n",
|
||||
"\n",
|
||||
"n = u.shape[0]\n",
|
||||
"'''training over total snapshots ratio and number of training snapshots'''\n",
|
||||
"ratio = 0.9 \n",
|
||||
"n_train = int(n*ratio)\n",
|
||||
"'''split u and p data'''\n",
|
||||
"u_train, u_test = u[:n_train], u[n_train:] #for mag(v)\n",
|
||||
"p_train, p_test = p[:n_train], p[n_train:] #for p\n",
|
||||
"'''split snapshots'''\n",
|
||||
"\"\"\"training over total snapshots ratio and number of training snapshots\"\"\"\n",
|
||||
"ratio = 0.9\n",
|
||||
"n_train = int(n * ratio)\n",
|
||||
"\"\"\"split u and p data\"\"\"\n",
|
||||
"u_train, u_test = u[:n_train], u[n_train:] # for mag(v)\n",
|
||||
"p_train, p_test = p[:n_train], p[n_train:] # for p\n",
|
||||
"\"\"\"split snapshots\"\"\"\n",
|
||||
"mu_train, mu_test = mu[:n_train], mu[n_train:]"
|
||||
]
|
||||
},
|
||||
@@ -183,8 +187,9 @@
|
||||
" \"\"\"\n",
|
||||
" Proper orthogonal decomposition with Radial Basis Function interpolation model.\n",
|
||||
" \"\"\"\n",
|
||||
"\n",
|
||||
" def __init__(self, pod_rank, rbf_kernel):\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" super().__init__()\n",
|
||||
" self.pod = PODBlock(pod_rank)\n",
|
||||
" self.rbf = RBFBlock(kernel=rbf_kernel)"
|
||||
@@ -207,8 +212,9 @@
|
||||
" \"\"\"\n",
|
||||
" Proper orthogonal decomposition with Radial Basis Function interpolation model.\n",
|
||||
" \"\"\"\n",
|
||||
"\n",
|
||||
" def __init__(self, pod_rank, rbf_kernel):\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" super().__init__()\n",
|
||||
" self.pod = PODBlock(pod_rank)\n",
|
||||
" self.rbf = RBFBlock(kernel=rbf_kernel)\n",
|
||||
@@ -223,6 +229,7 @@
|
||||
" \"\"\"\n",
|
||||
" coefficients = self.rbf(x)\n",
|
||||
" return self.pod.expand(coefficients)\n",
|
||||
"\n",
|
||||
" def fit(self, p, x):\n",
|
||||
" \"\"\"\n",
|
||||
" Call the :meth:`pina.model.layers.PODBlock.fit` method of the\n",
|
||||
@@ -231,8 +238,7 @@
|
||||
" :attr:`pina.model.layers.RBFBlock` attribute to fit the interpolation.\n",
|
||||
" \"\"\"\n",
|
||||
" self.pod.fit(x)\n",
|
||||
" self.rbf.fit(p, self.pod.reduce(x))\n",
|
||||
" "
|
||||
" self.rbf.fit(p, self.pod.reduce(x))"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -248,15 +254,16 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"'''create the model'''\n",
|
||||
"pod_rbfu = PODRBF(pod_rank=20, rbf_kernel='thin_plate_spline')\n",
|
||||
"\"\"\"create the model\"\"\"\n",
|
||||
"\n",
|
||||
"'''fit the model to velocity training data'''\n",
|
||||
"pod_rbfu = PODRBF(pod_rank=20, rbf_kernel=\"thin_plate_spline\")\n",
|
||||
"\n",
|
||||
"\"\"\"fit the model to velocity training data\"\"\"\n",
|
||||
"pod_rbfu.fit(mu_train, u_train)\n",
|
||||
"\n",
|
||||
"'''predict the parameter using the fitted model'''\n",
|
||||
"\"\"\"predict the parameter using the fitted model\"\"\"\n",
|
||||
"u_train_rbf = pod_rbfu(mu_train)\n",
|
||||
"u_test_rbf = pod_rbfu(mu_test)\n"
|
||||
"u_test_rbf = pod_rbfu(mu_test)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -282,12 +289,12 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"relative_u_error_train = torch.norm(u_train_rbf - u_train)/torch.norm(u_train)\n",
|
||||
"relative_u_error_test = torch.norm(u_test_rbf - u_test)/torch.norm(u_test)\n",
|
||||
"relative_u_error_train = torch.norm(u_train_rbf - u_train) / torch.norm(u_train)\n",
|
||||
"relative_u_error_test = torch.norm(u_test_rbf - u_test) / torch.norm(u_test)\n",
|
||||
"\n",
|
||||
"print('Error summary for POD-RBF model:')\n",
|
||||
"print(f' Train: {relative_u_error_train.item():e}')\n",
|
||||
"print(f' Test: {relative_u_error_test.item():e}')"
|
||||
"print(\"Error summary for POD-RBF model:\")\n",
|
||||
"print(f\" Train: {relative_u_error_train.item():e}\")\n",
|
||||
"print(f\" Test: {relative_u_error_test.item():e}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -323,23 +330,32 @@
|
||||
"fig, axs = plt.subplots(3, 4, figsize=(14, 10))\n",
|
||||
"\n",
|
||||
"relative_u_error_rbf = np.abs(u_test[idx] - u_idx_rbf.detach())\n",
|
||||
"relative_u_error_rbf = np.where(u_test[idx] < 1e-7, 1e-7, relative_u_error_rbf/u_test[idx])\n",
|
||||
" \n",
|
||||
"for i, (idx_, rbf_, rbf_err_) in enumerate(\n",
|
||||
" zip(idx, u_idx_rbf, relative_u_error_rbf)):\n",
|
||||
" axs[0, i].set_title('Prediction for ' f'$\\mu$ = {mu_test[idx_].item():.4f}')\n",
|
||||
" axs[1, i].set_title('True snapshot for ' f'$\\mu$ = {mu_test[idx_].item():.4f}')\n",
|
||||
" axs[2, i].set_title('Error for ' f'$\\mu$ = {mu_test[idx_].item():.4f}')\n",
|
||||
"relative_u_error_rbf = np.where(\n",
|
||||
" u_test[idx] < 1e-7, 1e-7, relative_u_error_rbf / u_test[idx]\n",
|
||||
")\n",
|
||||
"\n",
|
||||
" cm = axs[0, i].tricontourf(dataset.triang, rbf_.detach()) # POD-RBF prediction\n",
|
||||
"for i, (idx_, rbf_, rbf_err_) in enumerate(\n",
|
||||
" zip(idx, u_idx_rbf, relative_u_error_rbf)\n",
|
||||
"):\n",
|
||||
" axs[0, i].set_title(\"Prediction for \" f\"$\\mu$ = {mu_test[idx_].item():.4f}\")\n",
|
||||
" axs[1, i].set_title(\n",
|
||||
" \"True snapshot for \" f\"$\\mu$ = {mu_test[idx_].item():.4f}\"\n",
|
||||
" )\n",
|
||||
" axs[2, i].set_title(\"Error for \" f\"$\\mu$ = {mu_test[idx_].item():.4f}\")\n",
|
||||
"\n",
|
||||
" cm = axs[0, i].tricontourf(\n",
|
||||
" dataset.triang, rbf_.detach()\n",
|
||||
" ) # POD-RBF prediction\n",
|
||||
" plt.colorbar(cm, ax=axs[0, i])\n",
|
||||
" \n",
|
||||
" cm = axs[1, i].tricontourf(dataset.triang, u_test[idx_].flatten()) # Truth\n",
|
||||
"\n",
|
||||
" cm = axs[1, i].tricontourf(dataset.triang, u_test[idx_].flatten()) # Truth\n",
|
||||
" plt.colorbar(cm, ax=axs[1, i])\n",
|
||||
"\n",
|
||||
" cm = axs[2, i].tripcolor(dataset.triang, rbf_err_, norm=matplotlib.colors.LogNorm()) # Error for POD-RBF\n",
|
||||
" cm = axs[2, i].tripcolor(\n",
|
||||
" dataset.triang, rbf_err_, norm=matplotlib.colors.LogNorm()\n",
|
||||
" ) # Error for POD-RBF\n",
|
||||
" plt.colorbar(cm, ax=axs[2, i])\n",
|
||||
" \n",
|
||||
"\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
@@ -366,22 +382,23 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"'''create the model'''\n",
|
||||
"pod_rbfp = PODRBF(pod_rank=20, rbf_kernel='thin_plate_spline')\n",
|
||||
"\"\"\"create the model\"\"\"\n",
|
||||
"\n",
|
||||
"'''fit the model to pressure training data'''\n",
|
||||
"pod_rbfp = PODRBF(pod_rank=20, rbf_kernel=\"thin_plate_spline\")\n",
|
||||
"\n",
|
||||
"\"\"\"fit the model to pressure training data\"\"\"\n",
|
||||
"pod_rbfp.fit(mu_train, p_train)\n",
|
||||
"\n",
|
||||
"'''predict the parameter using the fitted model'''\n",
|
||||
"\"\"\"predict the parameter using the fitted model\"\"\"\n",
|
||||
"p_train_rbf = pod_rbfp(mu_train)\n",
|
||||
"p_test_rbf = pod_rbfp(mu_test)\n",
|
||||
"\n",
|
||||
"relative_p_error_train = torch.norm(p_train_rbf - p_train)/torch.norm(p_train)\n",
|
||||
"relative_p_error_test = torch.norm(p_test_rbf - p_test)/torch.norm(p_test)\n",
|
||||
"relative_p_error_train = torch.norm(p_train_rbf - p_train) / torch.norm(p_train)\n",
|
||||
"relative_p_error_test = torch.norm(p_test_rbf - p_test) / torch.norm(p_test)\n",
|
||||
"\n",
|
||||
"print('Error summary for POD-RBF model:')\n",
|
||||
"print(f' Train: {relative_p_error_train.item():e}')\n",
|
||||
"print(f' Test: {relative_p_error_test.item():e}')"
|
||||
"print(\"Error summary for POD-RBF model:\")\n",
|
||||
"print(f\" Train: {relative_p_error_train.item():e}\")\n",
|
||||
"print(f\" Test: {relative_p_error_test.item():e}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -409,10 +426,12 @@
|
||||
],
|
||||
"source": [
|
||||
"fig, axs = plt.subplots(2, 3, figsize=(14, 6))\n",
|
||||
"for ax, par, u in zip(axs.ravel(), dataset.params[66:72], dataset.snapshots['p'][66:72]):\n",
|
||||
"for ax, par, u in zip(\n",
|
||||
" axs.ravel(), dataset.params[66:72], dataset.snapshots[\"p\"][66:72]\n",
|
||||
"):\n",
|
||||
" cm = ax.tricontourf(dataset.triang, u, levels=16)\n",
|
||||
" plt.colorbar(cm, ax=ax)\n",
|
||||
" ax.set_title(f'$p$ field for $\\mu$ = {par[0]:.4f}')\n",
|
||||
" ax.set_title(f\"$p$ field for $\\mu$ = {par[0]:.4f}\")\n",
|
||||
"plt.tight_layout()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
@@ -442,11 +461,13 @@
|
||||
],
|
||||
"source": [
|
||||
"fig, axs = plt.subplots(2, 3, figsize=(14, 6))\n",
|
||||
"for ax, par, u in zip(axs.ravel(), dataset.params[98:104], dataset.snapshots['p'][98:104]):\n",
|
||||
"for ax, par, u in zip(\n",
|
||||
" axs.ravel(), dataset.params[98:104], dataset.snapshots[\"p\"][98:104]\n",
|
||||
"):\n",
|
||||
" cm = ax.tricontourf(dataset.triang, u, levels=16)\n",
|
||||
" plt.colorbar(cm, ax=ax)\n",
|
||||
" ax.set_title(f'$p$ field for $\\mu$ = {par[0]:.4f}')\n",
|
||||
"plt.tight_layout() \n",
|
||||
" ax.set_title(f\"$p$ field for $\\mu$ = {par[0]:.4f}\")\n",
|
||||
"plt.tight_layout()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
@@ -473,37 +494,42 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"'''excluding problematic snapshots'''\n",
|
||||
"\"\"\"excluding problematic snapshots\"\"\"\n",
|
||||
"\n",
|
||||
"data = list(range(300))\n",
|
||||
"data_to_consider = data[:67] + data[71:100] + data[102:]\n",
|
||||
"'''proceed as before'''\n",
|
||||
"newp=torch.tensor(dataset.snapshots['p'][data_to_consider]).float()\n",
|
||||
"newp = LabelTensor(newp, labels=[f's{i}' for i in range(newp.shape[1])])\n",
|
||||
"\"\"\"proceed as before\"\"\"\n",
|
||||
"newp = torch.tensor(dataset.snapshots[\"p\"][data_to_consider]).float()\n",
|
||||
"newp = LabelTensor(newp, labels=[f\"s{i}\" for i in range(newp.shape[1])])\n",
|
||||
"\n",
|
||||
"newmu=torch.tensor(dataset.params[data_to_consider]).float()\n",
|
||||
"newmu = LabelTensor(newmu, labels=['mu'])\n",
|
||||
"newmu = torch.tensor(dataset.params[data_to_consider]).float()\n",
|
||||
"newmu = LabelTensor(newmu, labels=[\"mu\"])\n",
|
||||
"\n",
|
||||
"newn = newp.shape[0]\n",
|
||||
"ratio = 0.9 \n",
|
||||
"new_train = int(newn*ratio)\n",
|
||||
"ratio = 0.9\n",
|
||||
"new_train = int(newn * ratio)\n",
|
||||
"\n",
|
||||
"new_p_train, new_p_test = newp[:new_train], newp[new_train:] \n",
|
||||
"new_p_train, new_p_test = newp[:new_train], newp[new_train:]\n",
|
||||
"\n",
|
||||
"new_mu_train, new_mu_test = newmu[:new_train], newmu[new_train:]\n",
|
||||
"\n",
|
||||
"new_pod_rbfp = PODRBF(pod_rank=20, rbf_kernel='thin_plate_spline')\n",
|
||||
"new_pod_rbfp = PODRBF(pod_rank=20, rbf_kernel=\"thin_plate_spline\")\n",
|
||||
"\n",
|
||||
"new_pod_rbfp.fit(new_mu_train, new_p_train)\n",
|
||||
"\n",
|
||||
"new_p_train_rbf = new_pod_rbfp(new_mu_train)\n",
|
||||
"new_p_test_rbf = new_pod_rbfp(new_mu_test)\n",
|
||||
"\n",
|
||||
"new_relative_p_error_train = torch.norm(new_p_train_rbf - new_p_train)/torch.norm(new_p_train)\n",
|
||||
"new_relative_p_error_test = torch.norm(new_p_test_rbf - new_p_test)/torch.norm(new_p_test)\n",
|
||||
"new_relative_p_error_train = torch.norm(\n",
|
||||
" new_p_train_rbf - new_p_train\n",
|
||||
") / torch.norm(new_p_train)\n",
|
||||
"new_relative_p_error_test = torch.norm(\n",
|
||||
" new_p_test_rbf - new_p_test\n",
|
||||
") / torch.norm(new_p_test)\n",
|
||||
"\n",
|
||||
"print('Error summary for POD-RBF model:')\n",
|
||||
"print(f' Train: {new_relative_p_error_train.item():e}')\n",
|
||||
"print(f' Test: {new_relative_p_error_test.item():e}')"
|
||||
"print(\"Error summary for POD-RBF model:\")\n",
|
||||
"print(f\" Train: {new_relative_p_error_train.item():e}\")\n",
|
||||
"print(f\" Test: {new_relative_p_error_test.item():e}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user