Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions source/source_md/md_func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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)
Expand Down
68 changes: 68 additions & 0 deletions source/source_md/unittests/test_md_func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Loading