From 14a0b2f0541a08c783359daa7498a22e722b7a51 Mon Sep 17 00:00:00 2001 From: HanatoK Date: Thu, 12 Sep 2024 17:20:29 -0500 Subject: [PATCH] refactor: simplify the template and function parameters of calc_fit_forces_impl --- src/colvaratoms.cpp | 30 ++++++++++++------------------ src/colvaratoms.h | 2 -- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/colvaratoms.cpp b/src/colvaratoms.cpp index 0f07c5045..1feadd1fc 100644 --- a/src/colvaratoms.cpp +++ b/src/colvaratoms.cpp @@ -1218,15 +1218,15 @@ void cvm::atom_group::calc_fit_gradients() cvm::log("Calculating fit gradients.\n"); cvm::atom_group *group_for_fit = fitting_group ? fitting_group : this; - auto accessor = [](const std::vector& v, size_t i){return v[i].grad;}; + auto accessor = [this](size_t i){return atoms[i].grad;}; if (is_enabled(f_ag_center) && is_enabled(f_ag_rotate)) - calc_fit_forces_impl(atoms, group_for_fit->fit_gradients, accessor); + calc_fit_forces_impl(group_for_fit->fit_gradients, accessor); if (is_enabled(f_ag_center) && !is_enabled(f_ag_rotate)) - calc_fit_forces_impl(atoms, group_for_fit->fit_gradients, accessor); + calc_fit_forces_impl(group_for_fit->fit_gradients, accessor); if (!is_enabled(f_ag_center) && is_enabled(f_ag_rotate)) - calc_fit_forces_impl(atoms, group_for_fit->fit_gradients, accessor); + calc_fit_forces_impl(group_for_fit->fit_gradients, accessor); if (!is_enabled(f_ag_center) && !is_enabled(f_ag_rotate)) - calc_fit_forces_impl(atoms, group_for_fit->fit_gradients, accessor); + calc_fit_forces_impl(group_for_fit->fit_gradients, accessor); if (cvm::debug()) cvm::log("Done calculating fit gradients.\n"); @@ -1234,10 +1234,8 @@ void cvm::atom_group::calc_fit_gradients() template void cvm::atom_group::calc_fit_forces_impl( - const main_force_container_T& forces_on_main_group, std::vector& forces_on_fitting_group, main_force_accessor_T accessor) const { const cvm::atom_group *group_for_fit = fitting_group ? fitting_group : this; @@ -1255,7 +1253,7 @@ void cvm::atom_group::calc_fit_forces_impl( for (size_t i = 0; i < size(); i++) { cvm::atom_pos pos_orig; if (B_ag_center) { - atom_grad += accessor(forces_on_main_group, i); + atom_grad += accessor(i); if (B_ag_rotate) pos_orig = rot_inv * (atoms[i].pos - ref_pos_cog); } else { if (B_ag_rotate) pos_orig = atoms[i].pos; @@ -1263,7 +1261,7 @@ void cvm::atom_group::calc_fit_forces_impl( if (B_ag_rotate) { // calculate \partial(R(q) \vec{x}_i)/\partial q) \cdot \partial\xi/\partial\vec{x}_i cvm::quaternion const dxdq = - rot.q.position_derivative_inner(pos_orig, accessor(forces_on_main_group, i)); + rot.q.position_derivative_inner(pos_orig, accessor(i)); sum_dxdq[0] += dxdq[0]; sum_dxdq[1] += dxdq[1]; sum_dxdq[2] += dxdq[2]; @@ -1297,19 +1295,15 @@ std::vector cvm::atom_group::calc_fit_forces( std::vector& forces_on_fitting_group) const { if (cvm::debug()) cvm::log("Calculating fit forces.\n"); - auto accessor = [](const std::vector& v, size_t i){return v[i];}; + auto accessor = [&forces_on_main_group](size_t i){return forces_on_main_group[i];}; if (is_enabled(f_ag_center) && is_enabled(f_ag_rotate)) - calc_fit_forces_impl( - forces_on_main_group, forces_on_fitting_group, accessor); + calc_fit_forces_impl(forces_on_fitting_group, accessor); if (is_enabled(f_ag_center) && !is_enabled(f_ag_rotate)) - calc_fit_forces_impl( - forces_on_main_group, forces_on_fitting_group, accessor); + calc_fit_forces_impl(forces_on_fitting_group, accessor); if (!is_enabled(f_ag_center) && is_enabled(f_ag_rotate)) - calc_fit_forces_impl( - forces_on_main_group, forces_on_fitting_group, accessor); + calc_fit_forces_impl(forces_on_fitting_group, accessor); if (!is_enabled(f_ag_center) && !is_enabled(f_ag_rotate)) - calc_fit_forces_impl( - forces_on_main_group, forces_on_fitting_group, accessor); + calc_fit_forces_impl(forces_on_fitting_group, accessor); if (cvm::debug()) cvm::log("Done calculating fit forces.\n"); diff --git a/src/colvaratoms.h b/src/colvaratoms.h index 1dced9a86..2dbe66305 100644 --- a/src/colvaratoms.h +++ b/src/colvaratoms.h @@ -512,10 +512,8 @@ class colvarmodule::atom_group // template void calc_fit_gradients_impl(); template void calc_fit_forces_impl( - const main_force_container_T& forces_on_main_group, std::vector& forces_on_fitting_group, main_force_accessor_T accessor) const;