"""Build the diamond-silicon volume scans.

No calculator is used. Every frame is the eight-atom conventional diamond cell with
the same fractional coordinates and a different cubic lattice constant:

    survey: 9 lattice constants from 5.1 to 5.9 Angstrom  (silicon-survey.extxyz)
    refine: 5 lattice constants from 5.3 to 5.7 Angstrom  (silicon-refine.extxyz)

    python silicon-build.py --output ../models
"""

import argparse
from pathlib import Path

import numpy as np

from atomli.build import bulk
from atomli.io import write

SCANS = {"silicon-survey": np.linspace(5.1, 5.9, 9), "silicon-refine": np.linspace(5.3, 5.7, 5)}


def build(output):
    output.mkdir(parents=True, exist_ok=True)
    frames = {}
    for name, lattices in SCANS.items():
        frames[name] = [bulk("Si", "diamond", a=float(a), cubic=True) for a in lattices]
        write(output / f"{name}.extxyz", frames[name])
    return frames


def write_figure(frames, spec):
    from atomli.visualize import Viewer

    viewer = Viewer(frames, height=420, look={"background": "white", "axes": True, "legend": True, "labels": False, "cell": True})
    viewer.set_frame(4)  # a = 5.5 Angstrom
    viewer.set_camera(position=(18.0, -14.0, 13.0), target=(2.75, 2.75, 2.75), up=(0, 0, 1))  # off-axis: no atoms hide behind others
    viewer.write_spec(spec, structure_path=spec.with_name("silicon-survey.extxyz"))


def main():
    here = Path(__file__).resolve().parent
    parser = argparse.ArgumentParser()
    parser.add_argument("--output", type=Path, default=here.parent / "models")
    parser.add_argument("--spec", type=Path, help="also write the viewer spec for the survey scan")
    args = parser.parse_args()
    frames = build(args.output)
    if args.spec:
        write_figure(frames["silicon-survey"], args.spec)


if __name__ == "__main__":
    main()
