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;
27}
29interface Computed {
30 solverId: string;
31 n: number;
32 uGrid: Float64Array;
33 point: ResultPoint;
34}
36// Only solvers that run in the browser can compute a field here.
37const BROWSER_SOLVERS = SOLVERS.filter((s) => s.runtime === "numbl");
39export function SolutionSection({ inst }: { inst: Laplace2dInstance }) {
40 const [solverId, setSolverId] = useState(BROWSER_SOLVERS[0].id);
41 const [n, setN] = useState<number>(
42 BROWSER_SOLVERS[0].sweepN[Math.floor(BROWSER_SOLVERS[0].sweepN.length * 0.7)]
43 );
44 const [busy, setBusy] = useState(false);
45 const [error, setError] = useState<string | null>(null);
46 const [computed, setComputed] = useState<Computed | null>(null);
47 const [showMarks, setShowMarks] = useState(false);
49 const exact = useMemo(() => exactGridValues(inst), [inst]);
51 const errField = useMemo(() => {
52 if (!computed) return null;
53 const out = new Float64Array(exact.length);
54 for (let i = 0; i < exact.length; i++) {
55 out[i] = Math.abs(computed.uGrid[i] - exact[i]);
56 }
57 return out;
58 }, [computed, exact]);
60 const solver = getSolver(solverId);
61 const showsComputed = computed !== null && computed.solverId === solverId;
63 // One color scale shared by the exact and computed solution fields, set
64 // by the dynamic range of the exact solution; a computed field that
65 // exceeds it clips.
66 const sharedRange = useMemo(() => {
67 const vabs = fieldAbsMax(inst, exact);
68 return { lo: -vabs, hi: vabs };
69 }, [inst, exact]);
71 const marks = useMemo(() => evalPoints(inst), [inst]);
73 async function compute() {
74 setBusy(true);
75 setError(null);
76 try {
77 const { point, uGrid } = await solutionInBrowser(inst.id, solverId, n);
78 setComputed({ solverId, n, uGrid, point });
79 } catch (err) {
80 setError(err instanceof Error ? err.message : String(err));
81 } finally {
82 setBusy(false);
83 }
84 }
86 return (
87 <div>
88 <div className="row" style={{ alignItems: "center", gap: 12, marginBottom: 12 }}>
89 <label>
90 solver{" "}
91 <select
92 value={solverId}
93 onChange={(e) => {
94 const id = e.target.value;
95 setSolverId(id);
96 const sw = getSolver(id).sweepN;
97 setN(sw[Math.floor(sw.length * 0.7)]);
98 }}
99 >
100 {BROWSER_SOLVERS.map((s) => (
101 <option key={s.id} value={s.id}>
102 {s.name}
103 </option>
104 ))}
105 </select>
106 </label>
107 <label>
108 n{" "}
109 <select value={n} onChange={(e) => setN(parseInt(e.target.value, 10))}>
110 {solver.sweepN.map((v) => (
111 <option key={v} value={v}>
112 {v}
113 </option>
114 ))}
115 </select>
116 </label>
117 <button className="primary" onClick={compute} disabled={busy}>
118 {busy ? "computing…" : "Compute in this browser"}
119 </button>
120 <label className="small">
121 <input
122 type="checkbox"
123 checked={showMarks}
124 onChange={(e) => setShowMarks(e.target.checked)}
125 />{" "}
126 show evaluation points on the error map
127 </label>
128 </div>
129 {error && <p className="small" style={{ color: "var(--series-2)" }}>{error}</p>}
130 <div className="row">
131 <FieldView
132 inst={inst}
133 values={exact}
134 mode="diverging"
135 range={sharedRange}
136 title="Exact solution"
137 caption="The exact solution on the visualization grid. The color scale is shared with the computed field."
138 />
139 {showsComputed && computed && (
140 <FieldView
141 inst={inst}
142 values={computed.uGrid}
143 mode="diverging"
144 range={sharedRange}
145 title={`${solver.name}, n = ${computed.n}`}
146 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.`}
147 />
148 )}
149 {showsComputed && errField && computed && (
150 <FieldView
151 inst={inst}
152 values={errField}
153 mode="logmag"
154 overlayPoints={showMarks ? marks : undefined}
155 title="Pointwise error (log scale)"
156 caption={
157 "Absolute difference from the exact solution; the color scale " +
158 "spans 8 decades below the maximum." +
159 (showMarks
160 ? ` Dots mark the ${marks.length} evaluation points where the reported errors are measured.`
161 : "")
162 }
163 />
164 )}
165 </div>
166 </div>
167 );
168}