-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathtest_einstein.py
More file actions
333 lines (266 loc) · 11.2 KB
/
test_einstein.py
File metadata and controls
333 lines (266 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
"""Tests for the Einstein model implementation."""
import pytest
import torch
from ase.build import bulk
import torch_sim as ts
from torch_sim.models.einstein import EinsteinModel
class TestEinsteinModel:
"""Test Einstein model implementation."""
@pytest.fixture
def simple_system(self):
"""Create a simple test system."""
device = torch.device("cpu")
dtype = torch.float64
# Create a simple 2-atom system
positions = torch.tensor(
[[0.0, 0.0, 0.0], [1.0, 0.0, 0.0]], dtype=dtype, device=device
)
masses = torch.tensor([1.0, 1.0], dtype=dtype, device=device)
cell = torch.eye(3, dtype=dtype, device=device) * 10.0 # Large cell
atomic_numbers = torch.tensor([1, 1], dtype=torch.int64, device=device)
state = ts.SimState(
positions=positions,
masses=masses,
cell=cell.unsqueeze(0),
pbc=True,
atomic_numbers=atomic_numbers,
)
return state, device, dtype
@pytest.fixture
def batched_system(self):
"""Create a batched test system."""
device = torch.device("cpu")
dtype = torch.float64
# Create two different 2-atom systems
si_atoms = bulk("Si", "diamond", a=5.43, cubic=True)
fe_atoms = bulk("Fe", "bcc", a=2.87, cubic=True)
state = ts.io.atoms_to_state([si_atoms, fe_atoms], device, dtype)
return state, device, dtype
def test_einstein_model_creation(
self, simple_system: tuple[ts.SimState, torch.device, torch.dtype]
):
"""Test basic Einstein model creation."""
state, device, dtype = simple_system
# Create equilibrium positions and frequencies
equilibrium_pos = state.positions
frequencies = torch.ones(2, dtype=dtype, device=device) * 0.1 # [N]
model = EinsteinModel(
equilibrium_position=equilibrium_pos,
frequencies=frequencies,
masses=state.masses,
device=device,
dtype=dtype,
)
assert model.device == device
assert model.dtype == dtype
assert model.compute_forces is True
def test_einstein_model_forward_single_system(
self, simple_system: tuple[ts.SimState, torch.device, torch.dtype]
):
"""Test forward pass with single system."""
state, device, dtype = simple_system
equilibrium_pos = state.positions
frequencies = torch.ones(2, dtype=dtype, device=device) * 0.1
model = EinsteinModel(
equilibrium_position=equilibrium_pos,
frequencies=frequencies,
masses=state.masses,
device=device,
dtype=dtype,
)
# Displace atoms slightly from equilibrium
displaced_state = state.clone()
displaced_state.positions += 0.1
results = model(displaced_state)
assert "energy" in results
assert "forces" in results
assert results["energy"].shape == (1,)
assert results["forces"].shape == (2, 3) # [N_atoms, 3]
# Forces should point back toward equilibrium
expected_force_direction = -(displaced_state.positions - equilibrium_pos)
force_directions = results["forces"]
# Check that forces point in the right direction (dot product > 0)
for i in range(2):
dot_product = torch.dot(force_directions[i], expected_force_direction[i])
assert dot_product > 0
def test_einstein_model_forward_batched_system(
self, batched_system: tuple[ts.SimState, torch.device, torch.dtype]
):
"""Test forward pass with batched system."""
state, device, dtype = batched_system
# Create equilibrium positions for the batched system
n_atoms = state.n_atoms
equilibrium_pos = state.positions
frequencies = torch.ones(n_atoms, dtype=dtype, device=device) * 0.05
model = EinsteinModel(
equilibrium_position=equilibrium_pos,
frequencies=frequencies,
masses=state.masses,
device=device,
dtype=dtype,
)
# Displace atoms slightly
displaced_state = state.clone()
displaced_state.positions += 0.05
results = model(displaced_state)
assert "energy" in results
assert "forces" in results
assert results["energy"].shape == (2,) # [n_systems]
assert results["forces"].shape == (n_atoms, 3) # [total_atoms, 3]
def test_einstein_model_from_frequencies(self):
"""Test creation from frequencies class method."""
device = torch.device("cpu")
dtype = torch.float64
# Create ASE atoms
atoms = bulk("Si", "diamond", a=5.43, cubic=True)
state = ts.io.atoms_to_state([atoms], device, dtype)
frequencies = torch.ones(len(atoms), dtype=dtype, device=device) * 0.05
model = EinsteinModel.from_atom_and_frequencies(
atom=state,
frequencies=frequencies,
reference_energy=1.0,
device=device,
dtype=dtype,
)
assert torch.allclose(model.reference_energy, torch.tensor(1.0, dtype=dtype))
assert model.frequencies.shape[0] == len(atoms)
def test_periodic_boundary_conditions(
self, simple_system: tuple[ts.SimState, torch.device, torch.dtype]
):
"""Test that PBC are handled correctly."""
state, device, dtype = simple_system
# Create model with equilibrium at origin
equilibrium_pos = torch.zeros((2, 3), dtype=dtype, device=device)
frequencies = torch.ones(2, dtype=dtype, device=device) * 1
model = EinsteinModel(
equilibrium_position=equilibrium_pos,
frequencies=frequencies,
device=device,
dtype=dtype,
)
# Place atoms near opposite faces of the cell
test_state = state.clone()
test_state.positions = torch.tensor(
[
[0.1, 0.0, 0.0], # Near one face
[9.9, 0.0, 0.0], # Near opposite face
],
dtype=dtype,
device=device,
)
results = model(test_state)
# Should handle PBC correctly - both atoms far from origin
# but should compute minimum image distances
assert torch.isfinite(results["energy"])
assert torch.isfinite(results["forces"]).all()
spring = frequencies**2 # since mass=1
target_energies = 0.5 * spring * (torch.tensor([0.1, -0.1]) ** 2)
target_forces = -spring[:, None] * torch.tensor(
[[0.1, 0.0, 0.0], [-0.1, 0.0, 0.0]], dtype=dtype, device=device
)
assert torch.allclose(results["energy"], target_energies.sum(), atol=1e-6)
assert torch.allclose(results["forces"], target_forces, atol=1e-6)
def test_energy_force_consistency(
self, simple_system: tuple[ts.SimState, torch.device, torch.dtype]
):
"""Test that forces are consistent with energy gradients."""
state, device, dtype = simple_system
equilibrium_pos = state.positions.clone()
frequencies = torch.ones(2, dtype=dtype, device=device) * 0.1
model = EinsteinModel(
equilibrium_position=equilibrium_pos,
frequencies=frequencies,
masses=state.masses,
device=device,
dtype=dtype,
)
# Create a displaced state with gradients enabled
test_positions = state.positions.clone() + 0.1
test_positions.requires_grad_(requires_grad=True)
test_state = state.clone()
test_state.positions = test_positions
results = model(test_state)
energy = results["energy"]
# Compute forces from gradients
forces_from_grad = -torch.autograd.grad(
energy, test_positions, create_graph=False
)[0]
forces_direct = results["forces"]
# Forces should match (within numerical precision)
torch.testing.assert_close(forces_direct, forces_from_grad, atol=1e-6, rtol=1e-6)
def test_get_free_energy(
self, simple_system: tuple[ts.SimState, torch.device, torch.dtype]
):
"""Test free energy calculation."""
state, device, dtype = simple_system
equilibrium_pos = state.positions
frequencies = torch.ones(2, dtype=dtype, device=device) * 0.1 # THz
model = EinsteinModel(
equilibrium_position=equilibrium_pos,
frequencies=frequencies,
masses=state.masses,
device=device,
dtype=dtype,
)
temperature = 300.0 # K
results = model.get_free_energy(temperature)
# Check that result is a dictionary with free energy
assert isinstance(results, dict)
assert "free_energy" in results
free_energy = results["free_energy"]
assert isinstance(free_energy, torch.Tensor)
assert free_energy.shape == (1,) # Single system
# Free energy should be finite
assert torch.isfinite(free_energy).all()
# At higher temperature, free energy should be lower (more negative)
results_high = model.get_free_energy(600.0)
free_energy_high = results_high["free_energy"]
assert free_energy_high < free_energy
def test_get_free_energy_batched(
self, batched_system: tuple[ts.SimState, torch.device, torch.dtype]
):
"""Test free energy calculation for batched systems."""
state, device, dtype = batched_system
n_atoms = state.n_atoms
equilibrium_pos = state.positions
frequencies = torch.ones(n_atoms, dtype=dtype, device=device) * 0.05
model = EinsteinModel(
equilibrium_position=equilibrium_pos,
frequencies=frequencies,
masses=state.masses,
system_idx=state.system_idx,
device=device,
dtype=dtype,
)
temperature = 300.0
results = model.get_free_energy(temperature)
# Check result format and shape
assert isinstance(results, dict)
assert "free_energy" in results
free_energy = results["free_energy"]
# Should have one free energy per system
assert free_energy.shape == (2,) # Two systems
assert torch.isfinite(free_energy).all()
def test_sample_method(
self, simple_system: tuple[ts.SimState, torch.device, torch.dtype]
):
"""Test sampling from Einstein model."""
state, device, dtype = simple_system
equilibrium_pos = state.positions
frequencies = torch.ones(2, dtype=dtype, device=device) * 0.1
model = EinsteinModel(
equilibrium_position=equilibrium_pos,
frequencies=frequencies,
masses=state.masses,
device=device,
dtype=dtype,
)
temperature = 300.0
sampled_state = model.sample(state, temperature)
# Check that sampled state has correct shape and type
assert isinstance(sampled_state, ts.SimState)
assert sampled_state.positions.shape == state.positions.shape
assert sampled_state.positions.dtype == dtype
assert sampled_state.positions.device == device
# Sampled positions should have finite values
assert torch.isfinite(sampled_state.positions).all()