From 618b26b81fe2bb6fb33cb9a1924c16e44bf45399 Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 6 Nov 2026 17:06:46 +0100 Subject: Fix stuck results while editing inputs Allow bivalence/switching temperatures below the design outdoor temperature instead of blocking the calculation, treat empty or partial numbers as invalid rather than 0, grey out stale results while an input is invalid, coalesce bursts of input events and evaluate the model once per temperature value (about 3.5x faster). Co-Authored-By: Claude Opus 5.5 --- web/app.js | 73 ++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 21 deletions(-) (limited to 'web/app.js') diff --git a/web/app.js b/web/app.js index d0e45f5..3f70176 100644 --- a/web/app.js +++ b/web/app.js @@ -174,19 +174,27 @@ function simulate(series, years, p) { backs: new Float32Array(n * 8760), temps: [], }; + // temperatures repeat a lot (0.1 K resolution), so evaluate the model once per value + const cache = new Map(); + const evaluate = (t) => { + let v = cache.get(t); + if (!v) { + const q = m.load(t), hp = m.heatPump(t); + const backup = m.backupAllowed(t) ? q - hp : 0; + v = { q, hp, backup, uncovered: q - hp - backup, el: hp > 0 ? hp / m.cop(t) : 0 }; + cache.set(t, v); + } + return v; + }; years.forEach((year, y) => { const t = temperatures(series.get(year), p.resolution); r.temps.push(t); for (let i = 0; i < t.length; i++) { - const q = m.load(t[i]); - const hp = m.heatPump(t[i]); - const backup = m.backupAllowed(t[i]) ? q - hp : 0; - const uncovered = q - hp - backup; + const { q, hp, backup, uncovered, el } = evaluate(t[i]); r.loads[y * 8760 + i] = q; r.hps[y * 8760 + i] = hp; r.backs[y * 8760 + i] = backup; if (q <= 0) continue; - const el = hp > 0 ? hp / m.cop(t[i]) : 0; r.els[y * 8760 + i] = el; r.q += q; r.hp += hp; r.backup += backup; r.uncovered += uncovered; r.el += el; r.heatingHours++; @@ -222,36 +230,51 @@ function simulate(series, years, p) { // ---------- form ---------- +/** Number from an input; NaN when it is empty or not a valid number (e.g. while typing "-"). */ +function num(id) { + const v = $(id).value.trim(); + return v === "" ? NaN : +v; +} + function params() { const cutoff = $("cutoff").value.trim(); return { - qn: +$("qn").value, - tn: +$("tn").value, - thg: +$("thg").value, + qn: num("qn"), + tn: num("tn"), + thg: num("thg"), mode: $("mode").value, - tbiv: +$("tbiv").value, + tbiv: num("tbiv"), cutoff: cutoff === "" ? NaN : +cutoff, - slope: +$("slope").value / 100, + slope: num("slope") / 100, resolution: $("resolution").value, type: $("type").value, device: $("device").value, - flowN: +$("flowN").value, + flowN: num("flowN"), flowCurve: $("flowCurve").checked, - priceEl: +$("priceEl").value, + priceEl: num("priceEl"), fuel: $("fuel").value, - priceBackup: +$("priceBackup").value, - effBackup: +$("effBackup").value / 100, + priceBackup: num("priceBackup"), + effBackup: num("effBackup") / 100, }; } function validate(p) { + const manualTbiv = p.mode === "alternative" || p.type !== "air" || p.device === "generic"; + const generic = p.type !== "air" || p.device === "generic"; + const required = [ + ["qn", "Norm-Heizlast"], ["tn", "Norm-Außentemperatur"], ["flowN", "Vorlauftemperatur"], + ["priceEl", "Strompreis"], ["priceBackup", "Preis Zusatzheizung"], ["effBackup", "Wirkungsgrad Zusatzheizung"], + ]; + if (p.mode !== "mono" && manualTbiv) required.push(["tbiv", $("tbivLabel").textContent]); + if (generic) required.push(["slope", "Leistungsänderung der WP"]); + for (const [key, label] of required) { + if (!Number.isFinite(p[key])) return `Bitte eine gültige Zahl für „${label}“ eingeben.`; + } if (!(p.qn > 0)) return "Norm-Heizlast muss größer als 0 sein."; if (!(p.flowN >= 25 && p.flowN <= 65)) return "Vorlauftemperatur muss zwischen 25 und 65 °C liegen."; if (!(p.effBackup > 0)) return "Wirkungsgrad der Zusatzheizung muss größer als 0 sein."; if (!(p.tn < p.thg)) return "Norm-Außentemperatur muss unter der Heizgrenztemperatur liegen."; - const manualTbiv = p.mode === "alternative" || p.type !== "air" || p.device === "generic"; - if (p.mode !== "mono" && manualTbiv && !(p.tbiv < p.thg)) return "Bivalenzpunkt muss unter der Heizgrenztemperatur liegen."; - if (p.mode !== "mono" && manualTbiv && p.tbiv < p.tn) return "Bivalenzpunkt unter der Norm-Außentemperatur: dann ist die Anlage praktisch monovalent."; + if (p.mode !== "mono" && manualTbiv && !(p.tbiv < p.thg)) return `${$("tbivLabel").textContent} muss unter der Heizgrenztemperatur liegen.`; return null; } @@ -312,6 +335,12 @@ function onTypeChange() { $("slope").value = $("type").value === "air" ? 3 : 0; } +let scheduled = null; +function scheduleRun() { + clearTimeout(scheduled); + scheduled = setTimeout(run, 60); +} + let runToken = 0; async function run() { updateFieldVisibility(); @@ -321,7 +350,9 @@ async function run() { } const p = params(); const error = validate(p); - if (error) { $("status").textContent = error; return; } + // keep the last results visible but greyed out until the input is valid again + $("results").classList.toggle("stale", !!error); + if (error) { $("status").innerHTML = `${error}`; return; } const token = ++runToken; const series = await loadSeries(station.id); if (token !== runToken) return; @@ -562,7 +593,7 @@ async function init() { ]); stationById = Object.fromEntries(meta.stations.map((s) => [s.id, s])); - $("plz").addEventListener("input", () => { showLocation(); run(); }); + $("plz").addEventListener("input", () => { showLocation(); scheduleRun(); }); $("tn").addEventListener("input", () => { tnEdited = true; }); $("tnReset").addEventListener("click", () => { tnEdited = false; @@ -577,8 +608,8 @@ async function init() { run(); }); onFuelChange(false); - $("form").addEventListener("input", run); - $("form").addEventListener("change", run); + $("form").addEventListener("input", scheduleRun); + $("form").addEventListener("change", scheduleRun); $("form").addEventListener("submit", (e) => e.preventDefault()); $("exportBins").addEventListener("click", exportBins); $("exportHours").addEventListener("click", exportHours); -- cgit v1.3