Skip to content

Commit f96911a

Browse files
MatteB03ndem0
authored andcommitted
Update tutorials 1 through 12 to current version 0.2
1 parent 9c6792a commit f96911a

File tree

82 files changed

+998
-1148
lines changed

Some content is hidden

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

82 files changed

+998
-1148
lines changed

tutorials/tutorial1/tutorial.ipynb

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
},
8181
{
8282
"cell_type": "code",
83-
"execution_count": 27,
83+
"execution_count": 1,
8484
"id": "2373a925",
8585
"metadata": {},
8686
"outputs": [],
@@ -134,13 +134,21 @@
134134
},
135135
{
136136
"cell_type": "code",
137-
"execution_count": 28,
137+
"execution_count": null,
138138
"id": "f2608e2e",
139139
"metadata": {},
140-
"outputs": [],
140+
"outputs": [
141+
{
142+
"name": "stderr",
143+
"output_type": "stream",
144+
"text": [
145+
"/home/matte_b/PINA/pina/operators.py: DeprecationWarning: 'pina.operators' is deprecated and will be removed in future versions. Please use 'pina.operator' instead.\n"
146+
]
147+
}
148+
],
141149
"source": [
142150
"from pina.problem import SpatialProblem\n",
143-
"from pina.operators import grad\n",
151+
"from pina.operator import grad\n",
144152
"from pina import Condition\n",
145153
"from pina.domain import CartesianDomain\n",
146154
"from pina.equation import Equation, FixedValue\n",
@@ -209,20 +217,20 @@
209217
},
210218
{
211219
"cell_type": "code",
212-
"execution_count": 29,
220+
"execution_count": 3,
213221
"id": "09ce5c3a",
214222
"metadata": {},
215223
"outputs": [],
216224
"source": [
217225
"# sampling 20 points in [0, 1] through discretization in all locations\n",
218-
"problem.discretise_domain(n=20, mode='grid', variables=['x'], domains='all')\n",
226+
"problem.discretise_domain(n=20, mode='grid', domains='all')\n",
219227
"\n",
220228
"# sampling 20 points in (0, 1) through latin hypercube sampling in D, and 1 point in x0\n",
221-
"problem.discretise_domain(n=20, mode='latin', variables=['x'], domains=['D'])\n",
222-
"problem.discretise_domain(n=1, mode='random', variables=['x'], domains=['x0'])\n",
229+
"problem.discretise_domain(n=20, mode='latin', domains=['D'])\n",
230+
"problem.discretise_domain(n=1, mode='random', domains=['x0'])\n",
223231
"\n",
224232
"# sampling 20 points in (0, 1) randomly\n",
225-
"problem.discretise_domain(n=20, mode='random', variables=['x'])"
233+
"problem.discretise_domain(n=20, mode='random')"
226234
]
227235
},
228236
{
@@ -235,7 +243,7 @@
235243
},
236244
{
237245
"cell_type": "code",
238-
"execution_count": 30,
246+
"execution_count": 4,
239247
"id": "329962b6",
240248
"metadata": {},
241249
"outputs": [],
@@ -255,7 +263,7 @@
255263
},
256264
{
257265
"cell_type": "code",
258-
"execution_count": 31,
266+
"execution_count": 5,
259267
"id": "d6ed9aaf",
260268
"metadata": {},
261269
"outputs": [
@@ -282,26 +290,26 @@
282290
" [0.],\n",
283291
" [0.],\n",
284292
" [0.],\n",
285-
" [0.]]), 'D': LabelTensor([[0.4156],\n",
286-
" [0.8975],\n",
287-
" [0.5223],\n",
288-
" [0.5617],\n",
289-
" [0.3636],\n",
290-
" [0.2104],\n",
291-
" [0.0502],\n",
292-
" [0.4684],\n",
293-
" [0.6188],\n",
294-
" [0.9159],\n",
295-
" [0.7120],\n",
296-
" [0.1375],\n",
297-
" [0.8148],\n",
298-
" [0.0322],\n",
299-
" [0.3204],\n",
300-
" [0.1807],\n",
301-
" [0.2869],\n",
302-
" [0.7945],\n",
303-
" [0.6901],\n",
304-
" [0.9740]])}\n",
293+
" [0.]]), 'D': LabelTensor([[0.1420],\n",
294+
" [0.3743],\n",
295+
" [0.7738],\n",
296+
" [0.2501],\n",
297+
" [0.5195],\n",
298+
" [0.1846],\n",
299+
" [0.8313],\n",
300+
" [0.0020],\n",
301+
" [0.0973],\n",
302+
" [0.6215],\n",
303+
" [0.4345],\n",
304+
" [0.6944],\n",
305+
" [0.2031],\n",
306+
" [0.5723],\n",
307+
" [0.9332],\n",
308+
" [0.7015],\n",
309+
" [0.4865],\n",
310+
" [0.3176],\n",
311+
" [0.8969],\n",
312+
" [0.9800]])}\n",
305313
"Input points labels: ['x']\n"
306314
]
307315
}
@@ -321,7 +329,7 @@
321329
},
322330
{
323331
"cell_type": "code",
324-
"execution_count": 32,
332+
"execution_count": 6,
325333
"id": "33cc80bc",
326334
"metadata": {},
327335
"outputs": [],
@@ -352,15 +360,17 @@
352360
},
353361
{
354362
"cell_type": "code",
355-
"execution_count": 33,
363+
"execution_count": 7,
356364
"id": "3bb4dc9b",
357365
"metadata": {},
358366
"outputs": [
359367
{
360368
"name": "stderr",
361369
"output_type": "stream",
362370
"text": [
363-
"GPU available: True (mps), used: False\n",
371+
"/home/matte_b/PINA/pina/solvers/__init__.py: DeprecationWarning: 'pina.solvers' is deprecated and will be removed in future versions. Please use 'pina.solver' instead.\n",
372+
"/home/matte_b/PINA/pina/callbacks/__init__.py: DeprecationWarning: 'pina.callbacks' is deprecated and will be removed in future versions. Please use 'pina.callback' instead.\n",
373+
"GPU available: False, used: False\n",
364374
"TPU available: False, using: 0 TPU cores\n",
365375
"HPU available: False, using: 0 HPUs\n"
366376
]
@@ -369,7 +379,7 @@
369379
"name": "stdout",
370380
"output_type": "stream",
371381
"text": [
372-
"Epoch 1499: 100%|██████████| 1/1 [00:00<00:00, 67.42it/s, v_num=2, train_loss_step=0.00468, val_loss=0.00466, train_loss_epoch=0.00468] "
382+
"Epoch 1499: 100%|██████████| 1/1 [00:00<00:00, 20.24it/s, v_num=90, val_loss=0.0191, bound_cond_loss=4.18e-5, phys_cond_loss=0.00118, train_loss=0.00122] "
373383
]
374384
},
375385
{
@@ -383,7 +393,7 @@
383393
"name": "stdout",
384394
"output_type": "stream",
385395
"text": [
386-
"Epoch 1499: 100%|██████████| 1/1 [00:00<00:00, 56.83it/s, v_num=2, train_loss_step=0.00468, val_loss=0.00466, train_loss_epoch=0.00468]\n"
396+
"Epoch 1499: 100%|██████████| 1/1 [00:00<00:00, 16.69it/s, v_num=90, val_loss=0.0191, bound_cond_loss=4.18e-5, phys_cond_loss=0.00118, train_loss=0.00122]\n"
387397
]
388398
}
389399
],
@@ -422,19 +432,20 @@
422432
},
423433
{
424434
"cell_type": "code",
425-
"execution_count": 34,
435+
"execution_count": 8,
426436
"id": "f5fbf362",
427437
"metadata": {},
428438
"outputs": [
429439
{
430440
"data": {
431441
"text/plain": [
432-
"{'train_loss_step': tensor(0.0047),\n",
433-
" 'val_loss': tensor(0.0047),\n",
434-
" 'train_loss_epoch': tensor(0.0047)}"
442+
"{'val_loss': tensor(0.0191),\n",
443+
" 'bound_cond_loss': tensor(4.1773e-05),\n",
444+
" 'phys_cond_loss': tensor(0.0012),\n",
445+
" 'train_loss': tensor(0.0012)}"
435446
]
436447
},
437-
"execution_count": 34,
448+
"execution_count": 8,
438449
"metadata": {},
439450
"output_type": "execute_result"
440451
}
@@ -454,7 +465,7 @@
454465
},
455466
{
456467
"cell_type": "code",
457-
"execution_count": 35,
468+
"execution_count": 9,
458469
"id": "19078eb5",
459470
"metadata": {},
460471
"outputs": [],
@@ -473,7 +484,7 @@
473484
},
474485
{
475486
"cell_type": "code",
476-
"execution_count": 36,
487+
"execution_count": 10,
477488
"id": "bf6211e6",
478489
"metadata": {},
479490
"outputs": [],
@@ -509,11 +520,8 @@
509520
}
510521
],
511522
"metadata": {
512-
"interpreter": {
513-
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
514-
},
515523
"kernelspec": {
516-
"display_name": "Python 3 (ipykernel)",
524+
"display_name": "Python 3",
517525
"language": "python",
518526
"name": "python3"
519527
},
@@ -527,7 +535,7 @@
527535
"name": "python",
528536
"nbconvert_exporter": "python",
529537
"pygments_lexer": "ipython3",
530-
"version": "3.11.7"
538+
"version": "3.12.3"
531539
}
532540
},
533541
"nbformat": 4,

