Skip to content

Commit

Permalink
enable macOS and Windows testsuite (#114)
Browse files Browse the repository at this point in the history
* enable macOS and Windows testsuite
* split tests and min-tests
  • Loading branch information
PicoCentauri authored Dec 5, 2024
1 parent 540871f commit 8b3a143
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 42 deletions.
13 changes: 13 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- What does this implement/fix? Explain your changes here. -->



# Contributor (creator of pull-request) checklist

- [ ] Tests updated (for new features and bugfixes)?
- [ ] Documentation updated (for new features)?
- [ ] Issue referenced (for PRs that solve an issue)?

# Reviewer checklist

- [ ] CHANGELOG updated with public API or any other important changes?
39 changes: 31 additions & 8 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,43 @@ on:
# Check all PR

jobs:
tests:
min:
runs-on: ${{ matrix.os }}
strategy:
matrix:
include:
# - os: ubuntu-22.04
# python-version: "3.9"
- os: ubuntu-22.04
python-version: "3.9"
- os: ubuntu-22.04
python-version: "3.12"
- os: macos-14
python-version: "3.12"
- os: windows-2022
python-version: "3.12"

steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- run: pip install tox coverage[toml]
- name: run Python minimal tests
run: tox -e tests-min

all:
runs-on: ${{ matrix.os }}
strategy:
matrix:
include:
- os: ubuntu-22.04
python-version: "3.9"
- os: ubuntu-22.04
python-version: "3.12"
- os: macos-14
python-version: "3.12"
- os: windows-2022
python-version: "3.12"
# - os: macos-14
# python-version: "3.12"
# - os: windows-2022
# python-version: "3.12"

steps:
- uses: actions/checkout@v4
Expand All @@ -30,7 +54,6 @@ jobs:
- run: pip install tox coverage[toml]
- name: run Python tests
run: |
tox -e tests-min
tox -e tests
coverage xml
- name: upload to codecov.io
Expand Down
18 changes: 11 additions & 7 deletions src/torchpme/utils/tuning/ewald.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def tune_ewald(
Example
-------
>>> import torch
>>> from vesin.torch import NeighborList
>>> from vesin import NeighborList
>>> positions = torch.tensor(
... [[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]], dtype=torch.float64
... )
Expand All @@ -96,19 +96,23 @@ def tune_ewald(
example, fixing the cutoff to 0.1
>>> smearing, parameter, cutoff = tune_ewald(
... torch.sum(charges**2, dim=0), cell, positions, cutoff=0.1, accuracy=1e-1
... torch.sum(charges**2, dim=0), cell, positions, cutoff=0.4, accuracy=1e-1
... )
You can check the values of the parameters, now the cutoff is fixed
>>> print(smearing)
0.03338293212285428
>>> print(round(smearing, 4))
0.1402
>>> print(parameter)
{'lr_wavelength': 0.009495381717988971}
We can also check the value of the other parameter like the ``lr_wavelength``
>>> print(round(parameter["lr_wavelength"], 3))
0.255
and finally as requested the value of the cutoff is fixed
>>> print(cutoff)
0.1
0.4
"""
_validate_parameters(sum_squared_charges, cell, positions, exponent, accuracy)
Expand Down
2 changes: 1 addition & 1 deletion src/torchpme/utils/tuning/p3m.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def tune_p3m(
Example
-------
>>> import torch
>>> from vesin.torch import NeighborList
>>> from vesin import NeighborList
>>> _ = torch.manual_seed(0)
>>> positions = torch.tensor(
... [[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]], dtype=torch.float64
Expand Down
2 changes: 1 addition & 1 deletion src/torchpme/utils/tuning/pme.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def tune_pme(
Example
-------
>>> import torch
>>> from vesin.torch import NeighborList
>>> from vesin import NeighborList
>>> _ = torch.manual_seed(0)
>>> positions = torch.tensor(
... [[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]], dtype=torch.float64
Expand Down
4 changes: 2 additions & 2 deletions tests/calculators/test_values_direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from torchpme import Calculator, CoulombPotential

sys.path.append(str(Path(__file__).parents[1]))
from helpers import neighbor_list_torch
from helpers import neighbor_list

DTYPE = torch.float64

Expand Down Expand Up @@ -184,7 +184,7 @@ def test_coulomb_exact(
positions = scaling_factor * (positions @ orthogonal_transformation)

# Choose a large cutoff that covers all atoms
neighbor_indices, neighbor_distances = neighbor_list_torch(
neighbor_indices, neighbor_distances = neighbor_list(
positions=positions,
periodic=False,
cutoff=scaling_factor * 10,
Expand Down
35 changes: 18 additions & 17 deletions tests/calculators/test_values_ewald.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
COULOMB_TEST_FRAMES,
compute_distances,
define_crystal,
neighbor_list_torch,
neighbor_list,
)

DTYPE = torch.float64
Expand Down Expand Up @@ -127,7 +127,7 @@ def test_madelung(crystal_name, scaling_factor, calc_name):
rtol = 9e-4

# Compute neighbor list
neighbor_indices, neighbor_distances = neighbor_list_torch(
neighbor_indices, neighbor_distances = neighbor_list(
positions=pos, periodic=True, box=cell, cutoff=sr_cutoff
)

Expand Down Expand Up @@ -178,7 +178,7 @@ def test_wigner(crystal_name, scaling_factor):
madelung_ref /= scaling_factor

# Compute neighbor list
neighbor_indices, neighbor_distances = neighbor_list_torch(
neighbor_indices, neighbor_distances = neighbor_list(
positions=positions, periodic=True, box=cell
)

Expand Down Expand Up @@ -272,14 +272,24 @@ def test_random_structure(
prefactor=torchpme.utils.prefactors.eV_A,
)

positions.requires_grad = True

neighbor_indices, neighbor_distances = neighbor_list_torch(
neighbor_indices, neighbor_shifts = neighbor_list(
positions=positions,
periodic=True,
box=cell,
cutoff=cutoff,
full_neighbor_list=full_neighbor_list,
neighbor_shifts=True,
)

# Use manual distance calculation to avoiding issues with the torchscript version of
# vesin on Windows. See https://github.com/Luthaf/vesin/issues/21 for more details.
positions.requires_grad = True

neighbor_distances = compute_distances(
positions=positions,
neighbor_indices=neighbor_indices,
cell=cell,
neighbor_shifts=neighbor_shifts,
)

calc.to(dtype=DTYPE)
Expand All @@ -303,21 +313,12 @@ def test_random_structure(
forces_target = torch.tensor(frame.get_forces(), dtype=DTYPE) / scaling_factor**2
torch.testing.assert_close(forces, forces_target @ ortho, atol=0.0, rtol=5e-3)

neighbor_indices, neighbor_shifts = neighbor_list_torch(
positions=positions,
periodic=True,
box=cell,
cutoff=cutoff,
full_neighbor_list=full_neighbor_list,
neighbor_shifts=True,
)

# Compute stress
def energy_wrt_strain(strain):
strained_R = positions + torch.einsum("ab,ib->ia", strain, positions)
strained_cell = cell + torch.einsum("ab,Ab->Aa", strain, cell)

d = compute_distances(
neighbor_distances = compute_distances(
strained_R,
neighbor_indices,
cell=strained_cell,
Expand All @@ -330,7 +331,7 @@ def energy_wrt_strain(strain):
cell=strained_cell,
positions=strained_R,
neighbor_indices=neighbor_indices,
neighbor_distances=d,
neighbor_distances=neighbor_distances,
)
).sum()

Expand Down
9 changes: 7 additions & 2 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Optional

import torch
from vesin.torch import NeighborList
from vesin import NeighborList

DTYPE = torch.float64
SQRT3 = math.sqrt(3)
Expand Down Expand Up @@ -239,7 +239,7 @@ def define_crystal(crystal_name="CsCl"):
return positions, charges, cell, madelung_ref, num_formula_units


def neighbor_list_torch(
def neighbor_list(
positions: torch.tensor,
periodic: bool = True,
box: Optional[torch.tensor] = None,
Expand All @@ -260,6 +260,11 @@ def neighbor_list_torch(
points=positions, box=box, periodic=periodic, quantities="ijdS"
)

i = torch.from_numpy(i.astype(int)).to(device=positions.device)
j = torch.from_numpy(j.astype(int))
d = torch.from_numpy(d).to(dtype=positions.dtype, device=positions.device)
S = torch.from_numpy(S).to(dtype=positions.dtype, device=positions.device)

neighbor_indices = torch.stack([i, j], dim=1)

if not neighbor_shifts:
Expand Down
2 changes: 1 addition & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ coverage[toml]
pytest
pytest-cov
scipy
vesin-torch >= 0.2.0
vesin >= 0.2.0
6 changes: 3 additions & 3 deletions tests/utils/test_tuning.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from torchpme.utils import tune_ewald, tune_p3m, tune_pme

sys.path.append(str(Path(__file__).parents[1]))
from helpers import define_crystal, neighbor_list_torch
from helpers import define_crystal, neighbor_list

DTYPE = torch.float32
DEVICE = "cpu"
Expand Down Expand Up @@ -51,7 +51,7 @@ def test_parameter_choose(calculator, tune, param_length, accuracy):
assert len(params) == param_length

# Compute neighbor list
neighbor_indices, neighbor_distances = neighbor_list_torch(
neighbor_indices, neighbor_distances = neighbor_list(
positions=pos, periodic=True, box=cell, cutoff=sr_cutoff
)

Expand Down Expand Up @@ -84,7 +84,7 @@ def test_odd_interpolation_nodes():
learning_rate=0.75,
)

neighbor_indices, neighbor_distances = neighbor_list_torch(
neighbor_indices, neighbor_distances = neighbor_list(
positions=pos, periodic=True, box=cell, cutoff=sr_cutoff
)

Expand Down

0 comments on commit 8b3a143

Please sign in to comment.