Atomic charges and spin
Fixed spin moment calculations
Fix the total spin moment of the cell during the SCF and examine how the energy changes with magnetization.
With total_spin, the SCF is carried out while the total spin moment of the cell is kept at the given value (the fixed spin moment method). The energy as a function of magnetization reveals the stability of the magnetic states.
"""
Advance/OF-DFT example: fixed spin moment calculation (energy versus magnetization).
`total_spin` fixes the total spin moment of the cell. The up and down electrons
then have different Fermi energies.
Copyright (C) 2026 AdvanceSoft Corporation, All rights reserved.
"""
import matplotlib.pyplot as plt
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"
# spin moment per atom: 0 - 2.0, with a finer step near the energy minimum
moments = np.concatenate([np.arange(0.0, 1.5, 0.25), np.arange(1.5, 2.01, 0.05)])
rows = []
for moment in moments:
atoms = bulk("Fe", "bcc", a=2.87).repeat((3, 3, 3)) # 3x3x3 primitive cells, 27 atoms
atoms.arrays["spin"] = [moment] * len(atoms)
atoms.calc = AOFDFTCalculator(
console = False,
logging_file = f"fe_fsm_{moment:.2f}.log",
kin_type = "AS26",
kin_model = "Crystal26",
vps_dir = VPS_DIR,
pao_dir = PAO_DIR,
e_cut = 180 * Hartree,
spin_type = "polarized",
scf_style = "magnetic",
total_spin = moment * len(atoms), # total spin moment of the cell is fixed
)
energy = atoms.get_potential_energy() / len(atoms)
calc = atoms.calc
rows.append((moment, energy, calc.e_fermi_up, calc.e_fermi_dw))
# energy relative to the minimum, in meV/atom
relative = (np.array([row[1] for row in rows]) - min(row[1] for row in rows)) * 1000.0
print(" M/atom | E - E_min (meV/atom) | E_F(up) (eV) | E_F(down) (eV)")
for (moment, _, e_up, e_dw), e_rel in zip(rows, relative):
print(f" {moment:6.2f} | {e_rel:20.2f} | {e_up:12.4f} | {e_dw:14.4f}")
plt.plot(moments, relative, marker="o")
plt.xlabel("Spin moment per atom")
plt.ylabel("Relative energy (meV/atom)")
plt.tight_layout()
plt.savefig("fe_fixed_spin_moment.png", dpi=150)
print("Saved fe_fixed_spin_moment.png")
Example output
Results with v1.0.0 (Crystal26). The energies are differences from the lowest value, per atom.
M/atom | E - E_min (meV/atom) | E_F(up) (eV) | E_F(down) (eV)
0.00 | 309.06 | -13.1893 | -13.1893
0.25 | 298.46 | -13.2295 | -13.0994
0.50 | 270.92 | -13.2361 | -12.9934
0.75 | 231.18 | -13.2293 | -12.8892
1.00 | 180.30 | -13.2041 | -12.7844
1.25 | 123.53 | -13.1555 | -12.6859
1.50 | 62.18 | -13.0364 | -12.5767
1.55 | 50.65 | -13.0005 | -12.5554
1.60 | 39.60 | -12.9580 | -12.5341
1.65 | 29.22 | -12.9072 | -12.5129
1.70 | 20.09 | -12.8468 | -12.4930
1.75 | 11.05 | -12.7658 | -12.4681
1.80 | 5.10 | -12.6697 | -12.4496
1.85 | 0.84 | -12.5372 | -12.4298
1.90 | 0.00 | -12.3369 | -12.4064
1.95 | 5.28 | -11.9807 | -12.3835
2.00 | 27.78 | -7.4269 | -12.3609
Saved fe_fixed_spin_moment.png

The energy has its minimum at 1.85 - 1.90 per atom, in agreement with the spin obtained without fixing the moment (1.88, see Spin-polarized calculation (bcc Fe)). The Fermi energies of the up and down spins change their order at this point.
With v1.0.0 (Crystal26), the total energy of bcc Fe drops strongly when the fixed spin moment exceeds 2.0 per atom (by 0.19 eV/atom at 2.05 and 1.9 eV/atom at 2.5). The current version of the model cannot treat this range correctly, so this example covers the range 0 - 2.0.
Notes
total_spinis the spin moment of the whole cell (the difference between the numbers of up and down electrons), not a value per atom.- When the spin moment is fixed, the numbers of up and down electrons are conserved separately, so each spin has its own Fermi energy, available as
calc.e_fermi_upandcalc.e_fermi_dw. Their difference corresponds to the effective magnetic field needed to hold that magnetization. - With
total_spin=None(default) the moment is not fixed, ande_fermi_upande_fermi_dwareNone. - Convergence is more stable when the initial spins in
atoms.arrays["spin"]are consistent with the fixed value. - At 2.0 per atom there are exactly 5 up-spin electrons per atom. The Fermi energy of the up spin changes strongly at this point, and the energy rises more steeply.
- The cell is a 3×3×3 repetition of the bcc primitive cell (27 atoms). With v1.0.0 the results change when the cell has a short period.