tutorials/tutorial1/tutorial.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
# What if our equation is also time-dependent? In this case, our `class` will inherit from both `SpatialProblem` and `TimeDependentProblem`:
5454
#
5555

56-
# In[ ]:
56+
# In[1]:
5757

5858

5959
## routine needed to run the notebook on Google Colab
@@ -89,11 +89,11 @@ class TimeSpaceODE(SpatialProblem, TimeDependentProblem):
8989
#
9090
# Once the `Problem` class is initialized, we need to represent the differential equation in **PINA**. In order to do this, we need to load the **PINA** operators from `pina.operators` module. Again, we'll consider Equation (1) and represent it in **PINA**:
9191

92-
# In[2]:
92+
# In[ ]:
9393

9494

9595
from pina.problem import SpatialProblem
96-
from pina.operators import grad
96+
from pina.operator import grad
9797
from pina import Condition
9898
from pina.domain import CartesianDomain
9999
from pina.equation import Equation, FixedValue
@@ -106,6 +106,11 @@ class SimpleODE(SpatialProblem):
106106
output_variables = ['u']
107107
spatial_domain = CartesianDomain({'x': [0, 1]})
108108

109+
domains ={
110+
'x0': CartesianDomain({'x': 0.}),
111+
'D': CartesianDomain({'x': [0, 1]})
112+
}
113+
109114
# defining the ode equation
110115
def ode_equation(input_, output_):
111116

