Skip to content

Commit 157786d

Browse files
Github action: auto-update.
1 parent 9b80577 commit 157786d

File tree

129 files changed

+4298
-3840
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+4298
-3840
lines changed

dev/_downloads/0293c114493d7ce78ab4aad5a952e0cd/plot_spectral_projection.ipynb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
},
2323
"outputs": [],
2424
"source": [
25-
"import torch\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom neuralop.layers.spectral_projection import spectral_projection_divergence_free\nfrom neuralop.losses.differentiation import FourierDiff, FiniteDiff\n \ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(f\"Using device: {device}\")"
25+
"import torch\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom neuralop.layers.spectral_projection import spectral_projection_divergence_free\nfrom neuralop.losses.differentiation import FourierDiff, FiniteDiff\n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(f\"Using device: {device}\")"
2626
]
2727
},
2828
{
2929
"cell_type": "markdown",
3030
"metadata": {},
3131
"source": [
32-
".. raw:: html\n\n <div style=\"margin-top: 3em;\"></div>\n\n## Divergence error computation functions\nWe define two functions to compute the divergence error \nusing spectral differentiation and finite differences.\n\n"
32+
".. raw:: html\n\n <div style=\"margin-top: 3em;\"></div>\n\n## Divergence error computation functions\nWe define two functions to compute the divergence error\nusing spectral differentiation and finite differences.\n\n"
3333
]
3434
},
3535
{
@@ -47,7 +47,7 @@
4747
"cell_type": "markdown",
4848
"metadata": {},
4949
"source": [
50-
".. raw:: html\n\n <div style=\"margin-top: 3em;\"></div>\n\n## Setting considered\nWe start from a divergence-free velocity field on [0, 2*pi] x [0, 2*pi] \nconstructed from the stream function \u03c8(x,y) = sin(x) * cos(6*y) \n\nVelocity components: \n u_x = \u2202\u03c8/\u2202y = -6 * sin(x) * sin(6*y) \n\n u_y = -\u2202\u03c8/\u2202x = - cos(x) * cos(6*y) \n\n-------------------------------------------------------\n\nMathematical verification of divergence-free property: \n\n \u2207\u00b7u = \u2202u_x/\u2202x + \u2202u_y/\u2202y \n\n = \u2202/\u2202x[-6*sin(x) * sin(6*y)] + \u2202/\u2202y[-cos(x) * cos(6*y)] \n\n = -6*cos(x) * sin(6*y) + 6*cos(x) * sin(6*y) \n\n = 0 \u2713 \n\n-------------------------------------------------------\n\n\nWe then add 10% noise to break the divergence-free property. \n\nWe then apply the spectral projection to restore the divergence-free property. \n\nWe then compute the divergence error for the original, noisy, and projected fields. \n\nWe repeat this at various resolutions, [256, 512, 1024, 2048, 4096, 8192].\n\n"
50+
".. raw:: html\n\n <div style=\"margin-top: 3em;\"></div>\n\n## Setting considered\nWe start from a divergence-free velocity field on [0, 2*pi] x [0, 2*pi]\nconstructed from the stream function \u03c8(x,y) = sin(x) * cos(6*y)\n\nVelocity components:\n u_x = \u2202\u03c8/\u2202y = -6 * sin(x) * sin(6*y)\n\n u_y = -\u2202\u03c8/\u2202x = - cos(x) * cos(6*y)\n\n-------------------------------------------------------\n\nMathematical verification of divergence-free property:\n\n \u2207\u00b7u = \u2202u_x/\u2202x + \u2202u_y/\u2202y\n\n = \u2202/\u2202x[-6*sin(x) * sin(6*y)] + \u2202/\u2202y[-cos(x) * cos(6*y)]\n\n = -6*cos(x) * sin(6*y) + 6*cos(x) * sin(6*y)\n\n = 0 \u2713\n\n-------------------------------------------------------\n\n\nWe then add 10% noise to break the divergence-free property.\n\nWe then apply the spectral projection to restore the divergence-free property.\n\nWe then compute the divergence error for the original, noisy, and projected fields.\n\nWe repeat this at various resolutions, [256, 512, 1024, 2048, 4096, 8192].\n\n"
5151
]
5252
},
5353
{
@@ -58,14 +58,14 @@
5858
},
5959
"outputs": [],
6060
"source": [
61-
"L = 2 * np.pi\nnoise_level = 0.1\nresolutions = [256, 512, 1024, 2048, 4096, 8192]\n\n\nerrors_original_spectral = []\nerrors_original_finite = []\nerrors_noisy_spectral = []\nerrors_noisy_finite = []\nerrors_prog_spectral = []\nerrors_prog_finite = []\n\nfor target_resolution in resolutions:\n \n # Create coordinate grids for this resolution\n xs = torch.arange(target_resolution, device=device, dtype=torch.float64) * (L / target_resolution)\n ys = torch.arange(target_resolution, device=device, dtype=torch.float64) * (L / target_resolution)\n X, Y = torch.meshgrid(xs, ys, indexing='ij')\n \n # Create divergence-free field using the stream function defined earlier\n u_x = -6.0 * torch.sin(X) * torch.sin(6.0 * Y)\n u_y = -torch.cos(X) * torch.cos(6.0 * Y)\n u = torch.stack([u_x, u_y], dim=0).unsqueeze(0).to(device=device, dtype=torch.float64)\n \n # Add noise to break divergence-free property\n mean_magnitude = torch.mean(torch.sqrt(u[:, 0]**2 + u[:, 1]**2))\n noise = torch.randn_like(u, dtype=torch.float64) * noise_level * mean_magnitude\n u_noisy = u + noise\n \n # Apply spectral projection to restore divergence-free property\n u_proj = spectral_projection_divergence_free(u_noisy, L, constraint_modes=(64, 64))\n \n # Compute divergence errors for all three fields\n errors_original_spectral.append(div_error_fourier(u, L))\n errors_original_finite.append(div_error_finite_diff(u, L))\n errors_noisy_spectral.append(div_error_fourier(u_noisy, L))\n errors_noisy_finite.append(div_error_finite_diff(u_noisy, L))\n errors_prog_spectral.append(div_error_fourier(u_proj, L))\n errors_prog_finite.append(div_error_finite_diff(u_proj, L))"
61+
"L = 2 * np.pi\nnoise_level = 0.1\nresolutions = [256, 512, 1024, 2048, 4096, 8192]\n\n\nerrors_original_spectral = []\nerrors_original_finite = []\nerrors_noisy_spectral = []\nerrors_noisy_finite = []\nerrors_prog_spectral = []\nerrors_prog_finite = []\n\nfor target_resolution in resolutions:\n # Create coordinate grids for this resolution\n xs = torch.arange(target_resolution, device=device, dtype=torch.float64) * (L / target_resolution)\n ys = torch.arange(target_resolution, device=device, dtype=torch.float64) * (L / target_resolution)\n X, Y = torch.meshgrid(xs, ys, indexing=\"ij\")\n \n # Create divergence-free field using the stream function defined earlier\n u_x = -6.0 * torch.sin(X) * torch.sin(6.0 * Y)\n u_y = -torch.cos(X) * torch.cos(6.0 * Y)\n u = torch.stack([u_x, u_y], dim=0).unsqueeze(0).to(device=device, dtype=torch.float64)\n \n # Add noise to break divergence-free property\n mean_magnitude = torch.mean(torch.sqrt(u[:, 0] ** 2 + u[:, 1] ** 2))\n noise = torch.randn_like(u, dtype=torch.float64) * noise_level * mean_magnitude\n u_noisy = u + noise\n\n # Apply spectral projection to restore divergence-free property\n u_proj = spectral_projection_divergence_free(u_noisy, L, constraint_modes=(64, 64))\n\n # Compute divergence errors for all three fields\n errors_original_spectral.append(div_error_fourier(u, L))\n errors_original_finite.append(div_error_finite_diff(u, L))\n errors_noisy_spectral.append(div_error_fourier(u_noisy, L))\n errors_noisy_finite.append(div_error_finite_diff(u_noisy, L))\n errors_prog_spectral.append(div_error_fourier(u_proj, L))\n errors_prog_finite.append(div_error_finite_diff(u_proj, L))"
6262
]
6363
},
6464
{
6565
"cell_type": "markdown",
6666
"metadata": {},
6767
"source": [
68-
".. raw:: html\n\n <div style=\"margin-top: 3em;\"></div>\n\n## Divergence Errors using Spectral Differentiation \nThe Fourier differentiation method computes derivatives in the spectral domain \nby transforming the field to Fourier space, applying the appropriate wavenumber \noperators, and transforming back. \n\nWe display the divergence error for the original, noisy, and projected fields \nat the different resolutions. \n\nNote that at lower resolutions, finite differences are not accurate enough \nto properly compute the divergence error, which is why the errors appear higher \ninitially but improve at higher resolutions. This is a limitation of \nfinite differences for computing derivatives, not an issue with the \nspectral projection itself. Spectral differentiation provides more accurate \nderivative calculations at lower resolutions.\n\n"
68+
".. raw:: html\n\n <div style=\"margin-top: 3em;\"></div>\n\n## Divergence Errors using Spectral Differentiation\nThe Fourier differentiation method computes derivatives in the spectral domain\nby transforming the field to Fourier space, applying the appropriate wavenumber\noperators, and transforming back.\n\nWe display the divergence error for the original, noisy, and projected fields\nat the different resolutions.\n\nNote that at lower resolutions, finite differences are not accurate enough\nto properly compute the divergence error, which is why the errors appear higher\ninitially but improve at higher resolutions. This is a limitation of\nfinite differences for computing derivatives, not an issue with the\nspectral projection itself. Spectral differentiation provides more accurate\nderivative calculations at lower resolutions.\n\n"
6969
]
7070
},
7171
{
@@ -76,14 +76,14 @@
7676
},
7777
"outputs": [],
7878
"source": [
79-
"# Spectral Differentiation table\nprint(\"-\" * 55)\nprint(f\"{'Resolution':<12} {'Original':<15} {'Noisy':<15} {'Projected':<15}\")\nprint(\"-\" * 55)\n\nfor i, res in enumerate(resolutions):\n print(f\"{res:<12} {errors_original_spectral[i]:<15.2e} {errors_noisy_spectral[i]:<15.2e} {errors_prog_spectral[i]:<15.2e}\")\n\n\n# Spectral Differentiation plot\nplt.figure(figsize=(10, 6))\nplt.semilogy(resolutions, errors_original_spectral, 'o-', label='Original', color='black', linewidth=2.5, markersize=6)\nplt.semilogy(resolutions, errors_noisy_spectral, 'o-', label='Noisy', color='green', linewidth=2.5, markersize=6)\nplt.semilogy(resolutions, errors_prog_spectral, 'o-', label='Projected', color='blue', linewidth=2.5, markersize=6)\nplt.xlabel('Resolution', fontsize=16)\nplt.ylabel('Divergence Error (L2 norm)', fontsize=16)\nplt.title('Spectral Divergence Errors', fontsize=18)\nplt.legend(fontsize=14)\nplt.grid(True, alpha=0.3)\nplt.xticks(fontsize=14)\nplt.yticks(fontsize=14)\nplt.tight_layout()\nplt.show()"
79+
"# Spectral Differentiation table\nprint(\"-\" * 55)\nprint(f\"{'Resolution':<12} {'Original':<15} {'Noisy':<15} {'Projected':<15}\")\nprint(\"-\" * 55)\n\nfor i, res in enumerate(resolutions):\n print(\n f\"{res:<12} {errors_original_spectral[i]:<15.2e} {errors_noisy_spectral[i]:<15.2e} {errors_prog_spectral[i]:<15.2e}\"\n )\n\n\n# Spectral Differentiation plot\nplt.figure(figsize=(10, 6))\nplt.semilogy(resolutions, errors_original_spectral, \"o-\", label=\"Original\", color=\"black\", linewidth=2.5, markersize=6)\nplt.semilogy(resolutions, errors_noisy_spectral, \"o-\", label=\"Noisy\", color=\"green\", linewidth=2.5, markersize=6)\nplt.semilogy(resolutions, errors_prog_spectral, \"o-\", label=\"Projected\", color=\"blue\", linewidth=2.5, markersize=6)\nplt.xlabel(\"Resolution\", fontsize=16)\nplt.ylabel(\"Divergence Error (L2 norm)\", fontsize=16)\nplt.title(\"Spectral Divergence Errors\", fontsize=18)\nplt.legend(fontsize=14)\nplt.grid(True, alpha=0.3)\nplt.xticks(fontsize=14)\nplt.yticks(fontsize=14)\nplt.tight_layout()\nplt.show()"
8080
]
8181
},
8282
{
8383
"cell_type": "markdown",
8484
"metadata": {},
8585
"source": [
86-
".. raw:: html\n\n <div style=\"margin-top: 3em;\"></div>\n\n## Divergence Errors using Finite Differences\nThe finite difference method approximates derivatives using central differences. \n\nWe display the divergence error for the original, noisy, and projected fields \nat the different resolutions.\n\n"
86+
".. raw:: html\n\n <div style=\"margin-top: 3em;\"></div>\n\n## Divergence Errors using Finite Differences\nThe finite difference method approximates derivatives using central differences.\n\nWe display the divergence error for the original, noisy, and projected fields\nat the different resolutions.\n\n"
8787
]
8888
},
8989
{
@@ -94,7 +94,7 @@
9494
},
9595
"outputs": [],
9696
"source": [
97-
"# Finite differences table\nprint(\"-\" * 55)\nprint(f\"{'Resolution':<12} {'Original':<15} {'Noisy':<15} {'Projected':<15}\")\nprint(\"-\" * 55)\n\nfor i, res in enumerate(resolutions):\n print(f\"{res:<12} {errors_original_finite[i]:<15.2e} {errors_noisy_finite[i]:<15.2e} {errors_prog_finite[i]:<15.2e}\")\n\n\n# Finite differences plot\nplt.figure(figsize=(10, 6))\nplt.semilogy(resolutions, errors_original_finite, 'o-', label='Original', color='black', linewidth=2.5, markersize=6)\nplt.semilogy(resolutions, errors_noisy_finite, 'o-', label='Noisy', color='green', linewidth=2.5, markersize=6)\nplt.semilogy(resolutions, errors_prog_finite, 'o-', label='Projected', color='blue', linewidth=2.5, markersize=6)\nplt.xlabel('Resolution', fontsize=16)\nplt.ylabel('Divergence Error (L2 norm)', fontsize=16)\nplt.title('Finite Difference Divergence Errors', fontsize=18)\nplt.legend(fontsize=14)\nplt.grid(True, alpha=0.3)\nplt.xticks(fontsize=14)\nplt.yticks(fontsize=14)\nplt.tight_layout()\nplt.show()"
97+
"# Finite differences table\nprint(\"-\" * 55)\nprint(f\"{'Resolution':<12} {'Original':<15} {'Noisy':<15} {'Projected':<15}\")\nprint(\"-\" * 55)\n\nfor i, res in enumerate(resolutions):\n print(\n f\"{res:<12} {errors_original_finite[i]:<15.2e} {errors_noisy_finite[i]:<15.2e} {errors_prog_finite[i]:<15.2e}\"\n )\n\n\n# Finite differences plot\nplt.figure(figsize=(10, 6))\nplt.semilogy(resolutions, errors_original_finite, \"o-\", label=\"Original\", color=\"black\", linewidth=2.5, markersize=6)\nplt.semilogy(resolutions, errors_noisy_finite, \"o-\", label=\"Noisy\", color=\"green\", linewidth=2.5, markersize=6)\nplt.semilogy(resolutions, errors_prog_finite, \"o-\", label=\"Projected\", color=\"blue\", linewidth=2.5, markersize=6)\nplt.xlabel(\"Resolution\", fontsize=16)\nplt.ylabel(\"Divergence Error (L2 norm)\", fontsize=16)\nplt.title(\"Finite Difference Divergence Errors\", fontsize=18)\nplt.legend(fontsize=14)\nplt.grid(True, alpha=0.3)\nplt.xticks(fontsize=14)\nplt.yticks(fontsize=14)\nplt.tight_layout()\nplt.show()"
9898
]
9999
}
100100
],
Binary file not shown.

