電子密度の解析
電子密度の保存と初期値としての再利用
収束した電子密度を NumPy ファイルに保存し、別の計算の初期電子密度として設定して SCF の反復回数を減らします。
rho プロパティは取得だけでなく設定もできます。計算の前に電子密度を設定すると、SCF 計算の初期電子密度として使われます。似た構造の計算を繰り返すときや、条件を変えて再計算するときに、反復回数を減らせます。
"""
Advance/OF-DFT example: save the electron density and reuse it as the initial guess.
Copyright (C) 2026 AdvanceSoft Corporation, All rights reserved.
"""
import re
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,
)
def scf_iterations(log_file):
"""Number of SCF iterations written in the log file."""
with open(log_file, encoding="utf-8") as f:
found = re.findall(r"SCF converged in (\d+) iterations", f.read())
return int(found[-1]) if found else None
# 1) converge the density once and save it as a NumPy file
atoms = bulk("Si", crystalstructure="diamond", a=5.43, cubic=True)
atoms.calc = AOFDFTCalculator(logging_file="si_first.log", **params)
atoms.get_potential_energy()
rho = atoms.calc.rho.detach().cpu().numpy() # shape (n_rho, na, nb, nc)
np.save("si_rho.npy", rho)
print(f"Saved si_rho.npy, shape = {rho.shape}")
# 2) slightly displaced structure, starting from the superposition of atomic densities (default)
displaced = atoms.copy()
displaced.rattle(stdev=0.02, seed=1)
displaced.calc = AOFDFTCalculator(logging_file="si_atomic_guess.log", **params)
e_atomic = displaced.get_potential_energy()
# 3) the same structure, starting from the saved density:
# set the `rho` property before the calculation
calc = AOFDFTCalculator(logging_file="si_saved_guess.log", **params)
calc.rho = np.load("si_rho.npy")
displaced.calc = calc
e_saved = displaced.get_potential_energy()
print(f"Atomic guess : {scf_iterations('si_atomic_guess.log')} iterations, E = {e_atomic:.6f} eV")
print(f"Saved density: {scf_iterations('si_saved_guess.log')} iterations, E = {e_saved:.6f} eV")
# 4) a density on a different grid is interpolated automatically (here: a finer grid)
calc = AOFDFTCalculator(logging_file="si_fine_grid.log", e_cut=250 * Hartree, **params)
calc.rho = np.load("si_rho.npy")
displaced.calc = calc
displaced.get_potential_energy()
print(f"Finer grid : {scf_iterations('si_fine_grid.log')} iterations, "
f"grid {rho.shape[1:]} -> {tuple(calc.rho.shape[1:])}")
実行結果の例
Saved si_rho.npy, shape = (1, 56, 56, 56)
Atomic guess : 18 iterations, E = -922.038343 eV
Saved density: 14 iterations, E = -922.038177 eV
Finer grid : 14 iterations, grid (56, 56, 56) -> (73, 73, 73)
保存した電子密度を初期値にすると、SCF の反復回数が 18 回から 14 回に減っています。
ポイント
calc.rhoに設定できるのは、形状(n_rho, na, nb, nc)または(na, nb, nc)のtorch.Tensor、numpy.ndarray、リスト、タプルです。- 設定した電子密度のグリッドが計算のグリッドと異なる場合は、FFT による補間が自動的に行われます。粗いグリッドで求めた電子密度を、細かいグリッドの計算の初期値にできます。
- 既定の初期電子密度は、孤立した中性原子の電子密度の重ね合わせ (
init_rho="atomic") です。 - 同じ Calculator オブジェクトで構造を変えながら計算を続ける場合 (構造最適化や分子動力学) は、前回の密度行列が自動的に初期値として使われるため、この操作は不要です。
- 保存には
numpy.saveのほか、CUBE ファイル を使うこともできます。