cantabile_edge, cantabile_modbus, cantabile_eebus 0.1.1919: full source

This commit is contained in:
Setti Solutions S.R.L committed 2026-11-12 13:44:54 +00:00
1 parent b24675fe9c
commit c0a6f8a579
21 files changed
+1055 -47

No files matched your search

+49 -5
View File
@@ -171,6 +171,9 @@ SPILL_EUR_KWH = 1e-3 # EUR per kWh of PV curtailed (ENG: never curtail whil
# above NO_PRICE_EUR_KWH so export wins over spill without a tariff)
END_VALUE_KEY = "battery.end_value_pct" # the Parameters page's (via the tariff bundle's params): % of the
# mean feed-in price a kWh left in the battery at the end is worth
WEAR_KEY = "battery.wear_ct" # the Parameters page's (via the tariff bundle's params): net ct per kWh a battery
# discharges, its ageing per kWh cycled (the owner's battery algorithm, 2026-11-12:
# in every decision, as the building's battery_core); a device's own wear_ct wins
END_VALUE_GAP_CT = 0.5 # ct/kWh: the end value at least this far below the cheapest import price (ENG: never
# buy grid energy into the battery only to keep it; building/mieterstrom.py's twin)
GRID_MAX_W = 43470.0 # W: the site's connection when the state does not say (state["grid_max_w"],
@@ -546,6 +549,21 @@ class Model:
self._end_v = max(0.0, v)
return self._end_v
def wear(self, p: dict) -> tuple[float, str]:
"""The battery's wear (ct per kWh discharged) and where it came from: the device's own wear_ct, else the
Parameters page's battery.wear_ct (state["params"]); 0 without a tariff (nothing is priced) or without
either (as before 2026-11-12: "none")."""
if not self.priced:
return 0.0, "none"
for raw, src in ((p.get("wear_ct"), "device"), ((self.s.get("params") or {}).get(WEAR_KEY), "parameter")):
try:
v = float(raw)
except (TypeError, ValueError):
continue
if math.isfinite(v) and v >= 0:
return v, src
return 0.0, "none"
def battery(self, d):
p, st, b = d["params"], d["state"], d["bounds"]
cap = max(1.0, float(p["capacity_wh"]))
@@ -566,6 +584,10 @@ class Model:
lam_h = max(0.0, float(p.get("self_discharge_pct_month") or 0.0)) / 100.0 / HOURS_PER_MONTH
phi = (1.0 - lam_h) ** self.dt
grid_side = p.get("standby_in_load") is False
wear_ct, wear_src = self.wear(p)
wear_eur = wear_ct / 100.0 * self.dt / 1000.0 # EUR per W discharged over a step
self.wear_used = getattr(self, "wear_used", {})
self.wear_used[d["id"]] = {"ct": wear_ct, "src": wear_src}
lbs, lgs = [], []
for t in range(self.T):
lo_t = lo_pct
@@ -573,7 +595,7 @@ class Model:
lo_t = min(max(lo_pct, pref["floor_pct"][t]), hi_pct)
floors.append(lo_t)
c = self.var(0.0, max_ch, cost=self.use_cost(t))
x = self.var(0.0, max_dis, cost=self.use_cost(t))
x = self.var(0.0, max_dis, cost=self.use_cost(t) + wear_eur)
z = self.var(0.0, 1.0, integer=True)
self.h.addConstr(c - max_ch * z <= 0)
self.h.addConstr(x + max_dis * z <= max_dis)
@@ -640,6 +662,7 @@ class Model:
t_out = self.s["t_out"]
cops = [cop_at(p, t_out[t]) if p.get("cop_curve") else cop for t in range(self.T)]
els, gss, dhs, hxs, bxs, prev_u = [], [], [], [], [], None
hts, occs = [], [] # per step the heat's terms; the occupancy the target used
if src == "hybrid" and st.get("source") in ("heat_pump", "boiler"):
prev_u = 1.0 if st["source"] == "heat_pump" else 0.0 # the source now: a change from it counts
a = self.dt / (r * c)
@@ -688,6 +711,7 @@ class Model:
dhs.append(ph)
heat = sum((k * v for k, v in terms), start=Lin())
hxs.append(heat)
hts.append(list(terms))
if combi is not None:
self.h.addConstr(heat <= max(qmax - combi[t], 0.0))
main = pw if pw is not None else pg if pg is not None else ph
@@ -702,6 +726,7 @@ class Model:
self.h.addConstr(tt - (1 - a) * prev - sign * self.dt / c / 1000.0 * heat
- self.dt / c / 1000.0 * gain == a * self.s["t_out"][t] + self.dt / c / 1000.0 * fixed)
occ = self.s["occupied"][t + 1] if t + 1 < self.T else self.s["occupied"][t]
occs.append(occ)
want, must = (comfort if occ else other), None
if pref:
want, must = pref["target"][t], pref["must"][t]
@@ -755,7 +780,13 @@ class Model:
"district": qmax}[src], "switched": switched,
# the shift envelope (electric sources only): Wh of electricity per K at the mean COP
"keep": 1 - a, "wh_per_k": 1000.0 * c / (sum(cops) / len(cops)) if cops and min(cops) > 0 else 0.0,
"band": band})
"band": band,
# for the heating levers the baseline does not count (heatlevers.py, E14): the
# room's model, the heat's terms per step, its source's conversion and facts
"a": a, "k": self.dt / c / 1000.0, "t0": t0, "hterms": hts, "sup": sup,
"elmax": pmax, "combi": combi, "comfort": comfort, "other": other,
"lo": lo, "hi": hi, "pref": bool(pref), "occ": occs,
"bivalent_c": p.get("bivalent_c")})
def hourly(self, v) -> list[float]:
"""A number, or 24 by local hour, per step."""
@@ -948,7 +979,9 @@ class Model:
"qmax": sup.qmax if sup else None,
# a boiler-heated tank's fuel (gas or heating oil: the plan's series, plan_series)
"fuel": sup.carrier if heater == "boiler" and sup else None,
"src": "electric" if heater in ("element", "heat_pump") else heater})
"src": "electric" if heater in ("element", "heat_pump") else heater,
# for the store's lever (heatlevers.py): its heater's conversion, its own setpoint without the plan
"sup": sup, "thermostat_c": p.get("thermostat_c")})
def cover(self, d, room_ids):
p, b = d["params"], d["bounds"]
@@ -1289,6 +1322,8 @@ def plan(s: dict, time_limit: float = 30.0) -> dict:
v_ct = m.end_value()
out["battery_end"] = {"value_ct": round(v_ct, 2), "store_eur": round(sum(
v_ct / 100.0 * (x(b["soc"][-1]) - b["soc0"]) / 1000.0 for b in bats), 2)}
# the wear on every kWh discharged (the owner's battery algorithm, 2026-11-12) and where it came from
out["battery_wear"] = getattr(m, "wear_used", {})
if parts is not None:
out["cost_parts"] = parts # cost_eur by carrier, the heat outlook a fixed term of its own
if outlook is not None:
@@ -2162,9 +2197,18 @@ def tank_thermostat_heat(m, v) -> list[float]:
2026-11-09 the baseline held E_min from the first step, and a tank
started warm showed its stored heat as saved: 0.75 EUR per plan for
300 L at 55 °C over a 45 °C least, at one price)."""
return tank_thermostat(m, v)[0]
def tank_thermostat(m, v, emin: float | None = None) -> tuple[list[float], float]:
"""tank_thermostat_heat holding emin (Wh above the cold water; None:
the comfort's least E_min): (the heat per step, W; the stored heat at
the horizon's end, Wh). The store's lever (heatlevers.py) holds the
store's own setpoint with it."""
e, dt, out = v["e0"], m.dt, []
emin = v["emin"] if emin is None else emin
for t in range(m.T):
need = (v["emin"] - v["phi"] * e) / dt + v["draw"][t] + v["p0"]
need = (emin - v["phi"] * e) / dt + v["draw"][t] + v["p0"]
if v["heater"] in ("element", "heat_pump"):
cap = v["pmax"] * v["cops"][t] if v["pmax"] else math.inf
else:
@@ -2172,7 +2216,7 @@ def tank_thermostat_heat(m, v) -> list[float]:
q = min(max(0.0, need), cap)
e = v["phi"] * e + dt * (q - v["draw"][t] - v["p0"])
out.append(q)
return out
return out, e
def tank_baseline_steps(m, v) -> dict: