"""
Case runner for Dynamics case14 — 2D grid (61x61 atomic mesh).

This script keeps program and data separated:
  - program: ../../dynamics.py
  - input:   ./input
  - output:  ./output
"""

from __future__ import annotations

import argparse
import importlib.util
from pathlib import Path


CASE_DIR = Path(__file__).resolve().parent
DYNAMICS_PATH = Path("..") / ".." / "dynamics.py"
INPUT_DIR = Path("input")
OUTPUT_DIR = Path("output")
CONFIG_FILE = INPUT_DIR / "input.txt"


def load_dynamics_module(module_path: Path):
    spec = importlib.util.spec_from_file_location("dynamics_module", module_path)
    if spec is None or spec.loader is None:
        raise ImportError(f"无法加载 dynamics.py: {module_path}")
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    return module


def main():
    parser = argparse.ArgumentParser(description="运行 Dynamics 示例案例 case14")
    parser.add_argument("--no-plot", action="store_true", help="跳过 matplotlib 绘图")
    args = parser.parse_args()

    dynamics_path = (CASE_DIR / DYNAMICS_PATH).resolve()
    input_dir = (CASE_DIR / INPUT_DIR).resolve()
    output_dir = (CASE_DIR / OUTPUT_DIR).resolve()
    config_path = (CASE_DIR / CONFIG_FILE).resolve()

    module = load_dynamics_module(dynamics_path)
    module.run_case(
        config_path=config_path,
        runtime_base=CASE_DIR,
        input_dir=input_dir,
        output_dir=output_dir,
        no_plot=args.no_plot,
    )


if __name__ == "__main__":
    main()
