LAMMPS EFF 电子力场动能计算指南:compute ke/eff 命令详解

原创2026-10-14 18:34:291,604 阅读
文章标签:科学计算高性能计算科研

LAMMPS EFF 电子力场动能计算指南:compute ke/eff 命令详解

导读

compute ke/eff 是 LAMMPS EFF 包 提供的核心动能统计命令,专门用于计算"电子力场(eFF)"模型中一组核与电子的运动动能。本文将以 doc/src/compute_ke_eff.rst 文档为主体,结合 src/EFF/compute_ke_eff.cpp 等源码实现与 examples/PACKAGES/eff 下的真实示例脚本,系统讲解该命令的语法、物理公式、与 thermo_style 动能关键词的本质区别、正确的 eFF 温度监控方式,以及并行实现细节。读完本文,你将能够在 eFF 模拟中正确调用该 compute、解读其标量输出,并借助 compute temp/eff 建立可靠的温度监测体系。

compute ke/eff 语法与基本用法

命令语法

compute ID group-ID ke/eff

其中 ID 与 group-ID 的语义与所有 compute 命令一致(ID 为该计算的自定义名称,group-ID 指定参与计算的原子组),ke/eff 是该 compute 的风格名。ke/eff 不接受额外的关键字参数,参数个数严格固定:

