Skip to content

Commit 4630fd4

Browse files
committed
refactor: Define accessor to reset the colvar total force
1 parent 8e7eb2d commit 4630fd4

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

src/colvar.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,7 +1409,7 @@ int colvar::calc()
14091409
error_code |= calc_cvcs();
14101410
if (!cvm::main()->proxy->total_forces_valid()) {
14111411
// Zero out the colvar total force when atomic total forces are not available
1412-
ft.reset();
1412+
reset_total_force();
14131413
}
14141414
if (error_code != COLVARS_OK) return error_code;
14151415
error_code |= collect_cvc_data();
@@ -1437,7 +1437,7 @@ int colvar::calc_cvcs(int first_cvc, size_t num_cvcs)
14371437
error_code |= calc_cvc_total_force(first_cvc, num_cvcs);
14381438
} else {
14391439
// Zero out the colvar total force when atomic total forces are not available
1440-
ft.reset();
1440+
reset_total_force();
14411441
}
14421442
// atom coordinates are updated by the next line
14431443
error_code |= calc_cvc_values(first_cvc, num_cvcs);
@@ -1687,6 +1687,7 @@ int colvar::calc_cvc_total_force(int first_cvc, size_t num_cvcs)
16871687
int colvar::collect_cvc_total_forces()
16881688
{
16891689
if (is_enabled(f_cv_total_force_calc)) {
1690+
16901691
ft.reset();
16911692

16921693
for (size_t i = 0; i < cvcs.size(); i++) {

src/colvar.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ 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+
}
90+
8591
/// \brief Typical fluctuation amplitude for this collective
8692
/// variable (e.g. local width of a free energy basin)
8793
///

src/colvar_gpu_calc.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ int colvarmodule_gpu_calc::cvc_calc_total_force(
3030
for (auto cvi = colvars.begin(); cvi != colvars.end(); cvi++) {
3131
// Calculate CVC total force
3232
if (!(*cvi)->is_enabled(colvardeps::f_cv_total_force_calc)) continue;
33+
if (!total_force_valid) {
34+
(*cvi)->reset_total_force();
35+
}
3336
const bool do_total_force =
3437
use_current_step ?
3538
(*cvi)->is_enabled(colvardeps::f_cv_total_force_current_step) :
@@ -41,12 +44,8 @@ int colvarmodule_gpu_calc::cvc_calc_total_force(
4144
const auto all_cvcs = (*cvi)->get_cvcs();
4245
for (auto cvc = all_cvcs.begin(); cvc != all_cvcs.end(); ++cvc) {
4346
if (!(*cvc)->is_enabled(colvardeps::f_cvc_active)) continue;
44-
if ((*cvc)->is_enabled(colvardeps::f_cvc_active)) {
45-
if (total_force_valid) {
46-
(*cvc)->calc_force_invgrads();
47-
} else {
48-
// TODO: (*cvc)->ft is a protected member. How could I do the reset?
49-
}
47+
if ((*cvc)->is_enabled(colvardeps::f_cvc_active) && total_force_valid) {
48+
(*cvc)->calc_force_invgrads();
5049
}
5150
}
5251
if (colvar_module->debug()) {

0 commit comments

Comments
 (0)