電子密度の解析
電子密度を CUBE ファイルに保存する
SCF 計算で得られた電子密度を取り出し、可視化ソフトで読み込める CUBE 形式で保存します。
電子密度は AOFDFTCalculator の rho プロパティから取得できます。ASE の write_cube に渡すだけで、NanoLabo や VESTA などの可視化ソフトで読み込める CUBE ファイルになります。
"""
Advance/OF-DFT example: save the electron density to a CUBE file.
Copyright (C) 2026 AdvanceSoft Corporation, All rights reserved.
"""
from ase.build import bulk
from ase.io.cube import read_cube_data, write_cube
from ase.units import Bohr
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"
atoms = bulk("Si", crystalstructure="diamond", a=5.43, cubic=True)
atoms.calc = AOFDFTCalculator(
console = False,
logging_file = "si_density.log",
kin_type = "AS26",
kin_model = "Crystal26",
vps_dir = VPS_DIR,
pao_dir = PAO_DIR,
)
# run SCF
atoms.get_potential_energy()
# electron density on the FFT grid: torch.Tensor of shape (n_rho, na, nb, nc), in electrons/Bohr^3
rho = atoms.calc.rho
print(f"Shape of rho : {tuple(rho.shape)}")
# rho[0] is the total electron density; convert torch.Tensor -> numpy.ndarray
density = rho[0].detach().cpu().numpy()
# sanity check: the density integrates to the number of valence electrons
volume = atoms.get_volume() / Bohr**3 # Bohr^3
print(f"Number of electrons: {density.mean() * volume:.6f}")
print(f"Max. / min. density: {density.max():.5f} / {density.min():.5f} e/Bohr^3")
# save in the Gaussian CUBE format (readable by NanoLabo, VESTA, VMD, ...)
with open("si_density.cube", "w") as f:
write_cube(f, atoms, data=density, comment="Electron density (e/Bohr^3) by Advance/OF-DFT")
print("Saved si_density.cube")
# read the file back with ASE
data, atoms_read = read_cube_data("si_density.cube")
print(f"Read back : grid {data.shape}, {len(atoms_read)} atoms")
rho プロパティの仕様
| 項目 | 内容 |
|---|---|
| 型 | torch.Tensor (倍精度) |
| 形状 | (n_rho, na, nb, nc)。na, nb, nc は a, b, c 軸方向の FFT グリッド数 |
| 単位 | 電子数 / Bohr³ |
n_rho | スピン分極なし:1 / スピン分極あり (collinear):2 |
| 成分 | rho[0]:全電子密度、rho[1]:スピン密度 (アップ − ダウン) |
ポイント
rhoは PyTorch のテンソルです。NumPy 配列に変換するにはdetach().cpu().numpy()を使います。GPU で計算した場合も同じ書き方で動作します。- 得られるのは擬ポテンシャル計算の価電子密度です。セル全体で積分すると価電子数に一致します (スクリプトでは
density.mean() * volumeで確認しています)。 - CUBE ファイルの密度は原子単位 (Bohr⁻³) で書くのが慣例で、
rhoの単位はそのまま対応しています。 - グリッド点
(i, j, k)の座標は、セルベクトルをa, b, cとして(i/na) a + (j/nb) b + (k/nc) cです。 - スピン密度の保存は スピン密度の可視化、2 つの電子密度の差は 差電子密度 をご覧ください。