Skip to content

Commit

Permalink
Remove for loop from EwaldCalculator
Browse files Browse the repository at this point in the history
Fix #80, suggested by @ceriottm
  • Loading branch information
sirmarcel committed Oct 21, 2024
1 parent 7aea00f commit d7b465e
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/torchpme/calculators/ewald.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,14 @@ def _compute_kspace(
charges_reshaped = charges
sum_idx = 1

# Actual computation of trigonometric factors
# Actual computation of trigonometric factors
cos_all = torch.cos(trig_args)
sin_all = torch.sin(trig_args)
cos_summed = torch.sum(cos_all * charges_reshaped, dim=sum_idx)
sin_summed = torch.sum(sin_all * charges_reshaped, dim=sum_idx)

# Add up the contributions to compute the potential
energy = torch.zeros_like(charges)
for i in range(num_atoms):
energy[i] += torch.sum(
G * cos_all[:, i] * cos_summed, dim=sum_idx - 1
) + torch.sum(G * sin_all[:, i] * sin_summed, dim=sum_idx - 1)
cos_summed_G = torch.sum(cos_all * charges_reshaped, dim=sum_idx) * G
sin_summed_G = torch.sum(sin_all * charges_reshaped, dim=sum_idx) * G

energy = (cos_summed_G @ cos_all + sin_summed_G @ sin_all).T
energy /= torch.abs(cell.det())

# Remove the self-contribution: Using the Coulomb potential as an
Expand Down

0 comments on commit d7b465e

Please sign in to comment.