基本の計算
SCF 計算の制御と収束の確認
ログの出力先、SCF の計算条件、収束判定の取得方法と、ログから収束履歴をプロットする方法です。
SCF 計算の経過は、既定ではコンソールに出力されます。多数の計算を続けて実行するときは console=False と logging_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")
実行結果の例
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 計算条件のプリセット
scf_style は、電荷密度の混合に関する条件 (mix_beta、mix_nstored、mix_precond、noisy_precond) をまとめて設定するプリセットです。
scf_style | 対象 | 設定される値 |
|---|---|---|
"bulk" | バルク結晶 | mix_beta=0.2, mix_nstored=32, mix_precond="TF", noisy_precond="damping" |
"magnetic" | 磁性体 | mix_beta=0.1, mix_nstored=4, mix_precond=None, noisy_precond="damping" |
"molecule" | 分子 | mix_beta=0.1, mix_nstored=4, mix_precond=None, noisy_precond="2/3" |
scf_style=None (既定) の場合は、kin_model と spin_type から自動的に選ばれます。mix_beta などを個別に指定すると、プリセットの値よりも優先されます。
ポイント
- セルには、fcc Al の基本単位格子を 3×3×3 に繰り返したもの (27 原子) を使っています。v1.0.0 では、基本単位格子 (1 原子) のように周期の短いセルで計算すると結果が変わるためです。
scf_must_conv=Trueを指定すると、SCF が収束しなかったときにRuntimeErrorが発生します。構造最適化や大量の自動計算で、未収束の結果を使ってしまうことを防げます。- 収束の状態は、計算後に
calc.scf_converged(bool) とcalc.scf_energy_err(eV) で確認できます。 conv_thrはエネルギーの収束閾値で、単位は eV です。既定値は1.0e-6 * Hartreeです。- 収束しにくい場合は、
mix_betaを小さくする、max_iterを増やす、といった調整が有効です。 - ログには、各 SCF ステップの全エネルギー (Hartree)、SCF 誤差、フェルミエネルギー、磁気モーメント、所要時間が記録されます。
logging_level=logging.DEBUGでより詳細な情報を出力できます。