Пример #1
0
def test_create_seed_periodic():
    system, pars = get_system('cau13')
    configuration = Configuration(system, pars)
    # change parameters randomly
    with pytest.raises(ValueError): # cell is too small
        configuration.supercell = [3, 1, 1]
    configuration.rcut = 12.0
    assert configuration.interaction_radius == 12.0 # should change too
    configuration.switch_width = 5.0
    configuration.tailcorrections = False

    seed_covalent = configuration.create_seed(kind='covalent')
    ff = yaff_generate(seed_covalent)
    energy_covalent = ff.compute()

    seed_dispersion = configuration.create_seed(kind='dispersion')
    ff = yaff_generate(seed_dispersion)
    energy_dispersion = ff.compute()

    seed_electrostatic = configuration.create_seed(kind='electrostatic')
    ff = yaff_generate(seed_electrostatic)
    energy_electrostatic = ff.compute()

    seed_nonbonded = configuration.create_seed(kind='nonbonded')
    ff = yaff_generate(seed_nonbonded)
    energy_nonbonded = ff.compute()

    seed_full = configuration.create_seed(kind='all')
    ff = yaff_generate(seed_full)
    energy_full = ff.compute()

    assert abs(energy_covalent) > 0.0
    assert abs(energy_dispersion) > 0.0
    assert abs(energy_electrostatic) > 0.0
    np.testing.assert_almost_equal(
            energy_nonbonded,
            energy_dispersion + energy_electrostatic,
            )
    np.testing.assert_almost_equal(
            energy_full,
            energy_covalent + energy_nonbonded,
            )
Пример #2
0
def test_wrapper_openmm_mic():
    system, pars = get_system('mil53')
    configuration = Configuration(system, pars)
    kind = 'all'

    # YAFF and OpenMM use a different switching function. If it is disabled,
    # the results between both are identical up to 6 decimals
    configuration.switch_width = 0.0 # disable switching
    configuration.rcut = 10.0 # request cutoff of 10 angstorm
    configuration.cell_interaction_radius = 10.0
    configuration.supercell = [2, 3, 5]
    configuration.update_properties(configuration.write())
    conversion = ExplicitConversion(pme_error_thres=1e-5)
    seed_mm = conversion.apply(configuration, seed_kind=kind)
    wrapper = OpenMMForceFieldWrapper.from_seed(seed_mm, 'Reference')


    u = molmod.units.angstrom
    seed_yaff = configuration.create_seed(kind=kind)
    positions = seed_yaff.system.pos.copy() / u
    rvecs = seed_yaff.system.cell._get_rvecs().copy() / u
    e, _ = wrapper.evaluate(positions, rvecs, do_forces=True)

    # make random periodic displacements
    for i in range(5):
        coefficients = np.random.randint(-3, high=3, size=(3, 1))
        atom = np.random.randint(0, high=seed_yaff.system.natom, size=(10,))
        positions[atom, :] += np.sum(coefficients * rvecs, axis=0)
        e_ = wrapper.evaluate(positions, rvecs, do_forces=False)
        assert np.allclose(e, e_)

    # make random periodic displacements and rewrap coordinates
    for i in range(5):
        coefficients = np.random.randint(-3, high=3, size=(3, 1))
        atom = np.random.randint(0, high=seed_yaff.system.natom, size=(10,))
        positions[atom, :] += np.sum(coefficients * rvecs, axis=0)
        wrap_coordinates(positions, rvecs, rectangular=True)
        e_ = wrapper.evaluate(positions, rvecs, do_forces=False)
        assert np.allclose(e, e_)
Пример #3
0
def test_topology_templates(tmp_path):
    def check_consistency_system_topology(system, topology):
        assert system.natom == len(list(topology.atoms()))
        for i, atom in zip(range(system.natom), topology.atoms()):
            assert system.numbers[i] == atom.element._atomic_number
        assert system.bonds.shape[0] == len(list(topology.bonds()))
        bonds_tuples = [tuple(sorted(list(bond))) for bond in system.bonds]
        for bond in topology.bonds():
            indices_top = tuple(sorted((bond[0].index, bond[1].index)))
            bonds_tuples.remove(indices_top)
        assert len(bonds_tuples) == 0

    system, pars = get_system('methane')
    configuration = Configuration(system, pars, topology=None)
    assert len(configuration.templates) == 1
    assert len(configuration.templates[0]) == 5 # five atoms for CH4 template
    nresidues = np.sum([len(r) for r in configuration.residues.values()])

    supercell = [3, 1, 2]
    configuration.supercell = supercell
    top, _ = configuration.create_topology()
    assert len(list(top.residues())) == np.prod(supercell) * nresidues
    seed = configuration.create_seed()
    check_consistency_system_topology(seed.system, top)

    system, pars = get_system('uio66')
    configuration = Configuration(system, pars, topology=None)
    assert len(configuration.templates) == 1
    assert len(configuration.templates[0]) == system.natom
    supercell = [2, 2, 2]
    configuration.supercell = supercell
    top, positions = configuration.create_topology()
    assert len(list(top.residues())) == np.prod(supercell)
    seed = configuration.create_seed()
    check_consistency_system_topology(seed.system, top)
    assert np.allclose(positions, seed.system.pos / molmod.units.angstrom)

    (system, topology), pars = get_system('polymer')
    configuration = Configuration(system, pars, topology=topology)
    assert len(configuration.templates) == 3
    top, positions = configuration.create_topology()
    assert len(list(top.residues())) == len(list(topology.residues()))
    seed = configuration.create_seed()
    check_consistency_system_topology(seed.system, top)
    assert np.allclose(positions, seed.system.pos / molmod.units.angstrom)
    a, b, c = top.getPeriodicBoxVectors()
    box = np.array([
        a.value_in_unit(unit.angstrom),
        b.value_in_unit(unit.angstrom),
        c.value_in_unit(unit.angstrom),
        ])
    assert np.allclose(box, seed.system.cell._get_rvecs() / molmod.units.angstrom)

    # test nonperiodic
    system, pars = get_system('alanine')
    configuration = Configuration(system, pars)
    assert len(configuration.templates) == 1
    top, positions = configuration.create_topology()
    seed = configuration.create_seed()
    check_consistency_system_topology(seed.system, top)
    assert np.allclose(positions, seed.system.pos / molmod.units.angstrom)