// The solution-and-error view: compute one solve in the browser at a // chosen resolution and show the field next to its pointwise error // against the exact solution. import { useMemo, useState } from "react"; import type { Laplace2dInstance } from "../../problems/laplace2d/spec"; import { exactU, vizGrid, VIZ_NGRID } from "../../problems/laplace2d/exact"; import { SOLVERS, getSolver } from "../../solvers"; import { solutionInBrowser } from "../workerClient"; import type { ResultPoint } from "../../harness/resultSchema"; import { FieldView } from "./FieldView"; function exactGridValues(inst: Laplace2dInstance): Float64Array { const { xs } = vizGrid(inst); const out = new Float64Array(VIZ_NGRID * VIZ_NGRID); for (let ix = 0; ix < VIZ_NGRID; ix++) { for (let iy = 0; iy < VIZ_NGRID; iy++) { out[ix * VIZ_NGRID + iy] = exactU(inst, xs[ix], xs[iy]); } } return out; } interface Computed { solverId: string; n: number; uGrid: Float64Array; point: ResultPoint; } export function SolutionSection({ inst }: { inst: Laplace2dInstance }) { const [solverId, setSolverId] = useState(SOLVERS[0].id); const [n, setN] = useState( SOLVERS[0].sweepN[Math.floor(SOLVERS[0].sweepN.length * 0.7)] ); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [computed, setComputed] = useState(null); const exact = useMemo(() => exactGridValues(inst), [inst]); const errField = useMemo(() => { if (!computed) return null; const out = new Float64Array(exact.length); for (let i = 0; i < exact.length; i++) { out[i] = Math.abs(computed.uGrid[i] - exact[i]); } return out; }, [computed, exact]); const solver = getSolver(solverId); const showsComputed = computed !== null && computed.solverId === solverId; async function compute() { setBusy(true); setError(null); try { const { point, uGrid } = await solutionInBrowser(inst.id, solverId, n); setComputed({ solverId, n, uGrid, point }); } catch (err) { setError(err instanceof Error ? err.message : String(err)); } finally { setBusy(false); } } return (
{error &&

{error}

}
{showsComputed && computed && ( )} {showsComputed && errField && computed && ( )}
); }