-
Notifications
You must be signed in to change notification settings - Fork 0
/
parabolic_pde.py
307 lines (229 loc) · 6.89 KB
/
parabolic_pde.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
from __future__ import annotations
from abc import ABC, abstractmethod
from collections import deque
from time import perf_counter
from typing import TYPE_CHECKING, Protocol, override
import marimo as mo
import numpy as np
from matplotlib.pyplot import subplots
from pandas import DataFrame
from seaborn import lineplot
if TYPE_CHECKING:
from collections.abc import Collection
from typing import Callable, Final
from matplotlib.axes import Axes
from matplotlib.figure import Figure
@mo.cache
def ref(t: np.ndarray, x: np.ndarray) -> np.ndarray:
"""真解
Params:
t[#t]
x[#x]
Returns:
u[#x, #t]
"""
assert t.ndim == 1
assert x.ndim == 1
return np.exp(-x[:, np.newaxis] + t[np.newaxis, :])
def setup_conditions(t: np.ndarray, x: np.ndarray) -> np.ndarray:
"""根据初始条件、边界条件准备预备解
Params:
t[#t]
x[#x]
Returns:
u[#x, #t]
"""
assert t.ndim == 1
assert x.ndim == 1
u = np.zeros((x.size, t.size))
u[:, 0] = np.exp(-x)
u[0, :] = np.exp(t)
u[-1, :] = np.exp(t - 1)
return u
class _Solvable(Protocol):
u: np.ndarray
t: np.ndarray
x: np.ndarray
def solve(self) -> None: ...
class _PerformanceMixin(_Solvable):
def error(self) -> np.ndarray:
return self.u - ref(self.t, self.x)
def max_error(self) -> float:
return np.abs(self.error()).max()
def timing(self, *, number: int = 10, repeat: int = 7) -> deque[float]:
"""Measure the time of `solve`
Params:
`number`: number of executions
`repeat`: repeat count
Returns:
average duration of an execution of each repetition, in seconds.
"""
durations: deque[float] = deque()
for _ in range(repeat):
start = perf_counter()
for _ in range(number):
self.solve()
durations.append((perf_counter() - start) / number)
return durations
class Solver(_PerformanceMixin, ABC):
"""PDE solver
init (and post_init) → solve
"""
dx: Final[float]
dt: Final[float]
# x[#x]
x: Final[np.ndarray]
# t[#t]
t: Final[np.ndarray]
# u[#x, #t]
u: np.ndarray
def __init__(self, *, t: np.ndarray, x: np.ndarray) -> None:
assert x.ndim == 1
assert t.ndim == 1
self.dt = np.diff(t).mean()
self.dx = np.diff(x).mean()
self.t = t
self.x = x
self.u = setup_conditions(t, x)
self.post_init()
def post_init(self) -> None:
"""Prepare after `__init__`"""
pass
@abstractmethod
def step(self, t: int) -> None:
"""Update u[1:-1, t]"""
pass
def solve(self) -> None:
"""Solve u"""
for t in range(self.t.size):
if t == 0:
continue
self.step(t)
def validate(self, t: int) -> None:
"""Validate the PDE at `t`
An optional abstract method.
Raise an `AssertionError` if invalid.
"""
pass
class AdaptiveSolver(_PerformanceMixin, ABC):
"""PDE solver with adaptive time step
init (and post_init) → solve
"""
dx: Final[float]
# x[#x]
x: Final[np.ndarray]
t_min: Final[float]
t_max: Final[float]
dt: float
# t[#t]
t: deque[float]
# u[#t][#x]
u: deque[np.ndarray]
u_boundary: Final[tuple[Callable[[float], float], Callable[[float], float]]]
def __init__(
self,
*,
t: tuple[float, float, float],
x: np.ndarray,
u_initial: np.ndarray,
u_boundary: tuple[Callable[[float], float], Callable[[float], float]],
) -> None:
"""
Params:
t: (min, max, initial step)
x[#x]
u_initial[#x]
u_boundary: (t ⇒ u(t, x_min), t ⇒ u(t, x_max))
"""
assert x.ndim == 1
assert u_initial.ndim == 1
self.dx = np.diff(x).mean()
self.x = x
(self.t_min, self.t_max, self.dt) = t
self.t = deque([self.t_min])
self.u = deque([u_initial])
self.u_boundary = u_boundary
self.post_init()
def post_init(self) -> None:
"""Prepare after `__init__`"""
pass
@abstractmethod
def step(self) -> None:
"""Append next u, t, and update dt"""
pass
def solve(self) -> None:
"""Solve u"""
while self.t[-1] < self.t_max:
self.step()
def validate(self, t: int) -> None:
"""Validate the PDE at `t`
An optional abstract method.
Params:
t: The index in `t`.
Raise an `AssertionError` if invalid.
"""
pass
@override
def error(self) -> np.ndarray:
return self.u_array() - ref(self.t_array(), self.x)
def u_array(self) -> np.ndarray:
"""u[#x, #t]"""
return np.array(list(self.u)).T
def t_array(self) -> np.ndarray:
"""t[#t]"""
return np.array(list(self.t))
def benchmark(
solver_cls: type[Solver | AdaptiveSolver],
*,
t_min: float,
t_max: float,
x_min: float,
x_max: float,
dx_list: Collection[float],
dt: float = 0.01,
) -> DataFrame:
"""Benchmark
Returns:
列为dx、最大误差、时长
"""
assert issubclass(solver_cls, (Solver, AdaptiveSolver))
# (dx, durations, max error)[]
stat: deque[tuple[float, deque[float], float]] = deque()
for dx in mo.status.progress_bar(dx_list): # type: ignore
dx: float
x = np.arange(x_min, x_max + dx, dx)
t = np.arange(t_min, t_max + dt, dt)
if issubclass(solver_cls, Solver):
solver = solver_cls(x=x, t=t)
else:
solver = solver_cls(
t=(t_min, t_max, dt),
x=x,
u_initial=np.exp(-x),
u_boundary=(np.exp, lambda t: np.exp(t - 1)),
)
timing = solver.timing()
stat.append((dx, timing, solver.max_error()))
return DataFrame(
[[dx, error, duration] for (dx, timing, error) in stat for duration in timing],
columns=["dx", "最大误差", "时长"],
)
def plot_benchmark(data: DataFrame) -> tuple[Figure, Axes]:
"""Plot the benchmark result
Params:
`df`: Output of `benchmark()`
"""
fig, axs = subplots(nrows=3, layout="constrained")
lineplot(ax=axs[0], data=data, x="dx", y="最大误差", markers=True)
axs[0].set_xlabel(r"$\mathrm{d}x$")
lineplot(ax=axs[1], data=data, x="dx", y="时长", markers=True)
axs[1].set_xlabel(r"$\mathrm{d}x$")
axs[2].set_ylabel("时长 / s")
# 时长需要计算误差线,必须放到纵轴
lineplot(ax=axs[2], data=data, y="时长", x="最大误差", markers=True)
axs[2].set_ylabel("时长 / s")
for ax in axs:
ax.set_xscale("log")
ax.set_yscale("log")
ax.grid(True)
return fig, axs