Skip to content

Commit 357595d

Browse files
committed
refactor: When atomic total forces are invalid, ensure colvar total force is zero after collection
1 parent f363f25 commit 357595d

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

src/colvar.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,10 +1407,6 @@ int colvar::calc()
14071407
error_code |= update_cvc_flags();
14081408
if (error_code != COLVARS_OK) return error_code;
14091409
error_code |= calc_cvcs();
1410-
if (!cvm::main()->proxy->total_forces_valid()) {
1411-
// Zero out the colvar total force when atomic total forces are not available
1412-
reset_total_force();
1413-
}
14141410
if (error_code != COLVARS_OK) return error_code;
14151411
error_code |= collect_cvc_data();
14161412
}
@@ -1435,9 +1431,6 @@ int colvar::calc_cvcs(int first_cvc, size_t num_cvcs)
14351431
// Use Jacobian derivative computed at previous timestep and the total forces from the same
14361432
// step, collected just now from the engine
14371433
error_code |= calc_cvc_total_force(first_cvc, num_cvcs);
1438-
} else {
1439-
// Zero out the colvar total force when atomic total forces are not available
1440-
reset_total_force();
14411434
}
14421435
// atom coordinates are updated by the next line
14431436
error_code |= calc_cvc_values(first_cvc, num_cvcs);
@@ -1476,6 +1469,11 @@ int colvar::collect_cvc_data()
14761469
}
14771470
error_code |= calc_colvar_properties();
14781471

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+
14791477
if (cvm::debug())
14801478
cvm::log("Done calculating colvar \""+this->name+"\"'s properties.\n");
14811479

src/colvar_gpu_calc.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ int colvarmodule_gpu_calc::cvc_calc_total_force(
2323
colvarmodule* colvar_module,
2424
bool use_current_step) {
2525
int error_code = COLVARS_OK;
26+
const bool total_force_valid = colvar_module->proxy ? colvar_module->proxy->total_forces_valid() : false;
27+
if (!total_force_valid) {
28+
return error_code;
29+
}
2630
#if defined (COLVARS_NVTX_PROFILING)
2731
cvc_calc_total_force_prof.start();
2832
#endif // defined (COLVARS_NVTX_PROFILING)
29-
const bool total_force_valid = colvar_module->proxy ? colvar_module->proxy->total_forces_valid() : false;
3033
for (auto cvi = colvars.begin(); cvi != colvars.end(); cvi++) {
3134
// Calculate CVC total force
3235
if (!(*cvi)->is_enabled(colvardeps::f_cv_total_force_calc)) continue;
33-
if (!total_force_valid) {
34-
(*cvi)->reset_total_force();
35-
}
3636
const bool do_total_force =
3737
use_current_step ?
3838
(*cvi)->is_enabled(colvardeps::f_cv_total_force_current_step) :
@@ -43,8 +43,7 @@ int colvarmodule_gpu_calc::cvc_calc_total_force(
4343
}
4444
const auto all_cvcs = (*cvi)->get_cvcs();
4545
for (auto cvc = all_cvcs.begin(); cvc != all_cvcs.end(); ++cvc) {
46-
if (!(*cvc)->is_enabled(colvardeps::f_cvc_active)) continue;
47-
if ((*cvc)->is_enabled(colvardeps::f_cvc_active) && total_force_valid) {
46+
if ((*cvc)->is_enabled(colvardeps::f_cvc_active)) {
4847
(*cvc)->calc_force_invgrads();
4948
}
5049
}
@@ -361,11 +360,16 @@ int colvarmodule_gpu_calc::cvc_calc_Jacobian_derivative(const std::vector<colvar
361360

362361
int colvarmodule_gpu_calc::cv_collect_cvc_data(const std::vector<colvar*>& colvars, colvarmodule* colvar_module) {
363362
int error_code = COLVARS_OK;
363+
const bool total_force_valid = colvar_module->proxy ? colvar_module->proxy->total_forces_valid() : false;
364364
#if defined (COLVARS_NVTX_PROFILING)
365365
cv_collect_cvc_data_prof.start();
366366
#endif // defined (COLVARS_NVTX_PROFILING)
367367
for (auto cvi = colvars.begin(); cvi != colvars.end(); cvi++) {
368-
error_code |= (*cvi)->collect_cvc_data();
368+
if (total_force_valid) {
369+
error_code |= (*cvi)->collect_cvc_data();
370+
} else {
371+
(*cvi)->reset_total_force();
372+
}
369373
if (colvar_module->get_error()) {
370374
return COLVARS_ERROR;
371375
}

0 commit comments

Comments
 (0)