"""
Advance/OF-DFT example: run on a GPU or with OpenMP threads.

Copyright (C) 2026 AdvanceSoft Corporation, All rights reserved.
"""

import time

import torch
from ase.build import bulk

from aofdft.calculator import AOFDFTCalculator

VPS_DIR = "path/to/DFT_DATA19_AS/VPS"  # edit these two paths for your environment
PAO_DIR = "path/to/DFT_DATA19_AS/PAO"

# use the GPU when PyTorch can see one, otherwise the CPU
device = "cuda" if torch.cuda.is_available() else "cpu"

# 2x2x2 supercell of fcc Al (32 atoms)
atoms = bulk("Al", "fcc", a=4.05, cubic=True) * (2, 2, 2)

atoms.calc = AOFDFTCalculator(
    console      = False,
    logging_file = "al32.log",
    kin_type     = "AS26",
    kin_model    = "Crystal26",
    vps_dir      = VPS_DIR,
    pao_dir      = PAO_DIR,
    device       = device,  # "cpu", "cuda" or "cuda:1" (device ID from `nvidia-smi -L`)
    num_threads  = 8,       # OpenMP threads; None -> OMP_NUM_THREADS or all physical cores
)

start   = time.time()
energy  = atoms.get_potential_energy()
elapsed = time.time() - start

print(f"Device       : {atoms.calc.device}")
print(f"Atoms        : {len(atoms)}")
print(f"Total energy : {energy:.6f} eV")
print(f"Elapsed time : {elapsed:.1f} s")
