-
Notifications
You must be signed in to change notification settings - Fork 79
/
test_hoomd.py
445 lines (355 loc) · 16.3 KB
/
test_hoomd.py
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import xml.etree.ElementTree
import numpy as np
import packaging.version
import pytest
import mbuild as mb
from mbuild.tests.base_test import BaseTest
from mbuild.utils.io import get_fn, has_foyer, has_gsd, has_hoomd, import_
if has_hoomd:
import hoomd
if "version" in dir(hoomd):
hoomd_version = packaging.version.parse(hoomd.version.version)
else:
hoomd_version = packaging.version.parse(hoomd.__version__)
@pytest.mark.skipif(not has_hoomd, reason="HOOMD is not installed")
class TestHoomdAny(BaseTest):
def test_empty_initial_snapshot(self):
import hoomd
from mbuild.formats.hoomd_snapshot import to_hoomdsnapshot
part = mb.Compound(name="Ar")
box = mb.Box(lengths=[5, 5, 5], angles=[90, 90, 90])
system = mb.fill_box(part, n_compounds=10, box=box)
if hoomd_version.major == 2:
hoomd.context.initialize("")
init_snap = hoomd.data.make_snapshot(
N=0, box=hoomd.data.boxdim(L=10)
)
else:
init_snap = hoomd.Snapshot()
init_snap.configuration.box = hoomd.Box.cube(L=10)
with pytest.raises(RuntimeError):
snap, _ = to_hoomdsnapshot(system, hoomd_snapshot=init_snap)
def test_compound_from_snapshot(self, ethane):
from mbuild.formats.hoomd_snapshot import (
from_snapshot,
to_hoomdsnapshot,
)
lengths = [5, 5, 5]
filled = mb.fill_box(ethane, n_compounds=5, box=mb.Box(lengths))
snap, _ = to_hoomdsnapshot(filled)
new_filled = from_snapshot(snap, scale=0.1)
assert filled.n_bonds == new_filled.n_bonds
assert filled.n_particles == new_filled.n_particles
assert np.array_equal(filled.box.angles, new_filled.box.angles)
assert np.array_equal(filled.box.lengths, new_filled.box.lengths)
for i in range(filled.n_particles):
assert np.allclose(filled[i].pos, new_filled[i].pos)
@pytest.mark.skipif(not has_gsd, reason="gsd is not installed")
def test_compound_from_gsdsnapshot(self, ethane):
import gsd.hoomd
from mbuild.formats.hoomd_snapshot import (
from_snapshot,
to_hoomdsnapshot,
)
lengths = [5, 5, 5]
filled = mb.fill_box(ethane, n_compounds=5, box=mb.Box(lengths))
snap, _ = to_hoomdsnapshot(filled)
# copy attributes from the snapshot to a gsd snapshot
gsd_snap = gsd.hoomd.Frame()
gsd_snap.particles.N = snap.particles.N
gsd_snap.particles.types = snap.particles.types
gsd_snap.particles.typeid = snap.particles.typeid
gsd_snap.particles.position = snap.particles.position
if hoomd_version.major == 2:
gsd_snap.configuration.box = np.array(
[
snap.box.Lx,
snap.box.Ly,
snap.box.Lz,
snap.box.xy,
snap.box.xy,
snap.box.yz,
]
)
else:
gsd_snap.configuration.box = snap.configuration.box
gsd_snap.bonds.N = snap.bonds.N
gsd_snap.bonds.group = snap.bonds.group
gsd_snap.particles.charge = snap.particles.charge
gsd_snap.validate()
new_filled = from_snapshot(gsd_snap, scale=0.1)
assert filled.n_bonds == new_filled.n_bonds
assert filled.n_particles == new_filled.n_particles
assert np.array_equal(filled.box.angles, new_filled.box.angles)
assert np.array_equal(filled.box.lengths, new_filled.box.lengths)
for i in range(filled.n_particles):
assert np.allclose(filled[i].pos, new_filled[i].pos)
def test_compound_to_snapshot(self, ethane):
from mbuild.formats.hoomd_snapshot import to_hoomdsnapshot
snap, _ = to_hoomdsnapshot(ethane)
assert snap.particles.N == 8
assert snap.bonds.N == 7
assert snap.angles.N == 0
def test_particles_to_snapshot(self):
from mbuild.formats.hoomd_snapshot import to_hoomdsnapshot
part = mb.Compound(name="Ar")
box = mb.Box(lengths=[5, 5, 5], angles=[90, 90, 90])
system = mb.fill_box(part, n_compounds=10, box=box)
snap, _ = to_hoomdsnapshot(system)
assert snap.particles.N == 10
assert snap.bonds.N == 0
assert snap.angles.N == 0
def test_snapshot_from_initial(self):
import hoomd
from mbuild.formats.hoomd_snapshot import to_hoomdsnapshot
part = mb.Compound(name="Ar")
box = mb.Box(lengths=[5, 5, 5], angles=[90, 90, 90])
system = mb.fill_box(part, n_compounds=10, box=box)
if hoomd_version.major == 2:
hoomd.context.initialize("")
init_snap = hoomd.data.make_snapshot(
N=10, box=hoomd.data.boxdim(L=10)
)
else:
init_snap = hoomd.Snapshot()
init_snap.particles.N = 10
init_snap.configuration.box = hoomd.Box.cube(L=10)
snap, _ = to_hoomdsnapshot(system, hoomd_snapshot=init_snap)
assert snap.particles.N == 20
assert snap.bonds.N == 0
assert snap.angles.N == 0
if hoomd_version.major == 2:
assert (snap.box.Lx, snap.box.Ly, snap.box.Lz) == (50, 50, 50)
assert (snap.box.xy, snap.box.xz, snap.box.yz) == (0, 0, 0)
else:
np.testing.assert_allclose(
snap.configuration.box, [50, 50, 50, 0, 0, 0]
)
def test_bad_input_to_snapshot(self):
from mbuild.formats.hoomd_snapshot import to_hoomdsnapshot
with pytest.raises(ValueError):
to_hoomdsnapshot("fake_object")
def test_non_param_struc_to_snapshot(self, ethane):
from mbuild.formats.hoomd_snapshot import to_hoomdsnapshot
structure = ethane.to_parmed()
snap, _ = to_hoomdsnapshot(structure)
assert snap.particles.N == 8
assert snap.bonds.N == 7
assert snap.angles.N == 0
@pytest.mark.skipif(not has_foyer, reason="Foyer is not installed")
def test_param_structure_to_snapshot(self, ethane):
from foyer.forcefield import Forcefield
from mbuild.formats.hoomd_snapshot import to_hoomdsnapshot
ff = Forcefield(name="oplsaa")
structure = ff.apply(ethane)
snap, _ = to_hoomdsnapshot(structure)
assert snap.particles.N == 8
assert snap.bonds.N == 7
assert snap.angles.N == 12
assert snap.dihedrals.N == 9
assert snap.pairs.N == 9
@pytest.mark.skipif(
not has_hoomd or hoomd_version.major != 2,
reason="HOOMD is not installed or is the wrong version",
)
class TestHoomdSimulation(BaseTest):
def test_bad_input_to_hoomdsimulation(self):
from mbuild.formats.hoomd_simulation import create_hoomd_simulation
with pytest.raises(ValueError):
create_hoomd_simulation("fake_object", 2)
def test_compound_to_hoomdsimulation(self, ethane):
from mbuild.formats.hoomd_simulation import create_hoomd_simulation
with pytest.raises(ValueError):
create_hoomd_simulation(ethane, 2.5)
@pytest.mark.skipif(not has_foyer, reason="Foyer is not installed")
def test_structure_to_hoomdsimulation(self, ethane):
import hoomd
from foyer.forcefield import Forcefield
from mbuild.formats.hoomd_simulation import create_hoomd_simulation
ff = Forcefield(name="oplsaa")
structure = ff.apply(ethane)
sim = hoomd.context.SimulationContext()
with sim:
create_hoomd_simulation(structure, 2.5)
sim_forces = hoomd.context.current.forces
pair_force = import_("hoomd.md.pair")
charge_force = import_("hoomd.md.charge")
special_pair_force = import_("hoomd.md.special_pair")
bond_force = import_("hoomd.md.bond")
angle_force = import_("hoomd.md.angle")
dihedral_force = import_("hoomd.md.dihedral")
assert isinstance(sim_forces[0], pair_force.lj)
assert isinstance(sim_forces[1], charge_force.pppm)
assert isinstance(sim_forces[2], pair_force.ewald)
assert isinstance(sim_forces[3], special_pair_force.lj)
assert isinstance(sim_forces[4], special_pair_force.coulomb)
assert isinstance(sim_forces[5], bond_force.harmonic)
assert isinstance(sim_forces[6], angle_force.harmonic)
assert isinstance(sim_forces[7], dihedral_force.opls)
@pytest.mark.skipif(not has_foyer, reason="Foyer is not installed")
def test_lj_to_hoomdsimulation(self):
import hoomd
from foyer.forcefield import Forcefield
from mbuild.formats.hoomd_simulation import create_hoomd_simulation
box = mb.Compound()
box.add(mb.Compound(name="Ar", pos=[1, 1, 1]))
box.add(mb.Compound(name="Ar", pos=[1, 1, 1]))
ff = Forcefield(forcefield_files=get_fn("lj.xml"))
structure = ff.apply(box)
structure.box = [10, 10, 10, 90, 90, 90]
sim = hoomd.context.SimulationContext()
with sim:
create_hoomd_simulation(structure, 2.5)
sim_forces = hoomd.context.current.forces
pair_force = import_("hoomd.md.pair")
assert isinstance(sim_forces[0], pair_force.lj)
@pytest.mark.skipif(not has_foyer, reason="Foyer is not installed")
@pytest.mark.skipif(not has_gsd, reason="GSD is not installed")
def test_hoomdsimulation_restart(self):
import gsd.hoomd
import hoomd
from foyer.forcefield import Forcefield
from mbuild.formats.hoomd_simulation import create_hoomd_simulation
box = mb.Compound()
box.add(mb.Compound(name="Ar", pos=[1, 1, 1]))
box.add(mb.Compound(name="Ar", pos=[1, 1, 1]))
ff = Forcefield(forcefield_files=get_fn("lj.xml"))
structure = ff.apply(box)
structure.box = [10, 10, 10, 90, 90, 90]
sim = hoomd.context.SimulationContext()
with sim:
hoomd_obj, ref_vals = create_hoomd_simulation(
structure, 2.5, restart=get_fn("restart.gsd")
)
sim_forces = hoomd.context.current.forces
pair_force = import_("hoomd.md.pair")
assert isinstance(sim_forces[0], pair_force.lj)
snap = hoomd_obj[0]
with gsd.hoomd.open(get_fn("restart.gsd")) as f:
rsnap = f[0]
assert np.array_equal(snap.particles.position, rsnap.particles.position)
def test_hoomdsimulation_nlist(self, ethane):
hoomd_simulation = import_("mbuild.formats.hoomd_simulation")
hoomd = import_("hoomd")
hoomd.md = import_("hoomd.md")
with pytest.raises(ValueError):
hoomd_simulation.create_hoomd_simulation(
ethane, 2.5, nlist=hoomd.md.nlist.tree
)
@pytest.mark.skipif(
not has_hoomd or hoomd_version.major < 3,
reason="HOOMD is not installed or is the wrong version",
)
class TestHoomdForcefield(BaseTest):
def test_bad_input_to_hoomd_forcefield(self):
from mbuild.formats.hoomd_forcefield import create_hoomd_forcefield
with pytest.raises(ValueError):
create_hoomd_forcefield("fake_object", 2.5)
def test_compound_to_hoomd_forcefield(self, ethane):
from mbuild.formats.hoomd_forcefield import create_hoomd_forcefield
with pytest.raises(ValueError):
create_hoomd_forcefield(ethane, 2.5)
@pytest.mark.skipif(not has_foyer, reason="Foyer is not installed")
def test_structure_to_hoomd_forcefield(self, ethane):
import hoomd
from foyer.forcefield import Forcefield
from mbuild.formats.hoomd_forcefield import create_hoomd_forcefield
ff = Forcefield(name="oplsaa")
structure = ff.apply(ethane)
snapshot, forces, ref_values = create_hoomd_forcefield(structure, 2.5)
assert isinstance(forces[0], hoomd.md.pair.LJ)
assert isinstance(forces[1], hoomd.md.pair.Ewald)
assert isinstance(forces[2], hoomd.md.long_range.pppm.Coulomb)
assert isinstance(forces[3], hoomd.md.special_pair.LJ)
assert isinstance(forces[4], hoomd.md.special_pair.Coulomb)
assert isinstance(forces[5], hoomd.md.bond.Harmonic)
assert isinstance(forces[6], hoomd.md.angle.Harmonic)
assert isinstance(forces[7], hoomd.md.dihedral.OPLS)
@pytest.mark.skipif(not has_foyer, reason="Foyer is not installed")
def test_lj_to_hoomd_forcefield(self):
import hoomd
from foyer.forcefield import Forcefield
from mbuild.formats.hoomd_forcefield import create_hoomd_forcefield
box = mb.Compound()
box.add(mb.Compound(name="Ar", pos=[1, 1, 1]))
box.add(mb.Compound(name="Ar", pos=[1, 1, 1]))
ff = Forcefield(forcefield_files=get_fn("lj.xml"))
structure = ff.apply(box)
structure.box = [10, 10, 10, 90, 90, 90]
snapshot, forces, ref_values = create_hoomd_forcefield(structure, 2.5)
assert isinstance(forces[0], hoomd.md.pair.LJ)
class TestHoomdXML(BaseTest):
def test_save(self, ethane):
ethane.save(filename="ethane.hoomdxml")
@pytest.mark.skipif(not has_foyer, reason="Foyer package not installed")
def test_save_forcefield(self, ethane):
ethane.save(filename="ethane-opls.hoomdxml", forcefield_name="oplsaa")
def test_save_box(self, ethane):
box = mb.box.Box(lengths=[2.0, 2.0, 2.0], angles=[90, 90, 90])
ethane.save(filename="ethane-box.hoomdxml", box=box)
def test_save_triclinic_box_(self, ethane):
box = mb.Box(lengths=np.array([2.0, 2.0, 2.0]), angles=[60, 70, 80])
ethane.save(filename="triclinic-box.hoomdxml", box=box)
def test_rigid(self, benzene):
n_benzenes = 10
benzene.name = "Benzene"
filled = mb.fill_box(
benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]
)
filled.label_rigid_bodies(
discrete_bodies="Benzene", rigid_particles="C"
)
filled.save(filename="benzene.hoomdxml")
xml_file = xml.etree.ElementTree.parse("benzene.hoomdxml").getroot()
body_text = xml_file[0].find("body").text
rigid_bodies = [int(body) for body in body_text.split("\n") if body]
for body_id in range(10):
assert rigid_bodies.count(body_id) == 6
assert rigid_bodies.count(-1) == n_benzenes * 6
@pytest.mark.skipif(not has_foyer, reason="Foyer package not installed")
def test_number_in_each_section(self, box_of_benzenes):
box_of_benzenes.save(
filename="benzene.hoomdxml", forcefield_name="oplsaa"
)
xml_file = xml.etree.ElementTree.parse("benzene.hoomdxml").getroot()
for attribute in ["position", "type", "mass", "charge"]:
body_text = xml_file[0].find(attribute).text
list_of_things = [x for x in body_text.split("\n") if x]
assert len(list_of_things) == 12 * 10
for attribute, number in [
("bond", 12),
("angle", 18),
("dihedral", 24),
]:
body_text = xml_file[0].find(attribute).text
list_of_things = [x for x in body_text.split("\n") if x]
assert len(list_of_things) == number * 10
def test_box_dimensions(self, benzene):
n_benzenes = 10
filled = mb.fill_box(
benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]
)
filled.save(filename="benzene.hoomdxml")
for atom in mb.load("benzene.hoomdxml"):
assert atom.pos.max() < 20
assert atom.pos.min() > -20
@pytest.mark.skipif(not has_foyer, reason="Foyer package not installed")
def test_auto_scale_forcefield(self, ethane):
ethane.save(
filename="ethane-opls.hoomdxml",
forcefield_name="oplsaa",
auto_scale=True,
)
xml_file = xml.etree.ElementTree.parse("ethane-opls.hoomdxml").getroot()
masses = xml_file[0].find("mass").text.splitlines()
# We use 1 and 5 since the first element of masses is empty
assert masses[1] == "1.0"
assert masses[5] == "1.0"
pair_coeffs = [
_.split("\t")
for _ in xml_file[0].find("pair_coeffs").text.splitlines()
]
# The first element is empty, the next element should be
# ['opls_135', '1.0000', '1.0000']
assert pair_coeffs[1][1] == "1.0000"
assert pair_coeffs[1][2] == "1.0000"