Basic calculations
Controlling and monitoring the SCF
Log output, SCF parameters, how to check convergence, and how to plot the convergence history from the log.
By default the progress of the SCF is printed on the console. When running many calculations in a row, it is convenient to set console=False and logging_file to keep the log in a file.
"""
Advance/OF-DFT example: control and monitor the SCF calculation.
Shows the logging options, the SCF parameters, how to detect a failed SCF,
and how to plot the convergence history written in the log file.
Copyright (C) 2026 AdvanceSoft Corporation, All rights reserved.
"""
import logging
import re
import matplotlib.pyplot as plt
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"
LOG_FILE = "al_scf.log"
atoms = bulk("Al", "fcc", a=4.05).repeat((3, 3, 3)) # 27 atoms
atoms.calc = AOFDFTCalculator(
kin_type = "AS26",
kin_model = "Crystal26",
vps_dir = VPS_DIR,
pao_dir = PAO_DIR,
# --- logging
console = False, # do not print the SCF log on the screen
logging_file = LOG_FILE, # write it to a file instead
logging_level = logging.INFO, # use logging.DEBUG for more details
# --- SCF
scf_style = "bulk", # preset of mixing parameters: "bulk" | "magnetic" | "molecule"
max_iter = 200, # maximum number of SCF iterations
conv_thr = 1.0e-6 * Hartree, # convergence threshold of the energy (in eV)
scf_must_conv = True, # raise RuntimeError when the SCF does not converge
)
try:
energy = atoms.get_potential_energy()
except RuntimeError as err:
raise SystemExit(f"SCF failed: {err}")
calc = atoms.calc
print(f"Converged : {calc.scf_converged}")
print(f"SCF energy error : {calc.scf_energy_err:.3e} eV")
print(f"Total energy : {calc.total_energy:.6f} eV")
print(f"Fermi energy : {calc.e_fermi:.4f} eV")
# read the SCF history (iteration, total energy / Ha, SCF error / Ha) from the log file
row = re.compile(r"^\s*(\d+) \|\s*(\S+) \|\s*(\S+) \|")
history = []
with open(LOG_FILE, encoding="utf-8") as f:
for line in f:
m = row.match(line)
if m:
history.append((int(m.group(1)), float(m.group(2)), float(m.group(3))))
iters = [h[0] for h in history]
errors = [h[2] * Hartree for h in history]
plt.semilogy(iters, errors, marker="o")
plt.axhline(1.0e-6 * Hartree, linestyle="--", color="gray", label="conv_thr")
plt.xlabel("SCF iteration")
plt.ylabel("SCF error (eV)")
plt.legend()
plt.tight_layout()
plt.savefig("al_scf_convergence.png", dpi=150)
print("Saved al_scf_convergence.png")
Example output
Converged : True
SCF energy error : 5.342e-06 eV
Total energy : -1810.788491 eV
Fermi energy : -5.7245 eV
Saved al_scf_convergence.png
SCF presets
scf_style is a preset that sets the charge-mixing parameters (mix_beta, mix_nstored, mix_precond, noisy_precond) together.
scf_style | Target | Values |
|---|---|---|
"bulk" | bulk crystals | mix_beta=0.2, mix_nstored=32, mix_precond="TF", noisy_precond="damping" |
"magnetic" | magnetic systems | mix_beta=0.1, mix_nstored=4, mix_precond=None, noisy_precond="damping" |
"molecule" | molecules | mix_beta=0.1, mix_nstored=4, mix_precond=None, noisy_precond="2/3" |
With scf_style=None (default) the preset is chosen automatically from kin_model and spin_type. Values given explicitly, such as mix_beta, take precedence over the preset.
Notes
- The cell is a 3×3×3 repetition of the fcc primitive cell of Al (27 atoms). With v1.0.0 the results change when the cell has a short period, as the primitive cell (1 atom) does.
- With
scf_must_conv=TrueaRuntimeErroris raised when the SCF does not converge. This prevents unconverged results from being used in structure optimizations or automated workflows. - After the calculation, check
calc.scf_converged(bool) andcalc.scf_energy_err(eV). conv_thris the convergence threshold of the energy in eV. The default is1.0e-6 * Hartree.- If the SCF is hard to converge, try a smaller
mix_betaor a largermax_iter. - The log records the total energy (Hartree), SCF error, Fermi energy, magnetic moments and elapsed time of each SCF step. Use
logging_level=logging.DEBUGfor more detail.