Atomic charges and spin
Comparing magnetic orderings
Compare the energies of non-magnetic, ferromagnetic and antiferromagnetic states by changing the initial spins.
In spin-polarized calculations, the magnetic state reached by the SCF depends on the initial spins. Ferromagnetic (FM) or antiferromagnetic (AFM) arrangements are selected through the signs of the initial spins in atoms.arrays["spin"].
"""
Advance/OF-DFT example: compare magnetic orderings of bcc Fe.
The magnetic ordering is selected by the initial spins given in atoms.arrays["spin"].
Copyright (C) 2026 AdvanceSoft Corporation, All rights reserved.
"""
import numpy as np
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"
params = dict(
console = False,
kin_type = "AS26",
kin_model = "Crystal26",
vps_dir = VPS_DIR,
pao_dir = PAO_DIR,
e_cut = 180 * Hartree,
n_grids = (96, 96, 96), # a multiple of 6, so that all atoms sit alike on the FFT grid
)
# label -> initial spins of the corner and body-center atoms of the cubic cell (None = non-magnetic)
orderings = {
"NM" : None,
"FM" : [1.0, 1.0],
"AFM": [1.0, -1.0],
}
results = {}
for label, spins in orderings.items():
atoms = bulk("Fe", "bcc", a=2.87, cubic=True).repeat((3, 3, 3)) # 54 atoms
if spins is None:
atoms.calc = AOFDFTCalculator(logging_file=f"fe_{label}.log", **params)
else:
atoms.arrays["spin"] = spins * (len(atoms) // 2)
atoms.calc = AOFDFTCalculator(
logging_file = f"fe_{label}.log",
spin_type = "polarized",
scf_style = "magnetic",
**params,
)
results[label] = atoms.get_potential_energy() / len(atoms)
# spins of the corner and body-center atoms, and the range of |spin| over all atoms
spin = atoms.arrays["spin"] if spins is not None else np.zeros(len(atoms))
print(f"{label:3s}: spins = [{spin[0]:+.3f}, {spin[1]:+.3f}], |spin| of all atoms = "
f"{np.abs(spin).min():.3f} ... {np.abs(spin).max():.3f}, converged = {atoms.calc.scf_converged}")
print()
for label in ("NM", "AFM"):
print(f"E({label}) - E(FM) = {(results[label] - results['FM']) * 1000.0:+.1f} meV/atom")
Example output
NM : spins = [+0.000, +0.000], |spin| of all atoms = 0.000 ... 0.000, converged = True
FM : spins = [+1.883, +1.883], |spin| of all atoms = 1.883 ... 1.883, converged = True
AFM: spins = [+1.859, -1.859], |spin| of all atoms = 1.859 ... 1.859, converged = True
E(NM) - E(FM) = +296.5 meV/atom
E(AFM) - E(FM) = +32.7 meV/atom
Of the three states, the ferromagnetic (FM) one is the most stable.
Notes
- The initial spins are signed values per atom. Opposite signs such as
[1.0, -1.0]start the SCF from an antiferromagnetic arrangement. The script repeats the values of the two atoms of one cubic cell over the whole cell. - A non-magnetic (NM) calculation is obtained by leaving
spin_typeat its default,"non-polarized". - Always inspect
atoms.arrays["spin"]after the calculation to confirm that the intended state was reached; the SCF can move to a different state. Besides the spins of the corner and body-centre atoms, the script prints the range of the absolute spins of all atoms. - An antiferromagnetic arrangement needs a supercell that matches the magnetic unit cell. Here the cubic bcc cell (2 atoms) is the unit, with opposite spins on the corner and body-centre atoms.
- The cell is a 3×3×3 repetition of the cubic cell (54 atoms). With v1.0.0 the results change when the cell has a short period.
- The FFT grid is set with
n_grids=(96, 96, 96). In this cell all atoms sit alike on the grid when the grid number is a multiple of 6. The default grid frome_cut(98) is not, and with v1.0.0 the SCF then converges to a state in which the spins differ from atom to atom. - Energy differences between magnetic states are often small, so compare them at the same cutoff energy, grid and convergence threshold. For bcc Fe the total energy depends strongly on the fineness of the grid; do not compare energies calculated with different cells or grids.