cantabile_edge, cantabile_modbus, cantabile_eebus 0.1.676: full source
This commit is contained in:
1 parent
3edd015039
commit
d48b9aff87
32 files changed
+4633
-133
No files matched your search
@@ -0,0 +1,136 @@
|
||||
# Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
|
||||
"""A room's thermal resistance R and capacity C from its history
|
||||
(docs/methods.md §6): the planner's first-order model (edge optimizer.py
|
||||
`room`),
|
||||
|
||||
θ_t = (1 − a) θ_{t−1} + a T_out,t + (Δt/C) P_t + γ G_t + c0,
|
||||
a = Δt / (R C),
|
||||
|
||||
written as a regression on 15-minute steps (Δt = 0.25 h):
|
||||
|
||||
θ_t − θ_{t−1} = a (T_out,t − θ_{t−1}) + b P_t + γ G_t + c0, b = Δt / C,
|
||||
|
||||
with P the heating power (kW; thermal where the site meters the heat
|
||||
output, else electric: the planner's COP turns that into heat), G the
|
||||
global irradiance (kW/m², the sun through the windows) and c0 the internal
|
||||
gains (people, appliances). Least squares on every step where all inputs
|
||||
are there; then C = Δt / b, R = Δt / (a C).
|
||||
|
||||
The other two terms go to the planner with R and C (Finding B6-a: R and C
|
||||
were fitted with them; without them the planner expects the room to cool
|
||||
faster than it does), as heat in the same units as P:
|
||||
|
||||
gain_w = 1000 c0 / b the internal gain (W, constant),
|
||||
solar_m2 = γ / b the sun's heat per irradiance (m²: W of heat
|
||||
per W/m² of global irradiance, an effective
|
||||
window area).
|
||||
|
||||
(electric fits: W and m² of electric equivalent; the box multiplies by
|
||||
the COP like C.)
|
||||
|
||||
Sanity before a fit is used (else the planner keeps its defaults, R 10
|
||||
K/kW and C 3 kWh/K): a > 0 and b > 0; R within 1..100 K/kW, C within
|
||||
0.3..50 kWh/K, τ = RC within 3..1000 h; at least a day of steps, the
|
||||
heating on and off in it, 2 K of spread in T_out − θ; and the fitted
|
||||
model simulates the room better than the defaults do (4-hour open-loop
|
||||
runs from every 4th hour: RMSE in K).
|
||||
|
||||
The site's heating power is used for every room (the sims: one zone per
|
||||
home): the rooms of a home with one heat meter each get the whole
|
||||
home's C. Per-room heating power, where metered per control, is the
|
||||
next step."""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
import numpy as np
|
||||
|
||||
DT_H = 0.25
|
||||
DEFAULT_R, DEFAULT_C = 10.0, 3.0
|
||||
R_BOUNDS, C_BOUNDS, TAU_BOUNDS = (1.0, 100.0), (0.3, 50.0), (3.0, 1000.0)
|
||||
MIN_STEPS = 96
|
||||
SIM_STEPS = 16
|
||||
SIM_EVERY = 16
|
||||
|
||||
|
||||
def simulate(theta0: float, tout: np.ndarray, p_kw: np.ndarray, ghi_kw: np.ndarray, r: float, c: float,
|
||||
gamma: float = 0.0, c0: float = 0.0) -> np.ndarray:
|
||||
a, b = DT_H / (r * c), DT_H / c
|
||||
out, th = [], theta0
|
||||
for k in range(len(tout)):
|
||||
th = (1 - a) * th + a * tout[k] + b * p_kw[k] + gamma * ghi_kw[k] + c0
|
||||
out.append(th)
|
||||
return np.array(out)
|
||||
|
||||
|
||||
def sim_rmse(theta: np.ndarray, tout: np.ndarray, p_kw: np.ndarray, ghi_kw: np.ndarray, r: float, c: float,
|
||||
gamma: float = 0.0, c0: float = 0.0) -> float | None:
|
||||
"""4-hour open-loop runs from every 4th hour where all inputs exist."""
|
||||
errs = []
|
||||
for s in range(0, len(theta) - SIM_STEPS - 1, SIM_EVERY):
|
||||
seg = slice(s + 1, s + 1 + SIM_STEPS)
|
||||
if np.isnan(theta[s]) or np.isnan(theta[seg]).any() or np.isnan(tout[seg]).any() or np.isnan(p_kw[seg]).any():
|
||||
continue
|
||||
sim = simulate(theta[s], tout[seg], p_kw[seg], np.nan_to_num(ghi_kw[seg]), r, c, gamma, c0)
|
||||
errs.append((sim - theta[seg]) ** 2)
|
||||
if not errs:
|
||||
return None
|
||||
return float(math.sqrt(np.concatenate(errs).mean()))
|
||||
|
||||
|
||||
def fit(theta: np.ndarray, tout: np.ndarray, p_w: np.ndarray, ghi: np.ndarray | None = None,
|
||||
power: str = "thermal") -> dict:
|
||||
"""theta, tout (°C), p_w (W), ghi (W/m²) per 15-minute slot, aligned.
|
||||
Returns {"status": "fitted"|"default", r_k_per_kw, c_kwh_per_k, tau_h,
|
||||
power, n, rmse_k (simulation, fitted), rmse_default_k, r2, note}, and
|
||||
when fitted gain_w and solar_m2 (the module's docstring)."""
|
||||
n_all = len(theta)
|
||||
ghi = np.zeros(n_all) if ghi is None else np.where(np.isnan(ghi), 0.0, ghi)
|
||||
p_kw = p_w / 1000.0
|
||||
g_kw = ghi / 1000.0
|
||||
prev = np.concatenate([[np.nan], theta[:-1]])
|
||||
dy = theta - prev
|
||||
x1 = tout - prev
|
||||
ok = ~(np.isnan(dy) | np.isnan(x1) | np.isnan(p_kw))
|
||||
out = {"status": "default", "r_k_per_kw": DEFAULT_R, "c_kwh_per_k": DEFAULT_C, "tau_h": DEFAULT_R * DEFAULT_C,
|
||||
"power": power, "n": int(ok.sum()), "rmse_k": None, "rmse_default_k": None, "r2": None, "note": ""}
|
||||
if ok.sum() < MIN_STEPS:
|
||||
out["note"] = f"{int(ok.sum())} usable steps (at least {MIN_STEPS})"
|
||||
return out
|
||||
on = p_kw[ok] > 0.05
|
||||
if on.all() or not on.any():
|
||||
out["note"] = "the heating was never " + ("off" if on.all() else "on") + " in the data"
|
||||
return out
|
||||
if np.ptp(x1[ok]) < 2.0:
|
||||
out["note"] = "less than 2 K between outside and inside"
|
||||
return out
|
||||
X = np.column_stack([x1[ok], p_kw[ok], g_kw[ok], np.ones(int(ok.sum()))])
|
||||
coef, *_ = np.linalg.lstsq(X, dy[ok], rcond=None)
|
||||
a, b, gamma, c0 = (float(v) for v in coef)
|
||||
resid = dy[ok] - X @ coef
|
||||
var = float(np.var(dy[ok]))
|
||||
out["r2"] = round(1 - float(np.var(resid)) / var, 3) if var > 0 else None
|
||||
if a <= 0 or b <= 0:
|
||||
out["note"] = f"no physical fit (a {a:.4g}, b {b:.4g})"
|
||||
return out
|
||||
c = DT_H / b
|
||||
r = DT_H / (a * c)
|
||||
tau = r * c
|
||||
out["fit"] = {"a": a, "b": b, "gamma": gamma, "c0": c0}
|
||||
if not (R_BOUNDS[0] <= r <= R_BOUNDS[1] and C_BOUNDS[0] <= c <= C_BOUNDS[1] and TAU_BOUNDS[0] <= tau <= TAU_BOUNDS[1]):
|
||||
out["note"] = f"outside the sanity bounds (R {r:.3g} K/kW, C {c:.3g} kWh/K, τ {tau:.3g} h)"
|
||||
return out
|
||||
fitted = sim_rmse(theta, tout, p_kw, g_kw, r, c, gamma, c0)
|
||||
default = sim_rmse(theta, tout, p_kw, g_kw, DEFAULT_R, DEFAULT_C)
|
||||
out["rmse_default_k"] = round(default, 3) if default is not None else None
|
||||
out["rmse_k"] = round(fitted, 3) if fitted is not None else None
|
||||
if fitted is None or default is None:
|
||||
out["note"] = "too few complete 4-hour stretches to check the fit"
|
||||
return out
|
||||
if fitted >= default:
|
||||
out["note"] = f"no better than the defaults ({fitted:.2f} K vs {default:.2f} K)"
|
||||
return out
|
||||
out.update(status="fitted", r_k_per_kw=round(r, 3), c_kwh_per_k=round(c, 3), tau_h=round(tau, 2),
|
||||
gain_w=round(1000.0 * c0 / b, 1), solar_m2=round(gamma / b, 3),
|
||||
note=f"simulates the room to {fitted:.2f} K (defaults {default:.2f} K)")
|
||||
return out
|
||||
Reference in new issue
Block a user