Co-authored-by: dario-coscia <dario-coscia@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
85b9edc74d
commit
f3ccfd4598
88
tutorials/tutorial9/tutorial.py
vendored
88
tutorials/tutorial9/tutorial.py
vendored
@@ -2,14 +2,14 @@
|
||||
# coding: utf-8
|
||||
|
||||
# # Tutorial: Applying Periodic Boundary Conditions in PINNs to solve the Helmholtz Problem
|
||||
#
|
||||
#
|
||||
# [](https://colab.research.google.com/github/mathLab/PINA/blob/master/tutorials/tutorial9/tutorial.ipynb)
|
||||
#
|
||||
# This tutorial demonstrates how to solve a one-dimensional Helmholtz equation with periodic boundary conditions (PBC) using Physics-Informed Neural Networks (PINNs).
|
||||
#
|
||||
# This tutorial demonstrates how to solve a one-dimensional Helmholtz equation with periodic boundary conditions (PBC) using Physics-Informed Neural Networks (PINNs).
|
||||
# We will use standard PINN training, augmented with a periodic input expansion as introduced in [*An Expert’s Guide to Training Physics-Informed Neural Networks*](https://arxiv.org/abs/2308.08468).
|
||||
#
|
||||
#
|
||||
# Let's start with some useful imports:
|
||||
#
|
||||
#
|
||||
|
||||
# In[1]:
|
||||
|
||||
@@ -42,33 +42,33 @@ warnings.filterwarnings("ignore")
|
||||
|
||||
|
||||
# ## Problem Definition
|
||||
#
|
||||
#
|
||||
# The one-dimensional Helmholtz problem is mathematically expressed as:
|
||||
#
|
||||
#
|
||||
# $$
|
||||
# \begin{cases}
|
||||
# \frac{d^2}{dx^2}u(x) - \lambda u(x) - f(x) &= 0 \quad \text{for } x \in (0, 2) \\
|
||||
# u^{(m)}(x = 0) - u^{(m)}(x = 2) &= 0 \quad \text{for } m \in \{0, 1, \dots\}
|
||||
# \end{cases}
|
||||
# $$
|
||||
#
|
||||
# In this case, we seek a solution that is $C^{\infty}$ (infinitely differentiable) and periodic with period 2, over the infinite domain $x \in (-\infty, \infty)$.
|
||||
#
|
||||
#
|
||||
# In this case, we seek a solution that is $C^{\infty}$ (infinitely differentiable) and periodic with period 2, over the infinite domain $x \in (-\infty, \infty)$.
|
||||
#
|
||||
# A classical PINN approach would require enforcing periodic boundary conditions (PBC) for all derivatives—an infinite set of constraints—which is clearly infeasible.
|
||||
#
|
||||
#
|
||||
# To address this, we adopt a strategy known as *coordinate augmentation*. In this approach, we apply a coordinate transformation $v(x)$ such that the transformed inputs naturally satisfy the periodicity condition:
|
||||
#
|
||||
#
|
||||
# $$
|
||||
# u^{(m)}(x = 0) - u^{(m)}(x = 2) = 0 \quad \text{for } m \in \{0, 1, \dots\}
|
||||
# $$
|
||||
#
|
||||
#
|
||||
# For demonstration purposes, we choose the specific parameters:
|
||||
#
|
||||
#
|
||||
# - $\lambda = -10\pi^2$
|
||||
# - $f(x) = -6\pi^2 \sin(3\pi x) \cos(\pi x)$
|
||||
#
|
||||
#
|
||||
# These yield an analytical solution:
|
||||
#
|
||||
#
|
||||
# $$
|
||||
# u(x) = \sin(\pi x) \cos(3\pi x)
|
||||
# $$
|
||||
@@ -111,39 +111,39 @@ problem.discretise_domain(200, "grid", domains=["phys_cond"])
|
||||
|
||||
|
||||
# As usual, the Helmholtz problem is implemented in **PINA** as a class. The governing equations are defined as `conditions`, which must be satisfied within their respective domains. The `solution` represents the exact analytical solution, which will be used to evaluate the accuracy of the predicted solution.
|
||||
#
|
||||
# For selecting collocation points, we use Latin Hypercube Sampling (LHS), a common strategy for efficient space-filling in high-dimensional domains
|
||||
#
|
||||
#
|
||||
# For selecting collocation points, we use Latin Hypercube Sampling (LHS), a common strategy for efficient space-filling in high-dimensional domains
|
||||
#
|
||||
# ## Solving the Problem with a Periodic Network
|
||||
#
|
||||
# Any $\mathcal{C}^{\infty}$ periodic function $u : \mathbb{R} \rightarrow \mathbb{R}$ with period $L \in \mathbb{N}$
|
||||
#
|
||||
# Any $\mathcal{C}^{\infty}$ periodic function $u : \mathbb{R} \rightarrow \mathbb{R}$ with period $L \in \mathbb{N}$
|
||||
# can be constructed by composing an arbitrary smooth function $f : \mathbb{R}^n \rightarrow \mathbb{R}$ with a smooth, periodic mapping$v : \mathbb{R} \rightarrow \mathbb{R}^n$ of the same period $L$. That is,
|
||||
#
|
||||
#
|
||||
# $$
|
||||
# u(x) = f(v(x)).
|
||||
# $$
|
||||
#
|
||||
# This formulation is general and can be extended to arbitrary dimensions.
|
||||
#
|
||||
# This formulation is general and can be extended to arbitrary dimensions.
|
||||
# For more details, see [*A Method for Representing Periodic Functions and Enforcing Exactly Periodic Boundary Conditions with Deep Neural Networks*](https://arxiv.org/pdf/2007.07442).
|
||||
#
|
||||
#
|
||||
# In our specific case, we define the periodic embedding as:
|
||||
#
|
||||
#
|
||||
# $$
|
||||
# v(x) = \left[1, \cos\left(\frac{2\pi}{L} x\right), \sin\left(\frac{2\pi}{L} x\right)\right],
|
||||
# $$
|
||||
#
|
||||
#
|
||||
# which constitutes the coordinate augmentation. The function $f(\cdot)$ is approximated by a neural network $NN_{\theta}(\cdot)$, resulting in the approximate PINN solution:
|
||||
#
|
||||
#
|
||||
# $$
|
||||
# u(x) \approx u_{\theta}(x) = NN_{\theta}(v(x)).
|
||||
# $$
|
||||
#
|
||||
# In **PINA**, this is implemented using the `PeriodicBoundaryEmbedding` layer for $v(x)$,
|
||||
# paired with any `pina.model` to define the neural network $NN_{\theta}$.
|
||||
#
|
||||
#
|
||||
# In **PINA**, this is implemented using the `PeriodicBoundaryEmbedding` layer for $v(x)$,
|
||||
# paired with any `pina.model` to define the neural network $NN_{\theta}$.
|
||||
#
|
||||
# Let’s see how this is put into practice!
|
||||
#
|
||||
#
|
||||
#
|
||||
#
|
||||
|
||||
# In[18]:
|
||||
|
||||
@@ -160,11 +160,11 @@ model = torch.nn.Sequential(
|
||||
|
||||
|
||||
# As simple as that!
|
||||
#
|
||||
# In higher dimensions, you can specify different periods for each coordinate using a dictionary.
|
||||
# For example, `periods = {'x': 2, 'y': 3, ...}` indicates a periodicity of 2 in the $x$ direction,
|
||||
#
|
||||
# In higher dimensions, you can specify different periods for each coordinate using a dictionary.
|
||||
# For example, `periods = {'x': 2, 'y': 3, ...}` indicates a periodicity of 2 in the $x$ direction,
|
||||
# 3 in the $y$ direction, and so on.
|
||||
#
|
||||
#
|
||||
# We will now solve the problem using the usual `PINN` and `Trainer` classes. After training, we'll examine the losses using the `MetricTracker` callback from `pina.callback`.
|
||||
|
||||
# In[ ]:
|
||||
@@ -240,15 +240,15 @@ with torch.no_grad():
|
||||
|
||||
# It's clear that the network successfully captures the periodicity of the solution, with the error also exhibiting a periodic pattern. Naturally, training for a longer duration or using a more expressive neural network could further improve the results.
|
||||
# ## What's next?
|
||||
#
|
||||
#
|
||||
# Congratulations on completing the one-dimensional Helmholtz tutorial with **PINA**! Here are a few directions you can explore next:
|
||||
#
|
||||
#
|
||||
# 1. **Train longer or with different architectures**: Experiment with extended training or modify the network's depth and width to evaluate improvements in accuracy.
|
||||
#
|
||||
#
|
||||
# 2. **Apply `PeriodicBoundaryEmbedding` to time-dependent problems**: Explore more complex scenarios such as spatiotemporal PDEs (see the official documentation for examples).
|
||||
#
|
||||
#
|
||||
# 3. **Try extra feature training**: Integrate additional physical or domain-specific features to guide the learning process more effectively.
|
||||
#
|
||||
#
|
||||
# 4. **...and many more!**: Extend to higher dimensions, test on other PDEs, or even develop custom embeddings tailored to your problem.
|
||||
#
|
||||
#
|
||||
# For more resources and tutorials, check out the [PINA Documentation](https://mathlab.github.io/PINA/).
|
||||
|
||||
Reference in New Issue
Block a user