Skip to content

Commit e1b6ce8

Browse files
committed
TL: added notebook with iteration generator
1 parent b9aa354 commit e1b6ce8

3 files changed

Lines changed: 243 additions & 7 deletions

File tree

blockops/blockIterationGenerator.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ def checkResults(self, u):
8181
def generateSymbols(self, nBlocks, nLevels, nPreSmooth=1, nPostSmooth=0):
8282
save_symbols = {}
8383
u_0 = sy.Symbol(r'u_0', commutative=False)
84-
phi = [sy.Symbol(f'\phi_{i}', commutative=False) for i in range(nLevels)]
85-
chi = [sy.Symbol(f'\chi_{i}', commutative=False) for i in range(nLevels)]
84+
phi = [sy.Symbol(rf'\phi_{i}', commutative=False) for i in range(nLevels)]
85+
chi = [sy.Symbol(rf'\chi_{i}', commutative=False) for i in range(nLevels)]
8686
T_c_to_f = [sy.Symbol(f'T_{i + 1}^{i}', commutative=False) for i in range(nLevels)]
8787
T_f_to_c = [sy.Symbol(f'T_{i}^{i + 1}', commutative=False) for i in range(nLevels)]
8888
A = [sy.Matrix(np.eye(nBlocks, dtype=int) * phi[i]) + sy.Matrix(np.eye(nBlocks, k=-1, dtype=int) * -chi[i]) for i in
@@ -237,10 +237,10 @@ def check(self, expr: sy.Expr, n: int):
237237
Current block
238238
"""
239239
expr_str = f'{expr}'
240-
unknowns = list(set(re.findall(re.compile('u\^\d+_\d+'), expr_str)))
240+
unknowns = list(set(re.findall(re.compile(r'u\^\d+_\d+'), expr_str)))
241241
tmpWildcard = {}
242242
for i in range(len(unknowns)):
243-
tmp_split = re.split('_|\^', unknowns[i])
243+
tmp_split = re.split(r'_|\^', unknowns[i])
244244
iteration = int(tmp_split[1])
245245
block = int(tmp_split[2])
246246
tmp_block = 'n' if n - int(block) == 0 else f'n-{n - int(block)}'
@@ -291,4 +291,4 @@ def u(n, k):
291291

292292
if __name__ == "__main__":
293293
# PararealGenerator(n=4)
294-
MultilevelGenerator(nBlocks=7, nLevels=3, nPreSmooth=1, nPostSmooth=0)
294+
MultilevelGenerator(nBlocks=7, nLevels=4, nPreSmooth=1, nPostSmooth=0)

notebook/08_blockGenerator.ipynb

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "5a905a68",
6+
"metadata": {},
7+
"source": [
8+
"# BlockIteration generator\n",
9+
"\n",
10+
"Using the formalism of block iterations, one can generate different algorithms using symbolic computations. In particular, we can use the time-multigrid formalism to determine the block iteration for Parareal and many of its extension.\n",
11+
"\n",
12+
"For instance, Parareal consist on a 2-level time-multigrid approach with 1 pre-smoothing step, and the block update formula can be obtained like this :"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 1,
18+
"id": "0d19156e",
19+
"metadata": {},
20+
"outputs": [
21+
{
22+
"name": "stdout",
23+
"output_type": "stream",
24+
"text": [
25+
"Found rule: \n",
26+
"\n",
27+
"u_{n+1}^{k+1}=\n",
28+
" F*u_{n}^{k}\n",
29+
" +G*u_{n}^{k+1}\n",
30+
" -G*u_{n}^{k}\n",
31+
"\n"
32+
]
33+
}
34+
],
35+
"source": [
36+
"from blockops.blockIterationGenerator import MultilevelGenerator\n",
37+
"\n",
38+
"MultilevelGenerator(nBlocks=7, nLevels=2, nPreSmooth=1, nPostSmooth=0);"
39+
]
40+
},
41+
{
42+
"cell_type": "markdown",
43+
"id": "d58e00bc",
44+
"metadata": {},
45+
"source": [
46+
"Now, if we consider 2 pre-smoothing, then we get Parareal with overlap (or MGRIT with FCF relaxation) :"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": 2,
52+
"id": "ea75d86f",
53+
"metadata": {},
54+
"outputs": [
55+
{
56+
"name": "stdout",
57+
"output_type": "stream",
58+
"text": [
59+
"Found rule: \n",
60+
"\n",
61+
"u_{n+1}^{k+1}=\n",
62+
" G*u_{n}^{k+1}\n",
63+
" +F**2*u_{n-1}^{k}\n",
64+
" -G*F*u_{n-1}^{k}\n",
65+
"\n"
66+
]
67+
}
68+
],
69+
"source": [
70+
"MultilevelGenerator(nBlocks=7, nLevels=2, nPreSmooth=2, nPostSmooth=0);"
71+
]
72+
},
73+
{
74+
"cell_type": "markdown",
75+
"id": "4891764a",
76+
"metadata": {},
77+
"source": [
78+
"and this can be continued to get FCFCF[...] relaxation :"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": 3,
84+
"id": "8ba5dd3b",
85+
"metadata": {},
86+
"outputs": [
87+
{
88+
"name": "stdout",
89+
"output_type": "stream",
90+
"text": [
91+
"Found rule: \n",
92+
"\n",
93+
"u_{n+1}^{k+1}=\n",
94+
" G*u_{n}^{k+1}\n",
95+
" +F**3*u_{n-2}^{k}\n",
96+
" -G*F**2*u_{n-2}^{k}\n",
97+
"\n"
98+
]
99+
}
100+
],
101+
"source": [
102+
"MultilevelGenerator(nBlocks=7, nLevels=2, nPreSmooth=3, nPostSmooth=0);"
103+
]
104+
},
105+
{
106+
"cell_type": "markdown",
107+
"id": "ceb74720",
108+
"metadata": {},
109+
"source": [
110+
"Now, more interesting could be to build a block formula for the three-level Parareal approach :"
111+
]
112+
},
113+
{
114+
"cell_type": "code",
115+
"execution_count": 4,
116+
"id": "ddcd2b14",
117+
"metadata": {},
118+
"outputs": [
119+
{
120+
"name": "stdout",
121+
"output_type": "stream",
122+
"text": [
123+
"Found rule: \n",
124+
"\n",
125+
"u_{n+1}^{k+1}=\n",
126+
" F*u_{n}^{k}\n",
127+
" +H*u_{n}^{k+1}\n",
128+
" -G*u_{n}^{k}\n",
129+
" +G*F*u_{n-1}^{k}\n",
130+
" -H*F*u_{n-1}^{k}\n",
131+
"\n"
132+
]
133+
}
134+
],
135+
"source": [
136+
"MultilevelGenerator(nBlocks=7, nLevels=3, nPreSmooth=1, nPostSmooth=0);"
137+
]
138+
},
139+
{
140+
"cell_type": "markdown",
141+
"id": "12c5ce1d",
142+
"metadata": {},
143+
"source": [
144+
"... and even go to 4 levels eventually :"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": 5,
150+
"id": "4c45b9d5",
151+
"metadata": {},
152+
"outputs": [
153+
{
154+
"name": "stdout",
155+
"output_type": "stream",
156+
"text": [
157+
"Found rule: \n",
158+
"\n",
159+
"u_{n+1}^{k+1}=\n",
160+
" F*u_{n}^{k}\n",
161+
" +K*u_{n}^{k+1}\n",
162+
" -G*u_{n}^{k}\n",
163+
" +G*F*u_{n-1}^{k}\n",
164+
" -K*F*u_{n-1}^{k}\n",
165+
" -T_1^0*T_2^1*\\phi_2**(-1)*\\chi_2*T_1^2*\\phi_1**(-1)*\\chi_1*T_0^1*u_{n-1}^{k}\n",
166+
" +T_1^0*T_2^1*\\phi_2**(-1)*\\chi_2*T_1^2*\\phi_1**(-1)*\\chi_1*T_0^1*F*u_{n-2}^{k}\n",
167+
" +T_1^0*T_2^1*T_3^2*\\phi_3**(-1)*\\chi_3*T_2^3*T_1^2*\\phi_1**(-1)*\\chi_1*T_0^1*u_{n-1}^{k}\n",
168+
" -T_1^0*T_2^1*T_3^2*\\phi_3**(-1)*\\chi_3*T_2^3*T_1^2*\\phi_1**(-1)*\\chi_1*T_0^1*F*u_{n-2}^{k}\n",
169+
"\n"
170+
]
171+
}
172+
],
173+
"source": [
174+
"MultilevelGenerator(nBlocks=7, nLevels=4, nPreSmooth=1, nPostSmooth=0);"
175+
]
176+
},
177+
{
178+
"cell_type": "markdown",
179+
"id": "f3671960",
180+
"metadata": {},
181+
"source": [
182+
"Note that until now, the formula were automatically simplified using those notations :\n",
183+
"\n",
184+
"$$\n",
185+
"\\begin{align}\n",
186+
"F &= \\phi_0^{-1}\\chi_0 \\\\\n",
187+
"G &= T_0^1 \\phi_1^{-1}\\chi_1 T_1^0 \\\\\n",
188+
"H &= T_0^1 T_1^2 \\phi_2^{-1}\\chi_2 T_2^1 T_1^0 \\\\\n",
189+
"K &= T_0^1 T_1^2 T_2^3 \\phi_3^{-1}\\chi_3 T_3^2 T_2^1 T_1^0 \\\\\n",
190+
"...&\n",
191+
"\\end{align}\n",
192+
"$$\n",
193+
"\n",
194+
"where $0$ is the index used for the finest level, and the increases with the coarsening. $T_{i}^{j}$ is then the transfer operator to go from level $i$ to level $j$.\n",
195+
"\n",
196+
"Considering the hypothesis that if $j<i$, $T_{i}^{j} T_{j}^{i} = I$ (which means that going from a coarse level to the fine level above then back to the coarse level again does not change the solution), we can simplify the formula for 4-level Parareal above as :\n",
197+
"\n",
198+
"```latex\n",
199+
"u_{n+1}^{k+1}=\n",
200+
" F*u_{n}^{k}\n",
201+
" +K*u_{n}^{k+1}\n",
202+
" -G*u_{n}^{k}\n",
203+
" +G*F*u_{n-1}^{k}\n",
204+
" -K*F*u_{n-1}^{k}\n",
205+
" -H*G*u_{n-1}^{k}\n",
206+
" +H*G*F*u_{n-2}^{k}\n",
207+
" +K*G*u_{n-1}^{k}\n",
208+
" -K*G*F*u_{n-2}^{k}\n",
209+
"```\n",
210+
"\n",
211+
"Now, don't hesitate to play and generate all kind of Parareal-like formula !"
212+
]
213+
}
214+
],
215+
"metadata": {
216+
"kernelspec": {
217+
"display_name": "base",
218+
"language": "python",
219+
"name": "python3"
220+
},
221+
"language_info": {
222+
"codemirror_mode": {
223+
"name": "ipython",
224+
"version": 3
225+
},
226+
"file_extension": ".py",
227+
"mimetype": "text/x-python",
228+
"name": "python",
229+
"nbconvert_exporter": "python",
230+
"pygments_lexer": "ipython3",
231+
"version": "3.13.9"
232+
}
233+
},
234+
"nbformat": 4,
235+
"nbformat_minor": 5
236+
}

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ requires = ["hatchling"]
33
build-backend = "hatchling.build"
44

55
[tool.hatch.build.targets.wheel]
6-
packages = ["time4apint"]
6+
packages = ["blockops"]
77

88
[tool.hatch.metadata]
99
allow-direct-references = true
1010

1111
[project]
12-
name = "time4apint"
12+
name = "blockops"
1313
version = "0.0.2"
1414
description = "Analysis tool for iterative Parallel-in-Time algorithms"
1515
dependencies = [

0 commit comments

Comments
 (0)