#!/usr/bin/env python3
"""Torn-verify for 013 — The DJ Has No Backside. Pure Python."""

from math import cos, pi, sqrt

FAILS = []


def check(name, condition, detail=""):
    print(("PASS" if condition else "FAIL") + "  " + name +
          (("  [" + detail + "]") if detail else ""))
    if not condition:
        FAILS.append(name)


BPM = 126
beat_hz = BPM / 60
omega = 2 * pi * beat_hz
rpm = BPM / 4
check("four beats turn the floppy exactly once",
      abs(rpm / 60 * 4 / beat_hz - 1) < 1e-15,
      f"{rpm:.1f} rpm at {BPM} bpm")

palette = ("red", "cyan", "gold", "violet")
check("the pom-pom colour is periodic on the four-beat bar",
      [palette[n % 4] for n in range(8)] ==
      ["red", "cyan", "gold", "violet"] * 2)


def response(w, w0, gamma, force=1):
    return force / sqrt((w0*w0 - w*w)**2 + (2*gamma*w)**2)


gamma = 0.34
on = response(omega, omega, gamma)
off = response(omega, 1.55 * omega, gamma)
check("the belly oscillator responds more strongly when tuned to the beat",
      on > 4 * off, f"on/off={on/off:.3f}")

# The sock displays a two-path interference intensity. It does not cause the slip.
samples = [0.5 + 0.5*cos(2*pi*n/64 + 0.7) for n in range(64)]
check("the quantum-sock interference intensity stays non-negative",
      min(samples) >= 0 and max(samples) <= 1,
      f"range=[{min(samples):.6f},{max(samples):.6f}]")

# Banana mechanics: the declared stage slope exceeds static friction.
mu_static, stage_tan = 0.08, 0.31
check("the banana, not the quantum pattern, crosses the slip threshold",
      stage_tan > mu_static,
      f"tan(theta)={stage_tan:.2f} > mu={mu_static:.2f}")

# A free damped pom-pom must lose mechanical energy.
theta, velocity, dt = 0.7, 0.0, 0.001
energies = []
for step in range(12000):
    acceleration = -2*0.22*velocity - 4.8*theta
    velocity += acceleration * dt
    theta += velocity * dt
    if step % 100 == 0:
        energies.append(0.5*velocity*velocity + 0.5*4.8*theta*theta)
check("the unforced pom-pom is a damped pendulum",
      energies[-1] < 0.01 * energies[0],
      f"Efinal/E0={energies[-1]/energies[0]:.6f}")

print()
if FAILS:
    print(f"TORN-VERIFY: {len(FAILS)} recorded claim(s) no longer hold: {FAILS}")
    raise SystemExit(1)
print("TORN-VERIFY: all recorded claims for 013 still hold.")
