# CCD_analysis/scripts/cfg.py
# RELIABILITY: HIGH. Paths and constants only, no CFD dependency.
"""Configuration constants for CCD analysis pipeline."""

import os

# -- Paths -------------------------------------------------------------------
_PROJ_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
ANALYSIS_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
CONFIG_DIR = os.path.join(ANALYSIS_DIR, "configs")
OUTPUT_DIR = os.path.join(ANALYSIS_DIR, "output")
MODEL_DIR = os.path.join(_PROJ_ROOT, "models")
LEGACY_CFD_DIR = os.path.join(_PROJ_ROOT, "LegacyCelerisLab")

# -- GPU config (overridden by --device flag) --------------------------------
DEVICE_ID = 2  # default

# -- Legacy CFD config paths (copied, independent) ---------------------------
CONFIG_CUDA = os.path.join(CONFIG_DIR, "config_cuda.json")
CONFIG_FLOWFIELD_BASE = os.path.join(CONFIG_DIR, "config_flowfield.json")

# -- Physics constants -------------------------------------------------------
U0 = 0.01             # inlet centre velocity (lattice units)
D_CYL = 20.0          # single cylinder diameter (lattice units)
D_REF = 40.0          # reference length = 2*D (used for code "Re")
L0 = 20.0             # base length unit (lattice)
DATA_TYPE = "FP32"

# -- Grid --------------------------------------------------------------------
NX = 1280
NY = 512
CENTER_Y = (NY - 1) / 2.0  # 255.5

# -- Sampling parameters ----------------------------------------------------
SAMPLE_INTERVAL = 800   # default for cloak/uncontrolled/target
SAMPLE_INTERVAL_ILLUSION = 600  # for illusion

# -- Geometry helpers --------------------------------------------------------
def nu_from_re(re_code: float, u0: float = U0) -> float:
    """Kinematic viscosity from code Reynolds number (ref length = 2D)."""
    return u0 * D_REF / re_code

# -- Object coordinates (lattice units) -------------------------------------
# Pinball (standard layout, for cloak/uncontrolled)
PINBALL_RADIUS = L0 / 2.0
FRONT_CENTER = (30.0 * L0, CENTER_Y)       # (600, 255.5)
BOTTOM_CENTER = (31.3 * L0, CENTER_Y - 0.75 * L0)  # (626, 240.5)
TOP_CENTER = (31.3 * L0, CENTER_Y + 0.75 * L0)     # (626, 270.5)

# Pinball (illusion layout — different positions)
ILLUSION_FRONT = (19.0 * L0, CENTER_Y)      # (380, 255.5)
ILLUSION_BOTTOM = (20.3 * L0, CENTER_Y + 0.75 * L0)  # (406, 270.5)
ILLUSION_TOP = (20.3 * L0, CENTER_Y - 0.75 * L0)     # (406, 240.5)

# Sensors
SENSOR_RADIUS = L0 / 4.0  # 5
SENSOR_CENTERS_CLOAK = [  # x=40*L0 for cloak/uncontrolled
    (40.0 * L0, CENTER_Y + 2.0 * L0),
    (40.0 * L0, CENTER_Y),
    (40.0 * L0, CENTER_Y - 2.0 * L0),
]
SENSOR_CENTERS_ILLUSION = [  # x=30*L0 for illusion
    (30.0 * L0, CENTER_Y + 2.0 * L0),
    (30.0 * L0, CENTER_Y),
    (30.0 * L0, CENTER_Y - 2.0 * L0),
]

# Target cylinder (2D cylinder for standard frequency / illusion target)
TARGET_CYLINDER_CENTER = (20.0 * L0, CENTER_Y)   # (400, 255.5)
TARGET_CYLINDER_RADIUS = 1.0 * L0                # 20

# -- Model paths -------------------------------------------------------------
MODEL_CLOAK_RE100 = os.path.join(MODEL_DIR, "old", "d1a3o12_re100.zip")
MODEL_CLOAK_250326 = os.path.join(MODEL_DIR, "250326", "d1a3o12_250326.zip")
MODEL_ILLUSION_1L = os.path.join(
    MODEL_DIR, "250525", "d1a3o14_250525_imit_1L_2U_600S.zip"
)

# -- Action parameters -------------------------------------------------------
ACTION_SCALE_CLOAK = 8.0
ACTION_BIAS_CLOAK = (0.0, -4.0, 4.0)

ACTION_SCALE_ILLUSION = 8.0
ACTION_BIAS_ILLUSION = (0.0, -2.0, 2.0)

# -- DRL parameters ----------------------------------------------------------
S_DIM_CLOAK = 12
S_DIM_ILLUSION = 14
A_DIM = 3
FIFO_LEN = 150
CONV_LEN = 30
STABILIZE_STEPS = int(4 * NX / U0)

# -- Phase resampling --------------------------------------------------------
N_TARGET_CYCLES = 4      # number of cycles to extract
N_PTS_PER_CYCLE = 24     # phase points per cycle
TOTAL_PHASE_FRAMES = N_TARGET_CYCLES * N_PTS_PER_CYCLE  # 96

# -- CCD parameters ----------------------------------------------------------
CCD_Q_DEFAULT = 12        # delay window size (half-cycle)
CCD_R_CANDIDATES = [6, 8, 10]  # POD truncation candidates
