Skip to content

Commit

Permalink
Examples and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
E-Rum committed Feb 9, 2025
1 parent c763a09 commit e8bd8f6
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 38 deletions.
9 changes: 4 additions & 5 deletions examples/01-charges-example.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
cutoff=cutoff,
neighbor_indices=neighbor_indices,
neighbor_distances=neighbor_distances,
dtype=dtype,
)

# %%
Expand Down Expand Up @@ -103,9 +102,9 @@
# will be used to *compute* the potential energy of the system.

calculator = torchpme.PMECalculator(
torchpme.CoulombPotential(smearing=smearing, dtype=dtype), dtype=dtype, **pme_params
torchpme.CoulombPotential(smearing=smearing), **pme_params
)

calculator.to(dtype=dtype)
# %%
#
# Single Charge Channel
Expand Down Expand Up @@ -207,9 +206,9 @@
# creating a new calculator with the metatensor interface.

calculator_metatensor = torchpme.metatensor.PMECalculator(
torchpme.CoulombPotential(smearing=smearing, dtype=dtype), dtype=dtype, **pme_params
torchpme.CoulombPotential(smearing=smearing), **pme_params
)

calculator_metatensor.to(dtype=dtype)
# %%
#
# Computation with metatensor involves using Metatensor's :class:`System
Expand Down
4 changes: 1 addition & 3 deletions examples/02-neighbor-lists-usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@
cutoff=cutoff,
neighbor_indices=neighbor_indices,
neighbor_distances=neighbor_distances,
dtype=dtype,
)

# %%
Expand Down Expand Up @@ -195,8 +194,7 @@ def distances(
# compute the potential.

pme = torchpme.PMECalculator(
potential=torchpme.CoulombPotential(smearing=smearing, dtype=dtype),
dtype=dtype,
potential=torchpme.CoulombPotential(smearing=smearing),
**pme_params,
)
potential = pme(
Expand Down
2 changes: 1 addition & 1 deletion examples/07-lode-demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ def __init__(self, potential: Potential, n_grid: int = 3):
)

# assumes a smooth exclusion region so sets the integration cutoff to half that
nodes, weights = get_full_grid(n_grid, potential.exclusion_radius.item() / 2)
nodes, weights = get_full_grid(n_grid, potential.exclusion_radius / 2)

# these are the "stencils" used to project the potential
# on an atom-centered basis. NB: weights might also be incorporated
Expand Down
14 changes: 8 additions & 6 deletions examples/08-combined-potential.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@
# evaluation, and so one has to set it also for the combined potential, even if it is
# not used explicitly in the evaluation of the combination.

pot_1 = InversePowerLawPotential(exponent=1, smearing=smearing, dtype=dtype)
pot_2 = InversePowerLawPotential(exponent=2, smearing=smearing, dtype=dtype)

potential = CombinedPotential(potentials=[pot_1, pot_2], smearing=smearing, dtype=dtype)
pot_1 = InversePowerLawPotential(exponent=1, smearing=smearing)
pot_2 = InversePowerLawPotential(exponent=2, smearing=smearing)
pot_1 = pot_1.to(dtype=dtype)
pot_2 = pot_2.to(dtype=dtype)
potential = CombinedPotential(potentials=[pot_1, pot_2], smearing=smearing)
potential = potential.to(dtype=dtype)

# Note also that :class:`CombinedPotential` can be used with any combination of
# potentials, as long they are all either direct or range separated. For instance, one
Expand Down Expand Up @@ -156,9 +158,9 @@
# much bigger system.

calculator = EwaldCalculator(
potential=potential, lr_wavelength=lr_wavelength, prefactor=eV_A, dtype=dtype
potential=potential, lr_wavelength=lr_wavelength, prefactor=eV_A
)

calculator.to(dtype=dtype)

# %%
#
Expand Down
21 changes: 4 additions & 17 deletions examples/10-tuning.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,10 @@
pme_params = {"mesh_spacing": 1.0, "interpolation_nodes": 4}

pme = torchpme.PMECalculator(
potential=torchpme.CoulombPotential(smearing=smearing, device=device, dtype=dtype),
device=device,
dtype=dtype,
potential=torchpme.CoulombPotential(smearing=smearing),
**pme_params, # type: ignore[arg-type]
)

pme.to(device=device, dtype=dtype)
# %%
# Run the calculator
# ~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -170,8 +168,6 @@
neighbor_indices=neighbor_indices,
neighbor_distances=neighbor_distances,
run_backward=True,
device=device,
dtype=dtype,
)
estimated_timing = timings(pme)

Expand Down Expand Up @@ -220,14 +216,11 @@ def timed_madelung(cutoff, smearing, mesh_spacing, interpolation_nodes, device,
)

pme = torchpme.PMECalculator(
potential=torchpme.CoulombPotential(
smearing=smearing, device=device, dtype=dtype
),
potential=torchpme.CoulombPotential(smearing=smearing),
mesh_spacing=mesh_spacing,
interpolation_nodes=interpolation_nodes,
device=device,
dtype=dtype,
)
pme.to(device=device, dtype=dtype)
potential = pme(
charges=charges,
cell=cell,
Expand All @@ -247,8 +240,6 @@ def timed_madelung(cutoff, smearing, mesh_spacing, interpolation_nodes, device,
run_backward=True,
n_warmup=1,
n_repeat=4,
device=device,
dtype=dtype,
)
estimated_timing = timings(pme)
return madelung, estimated_timing
Expand Down Expand Up @@ -457,8 +448,6 @@ def timed_madelung(cutoff, smearing, mesh_spacing, interpolation_nodes, device,
cutoff=5.0,
neighbor_indices=neighbor_indices,
neighbor_distances=neighbor_distances,
device=device,
dtype=dtype,
)

print(
Expand Down Expand Up @@ -492,8 +481,6 @@ def timed_madelung(cutoff, smearing, mesh_spacing, interpolation_nodes, device,
cutoff=cutoff,
neighbor_indices=filter_indices,
neighbor_distances=filter_distances,
device=device,
dtype=dtype,
)
timings_grid.append(timing)

Expand Down
9 changes: 4 additions & 5 deletions examples/basic-usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@
# contains all the necessary functions (such as those defining the short-range and
# long-range splits) for this potential and makes them useable in the rest of the code.

potential = CoulombPotential(smearing=smearing, device=device, dtype=dtype)
potential = CoulombPotential(smearing=smearing)
potential.to(device=device, dtype=dtype)

# %%
#
Expand Down Expand Up @@ -193,10 +194,8 @@
# Since our structure is relatively small, we use the :class:`EwaldCalculator`.
# We start by the initialization of the class.

calculator = EwaldCalculator(
potential=potential, lr_wavelength=lr_wavelength, device=device, dtype=dtype
)

calculator = EwaldCalculator(potential=potential, lr_wavelength=lr_wavelength)
calculator.to(device=device, dtype=dtype)
# %%
#
# Compute Energy
Expand Down
5 changes: 5 additions & 0 deletions src/torchpme/calculators/p3m.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ def __init__(
prefactor=prefactor,
)

if potential.smearing is None:
raise ValueError(
"Must specify smearing to use a potential with P3MCalculator"
)

cell = torch.eye(
3,
device=self.potential.smearing.device,
Expand Down
2 changes: 1 addition & 1 deletion src/torchpme/calculators/pme.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(

if potential.smearing is None:
raise ValueError(
"Must specify smearing to use a potential with EwaldCalculator"
"Must specify smearing to use a potential with PMECalculator"
)

self.mesh_spacing: float = mesh_spacing
Expand Down

0 comments on commit e8bd8f6

Please sign in to comment.