"""EV-20: Solar in shadow mode and beside another writer (T38-T46; N10, N16). `python3 -B test_ev20_writers.py` The real Controller on `tools/ev20_sun.py` (N batteries, the sun, the house and `chargersim.SimCharger` on one meter, a 1 s cycle on a model clock), plus unit rows for the pure parts; shared pieces in `tools/ev20_kit.py`. Each section names the T-ids and N-ids of reports/EV-20-test-plan.md it implements. "Std run": Car first, both batteries full, house 500 W, sun 2900 W: spare 2400 W, the car target 2300 W = 10 A on 1 phase, written 120 s after the start. Times are the cycle's second; `race` runs use the production order (read, the writer settles, the cycle). Mutating a module this file grades? Read MUTATION.md first (purge __pycache__). """ import asyncio import json 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")) from app import carmode, chargers as CH, ctl_car, homeview, messages # noqa: E402 from app import mqtt, validate, web, main as app_main # noqa: E402 from app.carmode import SolarPlan, amps_for, spare_w # noqa: E402 import ev20_sun as S # noqa: E402 from chargersim import SimCharger # noqa: E402 from ev20_kit import * # noqa: E402,F401,F403 from ev20_kit import LOG, check, fails, report # noqa: E402,F401 from named_absence import nth, present # noqa: E402,F401 from ev20_kit import (_dies, _err, _freeze_after_write, _shutdown, # noqa: E402,F401 _until) # =========================================================================== # print("\n7. SHADOW, ANOTHER WRITER (T38-T46)") # =========================================================================== # cloud = (lambda t: 600.0 + (5200.0 if (t // 400) % 3 == 0 else 1200.0 if (t // 400) % 3 == 1 else 2600.0)) for how, opts in (("on", {"shadow_mode": "on"}), ("auto, stopped", {"shadow_mode": "auto"})): ev = {0: lambda p, c: setattr(c, "auto", False)} \ if how != "on" else {} rs = std(3600, sun=cloud, opts=opts, events=ev) rt = std(3600, sun=cloud) sel_t = [x[0] for x in selects(rt)] near = (lambda t: any(st - 10.0 <= t <= st + 15.0 for st in sel_t)) diff = [(a["t"], (a["would"] or (0.0,))[0], b["a"]) for a, b in zip(rs["trace"], rt["trace"]) if (a["would"] or (0.0,))[0] != b["a"]] far = [x for x in diff if not near(x[0])] check(f"T38 [shadow {how}]: a 1 h cloudy run from the start: the charger " f"log is empty, and every cycle's 'would set' is the current of a " f"non-shadow twin, but within seconds of the twin's own phase " f"selects ({len(diff)} differ, {len(far)} away from a select: " f"{far[:3]}; selects {sel_t})", rs["sim"].log == [] and not far and len(diff) <= 20 * len(sel_t) and len(cur_writes(rt)) >= 3) # T39: another writer. for race in (False, True): tag = "race" if race else "step" n = LOG.mark() r39 = std(900, sun=const(600.0 + 1840.0), race=race, rows=True, events={300: lambda p, c: p.ch.set_current(13), 600: act("car_mode", "solar")}) tr39 = r39["trace"] held = next((x["t"] for x in tr39 if x["hold"]), None) other = [m for m in LOG.since(n, "WARNING") if "CHARGER_OTHER" in m] check(f"T39 [{tag}]: sim.set_current(13) at 300 s while Solar set 8 A: " f"the hold at 330 s, not 329 s (at {held}); no write over it", held is not None and 330 <= held <= 331 and not [x for x in cur_writes(r39) if 300 <= x[0] < 600]) check(f"T39 [{tag}]: the amber charger_other row and one warning until " f"Solar is pressed ({len(other)})", len(other) == 1 and "charger_other" in tr39[400]["amber"] and "charger_other" not in tr39[620]["amber"]) check(f"T39 [{tag}]: pressing Solar takes the charger as it is and lifts " f"the hold: Solar's own 8 A is written again " f"({cur_writes(r39)[-1:]})", cur_writes(r39)[-1][1] == 8.0 and 600 <= cur_writes(r39)[-1][0] <= 602 and not tr39[700]["hold"]) check("T39: the row's text in both languages", messages.row_charger_other_solar().nl != "" and "Solar" in str(messages.row_charger_other_solar()) and "Zon" in messages.row_charger_other_solar().nl) for name, fn in (("Stop", lambda p, c: setattr(c, "auto", False)), ("Off", act("car_mode", "off"))): rr = std(500, sun=const(600.0 + 1840.0), events={300: lambda p, c: p.ch.set_current(13), 310: fn}) check(f"T39 (b): {name} 10 s after the other writer, inside the 30 s: " f"the way-out 0 A is written ({cur_writes(rr)})", cur_writes(rr)[-1][1] == 0.0 and 310 <= cur_writes(rr)[-1][0] <= 311) n = LOG.mark() r39g = std(1500, sun=step_fn((400, 600.0 + 4600.0), (900, 600.0 + 1000.0), base=600.0 + 1840.0)) check("T39 guard: Solar's own writes, own 0 A pause and own phase sequence " "are never flagged (no hold, no CHARGER_OTHER)", not any(x["hold"] or x["mismatch"] is not None for x in r39g["trace"]) and not [m for m in LOG.since(n) if "CHARGER_OTHER" in m] and selects(r39g) and 0.0 in [a for _, a, _ in cur_writes(r39g)]) # T40: EV-05 r5 Major 3, the exits under the hold. EXITS = {"Stop": lambda p, c: setattr(c, "auto", False), "shadow on": lambda p, c: c.o.__setitem__("shadow_mode", "on"), "latch": lambda p, c: setattr(c.circ_wd, "tripped", True), "Off": act("car_mode", "off"), "charger error": _err} for name, fn in EXITS.items(): rr = std(1000, sun=const(600.0 + 2070.0), opts={"shadow_mode": "off"}, events={600: lambda p, c: p.ch.set_current(16), 700: fn}) check(f"T40 [{name}]: Solar at 9 A, another writer's 16 A at 600 s (hold " f"at 630 s), {name} at 700 s: nothing written after 600 s; the " f"charger still at 16 A ({cur_writes(rr)})", not [x for x in attempts(rr) if x[0] >= 600] and rr["sim"].setpoint_a == 16.0) # Review round 1, Major 3 (mutant mF): Solar watches another writer while it # does not write. Stop at 300 s (shadow_mode off), its 0 A refused 299-305 s; # another writer sets 16 A at 310 s: nothing written over it (N10, N16). simF = TSim(setpoint_a=0, windows=[(299.0, 305.0, "error")]) rF = std(600, sim=simF, opts={"shadow_mode": "off"}, events={300: lambda p, c: setattr(c, "auto", False), 310: lambda p, c: p.ch.set_current(16)}) lateF = [(t, v) for t, v in attempts(rF) if t >= 310] check(f"T40/T42 [Stop, its 0 A refused, another writer]: nothing written " f"over the 16 A, which stands ({attempts(rF)})", not lateF and simF.setpoint_a == 16.0 and rF["c"].car_solar.hold) # T41: the user changes Manual's values while Solar holds. r41 = std(1000, sun=const(600.0 + 2070.0), events={600: lambda p, c: p.ch.set_current(16), 700: act("car_current", 12), 710: act("car_phases", 3)}) check("T41: Manual's current and phases set while Solar holds: the hold is " "not lifted, nothing written over the 16 A", not [x for x in attempts(r41) if x[0] >= 600] and r41["sim"].setpoint_a == 16.0 and r41["c"].car_solar.hold) # T42: EV-05 r6 Major 2, shutdown after an exit and another writer. for name, fn, opts in (("shadow on", lambda p, c: c.o.__setitem__( "shadow_mode", "on"), {}), ("Stop (auto)", lambda p, c: setattr(c, "auto", False), {"shadow_mode": "auto"}), ("Stop (off)", lambda p, c: setattr(c, "auto", False), {"shadow_mode": "off"}), ("guard: writing", lambda p, c: None, {})): rr = std(600, opts=opts, finale=_shutdown, events={300: fn, 400: lambda p, c: p.ch.set_current(16)}) shut = [x for x in attempts(rr) if x[0] >= 599] check(f"T42 [{name}]: another writer's 16 A at 400 s, shutdown at 600 s: " f"no charger write at shutdown, 16 A stands ({shut})", shut == [] and rr["sim"].setpoint_a == 16.0 and (name != "guard: writing" or rr["c"].car_solar.hold)) # T43: a start in Solar with shadow on, or stopped, over another writer. for how, opts, ev in (("shadow on", {"shadow_mode": "on"}, {}), ("auto, stopped", {"shadow_mode": "auto"}, {0: lambda p, c: setattr(c, "auto", False)})): blk = {} ev = dict(ev) ev[250] = lambda p, c: blk.update(b=homeview.car_block(c, None)) rr = std(300, sim=SimCharger(setpoint_a=16), opts=opts, events=ev) check(f"T43 [{how}]: a start in Solar while another writer has the car at " f"16 A: nothing written; the card says what it would set " f"({blk.get('b', {}).get('why')!r})", rr["sim"].log == [] and "would" in blk.get("b", {}).get("why", "")) # T44: Stop, another writer, Start. r44 = std(900, rows=True, events={300: lambda p, c: setattr(c, "auto", False), 400: lambda p, c: p.ch.set_current(16), 600: lambda p, c: setattr(c, "auto", True)}) check(f"T44: Stop at 300 s (0 A taken), another writer's 16 A at 400 s, " f"Start at 600 s: Solar holds off, amber row, no write " f"({cur_writes(r44)})", [(t, a) for t, a, _ in cur_writes(r44)] == [(120.0, 10.0), (300.0, 0.0)] and "charger_other" in r44["trace"][700]["amber"]) # T45: EV-05 r7 Major 1, Solar to Off while not writing. T45 = { "a": ({"shadow_mode": "on"}, 16, {200: act("car_mode", "off")}), "b": ({"shadow_mode": "auto"}, 16, {0: lambda p, c: setattr(c, "auto", False), 200: act("car_mode", "off")}), "c": ({}, 0, {300: lambda p, c: c.o.__setitem__("shadow_mode", "on"), 400: lambda p, c: p.ch.set_current(16), 410: act("car_mode", "off")}), "d": ({}, 0, {300: lambda p, c: c.o.__setitem__("shadow_mode", "on"), 400: lambda p, c: p.ch.set_current(16), 600: act("car_mode", "off")}), } for k, (opts, sp, ev) in T45.items(): rr = std(800, sim=SimCharger(setpoint_a=sp), opts=opts, events=ev) late = [x for x in attempts(rr) if x[0] >= 300 or sp] check(f"T45 ({k}): Solar to Off while not writing: no write over the " f"other writer, the charger stays at 16 A ({late})", [x for x in late if not (k in "cd" and x == (300.0, 0.0))] == [] and rr["sim"].setpoint_a == 16.0) rr = std(400, sim=SimCharger(setpoint_a=16), opts={"shadow_mode": "on"}, events={200: act("car_mode", "manual")}) check("T45 guard: a shadow start, then Manual: nothing written", rr["sim"].log == []) # T46: EV-05 r8 Major 1, another writer's phases. r46 = run("T46", S.drive, step_fn((300, 700.0), (600, 900.0), base=500.0), const(3500.0), dur=900, first="car", phases=3, socs={"a": 10.0, "b": 10.0}, rows=True, events={140: lambda p, c: p.ch.set_phases(3), 300: lambda p, c: p.ch.set_phases(3)}) w46, s46 = cur_writes(r46), selects(r46) check(f"T46: Solar starts on 1 phase (select at the start), then another " f"writer selects 3 phases: while Solar runs on them it selects nothing " f"(a select back is EV-22's; the next start from the pause is on 1 " f"phase, Expected 5) ({[(x[0], x[2]) for x in s46]})", s46[:1] and s46[0][2] == 1 and not [x for x in s46 if 140 < x[0] < 700]) low = [x for x in w46 if 140 < x[0] < 700] check(f"T46 (a)(b): the arithmetic uses the 3 phases the car draws on, and a " f"lower current (the 6 A hold, the pause) is written within the 30 s " f"rule, never blocked by PHASE_GAP_S ({w46})", low and low[0][1] == 6.0 and low[0][0] <= 171 and [a for _, a, _ in low][-1:] == [0.0]) imp46 = sum(max(0.0, x["grid"]) for x in r46["trace"] if x["t"] > 140 and x["sp"] > 0) check(f"T46 (c): the car's grid import only within the hold rule " f"(<= 300 s of 6 A on 3 phases) ({imp46 / 3600:.0f} Wh)", imp46 <= 4140.0 * 302.0) r46h = std(900, sun=const(3500.0), phases=3, events={200: lambda p, c: p.ch.set_current(16), 300: lambda p, c: p.ch.set_phases(3)}) check("T46 (d)/N16: while EV-04's hold stands Solar writes nothing, a " "select included", not [x for x in attempts(r46h) if x[0] >= 200] and not [x for x in selects(r46h) if x[0] >= 200]) report()