|
| 1 | +"""Tests for the electrostatics ModelInterface wrappers.""" |
| 2 | + |
| 3 | +import traceback # noqa: I001 |
| 4 | + |
| 5 | +import pytest |
| 6 | +import torch |
| 7 | +from ase.build import bulk |
| 8 | + |
| 9 | +import torch_sim as ts |
| 10 | +from tests.conftest import DEVICE, DTYPE |
| 11 | +from tests.models.conftest import make_validate_model_outputs_test |
| 12 | + |
| 13 | +try: |
| 14 | + from torch_sim.models.electrostatics import DSFCoulombModel, EwaldModel, PMEModel |
| 15 | +except (ImportError, OSError, RuntimeError): |
| 16 | + pytest.skip( |
| 17 | + f"nvalchemiops not installed: {traceback.format_exc()}", |
| 18 | + allow_module_level=True, |
| 19 | + ) |
| 20 | + |
| 21 | + |
| 22 | +def _make_charged_state( |
| 23 | + device: torch.device = DEVICE, |
| 24 | + dtype: torch.dtype = DTYPE, |
| 25 | +) -> ts.SimState: |
| 26 | + """Build a small NaCl-like state with alternating +1/-1 site charges.""" |
| 27 | + atoms = bulk("NaCl", crystalstructure="rocksalt", a=5.64, cubic=True) |
| 28 | + state = ts.io.atoms_to_state(atoms, device, dtype) |
| 29 | + n = state.n_atoms |
| 30 | + charges = torch.empty(n, dtype=dtype, device=device) |
| 31 | + charges[::2] = 1.0 |
| 32 | + charges[1::2] = -1.0 |
| 33 | + state._atom_extras["partial_charges"] = charges # noqa: SLF001 |
| 34 | + return state |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def dsf_model() -> DSFCoulombModel: |
| 39 | + return DSFCoulombModel(cutoff=8.0, alpha=0.2, device=DEVICE, dtype=DTYPE) |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture |
| 43 | +def ewald_model() -> EwaldModel: |
| 44 | + return EwaldModel(cutoff=8.0, accuracy=1e-6, device=DEVICE, dtype=DTYPE) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.fixture |
| 48 | +def pme_model() -> PMEModel: |
| 49 | + return PMEModel(cutoff=8.0, accuracy=1e-6, device=DEVICE, dtype=DTYPE) |
| 50 | + |
| 51 | + |
| 52 | +def _add_partial_charges(state: ts.SimState) -> ts.SimState: |
| 53 | + """Inject alternating +/-0.5 site charges into a state.""" |
| 54 | + n = state.n_atoms |
| 55 | + charges = torch.zeros(n, dtype=state.positions.dtype, device=state.device) |
| 56 | + charges[::2] = 0.5 |
| 57 | + charges[1::2] = -0.5 |
| 58 | + state._atom_extras["partial_charges"] = charges # noqa: SLF001 |
| 59 | + return state |
| 60 | + |
| 61 | + |
| 62 | +test_dsf_model_outputs = make_validate_model_outputs_test( |
| 63 | + model_fixture_name="dsf_model", |
| 64 | + device=DEVICE, |
| 65 | + dtype=DTYPE, |
| 66 | + state_modifiers=[_add_partial_charges], |
| 67 | +) |
| 68 | +test_ewald_model_outputs = make_validate_model_outputs_test( |
| 69 | + model_fixture_name="ewald_model", |
| 70 | + device=DEVICE, |
| 71 | + dtype=DTYPE, |
| 72 | + state_modifiers=[_add_partial_charges], |
| 73 | +) |
| 74 | +test_pme_model_outputs = make_validate_model_outputs_test( |
| 75 | + model_fixture_name="pme_model", |
| 76 | + device=DEVICE, |
| 77 | + dtype=DTYPE, |
| 78 | + state_modifiers=[_add_partial_charges], |
| 79 | +) |
| 80 | + |
| 81 | + |
| 82 | +def test_dsf_nonzero_energy() -> None: |
| 83 | + """Charged system should produce nonzero electrostatic energy.""" |
| 84 | + model = DSFCoulombModel(cutoff=8.0, alpha=0.2, device=DEVICE, dtype=DTYPE) |
| 85 | + state = _make_charged_state() |
| 86 | + out = model(state) |
| 87 | + assert out["energy"].abs().item() > 0 |
| 88 | + |
| 89 | + |
| 90 | +def test_ewald_pme_energy_agreement() -> None: |
| 91 | + """Ewald and PME should give the same converged Coulomb energy.""" |
| 92 | + state = _make_charged_state() |
| 93 | + ewald = EwaldModel(cutoff=8.0, accuracy=1e-6, device=DEVICE, dtype=DTYPE) |
| 94 | + pme = PMEModel(cutoff=8.0, accuracy=1e-6, device=DEVICE, dtype=DTYPE) |
| 95 | + torch.testing.assert_close( |
| 96 | + ewald(state)["energy"], pme(state)["energy"], atol=1e-3, rtol=1e-3 |
| 97 | + ) |
| 98 | + |
| 99 | + |
| 100 | +def test_sum_model_lj_plus_dsf() -> None: |
| 101 | + """LJ + DSF should be additive through SumModel.""" |
| 102 | + from torch_sim.models.interface import SumModel |
| 103 | + from torch_sim.models.lennard_jones import LennardJonesModel |
| 104 | + |
| 105 | + lj = LennardJonesModel( |
| 106 | + sigma=2.8, epsilon=0.01, cutoff=7.0, device=DEVICE, dtype=DTYPE |
| 107 | + ) |
| 108 | + dsf = DSFCoulombModel(cutoff=8.0, alpha=0.2, device=DEVICE, dtype=DTYPE) |
| 109 | + combined = SumModel(lj, dsf) |
| 110 | + state = _make_charged_state() |
| 111 | + lj_out = lj(state) |
| 112 | + dsf_out = dsf(state) |
| 113 | + sum_out = combined(state) |
| 114 | + torch.testing.assert_close(sum_out["energy"], lj_out["energy"] + dsf_out["energy"]) |
| 115 | + torch.testing.assert_close(sum_out["forces"], lj_out["forces"] + dsf_out["forces"]) |
0 commit comments