def test_constrained_dihedral_minimization(minkey): minimizer = MINIMIZERS[minkey] mol = mdt.from_smiles('C=C') assert mol.atoms[0].atnum == mol.atoms[ 1].atnum == 6 # make sure we're picking the right atoms dihedral = mdt.DihedralMonitor(mol.atoms[0], mol.atoms[1]) assert dihedral.value == 0.0 dihedral.value = 45 * u.degrees constraint = dihedral.constrain() mol.set_energy_model(mdt.models.OpenBabelPotential, forcefield='mmff94s') e0_1 = mol.calculate_potential_energy() p0_1 = mol.positions.copy() if minkey == 'bfgs': # BFGS expected to fail here with pytest.raises(mdt.exceptions.NotSupportedError): minimizer(mol) return traj = minimizer(mol, nsteps=100) assert_something_resembling_minimization_happened(p0_1, e0_1, traj, mol) assert constraint.error() <= 1.0 * u.degree traj_twists = traj.dihedral(mol.atoms[0], mol.atoms[1]) assert (abs(traj_twists - 45 * u.degrees) <= 1.0 * u.degree).all() e0_2 = mol.potential_energy p0_2 = mol.positions.copy() mol.clear_constraints() traj2 = minimizer(mol, nsteps=100) assert_something_resembling_minimization_happened(p0_2, e0_2, traj2, mol) assert dihedral.value <= 5.0 * u.degrees
def test_dihedral_gradient_sign_convention(four_particle_45_twist): mol = four_particle_45_twist mol.atoms[-1].y += 0.4 * u.nm dihe = mdt.DihedralMonitor(*mol.atoms) calc_grad = dihe.gradient() num_grad = helpers.num_grad(mol, lambda: dihe.value) np.testing.assert_allclose(calc_grad, num_grad, atol=5.0 * helpers.DEFSTEP.defunits_value())
def test_dihedral_gradient(four_particle_45_twist): mol = four_particle_45_twist dihe = mdt.DihedralMonitor(*mol.atoms) calc_grad = dihe.gradient() num_grad = helpers.num_grad(mol, lambda: dihe.value) np.testing.assert_allclose(calc_grad.defunits_value(), num_grad.defunits_value(), atol=5.0 * helpers.DEFSTEP.defunits_value())