"""灯りの虫: 体の軸を速度へ寄せる回転 k3 を弱める（拡張模型 B のまま）（2026-11-11 21:4x、判定は plan_posture.md に先に書いた）。
使い方（リポジトリ直下から）: uv run python ayumu-lab/research/insect-light/k3.py > ayumu-lab/research/insect-light/k3_20261111.json"""
import sys, os, json, subprocess
import numpy as np
from concurrent.futures import ThreadPoolExecutor
here = os.path.dirname(os.path.abspath(__file__))
B = dict(K1="6", K2="1", PEXP="0.5")
PLACES = {"tube_near": ("tubew", dict(B, VT="2.0", PEXP_GEOM="max", RRANGE="0.1,0.2", ERANGE="0,35")),
          "up_above": ("bup", dict(B, VT="1.3", HALF="100", GLOW="0.3", RRANGE="0.15,0.5")),
          "up_below": ("bup", dict(B, VT="1.3", HALF="100", GLOW="0.3", RRANGE="0.15,0.5", ERANGE="0,35"))}
CLEAR = ("K1", "K2", "K3", "VT", "PEXP", "PEXP_GEOM", "G2", "HALF", "GLOW", "ERANGE", "RRANGE", "DUMP", "FALLGATE", "WTA", "ALT", "FVEC", "K3X")
def run(job):
    g2, gam, place, seed = job; shape, env = PLACES[place]
    e = {k: v for k, v in os.environ.items() if k not in CLEAR}; e.update(env, G2=g2, DT="0.0025", K3=gam)
    o = json.loads(subprocess.run([sys.executable, os.path.join(here, "fabian_model.py"), "1000", str(seed), "1.0", shape], env=e, capture_output=True, text=True).stdout.strip().splitlines()[-1])
    fe = round(float(np.degrees(np.arcsin(np.clip(o["vz"] / o["speed"], -1, 1)))), 1) if o.get("speed") else None
    return f"{g2}|{gam}|{place}|{seed}", dict({k: o.get(k) for k in ("azdiff", "vz", "speed", "dorsum_elev", "nose_elev", "inverted_above", "n_above", "n_insects")}, flight_elev=fe)
jobs = [(g2, gam, pl, s) for g2 in ("5", "0") for gam in ("15", "5", "2") for pl in PLACES for s in (1, 2, 3)]
with ThreadPoolExecutor(2) as ex: res = dict(ex.map(run, jobs))
print(json.dumps(res, ensure_ascii=False, indent=0))
