Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add general integer exponents #128

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/torchpme/potentials/inversepowerlaw.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Optional

import torch
from torch.special import gammainc, gammaincc, gammaln
from torch.special import gammaln

from .potential import Potential

Expand All @@ -17,6 +17,27 @@ def gamma(x: torch.Tensor) -> torch.Tensor:
return torch.exp(gammaln(x))


# Auxilary function for stable Fourier transform implementation
def gammainc_upper_over_powerlaw(exponent, zz):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Give the equation in the docstring here.

if exponent == 1:
return torch.exp(-zz) / zz
if exponent == 2:
return torch.sqrt(torch.pi / zz) * torch.erfc(torch.sqrt(zz))
if exponent == 3:
return -torch.expi(-zz)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know torch.expi doesn't exist in torch module ...

if exponent == 4:
return 2 * (
torch.exp(-zz) - torch.sqrt(torch.pi * zz) * torch.erfc(torch.sqrt(zz))
)
if exponent == 5:
return torch.exp(-zz) + zz * torch.expi(-zz)
if exponent == 6:
return (
(2 - 4 * zz) * torch.exp(-zz)
+ 4 * torch.sqrt(torch.pi) * zz**1.5 * torch.erfc(torch.sqrt(zz))
) / 3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need an error message if the exponent is not supported.

I think there is a check already at the init of InversePowerLawPotential.

I would maybe call this function in the init of InversePowerLawPotential for the given exponent and maybe zz=0 to not have a duplicated test. But up to you.



class InversePowerLawPotential(Potential):
"""
Inverse power-law potentials of the form :math:`1/r^p`.
Expand Down Expand Up @@ -46,7 +67,7 @@ class InversePowerLawPotential(Potential):

def __init__(
self,
exponent: float,
exponent: int,
smearing: Optional[float] = None,
exclusion_radius: Optional[float] = None,
dtype: Optional[torch.dtype] = None,
Expand Down Expand Up @@ -103,7 +124,7 @@ def lr_from_dist(self, dist: torch.Tensor) -> torch.Tensor:
x = 0.5 * dist**2 / smearing**2
peff = exponent / 2
prefac = 1.0 / (2 * smearing**2) ** peff
return prefac * gammainc(peff, x) / x**peff
return self.from_dist(dist) - prefac * gammainc_upper_over_powerlaw(exponent, x)

@torch.jit.export
def lr_from_k_sq(self, k_sq: torch.Tensor) -> torch.Tensor:
Expand Down Expand Up @@ -136,7 +157,7 @@ def lr_from_k_sq(self, k_sq: torch.Tensor) -> torch.Tensor:
return torch.where(
k_sq == 0,
0.0,
prefac * gammaincc(peff, masked) / masked**peff * gamma(peff),
prefac * gammainc_upper_over_powerlaw(exponent, masked),
)

def self_contribution(self) -> torch.Tensor:
Expand Down
Loading