"""
Advance/OF-DFT example: spin-polarized calculation of bcc Fe.

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

from ase.build import bulk
from ase.units import Hartree

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"

atoms = bulk("Fe", "bcc", a=2.87).repeat((3, 3, 3))  # 3x3x3 primitive cells, 27 atoms

# initial spin of each atom
atoms.arrays["spin"] = [1.0] * len(atoms)

atoms.calc = AOFDFTCalculator(
    console      = False,
    logging_file = "fe_spin.log",
    kin_type     = "AS26",
    kin_model    = "Crystal26",
    vps_dir      = VPS_DIR,
    pao_dir      = PAO_DIR,
    e_cut        = 180 * Hartree,
    spin_type    = "polarized",  # collinear spin-polarized calculation
    scf_style    = "magnetic",   # SCF preset for magnetic systems
)

atoms.get_potential_energy()
calc = atoms.calc

# after the SCF, atoms.arrays["spin"] holds the calculated spin of each atom
spins = atoms.arrays["spin"]
print(f"Spin of each atom         : {spins.min():.4f} ... {spins.max():.4f}")
print(f"Total magnetic moment     : {calc.total_magmom:.4f} ({calc.total_magmom / len(atoms):.4f} per atom)")
print(f"Absolute magnetic moment  : {calc.absolute_magmom:.4f}")
