def test_val_0D_1(self): """Values without PBC""" masses = np.array([1.] * 3) ric = RIC() ric.add_in_bend([1, 2, 3]) ric.setup(masses) for coords, ref in [([[1, 0, 0], [0, 0, 0], [1, 0, 0]], 0), ([[1, 0, 0], [0, 0, 0], [1, 1, 0]], 45), ([[1, 0, 0], [0, 0, 0], [0, 1, 0]], 90), ([[1, 0, 0], [0, 0, 0], [-1, 1, 0]], 135), ([[1, 0, 0], [0, 0, 0], [-1, 0, 0]], 180), ([[1, 1, 1], [0, 0, 0], [-1, -1, -1]], 180)]: ric.construct_b_matrix(None, np.array(coords, dtype=np.float64)) res = ric.get_val_in_bends()[0] self.assertAlmostEqual(res, np.radians(ref))
def test_2(self): """Multiple repeated calls""" ric = RIC() ric.add_stretch([1, 2]) ric.add_stretch([2, 3]) ric.add_stretch([3, 4]) ric.add_in_bend([1, 2, 3]) ric.add_in_bend([2, 3, 4]) ric.add_torsion([1, 0, 0, 0, 0, 2, 3, 4, 0, 0, 0, 0]) ric.add_eckart() ric.setup(self.masses) bmat = ric.construct_b_matrix(None, self.coords) b1 = np.copy(bmat) bmat = ric.construct_b_matrix(None, self.coords) b2 = np.copy(bmat) self.assertEqual(np.max(np.abs(b1 - b2)), 0) bmat_inv, rank = ric.invert_b_matrix() i1 = np.copy(bmat_inv) bmat = ric.construct_b_matrix(None, self.coords) b3 = np.copy(bmat) bmat_inv, rank = ric.invert_b_matrix() i2 = np.copy(bmat_inv) self.assertEqual(np.max(np.abs(b1 - b3)), 0) self.assertEqual(np.max(np.abs(i1 - i2)), 0) hess = ric.project_hessian(self.hess) h1 = np.copy(hess) bmat = ric.construct_b_matrix(None, self.coords) b4 = np.copy(bmat) bmat_inv, rank = ric.invert_b_matrix() i3 = np.copy(bmat_inv) hess = ric.project_hessian(self.hess) h2 = np.copy(hess) hess = ric.get_ric_hessian() h3 = np.copy(hess) self.assertEqual(np.max(np.abs(b1 - b4)), 0) self.assertEqual(np.max(np.abs(i1 - i3)), 0) self.assertEqual(np.max(np.abs(h1 - h2)), 0) self.assertEqual(np.max(np.abs(h1 - h3)), 0)
def test_grad_0D_rand(self): masses = np.array([1.] * 3) ric = RIC() ric.add_in_bend([1, 2, 3]) ric.setup(masses) def func(x): ric.construct_b_matrix(None, np.reshape(x, (-1, 3))) res = np.copy(ric.get_val_in_bends()) return res grad = nd.Gradient(func, step_nom=[0.01] * 9) for coords in 2 * np.random.random((10, 3, 3)) - 1: coords = np.array(coords, dtype=np.float64) ref = grad(coords.flatten()) res = ric.construct_b_matrix(None, coords)[0, :] self.assertLess(np.max(np.abs(ref - res)), 1.e-10)
def test_3(self): masses = np.array(range(1, 6), dtype=np.float64) ric = RIC() ric.add_in_bend([1, 2, 3]) ric.add_in_bend([3, 4, 5]) ric.setup(masses) self.assertListEqual(list(ric._ric.ric_def_in_bends[:, 0]), [1, 2, 3]) self.assertListEqual(list(ric._ric.ric_def_in_bends[:, 1]), [3, 4, 5]) self.assertListEqual(list(ric._ric.ric_ibr_in_bends), [1, 2]) self.assertEqual(ric.num_ric, 2) self.assertEqual(ric.num_stretch, 0) self.assertEqual(ric.num_in_bend, 2) self.assertEqual(ric.num_out_bend, 0) self.assertEqual(ric.num_lin_bend, 0) self.assertEqual(ric.num_torsion, 0) self.assertEqual(ric.num_eckart, 0)
def test_1(self): ric = RIC() ric.add_stretch([1, 2]) ric.add_stretch([2, 3]) ric.add_stretch([3, 4]) ric.add_in_bend([1, 2, 3]) ric.add_in_bend([2, 3, 4]) ric.add_torsion([1, 0, 0, 0, 0, 2, 3, 4, 0, 0, 0, 0]) ric.add_eckart() ric.setup(self.masses) bmat = ric.construct_b_matrix(None, self.coords) bmat_inv, rank = ric.invert_b_matrix() bmat_inv_ = nl.pinv(bmat) self.assertLess(np.max(np.abs(bmat_inv - bmat_inv_)), 7.e-14) hess = ric.project_hessian(self.hess) hess_ = np.dot(bmat_inv.T, np.dot(self.hess, bmat_inv)) self.assertLess(np.max(np.abs(hess - hess_)), 3.e-13)
def test_1(self): ric = RIC() ric.add_stretch([1, 2]) ric.add_stretch([2, 3]) ric.add_in_bend([2, 1, 3]) ric.add_eckart() ric.setup(self.masses) bmat = ric.construct_b_matrix(self.hmat, self.coords) self.assertEqual( ric.get_val_stretches()[0], np.sqrt(np.sum((self.coords[0, :] - self.coords[1, :])**2))) bmat_inv, rank = ric.invert_b_matrix() bmat_inv_ = nl.pinv(bmat) self.assertLess(np.max(np.abs(bmat_inv - bmat_inv_)), 2.e-13) hess = ric.project_hessian(self.hess) hess_ = np.dot(bmat_inv.T, np.dot(self.hess, bmat_inv)) self.assertLess(np.max(np.abs(hess - hess_)), 3.e-14)
def test_8(self): masses = np.array(range(1, 10), dtype=np.float64) ric = RIC() ric.add_stretch([2, 3]) ric.add_in_bend([3, 4, 5]) ric.add_out_bend([4, 5, 6, 7]) ric.add_lin_bend([5, 6, 7], 'xy') ric.add_torsion([1, 0, 0, 0, 0, 2, 3, 4, 5, 6, 7, 8]) ric.add_torsion([3, 4, 0, 0, 0, 5, 6, 7, 8, 9, 0, 0]) ric.add_eckart(rots=[True, False, True]) ric.setup(masses) self.assertTrue(np.all(ric._ric.atomic_masses == masses)) self.assertListEqual(list(ric._ric.cart_coords.shape), [3, 9]) self.assertListEqual(list(ric._ric.cart_hessian.shape), [3 * 9, 3 * 9]) self.assertListEqual(list(ric._ric.ric_ibr_stretches), [1]) self.assertListEqual(list(ric._ric.ric_ibr_in_bends), [2]) self.assertListEqual(list(ric._ric.ric_ibr_out_bends), [3]) self.assertListEqual(list(ric._ric.ric_ibr_lin_bends), [4, 5]) self.assertListEqual(list(ric._ric.ric_ibr_torsions), [6, 7]) self.assertListEqual(list(ric._ric.ric_ibr_eckart_trans), [8, 9, 10]) self.assertListEqual(list(ric._ric.ric_ibr_eckart_rots), [11, 12]) self.assertListEqual(list(ric._ric.bmat.shape), [3 * 9, 12]) self.assertEqual(ric._ric.ric_val_stretches.size, 1) self.assertEqual(ric._ric.ric_val_in_bends.size, 1) self.assertEqual(ric._ric.ric_val_out_bends.size, 1) self.assertEqual(ric._ric.ric_val_lin_bends.size, 2) self.assertListEqual(list(ric._ric.ric_hessian.shape), [12, 12]) self.assertEqual(ric.num_ric, 12) self.assertEqual(ric.num_stretch, 1) self.assertEqual(ric.num_in_bend, 1) self.assertEqual(ric.num_out_bend, 1) self.assertEqual(ric.num_lin_bend, 2) self.assertEqual(ric.num_torsion, 2) self.assertEqual(ric.num_eckart, 5)
def test_2(self): g1, g2 = Group([1,2,3]), Group([5,6,7]) ric = RIC() ric.add_stretch([1,3]) ric.add_stretch([2,3]) ric.add_distance([g1,4]) ric.add_distance([4,g2]) ric.add_stretch([5,6]) ric.add_stretch([5,7]) ric.add_in_bend([1,3,2]) ric.add_in_bend([1,3,4]) ric.add_in_bend([2,3,4]) ric.add_in_bend([4,5,6]) ric.add_in_bend([4,5,7]) ric.add_in_bend([6,5,7]) ric.add_lin_bend([3,4,5], 'yz') ric.add_out_bend([3,1,2,4]) ric.add_out_bend([3,2,4,1]) ric.add_out_bend([3,4,1,2]) ric.add_out_bend([5,4,6,7]) ric.add_out_bend([5,6,7,4]) ric.add_out_bend([5,7,4,6]) ric.add_dihedral([1,2], [g1,g2], [6,7]) ric.add_eckart() ric.setup(self.masses) bmat = ric.construct_b_matrix(None, self.coords) bmat_inv, rank = ric.invert_b_matrix() bmat_inv_ = nl.pinv(bmat) self.assertEqual(rank, self.coords.size) self.assertLess(np.max(np.abs(bmat_inv - bmat_inv_)), 1.e-13) hess = ric.project_hessian(self.hess) hess_ = np.dot(bmat_inv.T,np.dot(self.hess,bmat_inv)) self.assertLess(np.max(np.abs(hess - hess_)),1.e-13)
def test_1(self): ric = RIC() ric.add_stretch([1,3]) ric.add_stretch([2,3]) ric.add_stretch([3,4]) ric.add_stretch([4,5]) ric.add_stretch([5,6]) ric.add_stretch([5,7]) ric.add_in_bend([1,3,2]) ric.add_in_bend([1,3,4]) ric.add_in_bend([2,3,4]) ric.add_in_bend([4,5,6]) ric.add_in_bend([4,5,7]) ric.add_in_bend([6,5,7]) ric.add_lin_bend([3,4,5], 'yz') ric.add_out_bend([3,1,2,4]) ric.add_out_bend([3,2,4,1]) ric.add_out_bend([3,4,1,2]) ric.add_out_bend([5,4,6,7]) ric.add_out_bend([5,6,7,4]) ric.add_out_bend([5,7,4,6]) ric.add_torsion([1,2,0,0,0,3,5,6,7,0,0,0]) ric.add_eckart() ric.setup(self.masses) bmat = ric.construct_b_matrix(None, self.coords) bmat_inv, rank = ric.invert_b_matrix() bmat_inv_ = nl.pinv(bmat) self.assertEqual(rank, self.coords.size) self.assertLess(np.max(np.abs(bmat_inv - bmat_inv_)), 7.e-14) hess = ric.project_hessian(self.hess) hess_ = np.dot(bmat_inv.T,np.dot(self.hess,bmat_inv)) self.assertLess(np.max(np.abs(hess - hess_)),3.e-13)