|
|
|
@@ -24,63 +24,6 @@ def check(name, cond):
|
|
|
|
|
fails.append(name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
# COVERAGE AUDIT - measured, not executed. Read this before adding a mechanism.
|
|
|
|
|
#
|
|
|
|
|
# THE INVARIANT: every mechanism in compute() must be noticed by AT LEAST TWO
|
|
|
|
|
# checks when it is deleted. If you add a mechanism to compute(), re-run the
|
|
|
|
|
# audit and add it to the table. If a figure here drops, a check has started
|
|
|
|
|
# passing for a reason other than the one it names.
|
|
|
|
|
#
|
|
|
|
|
# THE TECHNIQUE, because there is no script to run: replace one mechanism in
|
|
|
|
|
# control.py with a no-op, run this file, count the failures, restore. That is
|
|
|
|
|
# the converse of the usual mutation - not "does a wrong value fail?" but "does
|
|
|
|
|
# anyone notice when the mechanism is GONE?". It is kept as a comment rather
|
|
|
|
|
# than as tooling on purpose: the only cheap way to automate it is to key on
|
|
|
|
|
# source lines, which goes stale silently, and a green audit that has quietly
|
|
|
|
|
# stopped testing anything is precisely the failure this ticket exists to fix.
|
|
|
|
|
# A comment cannot go stale-green, because it never claims to be running.
|
|
|
|
|
#
|
|
|
|
|
# Measured at 389d9ec. Numbers are the lead's independent reproduction.
|
|
|
|
|
#
|
|
|
|
|
# mechanism in compute() checks that fail when deleted
|
|
|
|
|
# ------------------------------------------ -----------------------------
|
|
|
|
|
# integrator freeze (AC 3) 2
|
|
|
|
|
# integrator clamp (AC 1) 6
|
|
|
|
|
# integrator bound follows max_w 4
|
|
|
|
|
# output clamp 3
|
|
|
|
|
# slew limit 4
|
|
|
|
|
# output freeze 2
|
|
|
|
|
# deadband 5
|
|
|
|
|
# quantisation 2
|
|
|
|
|
# saturation detector, `saturated_now = False` 11
|
|
|
|
|
# saturation duration (AC 2), fires instantly 2
|
|
|
|
|
# sat counter reset on a good cycle 6
|
|
|
|
|
# target_grid_w bias 3
|
|
|
|
|
# i_w=None seeding from prev_w 6
|
|
|
|
|
#
|
|
|
|
|
# The detector figure is for the `saturated_now = False` form specifically;
|
|
|
|
|
# disabling it further down as `frozen = False` is a weaker mutation and gives
|
|
|
|
|
# 10. Reproduce the same form or the number will not match.
|
|
|
|
|
#
|
|
|
|
|
# ⚠️ IT HAS FOUND A DEAD MECHANISM TWICE, BOTH THE SAME WAY: a clamp standing in
|
|
|
|
|
# for the mechanism under test. Deleting the integrator freeze once failed
|
|
|
|
|
# NOTHING, because the fixtures sat at max_w 2000 and the integrator bound
|
|
|
|
|
# truncated a wound value back to exactly 2000 - the assertion passed on the
|
|
|
|
|
# clamp. The output clamp was masked the same way by the integrator bound.
|
|
|
|
|
# Hence: A FIXTURE MUST SIT CLEAR OF EVERY RAIL IT IS NOT TESTING. Where a test
|
|
|
|
|
# names one mechanism, make that mechanism the binding one (see TCLAMP and TF).
|
|
|
|
|
#
|
|
|
|
|
# ⚠️ RUN MUTATIONS WITH `python -B` AND CLEAR app/__pycache__. CPython
|
|
|
|
|
# invalidates a .pyc on (source mtime in whole seconds, source size), so a
|
|
|
|
|
# same-second rewrite that also preserves the file size reuses stale bytecode
|
|
|
|
|
# and the suite reports on code you are no longer running. It under-reported one
|
|
|
|
|
# mutation here as 2 where the true figure is 6. The error is one-directional -
|
|
|
|
|
# stale bytecode can only under-report - so every figure above is a lower bound
|
|
|
|
|
# at worst, and the two zeros ever recorded were both confirmed by fixing them
|
|
|
|
|
# and watching the count rise, which a caching artefact cannot do.
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
print("control law")
|
|
|
|
|
|
|
|
|
|
# Deadband: inside meter noise, hold exactly - do not drift.
|
|
|
|
@@ -98,22 +41,13 @@ check("proportional step (gain 0.6)", d.target_w == 300)
|
|
|
|
|
d = compute(prev_w=0, grid_w=-500, actual_w=0, tuning=T)
|
|
|
|
|
check("export drives charging", d.target_w == -300)
|
|
|
|
|
|
|
|
|
|
# Clamp.
|
|
|
|
|
# ⚠️ integrator_max_w is lifted clear of max_w so that the OUTPUT clamp is the
|
|
|
|
|
# mechanism under test. Left at the default the integrator bound truncates
|
|
|
|
|
# first, these two assertions pass on that alone, and deleting the output clamp
|
|
|
|
|
# fails nothing - the same masking that hid the integrator freeze.
|
|
|
|
|
TCLAMP = Tuning(max_w=2000, slew_w=5000, integrator_max_w=5000)
|
|
|
|
|
d = compute(prev_w=1900, grid_w=1000, actual_w=1900, tuning=TCLAMP)
|
|
|
|
|
# Clamp
|
|
|
|
|
d = compute(prev_w=1900, grid_w=1000, actual_w=1900, tuning=Tuning(max_w=2000, slew_w=5000))
|
|
|
|
|
check("clamped to max_w", d.target_w == 2000)
|
|
|
|
|
|
|
|
|
|
# Slew: from 0 with a huge error, no more than slew_w in one cycle.
|
|
|
|
|
d = compute(prev_w=0, grid_w=5000, actual_w=0, tuning=Tuning(max_w=5000, slew_w=1000))
|
|
|
|
|
check("slew limits one cycle", d.target_w == 1000)
|
|
|
|
|
d = compute(prev_w=-1900, grid_w=-1000, actual_w=-1900, tuning=TCLAMP)
|
|
|
|
|
check("clamped to -max_w", d.target_w == -2000)
|
|
|
|
|
d = compute(prev_w=0, grid_w=-5000, actual_w=0, tuning=Tuning(max_w=5000, slew_w=1000))
|
|
|
|
|
check("slew limits one cycle, charging", d.target_w == -1000)
|
|
|
|
|
|
|
|
|
|
# Saturation needs DURATION: one diverging cycle must NOT freeze.
|
|
|
|
|
t = Tuning(saturation_w=500, saturation_cycles=3)
|
|
|
|
@@ -152,12 +86,12 @@ RUNAWAY_CYCLES = 150
|
|
|
|
|
HISTORICAL_W = 14768.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def runaway(tuning, sign=1):
|
|
|
|
|
def runaway(tuning):
|
|
|
|
|
"""Inverter off: it reports 0 W forever, the error never clears."""
|
|
|
|
|
prev, i_w, sat = 0.0, 0.0, 0
|
|
|
|
|
worst_i, worst_cmd = 0.0, 0.0
|
|
|
|
|
for _ in range(RUNAWAY_CYCLES):
|
|
|
|
|
d = compute(prev_w=prev, grid_w=sign * RUNAWAY_ERROR, actual_w=0.0,
|
|
|
|
|
d = compute(prev_w=prev, grid_w=RUNAWAY_ERROR, actual_w=0.0,
|
|
|
|
|
tuning=tuning, sat_count=sat, i_w=i_w)
|
|
|
|
|
prev, i_w, sat = d.target_w, d.i_w, d.sat_count
|
|
|
|
|
worst_i = max(worst_i, abs(i_w))
|
|
|
|
@@ -179,12 +113,6 @@ check(f"runaway with the detector defeated: integrator still bounded ({wi:.0f} W
|
|
|
|
|
wi <= TD.max_w)
|
|
|
|
|
check("runaway with the detector defeated: command still <= max_w", wc <= TD.max_w)
|
|
|
|
|
|
|
|
|
|
# The mirror: the same runaway driving the other way. An export that never
|
|
|
|
|
# clears winds the integrator negative just as hard.
|
|
|
|
|
wi, wc = runaway(Tuning(max_w=2000), sign=-1)
|
|
|
|
|
check(f"runaway (export direction): integrator bounded at {wi:.0f} W", wi <= 2000)
|
|
|
|
|
check("runaway (export direction): emitted command <= max_w", wc <= 2000)
|
|
|
|
|
|
|
|
|
|
# The bound is a separate quantity, and the useful direction is BELOW max_w:
|
|
|
|
|
# there it binds first and caps unwind latency tighter than the rail does.
|
|
|
|
|
d = compute(prev_w=0, grid_w=6000, actual_w=0,
|
|
|
|
@@ -193,22 +121,11 @@ check("integrator bound binds independently of the output clamp",
|
|
|
|
|
d.i_w == 1000 and d.target_w == 1000)
|
|
|
|
|
|
|
|
|
|
# Freeze = may not wind further in the direction it is already pushing.
|
|
|
|
|
# ⚠️ max_w is raised WELL above the fixtures on purpose. At the default 2000
|
|
|
|
|
# the integrator bound truncates a wound value back to exactly 2000 and
|
|
|
|
|
# satisfies these assertions on its own, so deleting the freeze outright
|
|
|
|
|
# failed nothing - the clamp was standing in for the mechanism under test.
|
|
|
|
|
# Any fixture here must sit clear of every rail, or it tests the rail.
|
|
|
|
|
TF = Tuning(saturation_w=500, saturation_cycles=3, max_w=5000)
|
|
|
|
|
TF = Tuning(saturation_w=500, saturation_cycles=3)
|
|
|
|
|
f1 = compute(prev_w=2000, grid_w=800, actual_w=0, tuning=TF, sat_count=3, i_w=2000.0)
|
|
|
|
|
check("frozen: integration does not wind further", f1.i_w == 2000.0 and f1.frozen)
|
|
|
|
|
f2 = compute(prev_w=2000, grid_w=-800, actual_w=0, tuning=TF, sat_count=3, i_w=2000.0)
|
|
|
|
|
check("frozen: unwinding is still allowed", f2.i_w < 2000.0)
|
|
|
|
|
# ...and the same two on the charging side. Every freeze rule in this file has
|
|
|
|
|
# a mirror, because the one that did not is the defect that got through review.
|
|
|
|
|
f3 = compute(prev_w=-2000, grid_w=-800, actual_w=0, tuning=TF, sat_count=3, i_w=-2000.0)
|
|
|
|
|
check("frozen (charging): integration does not wind further", f3.i_w == -2000.0)
|
|
|
|
|
f4 = compute(prev_w=-2000, grid_w=800, actual_w=0, tuning=TF, sat_count=3, i_w=-2000.0)
|
|
|
|
|
check("frozen (charging): unwinding is still allowed", f4.i_w > -2000.0)
|
|
|
|
|
|
|
|
|
|
# ⚠️ REGRESSION, and the reason the first cut of SAFETY-04 was rejected. A
|
|
|
|
|
# freeze encoded as "only corrections that shrink |i_w|" is unsatisfiable for
|
|
|
|
@@ -228,50 +145,6 @@ check("frozen at i_w=0: the freeze then clears", not z2.frozen)
|
|
|
|
|
z3 = compute(prev_w=100, grid_w=-1000, actual_w=800, tuning=T, sat_count=3, i_w=100.0)
|
|
|
|
|
check("frozen at i_w=+100: a 1 kW export still moves the command",
|
|
|
|
|
z3.frozen and z3.target_w < 0)
|
|
|
|
|
z4 = compute(prev_w=-100, grid_w=1000, actual_w=-800, tuning=T, sat_count=3, i_w=-100.0)
|
|
|
|
|
check("frozen at i_w=-100: a 1 kW import still moves the command",
|
|
|
|
|
z4.frozen and z4.target_w > 0)
|
|
|
|
|
|
|
|
|
|
# ⚠️ EXACTLY ZERO, BOTH DIRECTIONS. This boundary has a history: the first cut
|
|
|
|
|
# deadlocked here under import, and the fix for it deadlocked here under export
|
|
|
|
|
# because `if i_w > 0 ... else ...` files 0.0 under rising-only. main.py resets
|
|
|
|
|
# i_w to exactly 0.0 on every stop and every reseed, so it is a normal state,
|
|
|
|
|
# not a corner.
|
|
|
|
|
zi = compute(prev_w=0, grid_w=2000, actual_w=600, tuning=T, sat_count=3, i_w=0.0)
|
|
|
|
|
check("frozen at i_w=0.0: an import push moves the integrator",
|
|
|
|
|
zi.frozen and zi.i_w > 0)
|
|
|
|
|
ze = compute(prev_w=0, grid_w=-2000, actual_w=-600, tuning=T, sat_count=3, i_w=0.0)
|
|
|
|
|
check("frozen at i_w=0.0: an export push moves the integrator",
|
|
|
|
|
ze.frozen and ze.i_w < 0)
|
|
|
|
|
# The COMMAND still holds at 0 W in that second case, and that is release/1.0's
|
|
|
|
|
# rule, not a leftover: at prev_w == 0 the output freeze forbids starting to
|
|
|
|
|
# charge while saturated, because commanding 0 while the inverter reports
|
|
|
|
|
# hundreds of watts means something else is driving the bus. Asserted so that
|
|
|
|
|
# nobody "fixes" it by accident - the integrator moving is what this ticket
|
|
|
|
|
# owns, the command rule belongs to the output freeze.
|
|
|
|
|
check("frozen at i_w=0.0: the output freeze still blocks a charge from 0 W",
|
|
|
|
|
ze.target_w == 0.0)
|
|
|
|
|
# Where prev_w is already charging the output freeze does NOT block, and there
|
|
|
|
|
# the difference reaches the wire: held at 0.0 the integrator abandons the
|
|
|
|
|
# charge mid-export.
|
|
|
|
|
zc = compute(prev_w=-2000, grid_w=-4000, actual_w=-600, tuning=T, sat_count=3, i_w=0.0)
|
|
|
|
|
check("frozen at i_w=0.0: a charge is not abandoned during heavy export",
|
|
|
|
|
zc.target_w == -2000.0)
|
|
|
|
|
|
|
|
|
|
# The general property, rather than another handful of points: while frozen the
|
|
|
|
|
# integrator may be held ONLY when the correction would push it further from
|
|
|
|
|
# zero on the side it already sits. Any other hold is a deadlock.
|
|
|
|
|
stuck = []
|
|
|
|
|
for i0 in [x * 25.0 for x in range(-80, 81)]:
|
|
|
|
|
for g in [x * 100.0 for x in range(-40, 41)]:
|
|
|
|
|
err = g - T.target_grid_w
|
|
|
|
|
if abs(err) < T.deadband_w:
|
|
|
|
|
continue
|
|
|
|
|
dd = compute(prev_w=0.0, grid_w=g, actual_w=1500.0, tuning=T, sat_count=3, i_w=i0)
|
|
|
|
|
if dd.i_w == i0 and not ((i0 > 0 and err > 0) or (i0 < 0 and err < 0)):
|
|
|
|
|
stuck.append((i0, g))
|
|
|
|
|
check(f"frozen integrator never deadlocks, over {161*81} states"
|
|
|
|
|
+ (f" (e.g. {stuck[0]})" if stuck else ""), not stuck)
|
|
|
|
|
|
|
|
|
|
# False-positive guard: a normal 2 kW load step must not trip the detector,
|
|
|
|
|
# because the plant needs several cycles to catch up on every one of them.
|
|
|
|
@@ -323,67 +196,34 @@ print("SAFETY-04: the i_w=None path is still release/1.0, exactly")
|
|
|
|
|
|
|
|
|
|
def legacy(prev, grid, actual, t, sat_count):
|
|
|
|
|
"""release/1.0's control law, transcribed. Do not 'improve' this."""
|
|
|
|
|
reason = "tracking"
|
|
|
|
|
sc = min(sat_count + 1, 10) if abs(prev - actual) > t.saturation_w else 0
|
|
|
|
|
frozen = sc >= t.saturation_cycles
|
|
|
|
|
error = grid - t.target_grid_w
|
|
|
|
|
if abs(error) < t.deadband_w:
|
|
|
|
|
want, reason = prev, "deadband"
|
|
|
|
|
else:
|
|
|
|
|
want = prev + t.gain * error
|
|
|
|
|
want = prev if abs(error) < t.deadband_w else prev + t.gain * error
|
|
|
|
|
target = max(-t.max_w, min(t.max_w, want))
|
|
|
|
|
if target != want:
|
|
|
|
|
reason = "clamped"
|
|
|
|
|
slewed = max(prev - t.slew_w, min(prev + t.slew_w, target))
|
|
|
|
|
if slewed != target:
|
|
|
|
|
reason = "slew-limited"
|
|
|
|
|
target = slewed
|
|
|
|
|
target = max(prev - t.slew_w, min(prev + t.slew_w, target))
|
|
|
|
|
if frozen:
|
|
|
|
|
target = min(target, prev) if prev > 0 else max(target, prev)
|
|
|
|
|
reason = "saturated-freeze"
|
|
|
|
|
step = max(1, int(t.step_w))
|
|
|
|
|
return float(round(target / step) * step), sc, frozen, reason
|
|
|
|
|
return float(round(target / step) * step), sc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ⚠️ Compare EVERYTHING observable, not just the number. A previous version of
|
|
|
|
|
# this sweep compared (target_w, sat_count) only and passed 3024 cases while
|
|
|
|
|
# `reason` had silently lost a value - which is the kind of thing a sweep this
|
|
|
|
|
# broad exists to catch. `frozen` and `reason` are both in the tuple now.
|
|
|
|
|
#
|
|
|
|
|
# The one deliberate rename: what release/1.0 called "clamped" is now
|
|
|
|
|
# "i-clamped", because the truncation happens on the integrator before the
|
|
|
|
|
# command is derived from it. Aliased here rather than papered over - if any
|
|
|
|
|
# OTHER reason ever diverges, this check goes red.
|
|
|
|
|
ALIAS = {"i-clamped": "clamped"}
|
|
|
|
|
# Exhaustive over the interesting corners, both freeze states, both signs, and
|
|
|
|
|
# either side of the deadband. This is what makes the claim in control.py's
|
|
|
|
|
# integrator comment a checked fact rather than an assertion.
|
|
|
|
|
diffs = []
|
|
|
|
|
seen = set()
|
|
|
|
|
for tune in (Tuning(), Tuning(target_grid_w=-10.0), Tuning(max_w=5000, slew_w=5000)):
|
|
|
|
|
for prev in (-2000.0, -500.0, -100.0, 0.0, 100.0, 500.0, 2000.0):
|
|
|
|
|
for grid in (-6000.0, -1000.0, -500.0, -14.0, 0.0, 14.0, 500.0, 1000.0, 6000.0):
|
|
|
|
|
for actual in (-2000.0, 0.0, 600.0, 2000.0):
|
|
|
|
|
for sc in (0, 2, 3, 9):
|
|
|
|
|
d = compute(prev, grid, actual, tune, sc) # i_w defaults to None
|
|
|
|
|
seen.add(d.reason)
|
|
|
|
|
got = (d.target_w, d.sat_count, d.frozen,
|
|
|
|
|
ALIAS.get(d.reason, d.reason))
|
|
|
|
|
if got != legacy(prev, grid, actual, tune, sc):
|
|
|
|
|
diffs.append((prev, grid, actual, sc, got,
|
|
|
|
|
legacy(prev, grid, actual, tune, sc)))
|
|
|
|
|
check(f"i_w=None reproduces release/1.0 over {3*7*9*4*4} cases, reason included"
|
|
|
|
|
lt, lsc = legacy(prev, grid, actual, tune, sc)
|
|
|
|
|
if (d.target_w, d.sat_count) != (lt, lsc):
|
|
|
|
|
diffs.append((prev, grid, actual, sc, d.target_w, lt))
|
|
|
|
|
check(f"i_w=None reproduces release/1.0 over {3*7*9*4*4} cases"
|
|
|
|
|
+ (f" (first diff {diffs[0]})" if diffs else ""), not diffs)
|
|
|
|
|
|
|
|
|
|
# ...and the rename is not a quiet deletion: the signal SAFETY-03 alarms on has
|
|
|
|
|
# to actually occur in that sweep, or its hook is dead.
|
|
|
|
|
check("the integrator clamp reports itself as 'i-clamped'", "i-clamped" in seen)
|
|
|
|
|
|
|
|
|
|
# "clamped" stays reachable, but only where the integrator is deliberately
|
|
|
|
|
# allowed above the rail - then BOTH fire and the output clamp, which describes
|
|
|
|
|
# the value actually emitted, is the one reported.
|
|
|
|
|
dc = compute(prev_w=0, grid_w=6000, actual_w=0,
|
|
|
|
|
tuning=Tuning(max_w=2000, integrator_max_w=3000, slew_w=5000))
|
|
|
|
|
check("the output clamp still reports 'clamped' when it is the binding one",
|
|
|
|
|
dc.reason == "clamped" and dc.i_w == 3000 and dc.target_w == 2000)
|
|
|
|
|
|
|
|
|
|
print("capacity tariff")
|
|
|
|
|
check("no forecast means no cap", maintenance_charge_floor(2500, None, 3500) == 2500)
|
|
|
|
|
check("headroom caps the charge", maintenance_charge_floor(2500, 2000, 3500) == 1500)
|
|
|
|
|