Skip to content

Commit d88c7e8

Browse files
committed
Small fixes
1 parent 378efe0 commit d88c7e8

File tree

3 files changed

+93
-59
lines changed

3 files changed

+93
-59
lines changed

pySDC/playgrounds/PinT_Workshop_2025/0_pySDC.ipynb

Lines changed: 86 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@
99
"\n",
1010
"pySDC = Python + spectral deferred correction (SDC) is a prototyping code for all things SDC, including PFASST.\n",
1111
"It was started by Robert Speck about 10 years ago and is now primarily developed in Jülich and Hamburg.\n",
12-
"The goal is to make SDC related research as accessible as possible, hence Python.\n",
12+
"The goal is to make SDC related research as accessible as possible.\n",
1313
"It is very modular and abstract, which allows to focus only on small parts of the bigger SDC puzzle.\n",
14-
"Furthermore, all methods are implemented in serial as well as in parallal to allow both easy developping as well as measurements of performance.\n",
14+
"Furthermore, all methods are implemented in serial as well as in parallal to allow both easy developping as well as measuring performance.\n",
1515
"\n",
1616
"In this notebook, we will briefly look into SDC and pySDC.\n",
1717
"\n",
1818
"## Spectral deferred correction (SDC)\n",
1919
"SDC is a method for the numerical integration of initial value problems of the sort $$u(\\Delta t) = \\int_0^{\\Delta t} f(u) dt + u(0).$$\n",
2020
"For the numerical treatment, we start with discretizing time into $M$ **collocation nodes** $0 \\leq \\tau_m \\leq 1$.\n",
21-
"Then, we interpolate $f$ $$f(u(t))\\approx\\sum_{m=1}^{M} l_m^\\tau(t) f(u(\\tau_m))$$ using Lagrange polynomials $$l_j^{\\tau}(t)=\\frac{\\prod_{i=1, i\\neq j}^M (t-\\tau_i)}{\\prod_{i=1, i\\neq j}^M (\\tau_j-\\tau_i)}.$$\n",
21+
"Then, we interpolate $f$ $$f(u(t))\\approx\\sum_{m=1}^{M} l_m^\\tau(t/\\Delta t) f(u(\\Delta t \\tau_m))$$ using Lagrange polynomials $$l_j^{\\tau}(t)=\\frac{\\prod_{i=1, i\\neq j}^M (t-\\tau_i)}{\\prod_{i=1, i\\neq j}^M (\\tau_j-\\tau_i)}.$$\n",
2222
"\n",
23-
"Since only the Lagrange polynomials depend on $t$ in the interpolation, we get $$u_m := u_0 + \\Delta t\\sum_{j=1}^M q_{mj}f(u(\\Delta t\\tau_j)) \\approx u(\\Delta t \\tau_m),$$ with the **quadrature weights** $$q_{mj} = \\int_{0}^{\\tau_m} l_j^\\tau(s) ds.$$\n",
23+
"Since only the Lagrange polynomials depend on $t$ in the interpolation, plugging into the initial value problem, we get $$u_m := u_0 + \\Delta t\\sum_{j=1}^M q_{mj}f(u(\\Delta t\\tau_j)) \\approx u(\\Delta t \\tau_m),$$ with the **quadrature weights** $$q_{mj} = \\int_{0}^{\\tau_m} l_j^\\tau(s) ds.$$\n",
2424
"\n",
2525
"Collecting in vectors $\\vec{u} = (u_m)^T$, we get $$(I - \\Delta tQf)(\\vec{u}) = \\vec{u}_0,$$ with $f(\\vec{u}) = (f(u_m))^T$ and $\\vec{u_0} = (u_0)^T$.\n",
2626
"This is called the **collocation problem**.\n",
2727
"Before discussing how to solve this, let's look at a sample quadrature matrix.\n",
2828
"\n",
2929
"The generation of quadrature matrices has been spun out from pySDC to a stand-alone repository [qmat](https://github.com/Parallel-in-Time/qmat).\n",
30-
"If you use SDC outside of pySDC, consider using qmat."
30+
"If you use SDC outside of pySDC, please consider using qmat."
3131
]
3232
},
3333
{
@@ -59,7 +59,7 @@
5959
"metadata": {},
6060
"source": [
6161
"As you can see, $Q$ is densely populated.\n",
62-
"Inverting $Q$ by itself is no problem, but if you are integrating a PDE, the linear solves involved in inverting $Qf$ can become prohibitively expensive.\n",
62+
"Inverting $Q$ by itself is no problem, but if you are integrating a PDE, inverting $Qf$ can become prohibitively expensive.\n",
6363
"Instead, we solve the collocation problem iteratively.\n",
6464
"\n",
6565
"The simplest iterative scheme is standard Richardson iteration $$\\vec{u}^{k+1} = \\vec{u}_0 + \\Delta tQf(\\vec{u}^k),$$ but convergence is slow and limited.\n",
@@ -79,17 +79,17 @@
7979
"name": "stdout",
8080
"output_type": "stream",
8181
"text": [
82-
"[[0.19681548 0. 0. ]\n",
83-
" [0.39442431 0.42340844 0. ]\n",
84-
" [0.37640306 0.63782015 0.2 ]]\n"
82+
"QDelta=array([[0.19681548, 0. , 0. ],\n",
83+
" [0.39442431, 0.42340844, 0. ],\n",
84+
" [0.37640306, 0.63782015, 0.2 ]])\n"
8585
]
8686
}
8787
],
8888
"source": [
8989
"from qmat import genQDeltaCoeffs\n",
9090
"\n",
9191
"QDelta = genQDeltaCoeffs(\"LU\", Q=Q)\n",
92-
"print(QDelta)"
92+
"print(f'{QDelta=}')"
9393
]
9494
},
9595
{
@@ -102,19 +102,22 @@
102102
"For instance, you can do splitting, multi-level, multi-step, quantum-AI, bank heist, ...\n",
103103
"\n",
104104
"## pySDC\n",
105-
"Let's now look into how to set up pySDC and have a look at some examples of how pySDC has been used in the past.\n",
105+
"Before looking at some examples of what pySDC has been used for, we discuss its structure and how to set up a run.\n",
106106
"\n",
107107
"We will first go through all core modules that you can / should configure.\n",
108108
"Namely:\n",
109-
" - controller\n",
110-
" - step\n",
111-
" - level\n",
112-
" - sweeper\n",
113109
" - problem\n",
110+
" - sweeper\n",
111+
" - level\n",
112+
" - step\n",
113+
" - controller\n",
114114
" - hook\n",
115115
" - convergence controller\n",
116116
" - transfer class\n",
117117
"\n",
118+
"The main hierarchy in pySDC is controller > step > level > sweeper > problem.\n",
119+
"The rest are bells and whistles.\n",
120+
"\n",
118121
"### Problem\n",
119122
"At the bottom of the hierarchy are the problem objects.\n",
120123
"These implement how to evaluate and invert $f$ for a given problem.\n",
@@ -161,7 +164,7 @@
161164
},
162165
{
163166
"cell_type": "code",
164-
"execution_count": 31,
167+
"execution_count": 3,
165168
"id": "99161031-290b-45ff-95a5-67cae23557e8",
166169
"metadata": {},
167170
"outputs": [
@@ -185,7 +188,7 @@
185188
"Controller: <class 'pySDC.implementations.controller_classes.controller_nonMPI.controller_nonMPI'>\n",
186189
" all_to_done = False\n",
187190
" dump_setup = True\n",
188-
" fname = run_pid75826.log\n",
191+
" fname = run_pid77603.log\n",
189192
"--> hook_class = [<class 'pySDC.implementations.hooks.default_hook.DefaultHooks'>, <class 'pySDC.implementations.hooks.log_timings.CPUTimings'>, <class 'pySDC.implementations.hooks.log_solution.LogSolution'>]\n",
190193
" log_to_file = False\n",
191194
"--> logger_level = 15\n",
@@ -341,40 +344,58 @@
341344
},
342345
{
343346
"cell_type": "code",
344-
"execution_count": 30,
347+
"execution_count": 4,
345348
"id": "8717f118-1d85-46ac-9b83-11ee73e58086",
346349
"metadata": {},
347350
"outputs": [
348351
{
349352
"name": "stdout",
350353
"output_type": "stream",
351354
"text": [
352-
"hooks - INFO: Process 0 on time 0.000000 at stage IT_COARSE: Level: 1 -- Iteration: 1 -- Sweep: 1 -- residual: 7.06476284e-02\n",
353-
"hooks - INFO: Process 1 on time 0.010000 at stage IT_COARSE: Level: 1 -- Iteration: 1 -- Sweep: 1 -- residual: 2.31961858e-01\n",
354-
"hooks - INFO: Process 0 on time 0.000000 at stage IT_FINE: Level: 0 -- Iteration: 1 -- Sweep: 1 -- residual: 6.13891311e-01\n",
355-
"hooks - INFO: Process 1 on time 0.010000 at stage IT_FINE: Level: 0 -- Iteration: 1 -- Sweep: 1 -- residual: 5.84010351e-01\n",
356-
"hooks - INFO: Process 0 on time 0.000000 at stage IT_COARSE: Level: 1 -- Iteration: 2 -- Sweep: 1 -- residual: 2.30998355e-03\n",
357-
"hooks - INFO: Process 1 on time 0.010000 at stage IT_COARSE: Level: 1 -- Iteration: 2 -- Sweep: 1 -- residual: 3.13050936e-02\n",
358-
"hooks - INFO: Process 0 on time 0.000000 at stage IT_FINE: Level: 0 -- Iteration: 2 -- Sweep: 1 -- residual: 2.60639110e-03\n",
359-
"hooks - INFO: Process 1 on time 0.010000 at stage IT_FINE: Level: 0 -- Iteration: 2 -- Sweep: 1 -- residual: 9.44207299e-02\n",
360-
"hooks - INFO: Process 0 on time 0.000000 at stage IT_COARSE: Level: 1 -- Iteration: 3 -- Sweep: 1 -- residual: 1.17545361e-04\n",
361-
"hooks - INFO: Process 1 on time 0.010000 at stage IT_COARSE: Level: 1 -- Iteration: 3 -- Sweep: 1 -- residual: 1.41381525e-03\n",
362-
"hooks - INFO: Process 0 on time 0.000000 at stage IT_FINE: Level: 0 -- Iteration: 3 -- Sweep: 1 -- residual: 1.71685656e-04\n",
363-
"hooks - INFO: Process 1 on time 0.010000 at stage IT_FINE: Level: 0 -- Iteration: 3 -- Sweep: 1 -- residual: 2.93876380e-03\n",
364-
"hooks - INFO: Process 0 on time 0.000000 at stage IT_COARSE: Level: 1 -- Iteration: 4 -- Sweep: 1 -- residual: 2.72540253e-06\n",
365-
"hooks - INFO: Process 1 on time 0.010000 at stage IT_COARSE: Level: 1 -- Iteration: 4 -- Sweep: 1 -- residual: 3.15145873e-05\n",
366-
"hooks - INFO: Process 0 on time 0.000000 at stage IT_FINE: Level: 0 -- Iteration: 4 -- Sweep: 1 -- residual: 6.78325684e-06\n",
367-
"hooks - INFO: Process 1 on time 0.010000 at stage IT_FINE: Level: 0 -- Iteration: 4 -- Sweep: 1 -- residual: 3.04803873e-05\n",
368-
"hooks - INFO: Process 1 on time 0.010000 at stage IT_COARSE: Level: 1 -- Iteration: 5 -- Sweep: 1 -- residual: 1.04807085e-06\n",
369-
"hooks - INFO: Process 1 on time 0.010000 at stage IT_FINE: Level: 0 -- Iteration: 5 -- Sweep: 1 -- residual: 2.44912419e-06\n",
370-
"hooks - INFO: Finished run after 8.00e-01s\n",
371-
"hooks - INFO: Finished run after 8.00e-01s\n"
355+
"hooks - INFO: Process 0 on time 0.000000 at stage IT_COARSE: Level: 1 -- Iteration: 1 -- Sweep: 1 -- residual: 6.55548055e-02\n",
356+
"hooks - INFO: Process 1 on time 0.010000 at stage IT_COARSE: Level: 1 -- Iteration: 1 -- Sweep: 1 -- residual: 4.33590968e-01\n",
357+
"hooks - INFO: Process 0 on time 0.000000 at stage IT_FINE: Level: 0 -- Iteration: 1 -- Sweep: 1 -- residual: 5.69274516e-01\n",
358+
"hooks - INFO: Process 1 on time 0.010000 at stage IT_FINE: Level: 0 -- Iteration: 1 -- Sweep: 1 -- residual: 7.25755959e-01\n",
359+
"hooks - INFO: Process 0 on time 0.000000 at stage IT_COARSE: Level: 1 -- Iteration: 2 -- Sweep: 1 -- residual: 1.52277915e-02\n",
360+
"hooks - INFO: Process 1 on time 0.010000 at stage IT_COARSE: Level: 1 -- Iteration: 2 -- Sweep: 1 -- residual: 1.42393824e-01\n",
361+
"hooks - INFO: Process 0 on time 0.000000 at stage IT_FINE: Level: 0 -- Iteration: 2 -- Sweep: 1 -- residual: 5.95755213e-03\n",
362+
"hooks - INFO: Process 1 on time 0.010000 at stage IT_FINE: Level: 0 -- Iteration: 2 -- Sweep: 1 -- residual: 4.15178168e-02\n",
363+
"hooks - INFO: Process 0 on time 0.000000 at stage IT_COARSE: Level: 1 -- Iteration: 3 -- Sweep: 1 -- residual: 5.14520992e-03\n",
364+
"hooks - INFO: Process 1 on time 0.010000 at stage IT_COARSE: Level: 1 -- Iteration: 3 -- Sweep: 1 -- residual: 4.52164626e-02\n",
365+
"hooks - INFO: Process 0 on time 0.000000 at stage IT_FINE: Level: 0 -- Iteration: 3 -- Sweep: 1 -- residual: 1.64184578e-03\n",
366+
"hooks - INFO: Process 1 on time 0.010000 at stage IT_FINE: Level: 0 -- Iteration: 3 -- Sweep: 1 -- residual: 1.06977065e-02\n",
367+
"hooks - INFO: Process 0 on time 0.000000 at stage IT_COARSE: Level: 1 -- Iteration: 4 -- Sweep: 1 -- residual: 1.98454721e-03\n",
368+
"hooks - INFO: Process 1 on time 0.010000 at stage IT_COARSE: Level: 1 -- Iteration: 4 -- Sweep: 1 -- residual: 1.38149253e-02\n",
369+
"hooks - INFO: Process 0 on time 0.000000 at stage IT_FINE: Level: 0 -- Iteration: 4 -- Sweep: 1 -- residual: 1.09509225e-03\n",
370+
"hooks - INFO: Process 1 on time 0.010000 at stage IT_FINE: Level: 0 -- Iteration: 4 -- Sweep: 1 -- residual: 3.33496832e-03\n",
371+
"hooks - INFO: Process 0 on time 0.000000 at stage IT_COARSE: Level: 1 -- Iteration: 5 -- Sweep: 1 -- residual: 1.35676523e-03\n",
372+
"hooks - INFO: Process 1 on time 0.010000 at stage IT_COARSE: Level: 1 -- Iteration: 5 -- Sweep: 1 -- residual: 4.33758300e-03\n",
373+
"hooks - INFO: Process 0 on time 0.000000 at stage IT_FINE: Level: 0 -- Iteration: 5 -- Sweep: 1 -- residual: 7.69111709e-04\n",
374+
"hooks - INFO: Process 1 on time 0.010000 at stage IT_FINE: Level: 0 -- Iteration: 5 -- Sweep: 1 -- residual: 2.54391448e-03\n",
375+
"hooks - INFO: Process 0 on time 0.000000 at stage IT_COARSE: Level: 1 -- Iteration: 6 -- Sweep: 1 -- residual: 9.49296378e-04\n",
376+
"hooks - INFO: Process 1 on time 0.010000 at stage IT_COARSE: Level: 1 -- Iteration: 6 -- Sweep: 1 -- residual: 3.28987669e-03\n",
377+
"hooks - INFO: Process 0 on time 0.000000 at stage IT_FINE: Level: 0 -- Iteration: 6 -- Sweep: 1 -- residual: 5.45954260e-04\n",
378+
"hooks - INFO: Process 1 on time 0.010000 at stage IT_FINE: Level: 0 -- Iteration: 6 -- Sweep: 1 -- residual: 1.94430599e-03\n",
379+
"hooks - INFO: Process 0 on time 0.000000 at stage IT_COARSE: Level: 1 -- Iteration: 7 -- Sweep: 1 -- residual: 6.72609330e-04\n",
380+
"hooks - INFO: Process 1 on time 0.010000 at stage IT_COARSE: Level: 1 -- Iteration: 7 -- Sweep: 1 -- residual: 2.50390906e-03\n",
381+
"hooks - INFO: Process 0 on time 0.000000 at stage IT_FINE: Level: 0 -- Iteration: 7 -- Sweep: 1 -- residual: 3.89725136e-04\n",
382+
"hooks - INFO: Process 1 on time 0.010000 at stage IT_FINE: Level: 0 -- Iteration: 7 -- Sweep: 1 -- residual: 1.48542866e-03\n",
383+
"hooks - INFO: Process 0 on time 0.000000 at stage IT_COARSE: Level: 1 -- Iteration: 8 -- Sweep: 1 -- residual: 4.79710468e-04\n",
384+
"hooks - INFO: Process 1 on time 0.010000 at stage IT_COARSE: Level: 1 -- Iteration: 8 -- Sweep: 1 -- residual: 1.90666008e-03\n",
385+
"hooks - INFO: Process 0 on time 0.000000 at stage IT_FINE: Level: 0 -- Iteration: 8 -- Sweep: 1 -- residual: 2.79036000e-04\n",
386+
"hooks - INFO: Process 1 on time 0.010000 at stage IT_FINE: Level: 0 -- Iteration: 8 -- Sweep: 1 -- residual: 1.13298624e-03\n",
387+
"hooks - INFO: Process 0 on time 0.000000 at stage IT_COARSE: Level: 1 -- Iteration: 9 -- Sweep: 1 -- residual: 3.43322868e-04\n",
388+
"hooks - INFO: Process 1 on time 0.010000 at stage IT_COARSE: Level: 1 -- Iteration: 9 -- Sweep: 1 -- residual: 1.45034263e-03\n",
389+
"hooks - INFO: Process 0 on time 0.000000 at stage IT_FINE: Level: 0 -- Iteration: 9 -- Sweep: 1 -- residual: 2.00115081e-04\n",
390+
"hooks - INFO: Process 1 on time 0.010000 at stage IT_FINE: Level: 0 -- Iteration: 9 -- Sweep: 1 -- residual: 8.62214360e-04\n",
391+
"hooks - INFO: Finished run after 1.31e+00s\n",
392+
"hooks - INFO: Finished run after 1.31e+00s\n"
372393
]
373394
}
374395
],
375396
"source": [
376397
"# get initial conditions\n",
377-
"P = controller.MS[0].levels[0].prob\n",
398+
"P = controller.MS[0].levels[0].prob # observe the hierarchy\n",
378399
"uinit = P.u_exact(t=0)\n",
379400
"\n",
380401
"uend, stats = controller.run(u0=uinit, t0=0, Tend=2e-2)"
@@ -390,12 +411,12 @@
390411
"Not all SDC variants are always useful.\n",
391412
"But, if you actually have a think about what you're doing rather than just picking random configurations, SDC can be a quite powerful method!\n",
392413
"\n",
393-
"For now, let's through out the weird time coarsening and only do coarsening in space:"
414+
"For now, let's throw out the weird time coarsening and only do coarsening in space:"
394415
]
395416
},
396417
{
397418
"cell_type": "code",
398-
"execution_count": 36,
419+
"execution_count": 5,
399420
"id": "6318d0bf-bbea-4a35-929d-15c7693ae392",
400421
"metadata": {},
401422
"outputs": [
@@ -419,8 +440,8 @@
419440
"Controller: <class 'pySDC.implementations.controller_classes.controller_nonMPI.controller_nonMPI'>\n",
420441
" all_to_done = False\n",
421442
" dump_setup = True\n",
422-
" fname = run_pid75826.log\n",
423-
"--> hook_class = [<class 'pySDC.implementations.hooks.default_hook.DefaultHooks'>, <class 'pySDC.implementations.hooks.log_timings.CPUTimings'>, <class 'pySDC.implementations.hooks.default_hook.DefaultHooks'>, <class 'pySDC.implementations.hooks.log_timings.CPUTimings'>, <class 'pySDC.implementations.hooks.default_hook.DefaultHooks'>, <class 'pySDC.implementations.hooks.log_timings.CPUTimings'>, <class 'pySDC.implementations.hooks.default_hook.DefaultHooks'>, <class 'pySDC.implementations.hooks.log_timings.CPUTimings'>, <class 'pySDC.implementations.hooks.log_solution.LogSolution'>]\n",
443+
" fname = run_pid77603.log\n",
444+
"--> hook_class = [<class 'pySDC.implementations.hooks.default_hook.DefaultHooks'>, <class 'pySDC.implementations.hooks.log_timings.CPUTimings'>, <class 'pySDC.implementations.hooks.default_hook.DefaultHooks'>, <class 'pySDC.implementations.hooks.log_timings.CPUTimings'>, <class 'pySDC.implementations.hooks.log_solution.LogSolution'>]\n",
424445
" log_to_file = False\n",
425446
"--> logger_level = 15\n",
426447
"--> mssdc_jac = False\n",
@@ -521,8 +542,8 @@
521542
"hooks - INFO: Process 1 on time 0.010000 at stage IT_FINE: Level: 0 -- Iteration: 4 -- Sweep: 1 -- residual: 3.97339053e-05\n",
522543
"hooks - INFO: Process 1 on time 0.010000 at stage IT_COARSE: Level: 1 -- Iteration: 5 -- Sweep: 1 -- residual: 7.79394248e-08\n",
523544
"hooks - INFO: Process 1 on time 0.010000 at stage IT_FINE: Level: 0 -- Iteration: 5 -- Sweep: 1 -- residual: 2.63545359e-07\n",
524-
"hooks - INFO: Finished run after 6.40e-01s\n",
525-
"hooks - INFO: Finished run after 6.40e-01s\n"
545+
"hooks - INFO: Finished run after 5.81e-01s\n",
546+
"hooks - INFO: Finished run after 5.81e-01s\n"
526547
]
527548
}
528549
],
@@ -550,6 +571,14 @@
550571
"\n",
551572
"<img src=\"figs/scaling_GS3D_time.pdf\" style=\"width:20cm;\"/>\n",
552573
"\n",
574+
"Compare run time of various SDC configurations\n",
575+
"\n",
576+
"<img src=\"figs/timings_SDC_variants_GrayScott.pdf\" style=\"width:10cm;\"/>\n",
577+
"\n",
578+
"Compare wall time of SDC against reference RK method\n",
579+
"\n",
580+
"<img src=\"figs/wp-run_RBC-RK_comp-t-e_global_rel.pdf\" style=\"width:10cm;\"/>\n",
581+
"\n",
553582
"Recover from faults in PFASST by interpolating from nearby steps\n",
554583
"\n",
555584
"<img src=\"figs/ADVECTION_steps_vs_iteration_hf_7x7_INTERP.png\" style=\"width:10cm;\"/>\n",
@@ -558,13 +587,18 @@
558587
"\n",
559588
"<img src=\"figs/compression_order_time_advection_d=1.00e-06_n=4_MPI=True.png\" style=\"width:10cm;\"/>\n",
560589
"\n",
561-
"Compare run time of various SDC configurations\n",
562-
"\n",
563-
"<img src=\"figs/timings_SDC_variants_GrayScott.pdf\" style=\"width:10cm;\"/>\n",
564-
"\n",
565-
"Compare wall time of SDC against reference RK method\n",
566-
"\n",
567-
"<img src=\"figs/wp-run_RBC-RK_comp-t-e_global_rel.pdf\" style=\"width:10cm;\"/>"
590+
"As you can see, pySDC is a flexible tool, capable of loads of things.\n",
591+
"If you want to \n",
592+
" - design a novel SDC scheme\n",
593+
" - solve the heat equation\n",
594+
" - count iterations\n",
595+
" - solve very complicated equations\n",
596+
" - measure wall time in actual HPC settings\n",
597+
" - investigate any SDC related idea\n",
598+
" - like your code tested\n",
599+
"\n",
600+
"Then pySDC is the code for you!\n",
601+
"Get in touch if you want to collaborate or need help with anything!"
568602
]
569603
}
570604
],

0 commit comments

Comments
 (0)