088be4cAn interactive tutorial on the Dirichlet problem, accuracy against costJeremy Magland 1import { createElement, Fragment } from 'react'
2import type { Chapter } from '../../ui/types.ts'
3import driver from '../twodim/driver.m?raw'
4import script from './highorder2d.m?raw'
5import text from './text.md?raw'
6import { Fields2D, type Single2DOut } from '../twodim/panels.tsx'
7import { Convergence, WorkPrecision, fmtErr, type SweepOut } from '../onedim/panels.tsx'
8import { EXACT2D_CONTROL } from '../shared.ts'
9import type { MethodKey } from '../../plot/palette.ts'
11const SLOTS: MethodKey[] = ['fd2', 'fd4', 'spectral']
13export const ch07: Chapter = {
14 id: 'high-order-2d',
15 nav: 'Sixth order for the same price',
16 part: 'Two dimensions',
17 title: 'Sixth order for the same price',
18 blurb: 'The compact nine-point stencil, Chebyshev collocation on the square, and a million-fold difference in unknowns.',
19 text,
20 labs: [
21 {
22 id: 'fields',
23 title: 'The same grid, three stencils',
24 caption: 'Watch the error scale on the right, not the picture on the left: at N = 32 they differ by ten orders of magnitude.',
25 driver,
26 script,
27 controls: [
28 EXACT2D_CONTROL,
29 {
30 kind: 'select',
31 key: 'mid',
32 label: 'method',
33 options: [
34 { value: 1, label: '5-point' },
35 { value: 2, label: '9-point compact' },
36 { value: 3, label: 'Chebyshev' },
37 ],
38 },
39 { kind: 'slider', key: 'N', label: 'N', values: [8, 16, 24, 32, 48, 64], min: 8, max: 64 },
40 { kind: 'slider', key: 'par', label: 'shape parameter', min: 1, max: 8, step: 1, format: (v) => String(v) },
41 ],
42 defaults: { kind: 'expsin', par: 1, mode: 'single', mid: 2, N: 32, maxpix: 97 },
43 render: (d: Single2DOut) => createElement(Fields2D, { data: d }),
44 summary: (d: Single2DOut) => [
45 { label: 'unknowns', value: String(d.unknowns) },
46 { label: 'max error', value: fmtErr(d.maxerr) },
47 ],
48 },
49 {
50 id: 'sweep',
51 title: 'Order six against order two',
52 caption: 'Chebyshev is capped at N = 64, which is already 3969 unknowns and machine precision.',
53 driver,
54 script,
55 manual: true,
56 editorOpen: true,
57 controls: [EXACT2D_CONTROL, { kind: 'slider', key: 'par', label: 'shape parameter', min: 1, max: 8, step: 1, format: (v) => String(v) }],
58 defaults: {
59 kind: 'expsin',
60 par: 1,
61 mode: 'sweep',
62 ns: [8, 16, 32, 64, 128, 256, 512],
63 nmaxs: [512, 512, 64],
64 timed: 1,
65 },
66 render: (d: SweepOut) =>
67 createElement(
68 Fragment,
69 null,
70 createElement(Convergence, {
71 key: 'c',
72 data: d,
73 style: {
74 slots: SLOTS,
75 refs: [
76 { p: -2, label: 'slope −2' },
77 { p: -6, label: 'slope −6' },
78 ],
79 },
80 height: 340,
81 }),
82 createElement(WorkPrecision, { key: 'w', data: d, style: { slots: SLOTS }, height: 340 }),
83 ),
84 summary: (d: SweepOut) => [
85 { label: 'best 5-point', value: fmtErr(Math.min(...(d.E[0].filter((v) => v != null) as number[]))) },
86 { label: 'best 9-point', value: fmtErr(Math.min(...(d.E[1].filter((v) => v != null) as number[]))) },
87 { label: 'best Chebyshev', value: fmtErr(Math.min(...(d.E[2].filter((v) => v != null) as number[]))) },
88 ],
89 },
90 ],
91}