// src/model.js: a small heating simulation. The house loses heat to the outside, a thermostat switches the heater, and // the forecast of a cold snap is wrong at first and gets closer to the truth as the snap approaches. function mulberry32(seed) { let a = seed >>> 0; return function() { a |= 0; a = a + 0x9E3779B9 | 0; let t = Math.imul(a ^ a >>> 15, a + 1); t = t + Math.imul(t ^ t >>> 7, t + 61) ^ t; return ((t ^ t >>> 14) >>> 0) / 4294967296; }; } const HOUSE = { heat_capacity: 40, // kWh per degree loss: 0.35, // kW lost per degree of difference to the outside heater_kw: 6, target: 21, // thermostat target, degrees C band: 0.5, // thermostat dead band fade: 180, // a snap's forecast error holds until ~3 h before it starts, then fades to the truth horizon: 360, // forecast reach, minutes }; // Triangle dip of the outside temperature for one cold snap: degrees below the base at time t (minutes) function snapDip(s, t, start, depth) { const half = s.duration / 2; const x = Math.abs(t - (start + half)); return x < half ? depth * (1 - x / half) : 0; } function createHouse(weather, seed) { const rng = mulberry32(seed); const H = HOUSE; // Each snap gets a fixed forecast error, drawn once so the forecast stays consistent const snaps = weather.snaps.map(s => ({ ...s, noise: 1 + (rng() - 0.5) * 2 * (weather.forecast_noise || 0), noise_shift: (rng() - 0.5) * 2 * 60 * (weather.forecast_noise || 0), })); let temp = weather.start_temp ?? 20; let heating = false; function outsideAt(t) { let dip = 0; for (const s of snaps) dip += snapDip(s, t, s.t_start, s.depth); return weather.base_temp - dip; } // What the forecast believes at time `now` about the outside temperature at time t. // The past is observed truth. A future snap carries its error, scaled by how far off its start still is. function forecastOutsideAt(t, now) { if (t <= now) return outsideAt(t); let dip = 0; for (const s of snaps) { const startT = s.t_start; const f = Math.min(1, Math.max(0, (startT - now) / H.fade)); const scale = 1 + ((s.fc_scale ?? 1) * s.noise - 1) * f; const shift = ((s.fc_shift_min ?? 0) + s.noise_shift) * f; dip += snapDip(s, t, s.t_start + shift, s.depth * scale); } return weather.base_temp - dip; } function step(t, dt) { // Thermostat with a dead band if (temp < H.target - H.band) heating = true; else if (temp > H.target + H.band) heating = false; const power = heating ? H.heater_kw : 0; const lossKw = H.loss * (temp - outsideAt(t)); temp += (power - lossKw) * (dt / 60) / H.heat_capacity; return { temp, heating }; } function report(t) { // Forecast of the heating power needed over the next hours to hold the target const need = []; for (let m = 15; m <= H.horizon; m += 15) { const out = forecastOutsideAt(t + m, t); need.push({ t: t + m, kw: Math.round(Math.max(0, H.loss * (H.target - out)) * 100) / 100 }); } // Confidence is a hint, not the answer: lower when a snap is still far out let lead = 0; for (const s of snaps) { const until = s.t_start - t; if (until > 0 && until < 24 * 60) lead = Math.max(lead, Math.min(1, until / 360)); } const confidence = Math.max(0.3, Math.min(0.95, 1 - (weather.forecast_noise || 0) - 0.4 * lead)); return { outside: +outsideAt(t).toFixed(1), temp: +temp.toFixed(2), confidence: +confidence.toFixed(2), need }; } return { step, report, outsideAt }; }