"""
Advance/OF-DFT example: pseudopotential, PAO and basis-set presets.

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

from ase.build import bulk

from aofdft.calculator import (
    AOFDFTCalculator,
    VPS_GGA, VPS_LDA,                        # pseudopotential files for PBE / LDA
    PAO_MINI, BASIS_MINI,                    # minimal basis (default)
    PAO_MINI_AND_POLE, BASIS_MINI_AND_POLE,  # minimal basis + polarization
)

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

atoms    = bulk("SiC", crystalstructure="zincblende", a=4.36).repeat((2, 2, 2))  # 16 atoms
elements = sorted(set(atoms.get_chemical_symbols()))

# 1) look into the presets: file names and basis labels for each element
for name, pao, basis in [
    ("MINI (default)", PAO_MINI,          BASIS_MINI),
    ("MINI_AND_POLE",  PAO_MINI_AND_POLE, BASIS_MINI_AND_POLE),
]:
    print(name)
    for el in elements:
        print(f"  {el:2s} vps = {VPS_GGA[el]:16s} pao = {pao[el]:12s} basis = {basis[el]}")

common = dict(
    console     = False,
    kin_type    = "AS26",
    kin_model   = "Crystal26",
    vps_dir     = VPS_DIR,
    pao_dir     = PAO_DIR,
    mix_precond = None,  # without the Thomas-Fermi preconditioner of the default preset
)

# 2) default: PBE with the minimal basis
atoms.calc = AOFDFTCalculator(logging_file="sic_mini.log", **common)
print(f"\nPBE, MINI          : {atoms.get_potential_energy():.4f} eV")

# 3) add polarization functions with a preset pair (PAO files and basis labels must match)
atoms.calc = AOFDFTCalculator(
    logging_file = "sic_pole.log",
    pao_names    = PAO_MINI_AND_POLE,
    basis        = BASIS_MINI_AND_POLE,
    **common,
)
print(f"PBE, MINI_AND_POLE : {atoms.get_potential_energy():.4f} eV")

# 4) change the basis of one element only: copy a preset and edit it
my_basis      = dict(BASIS_MINI)
my_basis["C"] = "s1p1d1"
atoms.calc = AOFDFTCalculator(logging_file="sic_custom.log", basis=my_basis, **common)
print(f"PBE, C = s1p1d1    : {atoms.get_potential_energy():.4f} eV")

# 5) LDA: switch the XC functional together with the pseudopotential preset
atoms.calc = AOFDFTCalculator(
    logging_file = "sic_lda.log",
    xc_type      = "LDA",
    vps_names    = VPS_LDA,
    **common,
)
print(f"LDA, MINI          : {atoms.get_potential_energy():.4f} eV")
