Atomic charges and spin
Orbital populations
Write the electron population of each atomic orbital to a file and sum it for the s, p and d shells.
When a file name is given to population_file, the electron population of every pseudo-atomic orbital of each atom (the diagonal elements of the density matrix) is written to a text file after the SCF calculation. It gives the per-orbital breakdown of the Mulliken charges.
"""
Advance/OF-DFT example: orbital populations of each atom.
The populations are written to a text file by `population_file`. This script
also shows how to read the file and sum the populations for each shell.
Copyright (C) 2026 AdvanceSoft Corporation, All rights reserved.
"""
import re
from collections import defaultdict
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"
POP_FILE = "sic_population.txt"
def read_population(filename):
"""
Read the orbital population file.
Returns:
atoms (list[dict]): for each atom, {"element": str, "orbitals": {label: electrons}, "charge": float}
"""
head = re.compile(r"^Atom\s+index=\s*(\d+)\s+element=\s*(\w+)")
orbit = re.compile(r"^\s+(\d+)\s+(\S+)\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)\s+(-?\d+\.\d+)")
chrg = re.compile(r"^\s+Charge\s+(-?\d+\.\d+)")
atoms = []
with open(filename, encoding="utf-8") as f:
for line in f:
if m := head.match(line):
atoms.append({"element": m.group(2), "orbitals": {}, "charge": None})
elif m := orbit.match(line):
atoms[-1]["orbitals"][f"{m.group(1)} {m.group(2)}"] = float(m.group(3))
elif m := chrg.match(line):
atoms[-1]["charge"] = float(m.group(1))
return atoms
# SiC: 2x2x2 repetition of the primitive cell (16 atoms)
atoms = bulk("SiC", crystalstructure="zincblende", a=4.36).repeat((2, 2, 2))
atoms.calc = AOFDFTCalculator(
console = False,
logging_file = "sic.log",
population_file = POP_FILE, # orbital populations are written here after the SCF
kin_type = "AS26",
kin_model = "Crystal26",
vps_dir = VPS_DIR,
pao_dir = PAO_DIR,
)
atoms.get_potential_energy()
# print the file as it is
with open(POP_FILE, encoding="utf-8") as f:
print(f.read())
# sum the populations of each shell (s, p, d)
for index, atom in enumerate(read_population(POP_FILE)):
shells = defaultdict(float)
for label, electrons in atom["orbitals"].items():
shells[label.split()[1][0]] += electrons # "2 px" -> "p"
summary = " ".join(f"{shell} = {n:.3f}" for shell, n in shells.items())
print(f"atom {index} ({atom['element']}): {summary} charge = {atom['charge']:+.3f}")
Example of the output file
[ Orbital Populations of AOF-DFT ]
Atom index= 1 element= Si
==========================================================
Electron Spin-X Spin-Y Spin-Z
----------------------------------------------------------
1 s 1.520101 0.000000 0.000000 0.000000
2 px 0.151772 0.000000 0.000000 0.000000
2 py 0.151772 0.000000 0.000000 0.000000
2 pz 0.151772 0.000000 0.000000 0.000000
3 dz2 0.074063 0.000000 0.000000 0.000000
3 dxz 0.196458 0.000000 0.000000 0.000000
3 dyz 0.196458 0.000000 0.000000 0.000000
3 dx2-y2 0.074063 0.000000 0.000000 0.000000
3 dxy 0.196458 0.000000 0.000000 0.000000
----------------------------------------------------------
Total 2.712918 0.000000 0.000000 0.000000
Charge 1.287082
==========================================================
Atom index= 2 element= C
==========================================================
Electron Spin-X Spin-Y Spin-Z
----------------------------------------------------------
1 s 0.268646 0.000000 0.000000 0.000000
2 px 1.672812 0.000000 0.000000 0.000000
2 py 1.672812 0.000000 0.000000 0.000000
2 pz 1.672812 0.000000 0.000000 0.000000
----------------------------------------------------------
Total 5.287082 0.000000 0.000000 0.000000
Charge -1.287082
==========================================================
This is the part for the first two atoms of the file written for a SiC crystal (16 atoms). The last part of the script sums the populations of all atoms for each shell and prints:
atom 0 (Si): s = 1.520 p = 0.455 d = 0.738 charge = +1.287
atom 1 (C): s = 0.269 p = 5.018 charge = -1.287
atom 2 (Si): s = 1.520 p = 0.455 d = 0.738 charge = +1.287
atom 3 (C): s = 0.269 p = 5.018 charge = -1.287
atom 4 (Si): s = 1.520 p = 0.455 d = 0.738 charge = +1.287
atom 5 (C): s = 0.269 p = 5.018 charge = -1.287
atom 6 (Si): s = 1.520 p = 0.455 d = 0.738 charge = +1.287
atom 7 (C): s = 0.269 p = 5.018 charge = -1.287
atom 8 (Si): s = 1.520 p = 0.455 d = 0.738 charge = +1.287
atom 9 (C): s = 0.269 p = 5.018 charge = -1.287
atom 10 (Si): s = 1.520 p = 0.455 d = 0.738 charge = +1.287
atom 11 (C): s = 0.269 p = 5.018 charge = -1.287
atom 12 (Si): s = 1.520 p = 0.455 d = 0.738 charge = +1.287
atom 13 (C): s = 0.269 p = 5.018 charge = -1.287
atom 14 (Si): s = 1.520 p = 0.455 d = 0.738 charge = +1.287
atom 15 (C): s = 0.269 p = 5.018 charge = -1.287
Reading the file
- Orbital labels consist of the index of the radial function and the orbital type. For the basis
s1p1the rows are1 sand2 px, 2 py, 2 pz; bases with d orbitals add3 dz2, 3 dxz, 3 dyz, 3 dx2-y2, 3 dxy. In this example the basis iss1p1for C ands1p1d1for Si. - The
Electroncolumn is the electron population. In spin-polarized calculations theSpin-Zcolumn holds the spin population (up − down). Totalis the number of electrons on the atom andChargeis the Mulliken charge (valence electrons − electrons).- The cell is a 2×2×2 repetition of the primitive cell of SiC (16 atoms). With v1.0.0 the results change when the cell has a short period.
read_population()in the script is an example of a function that loads the file into one dictionary per atom, for shell-wise sums or comparisons between structures.