-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_hyperbolic.py
417 lines (305 loc) · 8.52 KB
/
3_hyperbolic.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import marimo
__generated_with = "0.9.27"
app = marimo.App(width="medium")
@app.cell(hide_code=True)
def __(mo):
mo.md(r"""# §5 双曲方程的差分方法""")
return
@app.cell(hide_code=True)
def __(mo):
mo.md(r"""## 准备工作""")
return
@app.cell
def __():
import marimo as mo
return (mo,)
@app.cell
def __():
import numpy as np
from numpy import linalg, newaxis
np.set_printoptions(precision=3, suppress=True)
return linalg, newaxis, np
@app.cell
def __():
from matplotlib import pyplot as plt
return (plt,)
@app.cell
def __():
from typing import override
return (override,)
@app.cell
def __():
from hyperbolic_pde import Solver, benchmark, plot_benchmark, ref
return Solver, benchmark, plot_benchmark, ref
@app.cell
def __():
from util import multi_diag, plot_surface, show_files, typst
return multi_diag, plot_surface, show_files, typst
@app.cell(hide_code=True)
def __(mo):
mo.md(r"""## 问题""")
return
@app.cell
def __():
a = 5
return (a,)
@app.cell(hide_code=True)
def __(a, typst):
typst(rf"""
#import "@preview/physica:0.9.3": pdv, eval
$
pdv(u,t) + {a} pdv(u,x) = 0, x in RR. \
eval(u)_(t=0) = cases(
2 &"if" x in (-1,0),
1 &"if" x = 0,
-2 &"if" x in (0,1),
0 &"otherwise"
).
$
""")
return
@app.cell(hide_code=True)
def __(mo):
dx = mo.ui.slider(
0.02, 0.1, 0.02, label=r"$\mathrm{d} x$", show_value=True, debounce=True
)
dx
return (dx,)
@app.cell
def __():
r = 1 / 6
return (r,)
@app.cell(hide_code=True)
def __(dx, mo, r):
dt = r * dx.value
mo.md(rf"$\mathrm{{d}}t = {dt}$.")
return (dt,)
@app.cell
def __(dx, np):
x = np.arange(-1.5, 5 + dx.value, dx.value)
return (x,)
@app.cell
def __(dt, np):
t = np.arange(0, 1.0, dt)
return (t,)
@app.cell
def __(plot_surface, ref, t, x):
plot_surface(t, x, ref(t, x), title="真解")
return
@app.cell
def __(np, t, x):
benchmark_kwargs = dict(
x_max=x[-1],
x_min=x[0],
t_max=x[-1],
t_min=t[0],
dt_dx_list=[(_dx / 6, _dx) for _dx in 2.0 ** np.arange(-7, -3, 1)],
)
return (benchmark_kwargs,)
@app.cell(hide_code=True)
def __(mo):
mo.md(r"""## 1 迎风(wind)""")
return
@app.cell
def __(Solver, a, multi_diag, np, override):
class SolverWind(Solver):
@override
def post_init(self) -> None:
# to_next_u[#next_x, #current_x]
self.to_next_u = (
np.eye(self.x.size)
+ a * self.dt * multi_diag([1, -1, 0], size=self.x.size) / self.dx
)
@override
def step(self, t) -> None:
self.u[:, t] = self.to_next_u @ self.u[:, t - 1]
return (SolverWind,)
@app.cell
def __(SolverWind, t, x):
solver_wind = SolverWind(t=t, x=x)
solver_wind.solve()
solver_wind.to_next_u
return (solver_wind,)
@app.cell
def __(plot_surface, solver_wind, t, x):
plot_surface(t, x, solver_wind.u, title="近似解")
return
@app.cell
def __(plot_surface, solver_wind, t, x):
plot_surface(t, x, solver_wind.error(), title="误差")
return
@app.cell(hide_code=True)
def __(mo):
mo.md(
r"""
为何毛刺这么多?!`┗|`O′|┛`
毛刺太影响最大误差了,大家都是 $2$ 的量级,指标无效。我们后面改用平均误差评估。
"""
)
return
@app.cell
def __(solver_wind):
solver_wind.max_error()
return
@app.cell
def __(solver_wind):
solver_wind.mean_error()
return
@app.cell
def __(SolverWind, benchmark, benchmark_kwargs, plot_benchmark):
_b = benchmark(SolverWind, **benchmark_kwargs)
plot_benchmark(_b)[0]
return
@app.cell(hide_code=True)
def __(mo):
mo.md(
r"""
## 2 蛙跳(frog)
这是三层格式,初始第一层用迎风格式。
"""
)
return
@app.cell
def __(Solver, a, multi_diag, override):
class SolverFrog(Solver):
@override
def step(self, t) -> None:
if t >= 2:
self.u[:, t] = self.u[:, t - 2]
self.u[1:-1, t] -= (a * self.dt / self.dx) * (
self.u[2:, t - 1] - self.u[:-2, t - 1]
)
else:
# 迎风
self.u[:, t] = (
self.u[:, t - 1]
+ (a * self.dt / self.dx)
* multi_diag([1, -1, 0], size=self.x.size)
@ self.u[:, t - 1]
)
return (SolverFrog,)
@app.cell
def __(SolverFrog, t, x):
solver_frog = SolverFrog(t=t, x=x)
solver_frog.solve()
return (solver_frog,)
@app.cell
def __(plot_surface, solver_frog, t, x):
plot_surface(t, x, solver_frog.u, title="近似解")
return
@app.cell
def __(plot_surface, solver_frog, t, x):
plot_surface(t, x, solver_frog.error(), title="误差")
return
@app.cell
def __(solver_frog):
solver_frog.max_error()
return
@app.cell
def __(solver_frog):
solver_frog.mean_error()
return
@app.cell
def __(SolverFrog, benchmark, benchmark_kwargs, plot_benchmark):
_b = benchmark(SolverFrog, **benchmark_kwargs)
plot_benchmark(_b)[0]
return
@app.cell(hide_code=True)
def __(mo):
mo.md(
r"""
## 3 Crank–Nicolson(cn)
试试[`scipy.sparse`](https://docs.scipy.org/doc/scipy/reference/sparse.html)。
"""
)
return
@app.cell
def __():
from scipy.sparse import diags_array
from scipy.sparse.linalg import spsolve
return diags_array, spsolve
@app.cell
def __(diags_array, np):
diags_array([np.ones(3), np.ones(2) * 2], offsets=[0, 1]) @ [100, 10, 1]
return
@app.cell
def __(diags_array):
# Broadcasting of scalars is supported (but shape needs to be specified)
diags_array([1, 2], offsets=[0, 1], shape=(3, 3)) @ [100, 10, 1]
return
@app.cell
def __(Solver, a, diags_array, np, override, spsolve):
class SolverCrankNicolson(Solver):
@override
def post_init(self) -> None:
# a_current[#previous_x, #current_x] (without boundary)
self.a_current = diags_array(
[
1,
self.dt * a / 2 / (2 * self.dx),
-self.dt * a / 2 / (2 * self.dx),
],
offsets=[0, 1, -1],
shape=(self.x.size - 2, self.x.size - 2),
# To perform inversion, first convert to either CSC or CSR format.
format="csc",
)
self.rhs = np.empty(self.x.size - 2)
@override
def step(self, t) -> None:
# A @ u_current + A' @ u_previous = 0
self.rhs[:] = -self.u[1:-1, t - 1] + self.dt * a / 2 * (
self.u[2:, t - 1] - self.u[:-2, t - 1]
) / (2 * self.dx)
self.u[1:-1, t] = spsolve(self.a_current, -self.rhs)
@override
def validate(self, t: int) -> None:
# (∂/∂x)[#x_without_boundary, #x_with_boundary]
dv_x = diags_array(
[1, -1], offsets=[0, 2], shape=(self.x.size - 2, self.x.size)
) / (2 * self.dx)
# (approximate ∂u/∂x)[#x_without_boundary]
approx_dv_x = dv_x @ (self.u[:, t] + self.u[:, t - 1]) / 2
# (approximate ∂u/∂t)[#x_without_boundary]
approx_dv_t = (self.u[1:-1, t] - self.u[1:-1, t - 1]) / self.dt
assert np.allclose(approx_dv_t, a * approx_dv_x)
return (SolverCrankNicolson,)
@app.cell
def __(SolverCrankNicolson, t, x):
solver_cn = SolverCrankNicolson(t=t, x=x)
solver_cn.solve()
# Validate the last `t`
solver_cn.validate(solver_cn.t.size - 1)
solver_cn.a_current.toarray()
return (solver_cn,)
@app.cell
def __(plot_surface, solver_cn, t, x):
plot_surface(t, x, solver_cn.u, title="近似解")
return
@app.cell
def __(plot_surface, solver_cn, t, x):
plot_surface(t, x, solver_cn.error(), title="误差")
return
@app.cell
def __(solver_cn):
solver_cn.max_error()
return
@app.cell
def __(solver_cn):
solver_cn.mean_error()
return
@app.cell
def __(SolverCrankNicolson, benchmark, benchmark_kwargs, plot_benchmark):
_b = benchmark(SolverCrankNicolson, **benchmark_kwargs)
plot_benchmark(_b)[0]
return
@app.cell(hide_code=True)
def __(mo):
mo.md(r"""## 附录""")
return
@app.cell(hide_code=True)
def __(show_files):
show_files(["pyproject.toml", "hyperbolic_pde.py", "util.py"])
return
if __name__ == "__main__":
app.run()