ComputeKEEff::ComputeKEEff(LAMMPS *lmp, int narg, char **arg) :
  Compute(lmp, narg, arg)
{
  if (narg != 3) error->all(FLERR,"Illegal compute ke/eff command");

从 src/EFF/compute_ke_eff.cpp 可以看到,构造器在 narg != 3(即没有恰好包含命令名、ID、group-ID 三个参数)时直接报错 Illegal compute ke/eff command。同时在构造器内会检查 atom->electron_flag,若当前体系不是 atom_style electron,会立即抛出 Compute ke/eff requires atom style electron 错误——这保证了该 compute 只用于显式电子模型。

最小示例

compute 1 all ke/eff

该命令创建一个名为 1 的计算,统计 all 组内所有核与电子的动能。在真实的 eFF 输入脚本中,它通常与 pair_style eff/cut、atom_style electron 配合出现,例如 examples/PACKAGES/eff/H2/in.h2 中的典型骨架:

units           electron
atom_style      electron
read_data       data.h2
pair_style      eff/cut 6.447968 pressure/evirials
pair_coeff      * *

compute         effTemp all temp/eff
compute         effPress all pressure effTemp

thermo          100
thermo_style    custom step etotal pe ke temp press v_press c_regionT
thermo_modify   temp effTemp press effPress

fix             1 all nve/eff
run             10000

eFF 动能物理模型与计算公式

为什么需要单独的 ke/eff

eFF(electron Force Field)将电子描述为位置和尺寸都随时间演化的"漂浮高斯波包",是一种混合 QM-经典方法,由 Caltech 的 Andres Jaramillo-Botero 等人开发并贡献给 LAMMPS(参见 src/EFF/README 与 src/EFF/compute_ke_eff.cpp 的贡献者信息)。在这种模型里,粒子分为两类:

  • 原子核:点电荷,只有平动速度;
  • 电子:高斯波包,除了平动速度 v 外,还有径向速度 s(波包尺寸 eradius 的变化率),对应电子自旋自由度。

因此,普通动能公式无法完整描述电子的能量,必须由 ke/eff 按 eFF 特有的方式求和。

动能计算公式

文档 doc/src/compute_ke_eff.rst 给出的公式为:

  • 每个原子核:½ m v²
  • 每个电子:½ (m_e v² + ¾ m_e s²)

其中 m 为核质量,m_e 为电子质量,v 为粒子平动速度,s 为电子径向速度。该公式与 compute temp/eff 文档中的描述完全一致(其中核贡献为 ½ m v²、电子贡献为 ½ (m v² + ¾ m s²)),体现了 eFF 内核对核、电子采用同一套动能口径。

源码实现印证

src/EFF/compute_ke_eff.cpp 中的 compute_scalar() 给出了上述公式在 LAMMPS 中的精确实现:

double **v = atom->v;        // 平动速度
double *ervel = atom->ervel; // 电子径向速度
double *mass = atom->mass;
int *spin = atom->spin;      // 电子自旋标记
double mefactor = domain->dimension/4.0;

double ke = 0.0;
for (int i = 0; i < nlocal; i++)
  if (mask[i] & groupbit) {
    ke += mass[type[i]]*(v[i][0]*v[i][0] + v[i][1]*v[i][1] +
                         v[i][2]*v[i][2]);
    if (abs(spin[i])==1) ke += mefactor*mass[type[i]]*ervel[i]*ervel[i];
  }

几个值得注意的实现细节:

  1. 自旋判定:abs(spin<a href="https://link.gitcode.com/i/fbee909c58886d711851b62672403450" target="_blank">i])==1 用于识别电子粒子(原子核 spin 为 0)。这也与 [src/EFF/atom_vec_electron.cpp 中 create_atom_post() 将新建电子的 spin 初始化为 1、eradius 初始化为 1.0 的行为一致。
  2. 径向贡献系数:mefactor = domain->dimension/4.0,即三维时为 0.75,正好对应公式中的 ¾ m s²;二维时为 0.5,说明该系数会随模拟维度自动调整(dimension=2 或 3)。同样的 mefactor 也出现在 src/EFF/compute_temp_eff.cpp 与 src/EFF/compute_ke_atom_eff.cpp,三者口径一致。
  3. 单位换算:init() 中设置 pfactor = 0.5 * force->mvv2e(src/EFF/compute_ke_eff.cpp),最终 scalar *= pfactor。mvv2e 是 LAMMPS 的"质量×速度²→能量"换算因子,由 units 风格决定——例如在默认的 units electron(能量单位为 Hartree、距离单位为 Bohr)下自动完成单位转换。因此输出标量始终是能量单位(如 Hartree 或 eV)。

与 thermo 输出中 ke 关键词的区别

这是理解 ke/eff 最重要的一点。文档 doc/src/compute_ke_eff.rst 特别强调:ke/eff 计算的量与 thermo_style 中的 ke 或 etotal 关键词所反映的动能存在微妙差异:

  • ke/eff:直接按上述简单公式求和得到"平动 + 径向(仅电子)"动能,是纯机械量的直接统计;
  • thermo 的 ke:通过体系温度反推,按每个自由度 ½ k_B T 的能量折算。

两者仅在一种情况下严格相等——当温度由 compute temp/eff 计算时。因为 temp/eff 的动能口径与 ke/eff 完全一致:在 src/EFF/compute_temp_eff.cpp 中,compute_scalar() 累加 Σ m v² + Σ mefactor·m·s²(与 ke/eff 的累加项相同),再通过 T = KE · mvv2e / (dof · boltz) 换算温度,其中 dof = dimension·N - dimension·N_e(N 为核数、N_e 为电子数),即每核按 3/2 kT 均分(见 src/EFF/compute_temp_eff.cpp 的 dof_compute())。

而其他计算温度的方式(如 compute temp)可能剔除不同的非热速度分量、或计入其他自由度,导致"温度反推的 ke"与真实的机械动能不一致。因此,在 eFF 模拟中不能想当然地把 thermo 输出的 ke 当作动能真值。

eFF 温度监控:必须使用 compute temp/eff

文档在 doc/src/compute_ke_eff.rst 中给出了醒目的警告:eFF 模型的温度应当通过 compute temp/eff 监控,并可用 thermo_modify 将其接入热力学输出:

compute         effTemp all temp/eff
thermo_style    custom step etotal pe ke temp press
thermo_modify   temp effTemp

这段配置的实际效果是:thermo_modify temp effTemp 将热力学输出中的 temp 一列替换为 effTemp(即 temp/eff 计算的温度),从而避免默认温度计算对电子径向自由度处理不当的问题。在 examples/PACKAGES/eff/H_plasma/in.h2bulk.nve 中可以看到同样的模式:

compute         effTemp all temp/eff
thermo          10
thermo_style    custom step pe temp press
thermo_modify   temp effTemp

此外,examples/PACKAGES/eff/H/in.h_atom.spe.ang 还演示了将 temp/eff 进一步传递给 compute pressure,使压强计算同样使用 eFF 温度:

compute         effTemp all temp/eff
compute         effPress all pressure effTemp
thermo_modify   temp effTemp press effPress

temp/eff 的其他特性(补充)

compute temp/eff 本身输出一个强度量(与原子数无关)标量温度,同时计算一个 6 分量动能张量(供压强张量使用,径向电子速度同样计入),属于"extensive"向量;它支持 compute_modify dynamic 处理原子数变化的体系,也能通过 compute_modify extra 调整由 fix shake、fix rigid 等约束导致的自由度扣除(参见 doc/src/compute_temp_eff.rst)。temp/eff 的温度定义适用于温度远低于费米温度的情形——对密度为液氢 2~5 倍的材料,该费米温度约为 86,000~170,000 K。

输出信息:全局标量与 extensiveness

ke/eff 计算的是全局标量(总动能 KE),文档 doc/src/compute_ke_eff.rst 明确说明:

  • 该标量可被任何接受全局标量 compute 输出的命令使用(如 thermo_style 中的 c_ID、variable equal c_ID 等),关于 LAMMPS 输出选项的总览参见 Howto output;
  • 该标量是 "extensive"(广延量),即随组内粒子数增加而成比例增长;
  • 单位为能量单位。

在源码层面,scalar_flag = 1; extscalar = 1;(src/EFF/compute_ke_eff.cpp)正是"全局标量 + extensive"的标志位组合。与之对照,temp/eff 设置 extscalar = 0(温度是强度量),两者在输出性质上正好互补。

并行实现:跨 MPI 进程的动能汇总

compute ke/eff 完全支持 MPI 并行。其并行策略非常直接:每个进程只对本进程负责的局域原子(nlocal 个,且属于 groupbit 组)累加动能,最后通过一次全局归约汇总:

MPI_Allreduce(&ke,&scalar,1,MPI_DOUBLE,MPI_SUM,world);
scalar *= pfactor;

即所有进程的局部 ke 求和到根标量后,再统一乘以 pfactor(含 mvv2e 单位换算)。由于动能是严格可加的广延量,这种"局部累加 + 全局归约"的实现保证了多进程结果与串行一致。归约发生在 compute_scalar() 每次被调用时(以 invoked_scalar = update->ntimestep 记录调用时间步),因此无论用多少 MPI 进程运行,输出的 KE 值都不受进程数影响。

相关命令:compute ke/atom/eff 与温度控制链

虽然 ke/eff 的文档标注 Related commands 为 none,但在实际 eFF 工作流中它与以下命令构成完整闭环:

compute ke/atom/eff(逐原子动能)

如需每个原子的动能,可使用 src/EFF/compute_ke_atom_eff.cpp 实现的 ke/atom/eff(per-atom 版本)。其每原子公式为:

ke[i] = 0.5 * mvv2e * mass[type[i]] *
        (v[i][0]*v[i][0] + v[i][1]*v[i][1] + v[i][2]*v[i][2]);
if (abs(spin[i])==1)
  ke[i] += 0.5 * mvv2e * mefactor * mass[type[i]] * ervel[i]*ervel[i];

与 ke/eff 完全同源的公式,只是逐原子输出(组外原子置 0)。在 examples/PACKAGES/eff/H_plasma/in.h2bulk.nve 中它被用于 dump 输出每个粒子的动能:

compute         peatom all pe/atom
compute         keatom all ke/atom/eff
dump            3 all custom 1000 ${sname}.nve.lammpstrj id type x y z c_1[1] c_1[2] c_peatom c_keatom

eFF 温度控制命令链

EFF 包内还提供了一整套基于 temp/eff 温度的控温与控压命令(同位于 src/EFF 目录):fix nve/eff(src/EFF/fix_nve_eff.cpp)、fix nvt/eff、fix npt/eff、fix nph/eff、fix langevin/eff、fix temp/rescale/eff、compute temp/region/eff、compute temp/deform/eff 等。典型的控温示例同样出现在 examples/PACKAGES/eff/H2/in.h2:

fix     3 all temp/rescale/eff 10 0.0 300.0 0.02 1.0
fix     1 all nve/eff

使用限制:EFF 包与 atom_style electron 前置条件

文档 doc/src/compute_ke_eff.rst 的 Restrictions 部分明确了两个硬性前置条件:

  1. ke/eff 属于 EFF 包,只有在编译 LAMMPS 时启用了该包才可用(详见 Build package 页面);未启用时执行脚本会得到 "Unrecognized compute style" 之类的错误。
  2. 运行体系必须使用 atom_style electron(详见 atom_style),即粒子携带 q(电荷)、spin(自旋标记)、eradius(电子半径)、ervel(径向速度)、erforce 等 eFF 专属属性(见 src/EFF/atom_vec_electron.cpp 对 electron_flag 与各 peratom 属性标志的设置)。electron 风格下代表电子的粒子是三维高斯波包,其带宽由 eradius 描述(见 doc/src/atom_style.rst)。

典型使用场景小结

场景 推荐配置 参考示例
统计 eFF 体系总动能 compute 1 all ke/eff examples/PACKAGES/eff/H2/in.h2
正确的温度监测 compute effTemp all temp/eff + thermo_modify temp effTemp examples/PACKAGES/eff/H_plasma/in.h2bulk.nve
逐原子动能输出 compute keatom all ke/atom/eff + dump 引用 c_keatom examples/PACKAGES/eff/H_plasma/in.h2bulk.nve
eFF 压强与控温 compute pressure effTemp + fix nvt/eff / fix langevin/eff examples/PACKAGES/eff/H/in.h_atom.spe.ang

总结

compute ke/eff 是 eFF 模型中统计体系动能的标准入口:它同时计入原子核平动动能与电子的平动 + 径向动能(¾ m s² 项随维度自动缩放),输出为 extensive 的全局能量标量,并通过 MPI 归约保证并行一致性。其核心价值在于与 thermo 默认 ke 关键词的口径差异——只有配合 compute temp/eff 与 thermo_modify temp 才能获得物理自洽的动能-温度描述。实际使用中请务必:启用 EFF 包编译、使用 atom_style electron、以 temp/eff 监控温度,并可从 examples/PACKAGES/eff 下的 H2、H 等离子体、CH4、Li 枝晶等示例脚本直接获取完整可运行的模板。

登录后查看全文
lammps