Skip to content

Commit 0eb407d

Browse files
committed
optimize code thanks to HanatoK's recommendations
1 parent ac2a7a6 commit 0eb407d

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/colvargrid_integrate.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ void colvargrid_integrate::update_weighted_div_local(const std::vector<int> &ix0
317317
{
318318
const size_t linear_index = computation_grid->address(ix0);
319319
cvm::real div_at_point = 0;
320-
for (std::vector<int> surrounding_point_relative_position :
320+
for (const std::vector<int>& surrounding_point_relative_position :
321321
surrounding_points_relative_positions) {
322322
std::vector<int> surrounding_point_coordinates = ix0;
323323
std::vector<cvm::real> gradient_at_surrounding_point(nd, 0);
@@ -330,7 +330,7 @@ void colvargrid_integrate::update_weighted_div_local(const std::vector<int> &ix0
330330
cvm::real weight = regularized_weights[gradients->address(surrounding_point_coordinates)];
331331

332332
for (size_t i = 0; i < nd; i++) {
333-
div_at_point += pow(-1, surrounding_point_relative_position[i] + 1) *
333+
div_at_point += (((surrounding_point_relative_position[i] & 1) << 1) - 1) *
334334
gradient_at_surrounding_point[i] * weight / widths[i];
335335
}
336336
}
@@ -1053,7 +1053,7 @@ void colvargrid_integrate::get_regularized_grad(std::vector<cvm::real> &F, std::
10531053
F[i] = multiplier * F[i];
10541054
}
10551055
}
1056-
1056+
// TODO: check if there's not a more optimized version avoiding push_back calls
10571057
std::vector<cvm::real> colvargrid_integrate::compute_averaged_border_normal_gradient(
10581058
std::vector<int> ghost_point_coordinates)
10591059
{
@@ -1071,12 +1071,13 @@ std::vector<cvm::real> colvargrid_integrate::compute_averaged_border_normal_grad
10711071
}
10721072
}
10731073
// Find the positions of the gradients to average
1074+
size_t nb_averaged_gradients = static_cast<int>(cvm::integer_power(2, static_cast<int>(directions_to_average_along.size())));
10741075
std::vector<std::vector<int>> gradients_to_average_relative_positions;
10751076
if (directions_to_average_along.size() == 0) {
10761077
std::vector<int> zero_vector(nd, 0);
10771078
gradients_to_average_relative_positions.push_back(zero_vector);
10781079
} else {
1079-
for (int i = 0; i < pow(2, directions_to_average_along.size()); i++) {
1080+
for (int i = 0; i < nb_averaged_gradients; i++) {
10801081
std::vector<int> gradient_to_average_relative_position(nd, 0);
10811082
std::vector<int> binary = convert_base_two(i, directions_to_average_along.size());
10821083
for (size_t bit_position = 0; bit_position < directions_to_average_along.size();

0 commit comments

Comments
 (0)