構造とエネルギー

第一原理分子動力学

ASE の分子動力学モジュールと組み合わせて、Langevin 熱浴で温度を保つ第一原理分子動力学 (NVT) を実行します。

カテゴリ
構造とエネルギー
使用する機能
ase.md.langevin.LangevinMaxwellBoltzmannDistributionase.constraints.FixComTrajectory
スクリプト
molecular_dynamics.py

AOFDFTCalculator は ASE の Calculator なので、ASE の分子動力学モジュール (Langevin、VelocityVerlet など) とそのまま組み合わせられます。ここでは Si の結晶 (8 原子) を、Langevin 熱浴で 300 K に保ちながら 200 fs 計算します。各ステップで電子状態を SCF 計算で求めるため、電子密度や原子電荷の時間変化も追跡できます。

molecular_dynamics.pyダウンロード
"""
Advance/OF-DFT example: first-principles molecular dynamics with ASE.

Crystalline Si is kept at 300 K with a Langevin thermostat (NVT). The calculator
keeps the converged density matrix and uses it as the initial guess of the next
step, so the SCF of every MD step after the first is short.

Copyright (C) 2026 AdvanceSoft Corporation, All rights reserved.
"""

import matplotlib.pyplot as plt
import numpy as np
from ase import units
from ase.build import bulk
from ase.constraints import FixCom
from ase.io.trajectory import Trajectory
from ase.md.langevin import Langevin
from ase.md.velocitydistribution import MaxwellBoltzmannDistribution, Stationary

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"

T_TARGET = 300              # target temperature (K)
DT       = 2.0              # time step (fs)
N_STEPS  = 100              # number of MD steps (200 fs)
FRICTION = 0.02 / units.fs  # friction coefficient of the thermostat

rng = np.random.default_rng(0)  # fixed random numbers, to make the run reproducible

atoms = bulk("Si", crystalstructure="diamond", a=5.43, cubic=True)  # 8 atoms
atoms.set_constraint(FixCom())  # keeps the centre of mass at rest during the MD

atoms.calc = AOFDFTCalculator(
    console      = False,
    logging_file = "si_md.log",
    kin_type     = "AS26",
    kin_model    = "Crystal26",
    vps_dir      = VPS_DIR,
    pao_dir      = PAO_DIR,
)

# initial velocities at the target temperature, without centre-of-mass motion
MaxwellBoltzmannDistribution(atoms, temperature_K=T_TARGET, rng=rng)
Stationary(atoms)

# NVT molecular dynamics with a Langevin thermostat
dyn  = Langevin(atoms, timestep=DT * units.fs, temperature_K=T_TARGET, friction=FRICTION,
                fixcm=False, rng=rng)
traj = Trajectory("si_md.traj", "w", atoms)
dyn.attach(traj.write, interval=1)

times, temperatures = [], []


def report():
    e_pot = atoms.get_potential_energy() / len(atoms)
    times.append(dyn.nsteps * DT)
    temperatures.append(atoms.get_temperature())
    print(f"step {dyn.nsteps:3d}  t = {times[-1]:5.1f} fs  E_pot = {e_pot:.5f} eV/atom  "
          f"T = {temperatures[-1]:6.1f} K", flush=True)


dyn.attach(report, interval=1)
dyn.run(N_STEPS)
traj.close()

n_last = N_STEPS // 2
print(f"Mean temperature of the last {n_last} steps: {np.mean(temperatures[-n_last:]):.0f} K")

# temperature as a function of time
fig, ax = plt.subplots(figsize=(6.0, 3.6))
ax.plot(times, temperatures)
ax.axhline(T_TARGET, color="gray", linestyle="--", linewidth=0.8)
ax.set_xlabel("Time (fs)")
ax.set_ylabel("Temperature (K)")
fig.tight_layout()
fig.savefig("si_md_temperature.png", dpi=150)
print("Saved si_md_temperature.png")

実行結果の例

v1.0.0 (Crystal26) の結果です。

text
step   0  t =   0.0 fs  E_pot = -115.26326 eV/atom  T =  246.9 K
step   1  t =   2.0 fs  E_pot = -115.26285 eV/atom  T =  233.3 K
step   2  t =   4.0 fs  E_pot = -115.26130 eV/atom  T =  291.4 K
...
step  98  t = 196.0 fs  E_pot = -115.23784 eV/atom  T =  401.5 K
step  99  t = 198.0 fs  E_pot = -115.23993 eV/atom  T =  371.9 K
step 100  t = 200.0 fs  E_pot = -115.24096 eV/atom  T =  367.8 K
Mean temperature of the last 50 steps: 306 K
Saved si_md_temperature.png
温度の推移 (v1.0.0、Crystal26)。破線は熱浴の温度です。
温度の推移 (v1.0.0、Crystal26)。破線は熱浴の温度です。
  • 最初の約 20 fs で、温度が 120 K まで下がっています。完全な結晶から始めると、原子が動き出すにつれて運動エネルギーがポテンシャルエネルギーに移るためです。そのあと熱浴からエネルギーが供給され、34 fs で 300 K に達しています。
  • その後の温度は、約 160〜450 K の範囲で揺らいでいます。8 原子の小さい系なので揺らぎは大きいですが、後半 50 ステップの平均は 306 K で、熱浴の温度に合っています。

ポイント

  • 温度は Langevin の temperature_K で指定します。friction は熱浴の強さで、0.02 / units.fs は緩和時間 50 fs に相当します。
  • 初期速度は MaxwellBoltzmannDistribution で与え、Stationary で重心の運動を取り除いています。速度は乱数で決まるので、原子数が少ないと、最初の温度は指定した値からずれます (この例では 247 K)。
  • FixCom は、系全体の重心を固定する拘束です。Langevin 熱浴は原子にランダムな力を加えるので、拘束がないと重心が少しずつ動きます。Langevin には重心を固定する引数 fixcm もありますが、ASE 3.28 以降では非推奨なので、fixcm=False にして FixCom を使っています。
  • 乱数の種を np.random.default_rng(0) で固定しているので、初期速度と熱浴の乱数は実行のたびに同じになります。
  • 2 ステップ目以降は、直前のステップで収束した密度行列が SCF 計算の初期値として自動的に使われます。この計算での SCF の反復回数は、最初のステップが 11 回、2 ステップ目以降は 5〜6 回でした。
  • 時間刻みは 2 fs にしています。適切な値は元素や温度によって変わります。時間刻みの決め方と、ほかの熱浴については、液体 Al の分子動力学と動径分布関数 を参照してください。
  • 熱浴を使わない NVE の計算には、ase.md.verlet.VelocityVerlet を使います。既定の設定では、SCF 計算を安定させるための前処理の影響で、NVE でも全エネルギーが少し変化します。全エネルギーの保存が重要な場合の設定も、上の記事の「ポイント」で説明しています。
  • 軌跡は ASE の Trajectory 形式で保存しています。ase gui si_md.traj で確認できます。
  • 計算コストは原子数に比例するため、Kohn-Sham DFT では難しい大きさの系でも第一原理分子動力学を検討できます。原子数が多い場合は GPU の利用をおすすめします。