dev/_downloads/07795e165bda9881ff980becb31905ab/plot_diffusion_advection_solver.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
"outputs": [],
2424
"source": [
25-
"import torch\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.animation as animation\n\nfrom neuralop.losses.differentiation import FiniteDiff \n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")"
25+
"import torch\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.animation as animation\n\nfrom neuralop.losses.differentiation import FiniteDiff\n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")"
2626
]
2727
},
2828
{
@@ -40,7 +40,7 @@
4040
},
4141
"outputs": [],
4242
"source": [
43-
"## Simulation parameters\nLx, Ly = 2.0, 2.0 # Domain lengths\nnx, ny = 64, 64 # Grid resolution\nT = 1.6 # Total simulation time\ndt = 0.001 # Time step\nnu = 0.02 # diffusion coefficient\ncx, cy = 1.0, 0.6 # advection speeds\n\n## Create grid\nX = torch.linspace(0, Lx, nx, device=device).repeat(ny, 1).T \nY = torch.linspace(0, Ly, ny, device=device).repeat(nx, 1) \ndx = Lx / (nx - 1)\ndy = Ly / (ny - 1)\nnt = int(T / dt)\n\n## Initialize finite difference operator\nfd = FiniteDiff(dim=2, h=(dx, dy))\n\n\n## Initial condition and source term\nu = (-torch.sin(2 * np.pi * Y) * torch.cos(2 * np.pi * X)\n + 0.3 * torch.exp(-((X - 0.75)**2 + (Y - 0.5)**2) / 0.02)\n - 0.3 * torch.exp(-((X - 1.25)**2 + (Y - 1.5)**2) / 0.02)).to(device)\n\ndef source_term(X, Y, t):\n return 0.2 * torch.sin(3 * np.pi * X) * torch.cos(3 * np.pi * Y) * torch.cos(4 * np.pi * t)"
43+
"## Simulation parameters\nLx, Ly = 2.0, 2.0 # Domain lengths\nnx, ny = 64, 64 # Grid resolution\nT = 1.6 # Total simulation time\ndt = 0.001 # Time step\nnu = 0.02 # diffusion coefficient\ncx, cy = 1.0, 0.6 # advection speeds\n\n## Create grid\nX = torch.linspace(0, Lx, nx, device=device).repeat(ny, 1).T\nY = torch.linspace(0, Ly, ny, device=device).repeat(nx, 1)\ndx = Lx / (nx - 1)\ndy = Ly / (ny - 1)\nnt = int(T / dt)\n\n## Initialize finite difference operator\nfd = FiniteDiff(dim=2, h=(dx, dy))\n\n\n## Initial condition and source term\nu = (\n -torch.sin(2 * np.pi * Y) * torch.cos(2 * np.pi * X)\n + 0.3 * torch.exp(-((X - 0.75) ** 2 + (Y - 0.5) ** 2) / 0.02)\n - 0.3 * torch.exp(-((X - 1.25) ** 2 + (Y - 1.5) ** 2) / 0.02)\n).to(device)\n\n\ndef source_term(X, Y, t):\n return 0.2 * torch.sin(3 * np.pi * X) * torch.cos(3 * np.pi * Y) * torch.cos(4 * np.pi * t)"
4444
]
4545
},
4646
{
@@ -58,7 +58,7 @@
5858
},
5959
"outputs": [],
6060
"source": [
61-
"u_evolution = [u.clone()]\n\nt = torch.tensor(0.0)\nfor _ in range(nt):\n \n # Compute derivatives\n u_x = fd.dx(u)\n u_y = fd.dy(u)\n u_xx = fd.dx(u_x)\n u_yy = fd.dy(u_y)\n\n # Evolve one step in time using Euler's method\n u = u + dt * (-cx * u_x - cy * u_y + nu * (u_xx + u_yy) + source_term(X, Y, t))\n t += dt\n u_evolution.append(u.clone())\n\nu_evolution = torch.stack(u_evolution).cpu().numpy()"
61+
"u_evolution = [u.clone()]\n\nt = torch.tensor(0.0)\nfor _ in range(nt):\n # Compute derivatives\n u_x = fd.dx(u)\n u_y = fd.dy(u)\n u_xx = fd.dx(u_x)\n u_yy = fd.dy(u_y)\n\n # Evolve one step in time using Euler's method\n u = u + dt * (-cx * u_x - cy * u_y + nu * (u_xx + u_yy) + source_term(X, Y, t))\n t += dt\n u_evolution.append(u.clone())\n\nu_evolution = torch.stack(u_evolution).cpu().numpy()"
6262
]
6363
},
6464
{
@@ -76,7 +76,7 @@
7676
},
7777
"outputs": [],
7878
"source": [
79-
"num_frames = 100\nframe_indices = torch.linspace(0, len(u_evolution) - 1, num_frames, dtype=torch.int).cpu().numpy()\nu_frames = u_evolution[frame_indices]\n\nfig, ax = plt.subplots(figsize=(6, 6))\ncmap_u = ax.imshow(u_frames[0], extent=[0, Lx, 0, Ly], origin=\"lower\", cmap=\"plasma\")\nax.set_title(\"Advection-Diffusion: u\")\nplt.colorbar(cmap_u, ax=ax, shrink=0.75)\n\ndef update(frame):\n cmap_u.set_data(u_frames[frame])\n ax.set_title(f\"Time: {frame_indices[frame] * dt:.3f}\")\n ax.set_xticks([])\n ax.set_yticks([])\n return cmap_u,\n\nani = animation.FuncAnimation(fig, update, frames=len(u_frames), interval=50, blit=False)"
79+
"num_frames = 100\nframe_indices = (\n torch.linspace(0, len(u_evolution) - 1, num_frames, dtype=torch.int).cpu().numpy()\n)\nu_frames = u_evolution[frame_indices]\n\nfig, ax = plt.subplots(figsize=(6, 6))\ncmap_u = ax.imshow(u_frames[0], extent=[0, Lx, 0, Ly], origin=\"lower\", cmap=\"plasma\")\nax.set_title(\"Advection-Diffusion: u\")\nplt.colorbar(cmap_u, ax=ax, shrink=0.75)\n\n\ndef update(frame):\n cmap_u.set_data(u_frames[frame])\n ax.set_title(f\"Time: {frame_indices[frame] * dt:.3f}\")\n ax.set_xticks([])\n ax.set_yticks([])\n return (cmap_u,)\n\n\nani = animation.FuncAnimation(fig, update, frames=len(u_frames), interval=50, blit=False)"
8080
]
8181
}
8282
],
Binary file not shown.

0 commit comments

Comments
 (0)