"""Exact, synthetic same-horizon energy accounting; no disk control or measurements.""" from fractions import Fraction from pathlib import Path import hashlib import json import sys def nonnegative(value): if isinstance(value, bool) or not isinstance(value, (int, str, Fraction)): raise ValueError("Use an integer, rational string or Fraction") try: result = Fraction(value) except (ValueError, ZeroDivisionError) as error: raise ValueError("Expected a finite rational") from error if result < 0: raise ValueError("Expected a nonnegative value") return result def cycle(high_w, low_w, transition_j, transition_s, dwell_s, wait_s=0): """Both alternatives begin/end ready over the same window; no intervening I/O.""" high, low, energy, duration, dwell, wait = map( nonnegative, (high_w, low_w, transition_j, transition_s, dwell_s, wait_s) ) if high <= low: raise ValueError("The selected low state must use less power") horizon = wait + duration + dwell baseline = high * horizon switched = high * wait + energy + low * dwell excess = energy - high * duration return { "horizon_s": horizon, "baseline_j": baseline, "switched_j": switched, "saved_j": baseline - switched, "transition_excess_j": excess, "break_even_dwell_s": max(Fraction(0), excess / (high - low)), } def results(): return { "kind": "synthetic same-horizon energy worksheet", "generator_sha256": hashlib.sha256(Path(__file__).read_bytes()).hexdigest(), "inputs": {"high_w": 8, "low_w": 2, "transition_j": 100, "transition_s": 5, "wait_s": 30}, "rows": [ {"dwell_s": dwell, **{key: str(value) for key, value in cycle(8, 2, 100, 5, dwell, 30).items()}} for dwell in (5, 10, 20) ], } if __name__ == "__main__": target = Path(__file__).with_name("results.json") output = json.dumps(results(), indent=2) + "\n" if sys.argv[1:] == ["--write"]: target.write_text(output, encoding="utf-8") elif sys.argv[1:] == ["--check"]: if target.read_text(encoding="utf-8") != output: raise SystemExit("Published results differ from the current generator") elif not sys.argv[1:]: print(output, end="") else: raise SystemExit("Usage: model.py [--write|--check]")