Research-Stack/5-Applications/scripts/gsp/burgers_triad_core.py
Devin AI e5f04ee6c3 refactor(infra): extract shared utilities from duplicated code patterns
Create 4-Infrastructure/lib/ with canonical implementations of:
- hashing.py: sha256_bytes, sha256_text, sha256_file
- q16.py: Q16_16 fixed-point constants and arithmetic
- jsonl.py: load_json, load_jsonl, write_jsonl, stable_json, canonical_json_bytes
- fraction_utils.py: Fraction serialization helpers for hardware probes

Refactor 66 files across 4-Infrastructure/{hardware,shim,infra} and
5-Applications/{scripts,tools-scripts,hutter_prize,text-to-cad} to import
from the shared library instead of maintaining local copies.

4-Infrastructure/auto/lib/q16.py now re-exports from lib.q16.

Net: -743 lines (490 added, 1233 removed)

Build: not applicable (Python-only change, py_compile verified on all 71 files)
Co-Authored-By: Allaun Silverfox <bigdataiscoming+9i37y6j2@protonmail.com>
2026-06-15 02:26:45 +00:00

112 lines
2.8 KiB
Python

import numpy as np
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
from lib.q16 import Q16_ONE, q16_mul
# Phase 1 — Build BurgersTriadCore
# Q16.16 in the AVM hot path
# Q16.16 Constants
Q16_SHIFT = 16
Q16_MAX = (1 << 31) - 1
Q16_MIN = -(1 << 31)
def float_to_q16(f: float) -> int:
return q16_sat(int(f * Q16_ONE))
def q16_to_float(q: int) -> float:
return q / Q16_ONE
_sat_count = 0
def get_sat_count() -> int:
global _sat_count
return _sat_count
def reset_sat_count():
global _sat_count
_sat_count = 0
def q16_sat(x: int) -> int:
"""Saturate to 32-bit signed integer."""
global _sat_count
if x > Q16_MAX:
_sat_count += 1
return Q16_MAX
if x < Q16_MIN:
_sat_count += 1
return Q16_MIN
return x
def triad_rhs(a: tuple[int, int, int], nu_eff: int) -> tuple[int, int, int]:
"""
Triad equations (Burgers):
da1/dt = -nu_eff a1 + 1/2(a1a2 + a2a3)
da2/dt = -4nu_eff a2 - 1/2 a1^2 + a1a3
da3/dt = -9nu_eff a3 - 3/2 a1a2
"""
a1, a2, a3 = a
# Precompute products
a1_a2 = q16_mul(a1, a2)
a2_a3 = q16_mul(a2, a3)
a1_a3 = q16_mul(a1, a3)
a1_a1 = q16_mul(a1, a1)
# 1/2 is (1 << 15), 3/2 is (3 << 15), etc. Or just multiply and divide by 2
# To maintain Q16 semantics, we can multiply by Q16 constants:
HALF = 1 << 15
THREE_HALVES = 3 << 15
# da1/dt = -nu_eff * a1 + 1/2 * (a1a2 + a2a3)
t1_1 = -q16_mul(nu_eff, a1)
t1_2 = q16_mul(HALF, q16_sat(a1_a2 + a2_a3))
da1 = q16_sat(t1_1 + t1_2)
# da2/dt = -4 * nu_eff * a2 - 1/2 * a1^2 + a1a3
FOUR = 4 << Q16_SHIFT
t2_1 = -q16_mul(q16_mul(FOUR, nu_eff), a2)
t2_2 = -q16_mul(HALF, a1_a1)
t2_3 = a1_a3
da2 = q16_sat(q16_sat(t2_1 + t2_2) + t2_3)
# da3/dt = -9 * nu_eff * a3 - 3/2 * a1a2
NINE = 9 << Q16_SHIFT
t3_1 = -q16_mul(q16_mul(NINE, nu_eff), a3)
t3_2 = -q16_mul(THREE_HALVES, a1_a2)
da3 = q16_sat(t3_1 + t3_2)
return (da1, da2, da3)
def rk2_step(a: tuple[int, int, int], nu_eff: int, dt: int) -> tuple[int, int, int]:
"""Midpoint RK2 step in Q16.16."""
a1, a2, a3 = a
k1 = triad_rhs(a, nu_eff)
# Midpoint
half_dt = dt >> 1
a_mid = (
q16_sat(a1 + q16_mul(k1[0], half_dt)),
q16_sat(a2 + q16_mul(k1[1], half_dt)),
q16_sat(a3 + q16_mul(k1[2], half_dt))
)
k2 = triad_rhs(a_mid, nu_eff)
# Full step
a_next = (
q16_sat(a1 + q16_mul(k2[0], dt)),
q16_sat(a2 + q16_mul(k2[1], dt)),
q16_sat(a3 + q16_mul(k2[2], dt))
)
return a_next
def energy3(a: tuple[int, int, int]) -> int:
"""E = 1/2 (a1^2 + a2^2 + a3^2) in Q16.16."""
a1, a2, a3 = a
sum_sq = q16_sat(q16_sat(q16_mul(a1, a1) + q16_mul(a2, a2)) + q16_mul(a3, a3))
HALF = 1 << 15
return q16_mul(HALF, sum_sq)