|
| 1 | +import pytest |
| 2 | +import mock |
| 3 | + |
| 4 | +from paths_cli.tests.wizard.mock_wizard import mock_wizard |
| 5 | + |
| 6 | +from paths_cli.wizard.openmm import ( |
| 7 | + _load_openmm_xml, _load_topology, openmm |
| 8 | +) |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def ad_openmm(tmpdir): |
| 12 | + """ |
| 13 | + Provide directory with files to start alanine depeptide sim in OpenMM |
| 14 | + """ |
| 15 | + mm = pytest.importorskip('simtk.openmm') |
| 16 | + u = pytest.importorskip('simtk.unit') |
| 17 | + openmmtools = pytest.importorskip('openmmtools') |
| 18 | + md = pytest.importorskip('mdtraj') |
| 19 | + testsystem = openmmtools.testsystems.AlanineDipeptideVacuum() |
| 20 | + integrator = openmmtools.integrators.VVVRIntegrator( |
| 21 | + 300 * u.kelvin, |
| 22 | + 1.0 / u.picosecond, |
| 23 | + 2.0 * u.femtosecond |
| 24 | + ) |
| 25 | + traj = md.Trajectory([testsystem.positions.value_in_unit(u.nanometer)], |
| 26 | + topology=testsystem.mdtraj_topology) |
| 27 | + files = {'integrator.xml': integrator, |
| 28 | + 'system.xml': testsystem.system} |
| 29 | + with tmpdir.as_cwd(): |
| 30 | + for fname, obj in files.items(): |
| 31 | + with open(fname, mode='w') as f: |
| 32 | + f.write(mm.XmlSerializer.serialize(obj)) |
| 33 | + |
| 34 | + traj.save('ad.pdb') |
| 35 | + |
| 36 | + return tmpdir |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize('obj_type', ['system', 'integrator', 'foo']) |
| 40 | +def test_load_openmm_xml(ad_openmm, obj_type): |
| 41 | + mm = pytest.importorskip('simtk.openmm') |
| 42 | + filename = f"{obj_type}.xml" |
| 43 | + inputs = [filename] |
| 44 | + expected_count = 1 |
| 45 | + if obj_type == 'foo': |
| 46 | + inputs.append('integrator.xml') |
| 47 | + expected_count = 2 |
| 48 | + |
| 49 | + wizard = mock_wizard(inputs) |
| 50 | + superclass = {'integrator': mm.CustomIntegrator, |
| 51 | + 'system': mm.System, |
| 52 | + 'foo': mm.CustomIntegrator}[obj_type] |
| 53 | + with ad_openmm.as_cwd(): |
| 54 | + obj = _load_openmm_xml(wizard, obj_type) |
| 55 | + assert isinstance(obj, superclass) |
| 56 | + |
| 57 | + assert wizard.console.input_call_count == expected_count |
| 58 | + |
| 59 | +@pytest.mark.parametrize('setup', ['normal', 'bad_filetype', 'no_file']) |
| 60 | +def test_load_topology(ad_openmm, setup): |
| 61 | + import openpathsampling as paths |
| 62 | + inputs = {'normal': [], |
| 63 | + 'bad_filetype': ['foo.bar'], |
| 64 | + 'no_file': ['foo.pdb']}[setup] |
| 65 | + expected_text = {'normal': "PDB file", |
| 66 | + 'bad_filetype': 'trr', |
| 67 | + 'no_file': 'No such file'}[setup] |
| 68 | + inputs += ['ad.pdb'] |
| 69 | + wizard = mock_wizard(inputs) |
| 70 | + with ad_openmm.as_cwd(): |
| 71 | + top = _load_topology(wizard) |
| 72 | + |
| 73 | + assert isinstance(top, paths.engines.openmm.topology.MDTrajTopology) |
| 74 | + assert wizard.console.input_call_count == len(inputs) |
| 75 | + assert expected_text in wizard.console.log_text |
| 76 | + |
| 77 | +def test_openmm(ad_openmm): |
| 78 | + inputs = ['system.xml', 'integrator.xml', 'ad.pdb', '10', '10000'] |
| 79 | + wizard = mock_wizard(inputs) |
| 80 | + with ad_openmm.as_cwd(): |
| 81 | + engine = openmm(wizard) |
| 82 | + assert engine.n_frames_max == 10000 |
| 83 | + assert engine.n_steps_per_frame == 10 |
0 commit comments