@@ -120,13 +125,10 @@ def ode_equation(input_, output_):
120125

121126
# conditions to hold
122127
conditions = {
123-
'x0': Condition(location=CartesianDomain({'x': 0.}), equation=FixedValue(1)), # We fix initial condition to value 1
124-
'D': Condition(location=CartesianDomain({'x': [0, 1]}), equation=Equation(ode_equation)), # We wrap the python equation using Equation
128+
'bound_cond': Condition(domain='x0', equation=FixedValue(1.)),
129+
'phys_cond': Condition(domain='D', equation=Equation(ode_equation))
125130
}
126131

127-
# sampled points (see below)
128-
input_pts = None
129-
130132
# defining the true solution
131133
def truth_solution(self, pts):
132134
return torch.exp(pts.extract(['x']))
@@ -149,14 +151,14 @@ def truth_solution(self, pts):
149151

150152

151153
# sampling 20 points in [0, 1] through discretization in all locations
152-
problem.discretise_domain(n=20, mode='grid', variables=['x'], locations='all')
154+
problem.discretise_domain(n=20, mode='grid', domains='all')
153155

154156
# sampling 20 points in (0, 1) through latin hypercube sampling in D, and 1 point in x0
155-
problem.discretise_domain(n=20, mode='latin', variables=['x'], locations=['D'])
156-
problem.discretise_domain(n=1, mode='random', variables=['x'], locations=['x0'])
157+
problem.discretise_domain(n=20, mode='latin', domains=['D'])
158+
problem.discretise_domain(n=1, mode='random', domains=['x0'])
157159

