Files
goodwe-addon/goodwe_controller/test_ev05_long.py
T
ems-agentandClaude 17a7b299d2
checks / unit (pull_request) Successful in 23m32s
infra-pin / fresh (pull_request) Successful in 1s
EV-05 round 2: the peak cut waits 15 s while a battery can give
Owner 2026-11-18 (dev finding 1, brief v4 Expected 8): while a battery is
above its cut-out, the car's peak cut waits until the guard has wanted power
for 15 s in a row (CUT_WAIT_S), so a cloud edge the batteries catch does not
pause the car. It cuts at once with no battery able, or with 120 s or less of
the 15-minute period left (CUT_NEAR_END_S: waiting 15 s then costs at most
8/7 of the cut, taken off the car at once).

Tests: T21/T22 cut at 15 s and not sooner, the period lands under the aim;
T21b a house step and a cloud edge the batteries catch: no cut, no pause;
T24 (c) late in the period: the cut at once, under the aim; T12's cut row
with the batteries at their cut-out. The harness's battery level model was
1000x too fast (round 1); fixed. The 10-hour day: 0 cut pauses in both
priorities.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-11-18 10:13:02 +01:00

427 lines
18 KiB
Python

"""EV-05: the car's Solar mode, the long runs. `python3 -B test_ev05_long.py`
Split from test_ev05.py so CI's parallel batch stays short: the shadow-mode
twin (T39), the acceptance day (T44), the fuzz (T45), and the hour-long
"nothing else changes" comparisons (T50, T51). Same harness,
`tools/ev05_sun.py`; the rows are named by their test-plan id.
Mutating a module this file grades? Read MUTATION.md first (purge __pycache__).
"""
import logging
import math
import os
import random
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, "tools"))
import ev05_sun as S # noqa: E402
from chargersim import SimCharger # noqa: E402
logging.disable(logging.CRITICAL)
fails = []
def check(name, cond):
print(f" {'ok ' if cond else 'FAIL'} {name}")
if not cond:
fails.append(name)
def const(w):
return lambda t: float(w)
def steps(*pairs):
def f(t):
v = pairs[0][1]
for at, w in pairs:
if t >= at:
v = w
return float(v)
return f
def cur(r):
return S.writes(r, "current")
def ph(r):
return S.writes(r, "phases")
FULL = {"a": 100.0, "b": 100.0}
ABLE = {"a": 60.0, "b": 60.0}
def quarters(r, aim):
"""(start, import average, car average) of each whole quarter."""
out = []
tr = r["trace"]
for q0 in range(0, int(tr[-1]["t"]) + 1 - 900, 900):
xs = [x for x in tr if q0 <= x["t"] < q0 + 900]
if len(xs) < 890:
continue
g = sum(max(0.0, x["grid"]) for x in xs) / len(xs)
car = sum(x["car"] for x in xs) / len(xs)
out.append((q0, g, car))
return out
def seq_ok(r):
"""Every select: a 0 A write before it, the car under 100 W or out of C or
20 s after the 0 A, then 10 s or more to the next current; no other write
in between."""
log = r["sim"].log
state = {x["t"]: x["st"] for x in r["trace"]}
bad = []
for i, x in enumerate(log):
if x[1] != "phases":
continue
prev = log[i - 1] if i else None
nxt = log[i + 1] if i + 1 < len(log) else None
if prev is None or prev[1] != "current" or prev[2] != 0.0:
bad.append(("no 0 A before", x))
continue
quiet = (x[3] < 100.0 or state.get(x[0] - 1) != "C"
or x[0] - prev[0] >= 20.0)
if not quiet or x[4] != 0.0:
bad.append(("select under load", x))
if nxt is not None and (nxt[1] != "current" or nxt[0] - x[0] < 10.0):
bad.append(("current too soon or not next", x, nxt))
return bad
def inv_sets(r):
return [x for x in r["plant"].sets if x[1].startswith("number.sp_")]
# =========================================================================== #
print("\nT39. SHADOW MODE: NOTHING WRITTEN, THE CARD SAYS WHAT IT WOULD SET (N10)")
# =========================================================================== #
def cloudy(t):
k = int(t) // 240
return 900.0 + (2600.0 if k % 3 else 400.0) + 300.0 * (k % 5)
twin = S.drive(house=const(500.0), sun=cloudy, first="car", socs=FULL,
opts={"ev_phases": "1"}, dur=3600)
for name, kw in (("on", dict(opts={"ev_phases": "1", "shadow_mode": "on"})),
("auto, control stopped",
dict(opts={"ev_phases": "1", "shadow_mode": "auto"},
events={0: lambda p, c: setattr(c, "auto", False)}))):
sh = S.drive(house=const(500.0), sun=cloudy, first="car", socs=FULL,
dur=3600, **kw)
diff = [(a["t"], a["a"], (b["would"] or (None,))[0])
for a, b in zip(twin["trace"], sh["trace"])
if b["would"] is not None and a["a"] != b["would"][0]]
check(f"T39 shadow {name}: the charger log is empty for a cloudy hour, and "
f"every second the card's 'would set' is the current of a twin run "
f"without shadow mode ({len(diff)} seconds differ: {diff[:3]})",
sh["sim"].log == [] and not diff
and len({a['a'] for a in twin['trace']}) >= 3)
# =========================================================================== #
print("\nT50, T51. NOTHING ELSE CHANGES FOR THE GUARD AND THE ZERO-GRID LOOP")
# =========================================================================== #
h50 = steps((0, 400.0), (600, 2600.0), (900, 800.0), (1800, 4200.0),
(2100, 300.0), (3000, 1500.0))
r50 = S.drive(house=h50, sun=const(0.0), plug=False, dur=3600)
r50o = S.drive(house=h50, sun=const(0.0), plug=False, dur=3600,
opts={"ev_charger": "off"})
check(f"T50: Solar with no car for an hour: the guard's floor and every battery "
f"command are identical to the run with no charger "
f"({len(inv_sets(r50))} commands)", inv_sets(r50) == inv_sets(r50o)
and [x["floor"] for x in r50["trace"]]
== [x["floor"] for x in r50o["trace"]] and cur(r50) == [])
sun51 = steps((0, 3000.0), (700, 5200.0), (1500, 2500.0))
r51 = S.drive(house=const(400.0), sun=sun51, first="car", socs=ABLE,
dur=2000)
carp = {int(x["t"]): x["car"] for x in r51["trace"]}
r51o = S.drive(house=lambda t: 400.0 + carp.get(math.ceil(t), 0.0),
sun=sun51, socs=ABLE, dur=2000, opts={"ev_charger": "off"})
check(f"T51: Car first, the zero-grid loop: the same meter series with the car "
f"as a Solar car and as house load gives the same battery commands "
f"({len(inv_sets(r51))} commands)", inv_sets(r51) == inv_sets(r51o)
and max(carp.values()) > 3000)
check("T51: the batteries charge with what the car leaves (Car first)",
-300 <= r51["plant"].batt(1999) <= 0)
# =========================================================================== #
print("\nT44. THE ACCEPTANCE DAY: 08:00-18:00, CLOUDS, HOUSE STEPS (A2)")
# =========================================================================== #
_rnd44 = random.Random(44)
CLOUDS44 = []
for _i in range(12):
a = _rnd44.randint(600, 34000)
CLOUDS44.append((a, a + _rnd44.randint(60, 480)))
STEPS44 = [(8000, 8300), (17000, 17300), (26000, 26300)]
def sun44(t):
base = max(0.0, 7500.0 * math.sin(math.pi * t / 36000.0))
return base * (0.15 if any(a <= t < b for a, b in CLOUDS44) else 1.0)
def house44(t):
w = 1450.0 + 1050.0 * math.sin(t / 1700.0)
return w + (3000.0 if any(a <= t < b for a, b in STEPS44) else 0.0)
long44 = sum(1 for a, b in CLOUDS44 if b - a > 300)
for first, race in (("batteries", False), ("car", True)):
# The batteries' levels move with their charge (A 15.36 kWh, B 20.48 kWh),
# so with Batteries first the car gets sun once they are full.
r44 = S.drive(house=house44, sun=sun44, first=first, socs={"a": 40.0,
"b": 60.0},
caps={"a": 15.36, "b": 20.48}, dur=36000, race=race,
opts={"slew_w": 0})
# `slew_w` 0 is the shipped default. With the harness's usual 400 W a
# cycle (the SAFETY-81 rig) this day also has no cut pause (dev report,
# round 2).
# On the harness clock, as the trace (the model's own clock runs ahead).
w44 = [(x[0], x[2]) for x in r44["plant"].wlog if x[1] == "current"]
p44 = [(x[0], x[2]) for x in r44["plant"].wlog if x[1] == "phases"]
# A phase change's own 0 A (a select follows within 25 s) is not a pause.
seq0 = {t for t, v in w44 if v == 0.0 and any(0 < pt - t <= 25
for pt, _n in p44)}
starts, pauses, paused, pause_t = 0, 0, True, []
for t, v in w44:
if v == 0.0 and t not in seq0:
paused = True
pauses += 1
pause_t.append(t)
elif v > 0 and paused:
starts += 1
paused = False
why_at = {x["t"]: x["why"] for x in r44["trace"]}
cut_t = [t for t in pause_t if why_at.get(t) == "cut"
or why_at.get(t - 1) == "cut"]
# Car first: no hold pause from a cloud under 300 s. Batteries first: a
# battery that gave power in the cloud is under 100 % again, so its
# rating is room and the car waits for it (the brief's room rule).
short_hit = [t for t in pause_t if t not in cut_t and first == "car"
and any(a <= t <= b + 2 for a, b in CLOUDS44 if b - a <= 300)]
print(f" pauses (second, why): {[(t, why_at.get(t)) for t in pause_t]}")
# N5 on the whole day: a start 120 s or more after its pause, a hold
# pause after 300 s of holding, non-zero writes 30 s apart.
nz = [t for t, v in w44 if v > 0]
n5 = [("30 s", b) for a, b in zip(nz, nz[1:]) if b - a < 30]
last_p = None
for t, v in w44:
if v == 0.0 and t not in seq0:
last_p = t
elif v > 0 and last_p is not None:
if t - last_p < 120:
n5.append(("start", t))
last_p = None
for t in pause_t:
if why_at.get(t) == "paused":
run = 0
while why_at.get(t - 1 - run) == "hold":
run += 1
if run < 298:
n5.append(("pause", t, run))
# The loop turns at most `slew_w` per cycle, so for some 15 s after each
# current step, cloud edge and house step the grid covers the change:
# counted apart (dev report). The rest is the car on grid power while
# the loop is settled, outside the holds.
edges = [t for t, v in w44] + [e for ab in CLOUDS44 + STEPS44 for e in ab]
edges.sort()
def settling(t):
return any(0 <= t - e < 15 for e in edges)
grid_wh = sum(min(x["car"], max(0.0, x["grid"])) for x in r44["trace"]
if x["why"] not in ("hold",) and not settling(x["t"])) / 3600.0
slew_wh = sum(min(x["car"], max(0.0, x["grid"])) for x in r44["trace"]
if settling(x["t"])) / 3600.0
steps44 = edges
over44 = [(q, round(g)) for q, g, car in quarters(r44, 3400.0)
if g > 3410.0 and g - car <= 3410.0]
gaps = [b[0] - a[0] for a, b in zip(p44, p44[1:])]
check(f"T44 {first} first ({'race' if race else 'step'}): starts {starts} "
f"(<= 1 + {long44} long clouds, Car first); peak-cut pauses "
f"{len(cut_t)} (round 2: none, the batteries catch the cloud edges)"
f"; pauses {pauses}; N5 dwells {n5[:3]}; selects {len(p44)} "
f"(gaps >= 600 s: {gaps[:4]}); no pause from a short cloud "
f"{short_hit[:3]}; the car's grid energy outside holds {grid_wh:.1f} "
f"Wh (and {slew_wh:.1f} Wh in the loop's 15 s after "
f"{len(steps44)} steps and edges); quarters over the aim by the car "
f"{over44}",
starts >= 1 and not n5 and all(g >= 600 for g in gaps)
and not cut_t and (first != "car" or starts <= 1 + long44)
and not seq_ok(r44) and not short_hit and grid_wh <= 10.0
and not over44)
# =========================================================================== #
print("\nT45. THE FUZZ: 14 SITES, 16800 STEPS (N1-N10)")
# The plan asks 14 x 400 steps; a busy site rarely has the 60 s calm a start
# needs, so each site runs 1200 steps to make enough writes to judge.
# =========================================================================== #
def fuzz(seed):
rnd = random.Random(seed)
stations = [10.0, 13.0, 16.0]
st = {"sun": rnd.uniform(0, 9000), "house": rnd.uniform(200, 3000),
"n": {}, "acts": [], "undo": [], "exits": []}
def exit_check(p, c):
"""N9: Solar to Off with the charger above 0 A, reachable, taking
writes and not held by another writer: a 0 A must follow."""
ch = p.ch
if c.car_mode == "solar" and not c.car_solar.hold \
and not ch.unavailable and ch.refuse is None \
and ch.readback_a > 0.5 and not p.ha_down:
st["exits"].append(p.t)
def later(p, fn):
st["undo"].append((p.t + rnd.randint(10, 40), fn))
def ev(p, c):
for due in [u for u in st["undo"] if u[0] <= p.t]:
st["undo"].remove(due)
due[1]()
if st.get("back") is not None and p.t >= st["back"]:
st["back"] = None # the user goes back to Solar
st["acts"].append(p.t)
return c.handle_action("car_mode", value="solar")
if st.get("press") is not None and p.t >= st["press"]:
st["press"] = None # the user presses the mode
st["acts"].append(p.t)
return c.handle_action("car_mode", value=c.car_mode)
r = rnd.random()
ch = p.ch
if r < 0.012:
st["sun"] = rnd.choice([0.0, 1000.0, 2500.0, 5000.0, 8000.0,
8000.0, 6500.0])
elif r < 0.02:
st["house"] = rnd.uniform(200, 4000)
elif r < 0.08:
st["sun"] = max(0.0, st["sun"] + rnd.uniform(-600, 600))
elif r < 0.082:
if ch.state != "A":
ch.unplug()
later(p, ch.plug_in)
st["n"]["plug"] = st["n"].get("plug", 0) + 1
elif r < 0.084:
ch.go_unavailable(True)
later(p, lambda: ch.go_unavailable(False))
st["n"]["down"] = st["n"].get("down", 0) + 1
elif r < 0.087:
ch.set_current(rnd.randint(0, 16))
st["n"]["other"] = st["n"].get("other", 0) + 1
st["press"] = p.t + rnd.randint(20, 90)
elif r < 0.089:
ch.refuse = rnd.choice(["ignore", "error"])
later(p, lambda: setattr(ch, "refuse", None))
elif r < 0.091:
p.meter_on = False
later(p, lambda: setattr(p, "meter_on", True))
st["n"]["meter"] = st["n"].get("meter", 0) + 1
elif r < 0.093:
c.o["shadow_mode"] = "on"
later(p, lambda: c.o.__setitem__("shadow_mode", "off"))
st["n"]["shadow"] = st["n"].get("shadow", 0) + 1
elif r < 0.095:
c.auto = False
later(p, lambda: setattr(c, "auto", True))
st["n"]["stop"] = st["n"].get("stop", 0) + 1
elif r < 0.098:
m = rnd.choice(["solar", "solar", "solar", "off", "manual"])
st["n"]["mode"] = st["n"].get("mode", 0) + 1
st["acts"].append(p.t)
if m == "off":
exit_check(p, c)
return c.handle_action("car_mode", value=m)
elif r < 0.1:
ch.station_a = rnd.choice(stations)
elif r < 0.103 and c.car_mode == "solar" and p.ch.readback_a > 0.5 \
and st.get("back") is None:
exit_check(p, c) # leave Solar while it charges
st["acts"].append(p.t)
st["back"] = p.t + rnd.randint(30, 120)
return c.handle_action("car_mode", value="off")
elif r < 0.2 and c.car_solar.seq is not None:
st["n"]["mid"] = st["n"].get("mid", 0) + 1
exit_check(p, c)
return c.handle_action("car_mode", value="off")
ctx = []
def note(p, c, t):
ctx.append((t, c.car_mode, c.car_writing(), c.car_solar.single,
c.car_solar.hold, p.ch.station_a))
r = S.drive(house=lambda t: st["house"], sun=lambda t: st["sun"],
first="car", opts={"ev_margin_w": 0, "ev_start_min": 1,
"ev_pause_min": 1}, socs=FULL, dur=1200,
events={t: ev for t in range(1, 1200)}, on_cycle=note,
race=seed % 2 == 1, sim=SimCharger(setpoint_a=rnd.choice(
[0, 10, 16])))
bad = []
by_t = {x[0]: x for x in ctx}
log = r["plant"].wlog # on the harness clock
last_sel, last_nz = None, None
for t, what, v, power, sp in log:
# A write logged at second t comes after that second's actions.
x = by_t.get(t) or by_t.get(t - 1)
mode = x[1] if x else "?"
if what == "current" and v > min(16.0, x[5] if x else 16.0) + 1e-9:
bad.append(("N1 over the clamp", t, v))
if what == "phases":
if sp != 0.0:
bad.append(("N2 select with the setpoint up", t))
if last_sel is not None and t - last_sel < 600:
bad.append(("N2 two selects in 600 s", t))
last_sel = t
if what == "current" and mode == "off" and v > 0:
bad.append(("N10 a non-zero write in Off", t, v))
if what == "current" and x is not None and not x[2] and v > 0 \
and mode == "solar":
bad.append(("N10 a non-zero Solar write while not writing", t, v))
if what == "current" and v > 0 and mode == "solar":
near_act = any(abs(t - a) <= 3 for a in st["acts"])
if last_nz is not None and t - last_nz < 30 and not near_act:
bad.append(("N5 two non-zero writes under 30 s", t, v))
last_nz = t
for te in st["exits"]:
if not any(x[1] == "current" and x[2] == 0.0 and te <= x[0] <= te + 3
for x in log):
bad.append(("N9 Solar to Off left the current up", te))
if r["c"].charger.clamp_faults:
bad.append(("N1 clamp fault", r["c"].charger.clamp_faults))
st["n"]["exits"] = len(st["exits"])
return bad, len(log), st["n"]
fz_bad, fz_n, fz_counts = [], 0, {}
for _seed in range(14):
_b, _n, _c = fuzz(_seed)
fz_bad += _b
fz_n += _n
for k, v in _c.items():
fz_counts[k] = fz_counts.get(k, 0) + v
check(f"T45: {fz_n} charger writes over 14 random Solar sites and 16800 steps "
f"(outages, unplugs, refusals, other writers, a silent meter, shadow and "
f"Stop, mode changes, half racing): none over the clamp, no select with "
f"the setpoint up or two in 600 s, every Solar to Off at 0 A (N9), no "
f"non-zero write in Off, in shadow "
f"or stopped, none under 30 s apart ({fz_bad[:3]})",
not fz_bad and fz_n > 100)
check(f"T45: each disturbance happened ({fz_counts})",
all(fz_counts.get(k, 0) > 2 for k in ("plug", "down", "other", "meter",
"shadow", "stop", "mode",
"exits")))
print()
if fails:
print(f"{len(fails)} FAILED")
for f in fails:
print(" -", f)
sys.exit(1)
print("all passed")