Update Tutorials (#544)

* update tutorials
* tutorial guidelines
* doc
This commit is contained in:
Dario Coscia
2025-04-23 16:19:07 +02:00
parent 7e403acf58
commit 29b14ee9b6
45 changed files with 6279 additions and 6726 deletions

View File

@@ -5,18 +5,13 @@
"id": "e80567a6",
"metadata": {},
"source": [
"# Tutorial: Two dimensional Darcy flow using the Fourier Neural Operator\n",
"# Tutorial: Modeling 2D Darcy Flow with the Fourier Neural Operator\n",
"\n",
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/mathLab/PINA/blob/master/tutorials/tutorial5/tutorial.ipynb)\n"
]
},
{
"cell_type": "markdown",
"id": "8762bbe5",
"metadata": {},
"source": [
"In this tutorial we are going to solve the Darcy flow problem in two dimensions, presented in [*Fourier Neural Operator for\n",
"Parametric Partial Differential Equation*](https://openreview.net/pdf?id=c8P9NQVtmnO). First of all we import the modules needed for the tutorial. Importing `scipy` is needed for input-output operations."
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/mathLab/PINA/blob/master/tutorials/tutorial5/tutorial.ipynb)\n",
"\n",
"In this tutorial, we are going to solve the **Darcy flow problem** in two dimensions, as presented in the paper [*Fourier Neural Operator for Parametric Partial Differential Equations*](https://openreview.net/pdf?id=c8P9NQVtmnO).\n",
"\n",
"We begin by importing the necessary modules for the tutorial:\n"
]
},
{
@@ -39,7 +34,7 @@
"except:\n",
" IN_COLAB = False\n",
"if IN_COLAB:\n",
" !pip install \"pina-mathlab\"\n",
" !pip install \"pina-mathlab[tutorial]\"\n",
" !pip install scipy\n",
" # get the data\n",
" !wget https://github.com/mathLab/PINA/raw/refs/heads/master/tutorials/tutorial5/Data_Darcy.mat\n",
@@ -48,10 +43,9 @@
"import matplotlib.pyplot as plt\n",
"import warnings\n",
"\n",
"# !pip install scipy # install scipy\n",
"from scipy import io\n",
"from pina.model import FNO, FeedForward # let's import some models\n",
"from pina import Condition, Trainer\n",
"from pina.model import FNO, FeedForward\n",
"from pina import Trainer\n",
"from pina.solver import SupervisedSolver\n",
"from pina.problem.zoo import SupervisedProblem\n",
"\n",
@@ -65,13 +59,15 @@
"source": [
"## Data Generation\n",
"\n",
"We will focus on solving a specific PDE, the **Darcy Flow** equation. The Darcy PDE is a second-order elliptic PDE with the following form:\n",
"We will focus on solving a specific PDE: the **Darcy Flow** equation. This is a second-order elliptic PDE given by:\n",
"\n",
"$$\n",
"-\\nabla\\cdot(k(x, y)\\nabla u(x, y)) = f(x) \\quad (x, y) \\in D.\n",
"-\\nabla\\cdot(k(x, y)\\nabla u(x, y)) = f(x, y), \\quad (x, y) \\in D.\n",
"$$\n",
"\n",
"Specifically, $u$ is the flow pressure, $k$ is the permeability field and $f$ is the forcing function. The Darcy flow can parameterize a variety of systems including flow through porous media, elastic materials and heat conduction. Here you will define the domain as a 2D unit square Dirichlet boundary conditions. The dataset is taken from the authors original reference.\n"
"Here, $u$ represents the flow pressure, $k$ is the permeability field, and $f$ is the forcing function. The Darcy flow equation can be used to model various systems, including flow through porous media, elasticity in materials, and heat conduction.\n",
"\n",
"In this tutorial, the domain $D$ is defined as a 2D unit square with Dirichlet boundary conditions. The dataset used is taken from the authors' original implementation in the referenced paper."
]
},
{
@@ -103,12 +99,12 @@
"id": "9a9defd4",
"metadata": {},
"source": [
"Let's visualize some data"
"Before diving into modeling, it's helpful to visualize some examples from the dataset. This will give us a better understanding of the input (permeability field) and the corresponding output (pressure field) that our model will learn to predict."
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"id": "c8501b6f",
"metadata": {
"ExecuteTime": {
@@ -143,12 +139,12 @@
"id": "89a77ff1",
"metadata": {},
"source": [
"We now create the Neural Operators problem class. Learning Neural Operators is similar as learning in a supervised manner, therefore we will use `SupervisedProblem`."
"We now define the problem class for learning the Neural Operator. Since this task is essentially a supervised learning problem—where the goal is to learn a mapping from input functions to output solutions—we will use the `SupervisedProblem` class provided by **PINA**."
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"id": "8b27d283",
"metadata": {
"ExecuteTime": {
@@ -169,14 +165,14 @@
"id": "1096cc20",
"metadata": {},
"source": [
"## Solving the problem with a FeedForward Neural Network\n",
"## Solving the Problem with a Feedforward Neural Network\n",
"\n",
"We will first solve the problem using a Feedforward neural network. We will use the `SupervisedSolver` for solving the problem, since we are training using supervised learning."
"We begin by solving the Darcy flow problem using a standard Feedforward Neural Network (FNN). Since we are approaching this task with supervised learning, we will use the `SupervisedSolver` provided by **PINA** to train the model."
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 7,
"id": "e34f18b0",
"metadata": {
"ExecuteTime": {
@@ -195,11 +191,18 @@
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 9: 100%|██████████| 100/100 [00:00<00:00, 289.72it/s, v_num=3, data_loss_step=0.102, train_loss_step=0.102, data_loss_epoch=0.105, train_loss_epoch=0.105] "
]
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "0b77243fe0274dada29b6bb5a15c47e8",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Training: | | 0/? [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
@@ -207,13 +210,6 @@
"text": [
"`Trainer.fit` stopped: `max_epochs=10` reached.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 9: 100%|██████████| 100/100 [00:00<00:00, 286.77it/s, v_num=3, data_loss_step=0.102, train_loss_step=0.102, data_loss_epoch=0.105, train_loss_epoch=0.105]\n"
]
}
],
"source": [
@@ -243,12 +239,12 @@
"id": "7b2c35be",
"metadata": {},
"source": [
"The final loss is pretty high... We can calculate the error by importing `LpLoss`."
"The final loss is relatively high, indicating that the model might not be capturing the solution accurately. To better evaluate the model's performance, we can compute the error using the `LpLoss` metric."
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 9,
"id": "0e2a6aa4",
"metadata": {
"ExecuteTime": {
@@ -261,8 +257,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Final error training 28.57%\n",
"Final error testing 28.59%\n"
"Final error training 28.54%\n",
"Final error testing 28.58%\n"
]
}
],
@@ -293,14 +289,14 @@
"id": "6b5e5aa6",
"metadata": {},
"source": [
"## Solving the problem with a Fourier Neural Operator (FNO)\n",
"## Solving the Problem with a Fourier Neural Operator\n",
"\n",
"We will now move to solve the problem using a FNO. Since we are learning operator this approach is better suited, as we shall see."
"We will now solve the Darcy flow problem using a Fourier Neural Operator (FNO). Since we are learning a mapping between functions—i.e., an operatorthis approach is more suitable and often yields better performance, as we will see."
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 10,
"id": "9af523a5",
"metadata": {
"ExecuteTime": {
@@ -319,11 +315,18 @@
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 9: 100%|██████████| 100/100 [00:02<00:00, 36.66it/s, v_num=4, data_loss_step=0.00164, train_loss_step=0.00164, data_loss_epoch=0.00229, train_loss_epoch=0.00229]"
]
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "6fbb56905e4c4799973669f533a2d73c",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Training: | | 0/? [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
@@ -331,13 +334,6 @@
"text": [
"`Trainer.fit` stopped: `max_epochs=10` reached.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 9: 100%|██████████| 100/100 [00:02<00:00, 36.56it/s, v_num=4, data_loss_step=0.00164, train_loss_step=0.00164, data_loss_epoch=0.00229, train_loss_epoch=0.00229]\n"
]
}
],
"source": [
@@ -376,12 +372,14 @@
"id": "84964cb9",
"metadata": {},
"source": [
"We can clearly see that the final loss is lower. Let's see in testing.. Notice that the number of parameters is way higher than a `FeedForward` network. We suggest to use GPU or TPU for a speed up in training, when many data samples are used."
"We can clearly observe that the final loss is significantly lower when using the FNO. Let's now evaluate its performance on the test set.\n",
"\n",
"Note that the number of trainable parameters in the FNO is considerably higher compared to a `FeedForward` network. Therefore, we recommend using a GPU or TPU to accelerate training, especially when working with large datasets."
]
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 11,
"id": "58e2db89",
"metadata": {
"ExecuteTime": {
@@ -394,8 +392,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Final error training 3.36%\n",
"Final error testing 3.54%\n"
"Final error training 3.52%\n",
"Final error testing 3.67%\n"
]
}
],
@@ -421,7 +419,7 @@
"id": "26e3a6e4",
"metadata": {},
"source": [
"As we can see the loss is way lower!"
"As we can see, the loss is significantly lower with the Fourier Neural Operator!"
]
},
{
@@ -429,9 +427,17 @@
"id": "ba1dfa4b",
"metadata": {},
"source": [
"## What's next?\n",
"## What's Next?\n",
"\n",
"We have made a very simple example on how to use the `FNO` for learning neural operator. Currently in **PINA** we implement 1D/2D/3D cases. We suggest to extend the tutorial using more complex problems and train for longer, to see the full potential of neural operators."
"Congratulations on completing the tutorial on solving the Darcy flow problem using **PINA**! There are many potential next steps you can explore:\n",
"\n",
"1. **Train the network longer or with different hyperparameters**: Experiment with different configurations of the neural network. You can try varying the number of layers, activation functions, or learning rates to improve accuracy.\n",
"\n",
"2. **Solve more complex problems**: The Darcy flow problem is just the beginning! Try solving other complex problems from the field of parametric PDEs. The original paper and **PINA** documentation offer many more examples to explore.\n",
"\n",
"3. **...and many more!**: There are countless directions to further explore. For instance, you could try to add physics informed learning!\n",
"\n",
"For more resources and tutorials, check out the [PINA Documentation](https://mathlab.github.io/PINA/)."
]
}
],