|
| 1 | +"""Tests for the polarization electric-field correction model.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +import torch |
| 5 | + |
| 6 | +import torch_sim as ts |
| 7 | +from tests.conftest import DEVICE, DTYPE |
| 8 | +from torch_sim.models.interface import ModelInterface, SerialSumModel |
| 9 | +from torch_sim.models.polarization import UniformPolarizationModel |
| 10 | + |
| 11 | + |
| 12 | +class DummyPolarResponseModel(ModelInterface): |
| 13 | + def __init__( |
| 14 | + self, |
| 15 | + *, |
| 16 | + polarization_key: str = "polarization", |
| 17 | + include_born_effective_charges: bool = True, |
| 18 | + include_polarizability: bool = True, |
| 19 | + device: torch.device = DEVICE, |
| 20 | + dtype: torch.dtype = DTYPE, |
| 21 | + ) -> None: |
| 22 | + super().__init__() |
| 23 | + self.polarization_key = polarization_key |
| 24 | + self.include_born_effective_charges = include_born_effective_charges |
| 25 | + self.include_polarizability = include_polarizability |
| 26 | + self._device = device |
| 27 | + self._dtype = dtype |
| 28 | + self._compute_forces = True |
| 29 | + self._compute_stress = True |
| 30 | + |
| 31 | + def forward(self, state: ts.SimState, **kwargs: object) -> dict[str, torch.Tensor]: |
| 32 | + del kwargs |
| 33 | + energy = torch.arange( |
| 34 | + 1, state.n_systems + 1, device=state.device, dtype=state.dtype |
| 35 | + ) |
| 36 | + forces = ( |
| 37 | + torch.arange(state.n_atoms * 3, device=state.device, dtype=state.dtype) |
| 38 | + .reshape(state.n_atoms, 3) |
| 39 | + .div(10.0) |
| 40 | + ) |
| 41 | + stress = ( |
| 42 | + torch.arange(state.n_systems * 9, device=state.device, dtype=state.dtype) |
| 43 | + .reshape(state.n_systems, 3, 3) |
| 44 | + .div(100.0) |
| 45 | + ) |
| 46 | + polarization = ( |
| 47 | + torch.arange(state.n_systems * 3, device=state.device, dtype=state.dtype) |
| 48 | + .reshape(state.n_systems, 3) |
| 49 | + .add(0.5) |
| 50 | + ) |
| 51 | + output: dict[str, torch.Tensor] = { |
| 52 | + "energy": energy, |
| 53 | + "forces": forces, |
| 54 | + "stress": stress, |
| 55 | + self.polarization_key: polarization, |
| 56 | + } |
| 57 | + if self.include_polarizability: |
| 58 | + diag = torch.tensor([1.0, 2.0, 3.0], device=state.device, dtype=state.dtype) |
| 59 | + output["polarizability"] = torch.diag_embed(diag.repeat(state.n_systems, 1)) |
| 60 | + if self.include_born_effective_charges: |
| 61 | + born_effective_charges = torch.zeros( |
| 62 | + state.n_atoms, 3, 3, device=state.device, dtype=state.dtype |
| 63 | + ) |
| 64 | + born_effective_charges[:, 0, 0] = 1.0 |
| 65 | + born_effective_charges[:, 1, 1] = 2.0 |
| 66 | + born_effective_charges[:, 2, 2] = 3.0 |
| 67 | + output["born_effective_charges"] = born_effective_charges |
| 68 | + return output |
| 69 | + |
| 70 | + |
| 71 | +def test_polarization_model_normalizes_raw_key_without_field( |
| 72 | + si_double_sim_state: ts.SimState, |
| 73 | +) -> None: |
| 74 | + base_model = DummyPolarResponseModel() |
| 75 | + combined_model = SerialSumModel( |
| 76 | + base_model, |
| 77 | + UniformPolarizationModel(device=DEVICE, dtype=DTYPE), |
| 78 | + ) |
| 79 | + |
| 80 | + base_output = base_model(si_double_sim_state) |
| 81 | + combined_output = combined_model(si_double_sim_state) |
| 82 | + |
| 83 | + torch.testing.assert_close(combined_output["energy"], base_output["energy"]) |
| 84 | + torch.testing.assert_close(combined_output["forces"], base_output["forces"]) |
| 85 | + torch.testing.assert_close(combined_output["stress"], base_output["stress"]) |
| 86 | + torch.testing.assert_close( |
| 87 | + combined_output["total_polarization"], base_output["polarization"] |
| 88 | + ) |
| 89 | + torch.testing.assert_close( |
| 90 | + combined_output["polarization"], base_output["polarization"] |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +def test_polarization_model_applies_linear_response_corrections( |
| 95 | + si_double_sim_state: ts.SimState, |
| 96 | +) -> None: |
| 97 | + base_model = DummyPolarResponseModel() |
| 98 | + combined_model = SerialSumModel( |
| 99 | + base_model, |
| 100 | + UniformPolarizationModel(device=DEVICE, dtype=DTYPE), |
| 101 | + ) |
| 102 | + field = torch.tensor( |
| 103 | + [[0.2, -0.1, 0.05], [-0.3, 0.4, 0.1]], |
| 104 | + device=DEVICE, |
| 105 | + dtype=DTYPE, |
| 106 | + ) |
| 107 | + state = ts.SimState.from_state(si_double_sim_state, external_E_field=field) |
| 108 | + |
| 109 | + base_output = base_model(state) |
| 110 | + combined_output = combined_model(state) |
| 111 | + expected_polarization = base_output["polarization"] + torch.einsum( |
| 112 | + "sij,sj->si", base_output["polarizability"], field |
| 113 | + ) |
| 114 | + expected_energy = base_output["energy"] - torch.einsum( |
| 115 | + "si,si->s", field, base_output["polarization"] |
| 116 | + ) |
| 117 | + expected_energy = expected_energy - 0.5 * torch.einsum( |
| 118 | + "si,sij,sj->s", field, base_output["polarizability"], field |
| 119 | + ) |
| 120 | + expected_forces = base_output["forces"] + torch.einsum( |
| 121 | + "imn,im->in", |
| 122 | + base_output["born_effective_charges"], |
| 123 | + field[state.system_idx], |
| 124 | + ) |
| 125 | + |
| 126 | + torch.testing.assert_close(combined_output["energy"], expected_energy) |
| 127 | + torch.testing.assert_close(combined_output["forces"], expected_forces) |
| 128 | + torch.testing.assert_close( |
| 129 | + combined_output["total_polarization"], expected_polarization |
| 130 | + ) |
| 131 | + torch.testing.assert_close( |
| 132 | + combined_output["polarization"], base_output["polarization"] |
| 133 | + ) |
| 134 | + torch.testing.assert_close(combined_output["stress"], base_output["stress"]) |
| 135 | + |
| 136 | + |
| 137 | +def test_polarization_model_adds_only_delta_for_blessed_name( |
| 138 | + si_double_sim_state: ts.SimState, |
| 139 | +) -> None: |
| 140 | + base_model = DummyPolarResponseModel(polarization_key="total_polarization") |
| 141 | + combined_model = SerialSumModel( |
| 142 | + base_model, |
| 143 | + UniformPolarizationModel(device=DEVICE, dtype=DTYPE), |
| 144 | + ) |
| 145 | + field = torch.tensor([[0.1, 0.0, 0.0], [0.0, -0.2, 0.3]], device=DEVICE, dtype=DTYPE) |
| 146 | + state = ts.SimState.from_state(si_double_sim_state, external_E_field=field) |
| 147 | + |
| 148 | + base_output = base_model(state) |
| 149 | + combined_output = combined_model(state) |
| 150 | + expected_total_polarization = base_output["total_polarization"] + torch.einsum( |
| 151 | + "sij,sj->si", base_output["polarizability"], field |
| 152 | + ) |
| 153 | + |
| 154 | + torch.testing.assert_close( |
| 155 | + combined_output["total_polarization"], expected_total_polarization |
| 156 | + ) |
| 157 | + assert "polarization" not in combined_output |
| 158 | + |
| 159 | + |
| 160 | +def test_polarization_model_requires_born_effective_charges_for_force_correction( |
| 161 | + si_double_sim_state: ts.SimState, |
| 162 | +) -> None: |
| 163 | + base_model = DummyPolarResponseModel(include_born_effective_charges=False) |
| 164 | + combined_model = SerialSumModel( |
| 165 | + base_model, |
| 166 | + UniformPolarizationModel(device=DEVICE, dtype=DTYPE), |
| 167 | + ) |
| 168 | + state = ts.SimState.from_state( |
| 169 | + si_double_sim_state, |
| 170 | + external_E_field=torch.ones( |
| 171 | + si_double_sim_state.n_systems, 3, device=DEVICE, dtype=DTYPE |
| 172 | + ), |
| 173 | + ) |
| 174 | + |
| 175 | + with pytest.raises(ValueError, match="born_effective_charges"): |
| 176 | + combined_model(state) |
| 177 | + |
| 178 | + |
| 179 | +def test_polarization_model_rejects_non_uniform_field_shape( |
| 180 | + si_double_sim_state: ts.SimState, |
| 181 | +) -> None: |
| 182 | + state = ts.SimState.from_state( |
| 183 | + si_double_sim_state, |
| 184 | + external_E_field=torch.zeros( |
| 185 | + si_double_sim_state.n_systems, 3, device=DEVICE, dtype=DTYPE |
| 186 | + ), |
| 187 | + ) |
| 188 | + state._system_extras["external_E_field"] = torch.zeros( # noqa: SLF001 |
| 189 | + state.n_atoms, 3, device=DEVICE, dtype=DTYPE |
| 190 | + ) |
| 191 | + model = UniformPolarizationModel(device=DEVICE, dtype=DTYPE) |
| 192 | + |
| 193 | + with pytest.raises(ValueError, match="shape \\(n_systems, 3\\)"): |
| 194 | + model(state) |
0 commit comments