Skip to content

Commit 1e3d569

Browse files
committed
fix: ensure that total forces are reset before calc_colvar_properties if
invalid This commit simplifies the logic of resetting the total forces by just checking if they are available before accumulating them in collect_cvc_total_forces. This can ensure that ft_reported is not reset after calc_colvar_properties. This commit also simplifies the GPU code path by just reusing the check in collect_cvc_total_forces.
1 parent 480a613 commit 1e3d569

File tree

3 files changed

+23
-36
lines changed

3 files changed

+23
-36
lines changed

src/colvar.cpp

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,7 @@ int colvar::collect_cvc_data()
14551455

14561456
int error_code = COLVARS_OK;
14571457

1458-
if (cvm::main()->proxy->total_forces_valid() && (!is_enabled(f_cv_total_force_current_step))) {
1458+
if (!is_enabled(f_cv_total_force_current_step)) {
14591459
// Total force depends on Jacobian derivative from previous timestep
14601460
// collect_cvc_total_forces() uses the previous value of jd
14611461
error_code |= collect_cvc_total_forces();
@@ -1469,11 +1469,6 @@ int colvar::collect_cvc_data()
14691469
}
14701470
error_code |= calc_colvar_properties();
14711471

1472-
if (!cvm::main()->proxy->total_forces_valid()) {
1473-
// Zero out the colvar total force when atomic total forces are not available
1474-
reset_total_force();
1475-
}
1476-
14771472
if (cvm::debug())
14781473
cvm::log("Done calculating colvar \""+this->name+"\"'s properties.\n");
14791474

@@ -1688,24 +1683,26 @@ int colvar::collect_cvc_total_forces()
16881683

16891684
ft.reset();
16901685

1691-
for (size_t i = 0; i < cvcs.size(); i++) {
1692-
if (!cvcs[i]->is_enabled()) continue;
1693-
if (cvm::debug())
1694-
cvm::log("Colvar component no. "+cvm::to_str(i+1)+
1695-
" within colvar \""+this->name+"\" has total force "+
1696-
cvm::to_str((cvcs[i])->total_force(),
1697-
cvm::cv_width, cvm::cv_prec)+".\n");
1698-
// linear combination is assumed
1699-
ft += (cvcs[i])->total_force() * (cvcs[i])->sup_coeff / active_cvc_square_norm;
1700-
}
1686+
if (cvm::main()->proxy->total_forces_valid()) {
1687+
for (size_t i = 0; i < cvcs.size(); i++) {
1688+
if (!cvcs[i]->is_enabled()) continue;
1689+
if (cvm::debug())
1690+
cvm::log("Colvar component no. "+cvm::to_str(i+1)+
1691+
" within colvar \""+this->name+"\" has total force "+
1692+
cvm::to_str((cvcs[i])->total_force(),
1693+
cvm::cv_width, cvm::cv_prec)+".\n");
1694+
// linear combination is assumed
1695+
ft += (cvcs[i])->total_force() * (cvcs[i])->sup_coeff / active_cvc_square_norm;
1696+
}
17011697

1702-
if (!(is_enabled(f_cv_hide_Jacobian) && is_enabled(f_cv_subtract_applied_force))) {
1703-
// This is by far the most common case
1704-
// Add the Jacobian force to the total force, and don't apply any silent
1705-
// correction internally: biases such as colvarbias_abf will handle it
1706-
// If f_cv_hide_Jacobian is enabled, a force of -fj is present in ft due to the
1707-
// Jacobian-compensating force
1708-
ft += fj;
1698+
if (!(is_enabled(f_cv_hide_Jacobian) && is_enabled(f_cv_subtract_applied_force))) {
1699+
// This is by far the most common case
1700+
// Add the Jacobian force to the total force, and don't apply any silent
1701+
// correction internally: biases such as colvarbias_abf will handle it
1702+
// If f_cv_hide_Jacobian is enabled, a force of -fj is present in ft due to the
1703+
// Jacobian-compensating force
1704+
ft += fj;
1705+
}
17091706
}
17101707
}
17111708

src/colvar.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,6 @@ class colvar : public colvarparse, public colvardeps {
8282
/// subtracted.
8383
colvarvalue const & total_force() const;
8484

85-
/// Reset the total force to zero
86-
inline void reset_total_force()
87-
{
88-
ft.reset();
89-
ft_reported.reset();
90-
}
91-
9285
/// \brief Typical fluctuation amplitude for this collective
9386
/// variable (e.g. local width of a free energy basin)
9487
///

src/colvar_gpu_calc.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -619,16 +619,13 @@ int colvarmodule_gpu_calc::cvc_calc_Jacobian_derivative(
619619

620620
int colvarmodule_gpu_calc::cv_collect_cvc_data(const std::vector<colvar*>& colvars, colvarmodule* colvar_module) {
621621
int error_code = COLVARS_OK;
622-
const bool total_force_valid = colvar_module->proxy ? colvar_module->proxy->total_forces_valid() : false;
623622
#if defined (COLVARS_NVTX_PROFILING)
624623
cv_collect_cvc_data_prof.start();
625624
#endif // defined (COLVARS_NVTX_PROFILING)
626625
for (auto cvi = colvars.begin(); cvi != colvars.end(); cvi++) {
627-
if (total_force_valid) {
628-
error_code |= (*cvi)->collect_cvc_data();
629-
} else {
630-
(*cvi)->reset_total_force();
631-
}
626+
// If the total forces are not available, it will be reset in
627+
// collect_cvc_total_forces anyway (called by collect_cvc_data)
628+
error_code |= (*cvi)->collect_cvc_data();
632629
if (colvar_module->get_error()) {
633630
return COLVARS_ERROR;
634631
}

0 commit comments

Comments
 (0)