158160
# sampling 20 points in (0, 1) randomly
159-
problem.discretise_domain(n=20, mode='random', variables=['x'])
161+
problem.discretise_domain(n=20, mode='random')
160162

161163

162164
# We are going to use latin hypercube points for sampling. We need to sample in all the conditions domains. In our case we sample in `D` and `x0`.
@@ -165,35 +167,35 @@ def truth_solution(self, pts):
165167

166168

167169
# sampling for training
168-
problem.discretise_domain(1, 'random', locations=['x0'])
169-
problem.discretise_domain(20, 'lh', locations=['D'])
170+
problem.discretise_domain(20, 'random', domains=['x0']) # TODO check
171+
problem.discretise_domain(20, 'lh', domains=['D'])
170172

171173

172174
# The points are saved in a python `dict`, and can be accessed by calling the attribute `input_pts` of the problem
173175

174176
# In[5]:
175177

176178

177-
print('Input points:', problem.input_pts)
178-
print('Input points labels:', problem.input_pts['D'].labels)
179+
print('Input points:', problem.discretised_domains)
180+
print('Input points labels:', problem.discretised_domains['D'].labels)
179181

180182

181183
# To visualize the sampled points we can use the `.plot_samples` method of the `Plotter` class
182184

183-
# In[5]:
185+
# In[6]:
184186

185187

186-
from pina import Plotter
188+
#from pina import Plotter
187189

188-
pl = Plotter()
189-
pl.plot_samples(problem=problem)
190+
#pl = Plotter()
191+
#pl.plot_samples(problem=problem)
190192

191193

192194
# ## Perform a small training
193195

194196
# Once we have defined the problem and generated the data we can start the modelling. Here we will choose a `FeedForward` neural network available in `pina.model`, and we will train using the `PINN` solver from `pina.solvers`. We highlight that this training is fairly simple, for more advanced stuff consider the tutorials in the ***Physics Informed Neural Networks*** section of ***Tutorials***. For training we use the `Trainer` class from `pina.trainer`. Here we show a very short training and some method for plotting the results. Notice that by default all relevant metrics (e.g. MSE error during training) are going to be tracked using a `lightining` logger, by default `CSVLogger`. If you want to track the metric by yourself without a logger, use `pina.callbacks.MetricTracker`.
195197

196-
# In[ ]:
198+
# In[7]:
197199

198200

199201
from pina import Trainer
@@ -222,7 +224,7 @@ def truth_solution(self, pts):
222224

223225
# After the training we can inspect trainer logged metrics (by default **PINA** logs mean square error residual loss). The logged metrics can be accessed online using one of the `Lightinig` loggers. The final loss can be accessed by `trainer.logged_metrics`
224226

225-
# In[7]:
227+
# In[8]:
226228

227229

228230
# inspecting final loss
@@ -231,19 +233,19 @@ def truth_solution(self, pts):
231233

232234
# By using the `Plotter` class from **PINA** we can also do some quatitative plots of the solution.
233235

234-
# In[8]:
236+
# In[9]:
235237

236238

237239
# plotting the solution
238-
pl.plot(solver=pinn)
240+
#pl.plot(solver=pinn)
239241

240242

241243
# The solution is overlapped with the actual one, and they are barely indistinguishable. We can also plot easily the loss:
242244

243-
# In[9]:
245+
# In[10]:
244246

245247

246-
pl.plot_loss(trainer=trainer, label = 'mean_loss', logy=True)
248+
#pl.plot_loss(trainer=trainer, label = 'mean_loss', logy=True)
247249

248250

249251
# As we can see the loss has not reached a minimum, suggesting that we could train for longer

tutorials/tutorial10/tutorial.ipynb

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

0 commit comments

Comments
 (0)