"""EV-21: the peak cut's paths: one cut per episode, the restart, the period's edge, a restart of the add-on, and every case where Solar cannot cut (then the alarm, the banking and the guard are as in Off). `python3 -B test_ev21_paths.py` Rows T22-T26, T29-T34, T36, T39-T46 and T48 of reports/EV-21-test-plan.md, and the formal check's C2 (a named bug at c79eb37). The real Controller on `tools/ev20_sun.py` through `tools/ev21_kit.py`; the second period runs 900-1800. "Twin": the same run in Off, the charger held at the same current and given the same other-writer events. Mutating a module this file grades? Read MUTATION.md first (purge __pycache__). """ import os import sys HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(HERE, "tools")) from app import carmode, ctl_car # noqa: E402 import ev20_sun as S # noqa: E402 from chargersim import SimCharger # noqa: E402 from ev20_kit import LOG, check, fails, const, step_fn # noqa: E402 from ev21_kit import (AIM, after, alarms, first_alarm, merge, # noqa: E402 never, quarter_avg, shape, twin, drive, _cutamps, replay_twin, near_end, refuse_n1) n_log0 = LOG.mark() SET = S.SET def stamp(p, c): """A non-zero write now on Solar's one write clock: its 30 s gate is closed for 30 s (a stand-in for a write it just made).""" c.car_solar.nonzero_at = p.t def zero_at(r, t0): z = [t for t, a, _ in after(r, t0) if a == 0.0] return z[0] if z else None def nonzero_after(r, t0): return [(t, a) for t, a, _ in after(r, t0) if a > 0.0] def attempts(r, t0=0): return [(t, v) for t, e, v in r["plant"].sets if e == SET and t >= t0] def close(a, b, s=2): if a is None or b is None: return a is None and b is None return abs(a - b) <= s def off_alarm_like(name, r, o, t0, extra="", ex_from=None): """The alarm at the Off twin's second (+2 s), car_explains never True from `ex_from` (default t0), and the same banked periods.""" a_s, a_o = first_alarm(r, t0), first_alarm(o, t0) t1 = t0 if ex_from is None else ex_from ex = [x["t"] for x in r["trace"] if x["t"] >= t1 and x["explains"]] check(f"{name}: the alarm at the Off twin's second +2 ({a_s} vs {a_o}); " f"car_explains never True ({ex[:2]}); banked as Off " f"({r['c'].car_peak_q} vs {o['c'].car_peak_q}){extra}", close(a_s, a_o) and not ex and r["c"].car_peak_q == o["c"].car_peak_q) # =========================================================================== # print("\n1. NO CUT FROM A STALE PROJECTION (T22, N9)") # =========================================================================== # def silent(p, c): p.meter_on = False r = shape("C", dur=1400, events={940: silent}) caps = [x["t"] for x in r["rec"].rows if x["t"] >= 940 and x["cap"] is not None] w = after(r, 940) check(f"T22: the meter silent from 940 s, the step at 960: no cut sized " f"({caps[:2]}); only Solar's unknown rule acts, its pause at the " f"meter's own age 330 s ({w[:2]})", not caps and all(t >= 940 + 325 for t, _a, _ok in w)) # =========================================================================== # print("\n2. ONE CUT PER EPISODE, DEEPER ONLY ON A GROWING EXCESS (T23, T24)") # =========================================================================== # for lag in (0.0, 3.0): def slow(p, c, lag=lag): p.ch.ramp_w_s, p.ch.lag_s = 700.0, lag # The 30 s gate is closed at the step (a write at 908), so Solar's own # sun rule cannot lower the car first: only the cut can. r = drive(step_fn((910, 8300.0), (970, 900.0), base=900.0), const(13000.0), dur=1100, first="car", phases=3, socs={"a": 10.0, "b": 10.0}, opts={"ev_phases": "3"}, events={0: slow, 908: stamp}) low = [(t, a) for t, a, _ in after(r, 905) if t < 938 and a < 16.0] ep = [x for x in r["rec"].rows if 905 <= x["t"] < 970 and x["cap"] is not None] want = _cutamps()(ep[0]["car0"], ep[0]["ex"], 3) if ep else None check(f"T23 [lag {lag:.0f} s]: the car at 16 A on 3 phases (ramp 700 " f"W/s), the house +7400 W at 910: exactly one cut write in the " f"episode ({low}), sized from its start ({want} A)", len(low) == 1 and low[0][1] == want) never(f"T23 [lag {lag:.0f}]", r) # T24: a growing excess in one episode: the second, deeper cut is measured # from the episode's start (car0 less the largest excess), never from the # lowered car. r = drive(step_fn((960, 900.0 + 5700.0), (966, 900.0 + 9000.0), base=900.0), const(13000.0), dur=1100, first="car", phases=3, socs={"a": 10.0, "b": 10.0}, opts={"ev_phases": "3"}, events={958: stamp}) rows24 = [x for x in r["rec"].rows if 958 <= x["t"] <= 980] caps = [] for x in rows24: if x["cap"] is not None and (not caps or x["cap"] != caps[-1][1]): caps.append((x["t"], x["cap"], x["ex"], x["car0"])) w = [(t, a) for t, a, _ in after(r, 958) if t <= 980 and a < 16.0] ok_from_start = all(cp == _cutamps()(c0, ex, 3) for _t, cp, ex, c0 in caps) check(f"T24: two cuts as the excess grows ({caps}); each is " f"floor((car0 - largest excess) / 690), never re-subtracted from the " f"lowered car; writes {w}", len(caps) == 2 and caps[1][1] < caps[0][1] and ok_from_start and [a for _t, a in w] == [cp for _t, cp, _e, _c in caps]) # =========================================================================== # print("\n3. THE RESTART (T25, T26, T41)") # =========================================================================== # sun25 = step_fn((960, 0.0), (990, 9000.0), base=7900.0) r = drive(step_fn((960, 3000.0), base=900.0), sun25, dur=1500, first="car", phases=3, socs={"a": 10.0, "b": 10.0}, opts={"ev_phases": "3"}) cut_t = zero_at(r, 960) und = [x["under"] for x in r["rec"].rows if x["t"] > 960 and x["under"] is not None] t_under = und[0] if und else None nz = nonzero_after(r, 961) lo = max(t_under + 120.0, cut_t + 120.0) if t_under and cut_t else None check(f"T25/OQ6: the cut at {cut_t}; the projection with the start power " f"under the aim from {t_under}; the first non-zero write {nz[:1]} not " f"before {lo} and within 10 s of it", bool(nz) and lo is not None and lo <= nz[0][0] <= lo + 10) never("T25", r) house26 = step_fn((960, 30000.0), (1020, 900.0), base=900.0) sun26 = step_fn((960, 0.0), (1020, 9000.0), base=7900.0) r = drive(house26, sun26, dur=1700, first="car", phases=3, socs={"a": 10.0, "b": 10.0}, opts={"ev_phases": "3"}) over_nz = [(t, a) for t, a in nonzero_after(r, 961) if (r["rec"].at(t) or {}).get("proj", 0) >= AIM] und = [x["under"] for x in r["rec"].rows if x["t"] > 1020 and x["under"] is not None] nz = nonzero_after(r, 961) check(f"T26: the projection stays over long after the cut: no non-zero " f"write while it is over the aim ({over_nz[:2]}); the first {nz[:1]} " f"120 s or more after it went under with the start power " f"({und[:1]})", not over_nz and bool(nz) and bool(und) and nz[0][0] >= und[0] + 120) # T41: a cut, a restart, a second cut in one period; then no restart until # the next period (OQ4). sun41 = step_fn((960, 0.0), (990, 9000.0), (1400, 0.0), (1430, 9000.0), base=7900.0) r = drive(step_fn((960, 3000.0), base=900.0), sun41, dur=2100, first="car", phases=3, socs={"a": 10.0, "b": 10.0}, opts={"ev_phases": "3"}) w = [(t, a) for t, a, _ in after(r, 950)] cuts = [x["cuts"] or 0 for x in r["rec"].rows if 900 <= x["t"] < 1800] second = [t for t, a in w if 1400 <= t < 1800 and a == 0.0] re2 = [t for t, a in w if 1401 <= t < 1800 and a > 0.0] q = quarter_avg(r, 900) nxt = [t for t, a in w if 1800 <= t <= 1800 + 130 and a > 0.0] check(f"T41/OQ4: after the second cut the car comes back in the next " f"period, by the start rule ({nxt[:1]})", bool(nxt)) check(f"T41: writes {w[:6]}: a restart between the cuts, the second cut " f"within 1 cycle of 1400 ({second[:1]}), {max(cuts, default=0)} cuts in the " f"period, no restart after it until 1800 ({re2[:1]}); the period " f"{q:.0f} W <= aim, nothing banked ({r['c'].car_peak_q})", any(960 < t < 1400 and a > 0 for t, a in w) and second and second[0] <= 1401 and max(cuts, default=0) == 2 and not re2 and q <= AIM and not r["c"].car_peak_q) never("T41", r) # =========================================================================== # print("\n4. SOLAR CANNOT CUT: AS OFF (T29-T33, C2)") # =========================================================================== # # C2, the named bug: every cannot-cut Solar case gave the car's figure to the # alarm path at c79eb37; Off gives None. r = shape("C", dur=950) c = r["c"] cases = [] for name, on, off in ( ("shadow", lambda: c.o.__setitem__("shadow_mode", "on"), lambda: c.o.__setitem__("shadow_mode", "off")), ("Stop", lambda: setattr(c, "auto", False), lambda: setattr(c, "auto", True)), ("circuit latch", lambda: setattr(c.circ_wd, "tripped", True), lambda: setattr(c.circ_wd, "tripped", False)), ("shutting down", lambda: setattr(c, "stopping", True), lambda: setattr(c, "stopping", False)), ("another writer", lambda: setattr(c.car_solar, "mismatch_since", 1.0), lambda: setattr(c.car_solar, "mismatch_since", None))): on() cases.append((name, c.car_alarm_w())) off() base_w = c.car_alarm_w() check(f"C2: in every cannot-cut Solar case car_alarm_w is None, as Off " f"({cases}); while Solar can cut it is the car's figure ({base_w})", all(v is None for _n, v in cases) and base_w is not None) # T29: another writer sets 16 A; the house steps 5 s later. for T in (950,): ev = {T: lambda p, c: p.ch.set_current(16)} kw = dict(dur=1300, step=T + 5) r = shape("C", events=ev, **kw) o = twin("C", events=ev, **kw) mism = [x["t"] for x in r["rec"].rows if x["t"] >= T and x["mism"] is not None] ex = [x["t"] for x in r["trace"] if x["t"] >= T and x["explains"]] off_alarm_like(f"T29 [other writer at {T}]", r, o, T) check(f"T29: car_explains False from the first cycle of the difference " f"({mism[:1]}, explains {ex[:1]}); no Solar write over it " f"({after(r, T)})", bool(mism) and mism[0] <= T + 1 and not ex and not after(r, T)) # EV-05 r8 M2: another writer starts the car soon after Solar's own 0 A. sun_p = step_fn((700, 900.0), base=7900.0) ev = {760: lambda p, c: p.ch.set_current(16)} r = drive(step_fn((765, 9000.0), base=900.0), sun_p, dur=1000, first="car", phases=3, socs={"a": 10.0, "b": 10.0}, opts={"ev_phases": "3"}, events=ev) o = drive(step_fn((765, 9000.0), base=900.0), sun_p, dur=1000, first="car", phases=3, socs={"a": 10.0, "b": 10.0}, opts={"ev_phases": "3"}, events=ev, mode="off", setpoint=10) off_alarm_like("T29 [r8 M2: another writer after Solar's own 0 A]", r, o, 760) # T30: the cut refused ("error", "ignore"); retried only as 0 A, 60 s apart, # 3 tries at most; lifted before the 3rd: taken, and graded from then. for kind in ("error", "ignore"): def refuse(p, c, kind=kind): p.ch.refuse = kind def lift(p, c): p.ch.refuse = None r = shape("C", dur=1300, events={959: refuse, 1050: lift}) o = twin("C", dur=1300) # Graded out only in the cut's own cycle (nothing written yet); from the # cycle after its write, as Off until the read-back shows it. off_alarm_like(f"T30 [{kind}]", r, o, 955, ex_from=961) at = attempts(r, 959) gaps = [b[0] - a[0] for a, b in zip(at, at[1:])] rb0 = [x["t"] for x in r["trace"] if x["t"] > 960 and x["rb"] == 0.0] fl = [(x["t"], x["floor"], y["floor"]) for x, y in zip(r["trace"], o["trace"]) if 961 <= x["t"] < (rb0[0] if rb0 else 1300) and abs((x["floor"] or 0.0) - (y["floor"] or 0.0)) > 50.0] can_back = [x["t"] for x in r["rec"].rows if x["t"] >= (rb0[0] if rb0 else 1e9) and x["can"]] check(f"T30 [{kind}]: tries {at}: only 0 A, at least 60 s apart " f"({gaps}), at most 3; after the lift taken (read back 0 at " f"{rb0[:1]}), graded from that cycle ({can_back[:1]})", at and all(v == 0.0 for _t, v in at) and len(at) <= 3 and all(g >= 60 for g in gaps) and bool(rb0) and bool(can_back) and can_back[0] <= rb0[0] + 1) check(f"T30 [{kind}]: until the read-back shows the cut, the guard's " f"floor is the Off twin's within 50 W ({fl[:2]})", not fl) # T31: a late read-back (12 s): as Off until it shows, can cut from then. def lag12(p, c): p.ch.lag_s = 12.0 r = shape("C", dur=1200, events={950: lag12}) o = twin("C", dur=1200) rb0 = [x["t"] for x in r["trace"] if x["t"] > 960 and x["rb"] == 0.0] early = [x["t"] for x in r["rec"].rows if 962 <= x["t"] < (rb0[0] if rb0 else 0) and x["can"]] back = [x["t"] for x in r["rec"].rows if rb0 and x["t"] >= rb0[0] and x["can"]] check(f"T31: a cut read back 12 s late (at {rb0[:1]}): not graded before " f"({early[:2]}), can cut from the first cycle that shows it " f"({back[:1]}); car_peak_q {r['c'].car_peak_q}", bool(rb0) and not early and bool(back) and back[0] <= rb0[0] + 1 and r["c"].car_peak_q == {}) # T32: not writing (shadow, Stop, circuit latch), then another writer's 16 A # after Solar's way-out 0 A, then the house step (EV-05 r6). for name, fn, opts in (("Stop", lambda p, c: setattr(c, "auto", False), {"shadow_mode": "auto"}), ("shadow on", lambda p, c: c.o.__setitem__( "shadow_mode", "on"), {}), ("circuit latch", lambda p, c: setattr( c.circ_wd, "tripped", True), {})): ev = {930: fn, 940: lambda p, c: p.ch.set_current(16)} r = shape("C", dur=1300, events=ev, opts=opts) o = twin("C", dur=1300, events=ev, opts=opts) off_alarm_like(f"T32 [{name}]", r, o, 935) # T33: a charger fault (mode3 E) and a stuck reading while a cut is due. for name, fn in (("mode3 E", lambda p, c: p.ch.fault("E")), ("stuck reading", lambda p, c: p.ch.freeze())): r = shape("C", house_w=9000.0, dur=1300, events={955: fn}) o = replay_twin(r, "C", house_w=9000.0, dur=1300, events={955: fn}) flt = [x["t"] for x in r["rec"].rows if x["fault"] is not None] off_alarm_like(f"T33 [{name}]", r, o, 950, ex_from=flt[0] if flt else 950, extra=f"; Solar's fault from {flt[:1]}") # =========================================================================== # print("\n5. PHASES: A CUT NEVER SELECTS, A PHASE GAP NEVER BLOCKS IT (T34)") # =========================================================================== # # (i) Solar's own 1 -> 3 select, then a partial cut 140 s later. kw = dict(dur=700, first="car", phases=1, socs={"a": 10.0, "b": 10.0}, opts={"ev_phases": "auto"}) r0 = drive(const(900.0), const(9280.0), **kw) sel = [x[0] for x in r0["sim"].log if x[1] == "phases"] t0 = int(sel[0]) if sel else 400 ts = t0 + 140 kw["dur"] = ts + 60 r = drive(step_fn((ts, 900.0 + 4100.0), base=900.0), const(9280.0), **kw) w = [(t, a) for t, a, _ in after(r, ts)] ph = [x for x in r["sim"].log if x[1] == "phases" and x[0] >= ts] ep = [x for x in r["rec"].rows if x["t"] >= ts and x["cap"] is not None] check(f"T34 (i): Solar's select at {t0}, a partial cut at {ts}: written " f"within 1 cycle ({w[:2]}; cap {ep[0]['cap'] if ep else None}), the " f"600 s gap does not block it, no select in the cut ({ph})", bool(w) and w[0][0] <= ts + 1 and 6.0 <= w[0][1] < 12.0 and not ph) # (ii) Another writer selects 3 phases while Solar's plan says 1. kw = dict(dur=1100, first="car", phases=1, socs={"a": 10.0, "b": 10.0}, opts={"ev_phases": "auto"}) ev = {940: lambda p, c: p.ch.set_phases(3)} r = drive(step_fn((960, 3000.0), base=900.0), step_fn((960, 0.0), base=900.0 + 2300.0 + 100.0), events=ev, **kw) w = [(t, a) for t, a, _ in after(r, 960)] rec960 = r["rec"].at(960) or {} check(f"T34 (ii): another writer's 3 phases under Solar's 10 A: within 1 " f"cycle the cut at 690 W/A ({w[:1]}) or Solar unable to cut (can " f"{rec960.get('can')}, explains {rec960.get('explains')}); never " f"graded out without a write", (bool(w) and w[0][0] <= 961) or (not rec960.get("explains"))) # (iii) Formal C12: the cut comes DURING Solar's own 1 -> 3 change (the car # held at its 0 A, the select out or not yet): the change ends at that 0 A, # no closing current goes out into the excess, no select after the cut. log0 = [x for x in r0["c"].car_solar.log] sel_t = [t for k, a, t, ok in log0 if k == "phases"] zero_b = [t for k, a, t, ok in log0 if k == "current" and a == 0.0 and sel_t and t <= sel_t[0]] tb = int(zero_b[-1]) if zero_b else 400 kw = dict(dur=tb + 120, first="car", phases=1, socs={"a": 10.0, "b": 10.0}, opts={"ev_phases": "auto"}) r = drive(step_fn((tb + 3, 9280.0 + 3400.0 + 2000.0), base=900.0), const(9280.0), **kw) w = [(t, a) for t, a, _ in after(r, tb + 3)] sel_after = [x for x in r["sim"].log if x[1] == "phases" and x[0] > tb + 4] caps = [x["t"] for x in r["rec"].rows if x["t"] >= tb + 3 and x["cap"] is not None] check(f"T34 (iii) / C12: Solar's own change began at {tb}; the house heads " f"the period over at {tb + 3}: the cut is due at {caps[:1]}, no closing " f"current goes out ({w[:2]}), no select after it ({sel_after[:1]})", bool(caps) and caps[0] <= tb + 4 and not [a for _t, a in w if a > 0.0] and not sel_after) never("T34 (iii)", r) # =========================================================================== # print("\n6. THE COVER OFF (T36) AND THE PERIOD'S EDGE (T39, T40)") # =========================================================================== # for kind in ("M", "C"): r = shape(kind, cover=False, dur=1300) q = quarter_avg(r, 900) w = after(r, 960) over = [x["t"] for x in r["rec"].rows if 960 <= x["t"] < 1000 and (x["proj"] or 0) > AIM and x["cap"] is None and x["car"] > 100] check(f"T36 [{kind}, cover off]: the cut first, at once where it is due " f"({w[:2]}; uncut seconds over the aim with the car {over[:3]}); " f"the period {q:.0f} W", not over and (q <= AIM or kind == "M")) for n in (2, 1): # The old period heads over at 1700 (100 s left: at once); the house is # back to 900 W at 1790, so the new period starts under the aim. r = near_end(1700, n=n, house_after=9000.0, sun_back=1810, dur=2100, house_more=((1790, 900.0),)) w = [(t, a) for t, a, _ in after(r, 1695)] cut = [t for t, a in w if a == 0.0] nz = [t for t, a in w if a > 0.0] new_q = [x["t"] for x in r["rec"].rows if 1800 <= x["t"] < 1930 and x["cap"] is not None] check(f"T39 [N={n}]: the old period's cut at {cut[:1]}; none at or " f"after :15:00 for the old excess (cut state in the new period " f"{new_q[:1]}); the restart by the start rule only (sun back at " f"1810, start at {nz[:1]}) ({w[:4]})", bool(cut) and cut[0] <= 1701 and len(cut) == 1 and not new_q and bool(nz) and nz[0] >= 1810 + 120) for n in (2, 1): ev = {t: refuse_n1 for t in range(0, 1901)} if n == 1 else {} r = drive(step_fn((1750, 3000.0), base=900.0), step_fn((1750, 0.0), base=900.0 + 3680.0 + 100.0), dur=1900, first="car", phases=1, events=ev, rows=n == 2, ratings={"a": 5000.0, "b": 4600.0} if n == 2 else {"a": 5000.0}, socs={u: 10.0 for u in ("ab" if n == 2 else "a")}, opts={"ev_phases": "1"}) hold_s = r["c"].p1.averager.max_hold_s cut = zero_at(r, 1740) check(f"T40 [N={n}]: the old period stays under the aim; the new one " f"heads over: the cut in its first cycle ({cut}), at N=2 up to " f"max_hold_s ({hold_s:.0f} s) before the edge, never later", cut is not None and 1800 - (hold_s if n == 2 else 0) - 1 <= cut <= 1801) # =========================================================================== # print("\n7. ADD-ON RESTART (T42), UNTRUSTED CAR (T43), HA DOWN (T44)") # =========================================================================== # # (a) a cut at 960, the add-on restarts at 1000 on the same store and charger. r1 = shape("C", house_w=9000.0, dur=1000 - 0) sim, store = r1["sim"], r1["store"] r2 = drive(step_fn((0, 9000.0), base=9000.0), const(0.0), t0=1000.0, dur=200, first="car", phases=3, socs={"a": 10.0, "b": 10.0}, opts={"ev_phases": "3"}, sim=sim, store=store, plug=False) w2 = [(t, a) for t, a, _ in after(r2, 1000)] keys = [k for k in store.data if "cut" in str(k).lower()] check(f"T42 (a): after a restart in an over period, D2's 0 A first and no " f"non-zero write ({w2[:3]}); no store key carries a cut ({keys})", (not w2 or w2[0][1] == 0.0) and not [a for _t, a in w2 if a > 0] and not keys) # (b) a stop at :14:30, back at :15:30, the new period under the aim. r1 = shape("C", step=1770, dur=1770) r2 = drive(const(900.0), const(7900.0), t0=1830.0, dur=120, first="car", phases=3, socs={"a": 10.0, "b": 10.0}, opts={"ev_phases": "3"}, sim=r1["sim"], store=r1["store"], plug=False) caps = [x["t"] for x in r2["rec"].rows if x["cap"] is not None] check(f"T42 (b): back in a new period under the aim: no cut ({caps[:1]})", not caps) # T43: a car figure the energy meter cannot corroborate (no energy entity): # a cut due writes 0 A (OQ2); the alarm as Off. def no_energy(p, c): p.ch.no_energy = True H9 = 9000.0 r = shape("C", house_w=H9, dur=1100, events={950: no_energy}) o = twin("C", house_w=H9, dur=1100, events={950: no_energy}) w = after(r, 960) check(f"T43: an untrusted car figure: the cut is 0 A ({w[:1]}); the alarm " f"at the Off twin's second +2 ({first_alarm(r, 900)} vs " f"{first_alarm(o, 900)})", bool(w) and w[0][1] == 0.0 and first_alarm(r, 900) is not None and first_alarm(r, 900) - first_alarm(o, 900) <= 2) def ha(on): def f(p, c): p.ha_down = on return f r = shape("C", dur=1200, events={960: ha(True), 1020: ha(False)}, rows=False) during = attempts(r, 960) during = [x for x in during if x[0] < 1020] back = [(t, a) for t, a, _ in after(r, 1020)] check(f"T44: Home Assistant down 60 s from the step: nothing tried while " f"down ({during}); back: 0 A first, never higher ({back[:2]})", not during and bool(back) and back[0][1] == 0.0) # =========================================================================== # print("\n8. A CAR THAT DOES NOT FOLLOW (T45) AND THE FLOOR AFTER A CUT (T46)") # =========================================================================== # def freeze_p(p, c): p.ch.freeze_power() r = shape("C", house_w=9000.0, dur=1200, events={959: freeze_p}) o = twin("C", house_w=9000.0, dur=1200) a_s, a_o = first_alarm(r, 900), first_alarm(o, 900) check(f"T45: the read-back says 0 A, the power stays: the alarm no later than " f"the Off twin + 62 s ({a_s} vs {a_o})", a_s is not None and a_o is not None and a_s - a_o <= 62) r = shape("W", dur=1100) z = zero_at(r, 960) rec = r["rec"] dec = (rec.at(z) or {}) if z is not None else {} rb = [x["t"] for x in r["trace"] if z and x["t"] > z and x["rb"] == 0.0] car_b = (rec.at(z - 1) or {}).get("car", 0.0) if z is not None else 0.0 lim = None bad = [] if rb: car_a = (rec.at(rb[0] + 5) or {}).get("car", 0.0) lim = max(0.0, (dec.get("want") or 0.0) - (car_b - car_a)) + 150.0 # A cycle whose battery reading moved since the last one reads the # battery a cycle before the meter (want = battery + excess): a skew of # the measurement, not a floor raised for the car; those are skipped. tb = {x["t"]: x["batt"] for x in r["trace"]} bad = [(x["t"], round(x["want"] or 0)) for x in rec.rows if rb[0] + 5 <= x["t"] <= rb[0] + 60 and (x["want"] or 0) > lim and abs(tb.get(x["t"], 0.0) - tb.get(x["t"] - 1, 0.0)) < 50.0] check(f"T46: from 5 s after the cut's read-back the guard wants no more than " f"its want at the cut less what the car gave back, +150 W ({lim}; " f"{bad[:3]})", bool(rb) and not bad) # =========================================================================== # print("\n9. THE WALL CLOCK JUMPS (T48)") # =========================================================================== # def jump(dt): def f(p, c): p.utc_off = dt return f r0 = shape("W", dur=1100) r = shape("W", dur=1100, events={965: jump(-3600.0)}) check(f"T48 [-3600 s during the wait]: the 15 s wait runs on the monotonic " f"clock: the cut at the same second ({zero_at(r, 960)} vs " f"{zero_at(r0, 960)})", zero_at(r, 960) == zero_at(r0, 960) and zero_at(r0, 960) is not None) r = shape("W", dur=1100, events={965: jump(3600.0)}) blind = [x["t"] for x in r["rec"].rows if x["t"] >= 965 and x["cap"] is not None] z = zero_at(r, 960) check(f"T48 [+3600 s during the wait]: the wall clock jumps into a period " f"the meter has not covered, so the guard is blind " f"({r['c'].peak_guard.blind!r:.40}); no cut is sized from that " f"projection (N9; cut state {blind[:1]}, writes {after(r, 965)[:2]})", r["c"].peak_guard.blind is not None and not blind and not [a for _t, a, _ok in after(r, 965) if a > 6.0]) for dt in (3600.0, -3600.0): r0 = near_end(1700) r = near_end(1700, events={1702: jump(dt)}) z, z0 = zero_at(r, 1700), zero_at(r0, 1700) up = [(t, a) for t, a, _ok in after(r, 1701) if a > 0.0] check(f"T48 [{dt:+.0f} s just after a near-end cut]: the cut at the same " f"second ({z} vs {z0}), never undone by the jump ({up[:2]})", z == z0 and z is not None and not [t for t, a in up if t < 1800]) tb = LOG.tracebacks(n_log0) check(f"N11: no traceback in this file's runs ({tb[:1]})", not tb) print(f"\n{'FAILED: ' + str(len(fails)) if fails else 'ALL PASSED'}") sys.exit(1 if fails else 0)