/ concept-collection / fastandaccurate
Sign in
concept-collection / fastandaccurate
fastandaccurate / src / app / components / SolutionSection.tsx
165 lines · 5.4 KBBlameHistoryRaw
1// The solution-and-error view: compute one solve in the browser at a
2// chosen resolution and show the field next to its pointwise error
3// against the exact solution.
5import { useMemo, useState } from "react";
6import type { Laplace2dInstance } from "../../problems/laplace2d/spec";
7import {
8 evalPoints,
9 exactU,
10 vizGrid,
11 VIZ_NGRID,
12} from "../../problems/laplace2d/exact";
13import { SOLVERS, getSolver } from "../../solvers";
14import { solutionInBrowser } from "../workerClient";
15import type { ResultPoint } from "../../harness/resultSchema";
16import { FieldView, fieldAbsMax } from "./FieldView";
18function exactGridValues(inst: Laplace2dInstance): Float64Array {
19 const { xs } = vizGrid(inst);
20 const out = new Float64Array(VIZ_NGRID * VIZ_NGRID);
21 for (let ix = 0; ix < VIZ_NGRID; ix++) {
22 for (let iy = 0; iy < VIZ_NGRID; iy++) {
23 out[ix * VIZ_NGRID + iy] = exactU(inst, xs[ix], xs[iy]);
24 }
25 }
26 return out;
29interface Computed {
30 solverId: string;
31 n: number;
32 uGrid: Float64Array;
33 point: ResultPoint;
36export function SolutionSection({ inst }: { inst: Laplace2dInstance }) {
37 const [solverId, setSolverId] = useState(SOLVERS[0].id);
38 const [n, setN] = useState<number>(
39 SOLVERS[0].sweepN[Math.floor(SOLVERS[0].sweepN.length * 0.7)]
40 );
41 const [busy, setBusy] = useState(false);
42 const [error, setError] = useState<string | null>(null);
43 const [computed, setComputed] = useState<Computed | null>(null);
44 const [showMarks, setShowMarks] = useState(false);
46 const exact = useMemo(() => exactGridValues(inst), [inst]);
48 const errField = useMemo(() => {
49 if (!computed) return null;
50 const out = new Float64Array(exact.length);
51 for (let i = 0; i < exact.length; i++) {
52 out[i] = Math.abs(computed.uGrid[i] - exact[i]);
53 }
54 return out;
55 }, [computed, exact]);
57 const solver = getSolver(solverId);
58 const showsComputed = computed !== null && computed.solverId === solverId;
60 // One color scale shared by the exact and computed solution fields, set
61 // by the dynamic range of the exact solution; a computed field that
62 // exceeds it clips.
63 const sharedRange = useMemo(() => {
64 const vabs = fieldAbsMax(inst, exact);
65 return { lo: -vabs, hi: vabs };
66 }, [inst, exact]);
68 const marks = useMemo(() => evalPoints(inst), [inst]);
70 async function compute() {
71 setBusy(true);
72 setError(null);
73 try {
74 const { point, uGrid } = await solutionInBrowser(inst.id, solverId, n);
75 setComputed({ solverId, n, uGrid, point });
76 } catch (err) {
77 setError(err instanceof Error ? err.message : String(err));
78 } finally {
79 setBusy(false);
80 }
81 }
83 return (
84 <div>
85 <div className="row" style={{ alignItems: "center", gap: 12, marginBottom: 12 }}>
86 <label>
87 solver{" "}
88 <select
89 value={solverId}
90 onChange={(e) => {
91 const id = e.target.value;
92 setSolverId(id);
93 const sw = getSolver(id).sweepN;
94 setN(sw[Math.floor(sw.length * 0.7)]);
95 }}
96 >
97 {SOLVERS.map((s) => (
98 <option key={s.id} value={s.id}>
99 {s.name}
100 </option>
101 ))}
102 </select>
103 </label>
104 <label>
105 n{" "}
106 <select value={n} onChange={(e) => setN(parseInt(e.target.value, 10))}>
107 {solver.sweepN.map((v) => (
108 <option key={v} value={v}>
109 {v}
110 </option>
111 ))}
112 </select>
113 </label>
114 <button className="primary" onClick={compute} disabled={busy}>
115 {busy ? "computing…" : "Compute in this browser"}
116 </button>
117 <label className="small">
118 <input
119 type="checkbox"
120 checked={showMarks}
121 onChange={(e) => setShowMarks(e.target.checked)}
122 />{" "}
123 show evaluation points on the error map
124 </label>
125 </div>
126 {error && <p className="small" style={{ color: "var(--series-2)" }}>{error}</p>}
127 <div className="row">
128 <FieldView
129 inst={inst}
130 values={exact}
131 mode="diverging"
132 range={sharedRange}
133 title="Exact solution"
134 caption="The exact solution on the visualization grid. The color scale is shared with the computed field."
135 />
136 {showsComputed && computed && (
137 <FieldView
138 inst={inst}
139 values={computed.uGrid}
140 mode="diverging"
141 range={sharedRange}
142 title={`${solver.name}, n = ${computed.n}`}
143 caption={`Computed in this browser: rel max error ${computed.point.relMax.toExponential(2)} at the evaluation points, solve ${(computed.point.solveSeconds * 1000).toPrecision(3)} ms. Same color scale as the exact solution.`}
144 />
145 )}
146 {showsComputed && errField && computed && (
147 <FieldView
148 inst={inst}
149 values={errField}
150 mode="logmag"
151 overlayPoints={showMarks ? marks : undefined}
152 title="Pointwise error (log scale)"
153 caption={
154 "Absolute difference from the exact solution; the color scale " +
155 "spans 8 decades below the maximum." +
156 (showMarks
157 ? ` Dots mark the ${marks.length} evaluation points where the reported errors are measured.`
158 : "")
159 }
160 />
161 )}
162 </div>
163 </div>
164 );
moveopenescclose