Skip to content

Commit

Permalink
test readme
Browse files Browse the repository at this point in the history
  • Loading branch information
PicoCentauri committed Jan 24, 2025
1 parent 9b1dc80 commit c675299
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 56 deletions.
86 changes: 42 additions & 44 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,50 +69,48 @@ Quickstart

Here is a simple example to get started with *torch-pme*:

.. code-block:: python
import torch
import torchpme
# Single charge in a cubic box
positions = torch.zeros((1, 3), requires_grad=True)
cell = 8 * torch.eye(3)
charges = torch.tensor([[1.0]])
# No neighbors for a single atom; use `vesin` for neighbors if needed
neighbor_indices = torch.zeros((0, 2), dtype=torch.int64)
neighbor_distances = torch.zeros((0,))
# Tune P3M parameters
smearing, p3m_parameters, _ = torchpme.tuning.tune_p3m(
charges=charges,
cell=cell,
positions=positions,
cutoff=5.0,
neighbor_indices=neighbor_indices,
neighbor_distances=neighbor_distances,
)
# Initialize potential and calculator
potential = torchpme.CoulombPotential(smearing)
calculator = torchpme.P3MCalculator(potential, **p3m_parameters)
# Compute (per-atom) potentials
potentials = calculator.forward(
charges=charges,
cell=cell,
positions=positions,
neighbor_indices=neighbor_indices,
neighbor_distances=neighbor_distances,
)
# Calculate total energy and forces
energy = torch.sum(charges * potentials)
energy.backward()
forces = -positions.grad
print("Energy:", energy.item())
print("Forces:", forces)
.. doctest::
:hide:

>>> import torch
>>> import torchpme

>>> # Single charge in a cubic box
>>> positions = torch.zeros((1, 3), requires_grad=True)
>>> cell = 8 * torch.eye(3)
>>> charges = torch.tensor([[1.0]])

>>> # No neighbors for a single atom; use `vesin` for neighbors if needed
>>> neighbor_indices = torch.zeros((0, 2), dtype=torch.int64)
>>> neighbor_distances = torch.zeros((0,))

>>> # Tune P3M parameters
>>> smearing, p3m_parameters, _ = torchpme.tuning.tune_p3m(
... charges=charges,
... cell=cell,
... positions=positions,
... cutoff=5.0,
... neighbor_indices=neighbor_indices,
... neighbor_distances=neighbor_distances,
... )

>>> # Initialize potential and calculator
>>> potential = torchpme.CoulombPotential(smearing)
>>> calculator = torchpme.P3MCalculator(potential, **p3m_parameters)

>>> # Compute (per-atom) potentials
>>> potentials = calculator.forward(
... charges=charges,
... cell=cell,
... positions=positions,
... neighbor_indices=neighbor_indices,
... neighbor_distances=neighbor_distances,
... )

>>> # Calculate total energy and forces
>>> energy = torch.sum(charges * potentials)
>>> energy.backward()
>>> forces = -positions.grad

For more examples and details, please refer to the `documentation`_.

Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ ignore_missing_imports = true
[tool.pytest.ini_options]
python_files = ["*.py"]
testpaths = ["tests"]
addopts = [
"--cov",
"--cov-append",
"--cov-report=",
"--import-mode=append",
]
filterwarnings = [
"error",
"ignore:ast.Str is deprecated and will be removed in Python 3.14:DeprecationWarning",
Expand Down
1 change: 1 addition & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ ase >= 3.23
coverage[toml]
pytest
pytest-cov
pytest-doctestplus
scipy
vesin >= 0.3.0
vesin[torch] >= 0.3.0
16 changes: 4 additions & 12 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ lint_folders =
"{toxinidir}/src" \
"{toxinidir}/tests"

test_options = \
--cov \
--cov-append \
--cov-report= \
--import-mode=append

[testenv:build]
description = Asserts package build integrity.
usedevelop = true
Expand All @@ -44,19 +38,17 @@ extras = metatensor

commands =
# Run unit tests
pytest {[testenv]test_options} {posargs}
pytest {posargs}

# Run documentation tests
pytest --doctest-modules --pyargs torchpme
# Run documentation tests on all Python files and the README.rst
pytest --doctest-modules --pyargs torchpme --doctest-glob=*.rst README.rst

[testenv:tests-min]
description = Run the minimal core tests with pytest and {basepython}.
usedevelop = true
deps = -r tests/requirements.txt

commands =
# Run unit tests
pytest {[testenv]test_options} {posargs}
commands = pytest {posargs}

[testenv:lint]
description = Run linters and type checks
Expand Down

0 comments on commit c675299

Please sign in to comment.