Skip to content

Commit 4b4ffe8

Browse files
update tutorials for version 0.3
1 parent 9c6b724 commit 4b4ffe8

23 files changed

Lines changed: 660 additions & 1838 deletions

File tree

tutorials/tutorial1/tutorial.ipynb

Lines changed: 38 additions & 80 deletions
Large diffs are not rendered by default.

tutorials/tutorial10/tutorial.ipynb

Lines changed: 14 additions & 55 deletions
Large diffs are not rendered by default.

tutorials/tutorial11/tutorial.ipynb

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
{
2222
"cell_type": "code",
23-
"execution_count": 1,
23+
"execution_count": null,
2424
"metadata": {},
2525
"outputs": [],
2626
"source": [
@@ -37,7 +37,7 @@
3737
"import warnings\n",
3838
"\n",
3939
"from pina import Trainer\n",
40-
"from pina.solver import SupervisedSolver\n",
40+
"from pina.solver import SupervisedSingleModelSolver\n",
4141
"from pina.model import FeedForward\n",
4242
"from pina.problem.zoo import SupervisedProblem\n",
4343
"\n",
@@ -53,7 +53,7 @@
5353
},
5454
{
5555
"cell_type": "code",
56-
"execution_count": 2,
56+
"execution_count": null,
5757
"metadata": {},
5858
"outputs": [],
5959
"source": [
@@ -71,16 +71,16 @@
7171
" input_dimensions=1,\n",
7272
")\n",
7373
"\n",
74-
"# create the SupervisedSolver object\n",
75-
"solver = SupervisedSolver(problem, model, use_lt=False)"
74+
"# create the SupervisedSingleModelSolver object\n",
75+
"solver = SupervisedSingleModelSolver(problem, model, use_lt=False)"
7676
]
7777
},
7878
{
7979
"cell_type": "markdown",
8080
"metadata": {},
8181
"source": [
82-
"Till now we just followed the extact step of the previous tutorials. The `Trainer` object\n",
83-
"can be initialized by simiply passing the `SupervisedSolver` solver"
82+
"Untill now we just followed the extact step of the previous tutorials. The `Trainer` object\n",
83+
"can be initialized by simiply passing the `SupervisedSingleModelSolver` solver:"
8484
]
8585
},
8686
{
@@ -132,7 +132,7 @@
132132
"source": [
133133
"## Trainer Logging\n",
134134
"\n",
135-
"In **PINA** you can log metrics in different ways. The simplest approach is to use the `MetricTracker` class from `pina.callbacks`, as seen in the [*Introduction to Physics Informed Neural Networks training*](https://github.com/mathLab/PINA/blob/master/tutorials/tutorial1/tutorial.ipynb) tutorial.\n",
135+
"In **PINA** you can log metrics in different ways. The simplest approach is to use the `MetricTracker` class from `pina.callback`, as seen in the [*Introduction to Physics Informed Neural Networks training*](https://github.com/mathLab/PINA/blob/master/tutorials/tutorial1/tutorial.ipynb) tutorial.\n",
136136
"\n",
137137
"However, especially when we need to train multiple times to get an average of the loss across multiple runs, `lightning.pytorch.loggers` might be useful. Here we will use `TensorBoardLogger` (more on [logging](https://lightning.ai/docs/pytorch/stable/extensions/logging.html) here), but you can choose the one you prefer (or make your own one).\n",
138138
"\n",
@@ -156,7 +156,7 @@
156156
" output_dimensions=1,\n",
157157
" input_dimensions=1,\n",
158158
" )\n",
159-
" solver = SupervisedSolver(problem, model, use_lt=False)\n",
159+
" solver = SupervisedSingleModelSolver(problem, model, use_lt=False)\n",
160160
" trainer = Trainer(\n",
161161
" solver=solver,\n",
162162
" accelerator=\"cpu\",\n",
@@ -194,7 +194,7 @@
194194
"\n",
195195
"## Trainer Callbacks\n",
196196
"\n",
197-
"Whenever we need to access certain steps of the training for logging, perform static modifications (i.e. not changing the `Solver`), or update `Problem` hyperparameters (static variables), we can use **Callbacks**. Notice that **Callbacks** allow you to add arbitrary self-contained programs to your training. At specific points during the flow of execution (hooks), the Callback interface allows you to design programs that encapsulate a full set of functionality. It de-couples functionality that does not need to be in **PINA** `Solver`s.\n",
197+
"Whenever we need to access certain steps of the training for logging, perform static modifications (i.e. not changing the `Solver`), or update `Problem` hyperparameters (static variables), we can use **Callbacks**. Notice that **Callbacks** allow you to add arbitrary self-contained programs to your training. At specific points during the flow of execution (hooks), the Callback interface allows you to design programs that encapsulate a full set of functionality. It de-couples functionality that does not need to be in **PINA** Solvers.\n",
198198
"\n",
199199
"Lightning has a callback system to execute them when needed. **Callbacks** should capture NON-ESSENTIAL logic that is NOT required for your lightning module to run.\n",
200200
"\n",
@@ -206,12 +206,12 @@
206206
"* Directly calling methods (e.g., on_validation_end) is strongly discouraged.\n",
207207
"* Whenever possible, your callbacks should not depend on the order in which they are executed.\n",
208208
"\n",
209-
"We will try now to implement a naive version of `MetricTraker` to show how callbacks work. Notice that this is a very easy application of callbacks, fortunately in **PINA** we already provide more advanced callbacks in `pina.callbacks`."
209+
"We will try now to implement a naive version of `MetricTraker` to show how callbacks work. Notice that this is a very easy application of callbacks, fortunately in **PINA** we already provide more advanced callbacks in `pina.callback`."
210210
]
211211
},
212212
{
213213
"cell_type": "code",
214-
"execution_count": 6,
214+
"execution_count": null,
215215
"metadata": {},
216216
"outputs": [],
217217
"source": [
@@ -227,7 +227,10 @@
227227
"\n",
228228
" def on_train_epoch_end(\n",
229229
" self, trainer, __\n",
230-
" ): # function called at the end of each epoch\n",
230+
" ): \n",
231+
" \"\"\"\n",
232+
" Function called at the end of each epoch.\n",
233+
" \"\"\"\n",
231234
" self.saved_metrics.append(\n",
232235
" {key: value for key, value in trainer.logged_metrics.items()}\n",
233236
" )"
@@ -252,7 +255,7 @@
252255
" output_dimensions=1,\n",
253256
" input_dimensions=1,\n",
254257
")\n",
255-
"solver = SupervisedSolver(problem, model, use_lt=False)\n",
258+
"solver = SupervisedSingleModelSolver(problem, model, use_lt=False)\n",
256259
"trainer = Trainer(\n",
257260
" solver=solver,\n",
258261
" accelerator=\"cpu\",\n",
@@ -276,22 +279,9 @@
276279
},
277280
{
278281
"cell_type": "code",
279-
"execution_count": 8,
282+
"execution_count": null,
280283
"metadata": {},
281-
"outputs": [
282-
{
283-
"data": {
284-
"text/plain": [
285-
"[{'data_loss': tensor(104.4973), 'train_loss': tensor(104.4973)},\n",
286-
" {'data_loss': tensor(104.3082), 'train_loss': tensor(104.3082)},\n",
287-
" {'data_loss': tensor(104.1189), 'train_loss': tensor(104.1189)}]"
288-
]
289-
},
290-
"execution_count": 8,
291-
"metadata": {},
292-
"output_type": "execute_result"
293-
}
294-
],
284+
"outputs": [],
295285
"source": [
296286
"trainer.callbacks[0].saved_metrics[:3] # only the first three epochs"
297287
]
@@ -312,16 +302,16 @@
312302
"outputs": [],
313303
"source": [
314304
"model = FeedForward(\n",
315-
" layers=[10, 10],\n",
305+
" layers=[64, 64],\n",
316306
" func=torch.nn.Tanh,\n",
317307
" output_dimensions=1,\n",
318308
" input_dimensions=1,\n",
319309
")\n",
320-
"solver = SupervisedSolver(problem, model, use_lt=False)\n",
310+
"solver = SupervisedSingleModelSolver(problem, model, use_lt=False)\n",
321311
"trainer = Trainer(\n",
322312
" solver=solver,\n",
323313
" accelerator=\"cpu\",\n",
324-
" max_epochs=-1,\n",
314+
" max_epochs=1000,\n",
325315
" enable_model_summary=False,\n",
326316
" enable_progress_bar=False,\n",
327317
" val_size=0.2,\n",
@@ -378,7 +368,7 @@
378368
" input_dimensions=1,\n",
379369
")\n",
380370
"\n",
381-
"solver = SupervisedSolver(problem, model, use_lt=False)\n",
371+
"solver = SupervisedSingleModelSolver(problem, model, use_lt=False)\n",
382372
"trainer = Trainer(\n",
383373
" solver=solver,\n",
384374
" accelerator=\"cpu\",\n",
@@ -415,7 +405,7 @@
415405
" output_dimensions=1,\n",
416406
" input_dimensions=1,\n",
417407
")\n",
418-
"solver = SupervisedSolver(problem, model, use_lt=False)\n",
408+
"solver = SupervisedSingleModelSolver(problem, model, use_lt=False)\n",
419409
"trainer = Trainer(\n",
420410
" solver=solver,\n",
421411
" accelerator=\"cpu\",\n",
@@ -454,7 +444,7 @@
454444
" output_dimensions=1,\n",
455445
" input_dimensions=1,\n",
456446
")\n",
457-
"solver = SupervisedSolver(problem, model, use_lt=False)\n",
447+
"solver = SupervisedSingleModelSolver(problem, model, use_lt=False)\n",
458448
"trainer = Trainer(\n",
459449
" solver=solver,\n",
460450
" accelerator=\"cpu\",\n",

tutorials/tutorial12/tutorial.ipynb

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/mathLab/PINA/blob/master/tutorials/tutorial12/tutorial.ipynb)\n",
1010
"\n",
1111
"\n",
12-
"In this tutorial, we will explore how to use the `Equation` class in **PINA**. We will focus on how to leverage this class, along with its inherited subclasses, to enforce residual minimization in **Physics-Informed Neural Networks (PINNs)**.\n",
12+
"In this tutorial, we will explore how to use the `Equation` class in **PINA**. We will focus on how to leverage this class, along with its inherited subclasses, to enforce residual minimization in **Physics-Informed Solvers**.\n",
1313
"\n",
1414
"By the end of this guide, you'll understand how to integrate physical laws and constraints directly into your model training, ensuring that the solution adheres to the underlying differential equations.\n",
1515
"\n",
@@ -53,16 +53,17 @@
5353
"# useful imports\n",
5454
"from pina import Condition\n",
5555
"from pina.problem import SpatialProblem, TimeDependentProblem\n",
56-
"from pina.equation import Equation, FixedValue\n",
56+
"from pina.equation import Equation\n",
57+
"from pina.equation.zoo import FixedValue\n",
5758
"from pina.domain import CartesianDomain\n",
58-
"from pina.operator import grad, fast_grad, laplacian"
59+
"from pina.operator import grad, laplacian"
5960
]
6061
},
6162
{
6263
"cell_type": "markdown",
6364
"metadata": {},
6465
"source": [
65-
"Let's begin by defining the Burgers equation and its initial condition as Python functions. These functions will take the model's `input` (spatial and temporal coordinates) and `output` (predicted solution) as arguments. The goal is to compute the residuals for the Burgers equation, which we will minimize during training."
66+
"Let's begin by defining the Burgers equation and its initial condition as Python functions. These functions will take the model's `input_` (spatial and temporal coordinates) and `output_` (predicted solution) as arguments. The goal is to compute the residuals for the Burgers equation, which we will minimize during training."
6667
]
6768
},
6869
{
@@ -135,16 +136,14 @@
135136
"cell_type": "markdown",
136137
"metadata": {},
137138
"source": [
138-
"The `Equation` class takes as input a function (in this case it happens twice, with `initial_condition` and `burgers_equation`) which computes a residual of an equation, such as a PDE. In a problem class such as the one above, the `Equation` class with such a given input is passed as a parameter in the specified `Condition`. \n",
139139
"\n",
140-
"The `FixedValue` class takes as input a value of the same dimensions as the output functions. This class can be used to enforce a fixed value for a specific condition, such as Dirichlet boundary conditions, as demonstrated in our example.\n",
141140
"\n",
142-
"Once the equations are set as above in the problem conditions, the PINN solver will aim to minimize the residuals described in each equation during the training phase. \n",
143141
"\n",
144-
"### Available classes of equations:\n",
145-
"- `FixedGradient` and `FixedFlux`: These work analogously to the `FixedValue` class, where we can enforce a constant value on the gradient or the divergence of the solution, respectively.\n",
146-
"- `Laplace`: This class can be used to enforce that the Laplacian of the solution is zero.\n",
147-
"- `SystemEquation`: This class allows you to enforce multiple conditions on the same subdomain by passing a list of residual equations defined in the problem.\n",
142+
"The `Equation` class takes as input a function that computes the residual of an equation, such as a PDE. In the example above, this is done twice, using `initial_condition` and `burgers_equation`. Once defined, each `Equation` object is passed to a specific `Condition` inside the problem class. When multiple residual equations need to be enforced on the same subdomain, they can be grouped using `SystemEquation`, which takes a list of equations defined in the problem.\n",
143+
"\n",
144+
"For common use cases, PINA also provides several predefined equation classes. The `FixedValue`, `FixedGradient`, `FixedFlux`, and `FixedLaplacian` classes can be used to enforce fixed constraints on the solution or on its derivatives. In particular, `FixedValue` enforces a constant value with the same dimensions as the output function, making it suitable for conditions such as Dirichlet boundary conditions. Similarly, `FixedGradient` and `FixedFlux` enforce fixed values on the gradient or flux of the solution, respectively, while `FixedLaplacian` can be used to impose a fixed value on the Laplacian of the solution. Many ready-to-use equations are also available in the `pina.equation.zoo` module.\n",
145+
"\n",
146+
"Once the equations are assigned to the problem conditions, the physics-informed solver aims to minimize the residuals defined by each equation during the training phase.\n",
148147
"\n",
149148
"## Defining a new Equation class\n",
150149
"`Equation` classes can also be inherited to define a new class. For example, we can define a new class `Burgers1D` to represent the Burgers equation. During the class call, we can pass the viscosity parameter $\\nu$:\n",
@@ -240,7 +239,7 @@
240239
"From here, you can:\n",
241240
"\n",
242241
"- **Define Additional Complex Equation Classes**: Create your own equation classes, such as `SchrodingerEquation`, `NavierStokesEquation`, etc.\n",
243-
"- **Define More `FixedOperator` Classes**: Implement operators like `FixedCurl`, `FixedDivergence`, and others for more advanced simulations.\n",
242+
"- **Define More `FixedOperator` Classes**: Implement operators like `FixedCurl` for more advanced simulations.\n",
244243
"- **Integrate Custom Equations and Operators**: Combine your custom equations and operators into larger systems for more complex simulations.\n",
245244
"- **and many more!**: Explore for example different residual minimization techniques to improve the performance and accuracy of your models.\n",
246245
"\n",

tutorials/tutorial13/tutorial.ipynb

Lines changed: 28 additions & 73 deletions
Large diffs are not rendered by default.

tutorials/tutorial14/tutorial.ipynb

Lines changed: 71 additions & 30 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)