From 05515df4c2a629d67f06549c63b4120a4aacc0e3 Mon Sep 17 00:00:00 2001 From: abacus_fixer Date: Wed, 25 Nov 2026 22:29:22 +0800 Subject: [PATCH] fix(md): reject init_vel when STRU velocities are all zero When init_vel=true but the STRU file carries no velocity information, all atom velocities start at zero. The previous guard only rescaled velocities when the computed initial temperature was positive, so the zero-velocity case silently fell through with md_tfirst kept at its target value. The NHC thermostat then diverged exponentially from rest and aborted with the misleading message "Please set a proper md_tfreq!". Abort early with a clear message that names the real cause and tells the user to set init_vel=false to use random initial velocities. Add unit tests covering both MDCell branches (with and without backing UnitCell), the restart exemption, and the normal rescale path. --- source/source_md/md_func.cpp | 18 +++++- source/source_md/unittests/test_md_func.cpp | 68 +++++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/source/source_md/md_func.cpp b/source/source_md/md_func.cpp index 3b80b42250..54b928ac4a 100644 --- a/source/source_md/md_func.cpp +++ b/source/source_md/md_func.cpp @@ -240,7 +240,14 @@ void init_vel( read_vel(unit, velocities.data()); double kinetic = 0.0; const double current = current_temp(kinetic, unit.nat, frozen_freedom, masses.data(), velocities.data()); - if (!restart && temperature >= 0.0 && current > 0.0) + if (!restart && current <= 0.0) + { + ModuleBase::WARNING_QUIT( + "MD_func::init_vel", + "init_vel is set to true, but no velocity information was found in the STRU file " + "(all velocities are zero). Set init_vel to false to use random initial velocities."); + } + if (!restart && temperature >= 0.0) { rescale_vel(unit.nat, temperature, masses.data(), frozen_freedom, velocities.data()); } @@ -308,7 +315,14 @@ void init_vel( { double kinetic = 0.0; const double current = current_temp(kinetic, mdcell, frozen_freedom); - if (!restart && current > 0.0 && temperature > 0.0) + if (!restart && current <= 0.0) + { + ModuleBase::WARNING_QUIT( + "MD_func::init_vel", + "init_vel is set to true, but no velocity information was found in the STRU file " + "(all velocities are zero). Set init_vel to false to use random initial velocities."); + } + if (!restart && temperature > 0.0) { const double factor = sqrt(temperature / current); for (LocalAtom &atom : atoms) diff --git a/source/source_md/unittests/test_md_func.cpp b/source/source_md/unittests/test_md_func.cpp index 7d24cb48f4..8d5cd68efd 100644 --- a/source/source_md/unittests/test_md_func.cpp +++ b/source/source_md/unittests/test_md_func.cpp @@ -598,6 +598,74 @@ TEST_F(MD_func_test, print_stress) std::remove(log_path.c_str()); } +TEST_F(MD_func_test, init_vel_rejects_zero_stru_velocities_with_backing_unitcell) +{ + for (int it = 0; it < ucell.ntype; ++it) + { + for (int ia = 0; ia < ucell.atoms[it].na; ++ia) + { + ucell.atoms[it].vel[ia].set(0.0, 0.0, 0.0); + } + } + MDCell mdcell; + DomainDecomposition decomp; + initialize_mdcell_from_ucell(mdcell, ucell, decomp); + double temperature = 300.0 / ModuleBase::Hartree_to_K; + std::int64_t frozen_freedom = 0; + EXPECT_EXIT( + MD_func::init_vel(mdcell, true, false, temperature, frozen_freedom), + ::testing::ExitedWithCode(1), + ""); +} + +TEST_F(MD_func_test, init_vel_rejects_zero_stru_velocities_without_backing_unitcell) +{ + MDCell mdcell = Setcell::setup_mdcell(ucell); + for (LocalAtom &atom : mdcell.owned_atoms()) + { + atom.vel.set(0.0, 0.0, 0.0); + } + double temperature = 300.0 / ModuleBase::Hartree_to_K; + std::int64_t frozen_freedom = 0; + EXPECT_EXIT( + MD_func::init_vel(mdcell, true, false, temperature, frozen_freedom), + ::testing::ExitedWithCode(1), + ""); +} + +TEST_F(MD_func_test, init_vel_restart_allows_zero_stru_velocities) +{ + for (int it = 0; it < ucell.ntype; ++it) + { + for (int ia = 0; ia < ucell.atoms[it].na; ++ia) + { + ucell.atoms[it].vel[ia].set(0.0, 0.0, 0.0); + } + } + MDCell mdcell; + DomainDecomposition decomp; + initialize_mdcell_from_ucell(mdcell, ucell, decomp); + double temperature = 300.0 / ModuleBase::Hartree_to_K; + std::int64_t frozen_freedom = 0; + // restart = true must not abort even when the STRU velocities are all zero, + // because the velocities will be restored from the restart file later. + MD_func::init_vel(mdcell, true, true, temperature, frozen_freedom); + SUCCEED(); +} + +TEST_F(MD_func_test, init_vel_scales_nonzero_stru_velocities_to_target_temperature) +{ + MDCell mdcell; + DomainDecomposition decomp; + initialize_mdcell_from_ucell(mdcell, ucell, decomp); + double temperature = 300.0 / ModuleBase::Hartree_to_K; + std::int64_t frozen_freedom = 0; + MD_func::init_vel(mdcell, true, false, temperature, frozen_freedom); + double kinetic = 0.0; + const double current = MD_func::current_temp(kinetic, mdcell, frozen_freedom); + EXPECT_NEAR(current, temperature, 1e-12); +} + TEST_F(MD_func_test, current_md_info_mdcell_accepts_step_only_restart) { const std::string restart_file = testing::TempDir() + "Restart_md.txt";