def test_val_0D_ind(self): """Values without PBC""" masses = np.array([1.] * 4) ric = RIC() ric.add_lin_bend([1, 2, 3], 4) ric.setup(masses) for coords, ref in [ ([[-1, 0, 0], [0, 0, 0], [1, 0, 0], [0, 1, 0]], [0, 0]), # x-y ([[-1, 0, 0], [0, 0, 0], [1, 0, 0], [0, 0, 1]], [0, 0]), # x-z ([[-1, 0, 0], [0, 0, 0], [1, 0, 0], [0, 1, 1]], [0, 0]), # x-yz ([[-1, 0, 0], [0, 0, 0], [1, 0, 0], [1, 1, 1]], [0, 0]), # x-xyz ([[0, -1, 0], [0, 0, 0], [0, 1, 0], [2, 0, 0]], [0, 0]), # y-x ([[0, -1, 0], [0, 0, 0], [0, 1, 0], [0, 0, 2]], [0, 0]), # y-z ([[0, -1, 0], [0, 0, 0], [0, 1, 0], [0, 2, 2]], [0, 0]), # y-yz ([[0, -1, 0], [0, 0, 0], [0, 1, 0], [2, 2, 2]], [0, 0]), # y-xyz ([[0, 0, -1], [0, 0, 0], [0, 0, 1], [3, 0, 0]], [0, 0]), # z-x ([[0, 0, -1], [0, 0, 0], [0, 0, 1], [0, 3, 0]], [0, 0]), # z-y ([[0, 0, -1], [0, 0, 0], [0, 0, 1], [3, 3, 0]], [0, 0]), # z-xy ([[0, 0, -1], [0, 0, 0], [0, 0, 1], [3, 3, 3]], [0, 0]), # z-xyz ]: ric.construct_b_matrix(None, np.array(coords, dtype=np.float64)) res = ric.get_val_lin_bends() #print coords #print ref #print res self.assertAlmostEqual(res[0], np.radians(ref[0])) self.assertAlmostEqual(res[1], np.radians(ref[1]))
def test_grad_0D(self): masses = np.array([1.]*4) ric = RIC() ric.add_out_bend([1,2,3,4]) ric.setup(masses) def func(x): ric.construct_b_matrix(None,np.reshape(x,(-1,3))) res = np.copy(ric.get_val_out_bends()) return res grad = nd.Gradient(func,step_nom=[0.01]*12) for coords in [ [[ 0 ,0, 0],[0,1, 0],[-1,-1,0],[1,-1,0]], [[ 0 ,0, 0],[0,1, 0],[-2,-2,0],[1,-1,0]], [[ 0 ,0, 0],[0,1, 1],[-1,-1,0],[1,-1,0]], [[ 0 ,0, 0],[0,1, 0],[-1,-1,0],[1,-1,1]], [[ 0 ,0, 0],[0,1, 1],[-2,-2,1],[2,-2,1]], [[.1,.1,.9],[0,1, 0],[-1,-1,0],[1,-1,0]], ]: 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_values_2(self): masses = np.array([1.]*11) hmat = np.array([[0.,0.,0.]]*3) coords = np.array([[ 1., 0., 0.], # 1: 0 [ 1., 1.,-1.], # 2: 45 [ 0., 1.,-2.], # 3: 90 [-1., 1.,-3.], # 4: 135 [-1., 0.,-4.], # 5: 180 [ 0., 0., 0.], # central [ 0., 0., 1.], # central [ 1., 0., 1.], # 1: 0 [ 1.,-1., 2.], # 2: -45 [ 0.,-1., 3.], # 3: -90 [-1.,-1., 4.]]) # 4: -135 for ivals, ref in [([1,1], 0),([1,2], 45),([1,3], 90),([1,4], 135), ([2,1], 45),([2,2], 90),([2,3], 135),([2,4], 180), ([3,1], 90),([3,2], 135),([3,3], 180),([3,4],-135), ([4,1], 135),([4,2], 180),([4,3],-135),([4,4], -90), ([5,1], 180),([5,2],-135),([5,3], -90),([5,4], -45)]: ric = RIC() ric.add_torsion([1,2,3,4,5,6,7,8,9,10,11,0],ivals=ivals) ric.setup(masses) ric.construct_b_matrix(hmat,coords) res = ric.get_val_torsions()[0] self.assertAlmostEqual(res,np.radians(ref)) del ric
def test_grad_0D_ind_rand(self): masses = np.array([1.] * 4) ric = RIC() ric.add_lin_bend([1, 2, 3], 4) ric.setup(masses) def func(x): ric.construct_b_matrix(None, np.reshape(x, (-1, 3))) res = np.copy(ric.get_val_lin_bends()) return res jac = nd.Jacobian(func, step_nom=[0.01] * 12) # This is buggy! def func0(x): ric.construct_b_matrix(None, np.reshape(x, (-1, 3))) res = np.copy(ric.get_val_lin_bends()) return res[0] def func1(x): ric.construct_b_matrix(None, np.reshape(x, (-1, 3))) res = np.copy(ric.get_val_lin_bends()) return res[1] grad0 = nd.Gradient(func0, step_nom=[0.01] * 12) grad1 = nd.Gradient(func1, step_nom=[0.01] * 12) for coords in 2 * np.random.random((10, 4, 3)) - 1: coords = np.array(coords, dtype=np.float64) res = ric.construct_b_matrix(None, coords) vals = np.degrees(ric.get_val_lin_bends()) #if np.any(np.abs(vals) > 45): continue # HACK: disable the axes updates _inds = np.copy(ric._ric.ric_lin_bend_inds) ric._ric.ric_lin_bend_inds[:] = 0 #ref = jac(coords.flatten()) ref0 = grad0(coords.flatten()) ref1 = grad1(coords.flatten()) # HACK: enbale the axes updates ric._ric.ric_lin_bend_inds = _inds #print coords #print vals #print ref #print ref0 #print ref1 #print res #self.assertLess(np.max(np.abs(ref-res)),1.e-8) self.assertLess(np.max(np.abs(ref0 - res[0])), 1.e-8) self.assertLess(np.max(np.abs(ref1 - res[1])), 1.e-8)
def test_grad_0D_yz(self): masses = np.array([1.] * 3) ric = RIC() ric.add_lin_bend([1, 2, 3], 'yz') ric.setup(masses) def func(x): assert x.dtype == np.float64 ric.construct_b_matrix(None, np.reshape(x, (-1, 3))) res = np.copy(ric.get_val_lin_bends()) assert res.dtype == np.float64 return res jac = nd.Jacobian(func, step_nom=[0.01] * 9) # This is buggy! def func0(x): ric.construct_b_matrix(None, np.reshape(x, (-1, 3))) res = np.copy(ric.get_val_lin_bends()) return res[0] def func1(x): ric.construct_b_matrix(None, np.reshape(x, (-1, 3))) res = np.copy(ric.get_val_lin_bends()) return res[1] grad0 = nd.Gradient(func0, step_nom=[0.01] * 9) grad1 = nd.Gradient(func1, step_nom=[0.01] * 9) for coords in [ [[-1, 0, 0], [0, 0, 0], [1, 0, 0]], # Ref [[-1, -1, -1], [0, 0, 0], [1, 1, 1]], # Diagonal [[0, 0, 0], [1, 1, 1], [2, 2, 2]], # Offset [[-3, 0, 0], [0, 0, 0], [5, 0, 0]], # Longer [[-1, 0, 0], [0, 0, 0], [1, -1, 0]], # -y [[-1, 0, 0], [0, 0, 0], [1, 1, 0]], # +y [[-1, 0, 0], [0, 0, 0], [1, 0, -1]], # -z [[-1, 0, 0], [0, 0, 0], [1, 0, 1]], # +z ]: coords = np.array(coords, dtype=np.float64) #ref = jac(coords.flatten()) ref0 = grad0(coords.flatten()) ref1 = grad1(coords.flatten()) res = ric.construct_b_matrix(None, coords) #print coords #print ref #print ref0 #print ref1 #print res #self.assertLess(np.max(np.abs(ref-res)),1.e-8) self.assertLess(np.max(np.abs(ref0 - res[0])), 1.e-8) self.assertLess(np.max(np.abs(ref1 - res[1])), 1.e-8)
def test_val_0D_1(self): """Values without PBC""" masses = np.array([1., 2., 1.]) ric = RIC() ric.add_eckart(trans=[True] * 3, rots=[False] * 3) ric.setup(masses) for coords, ref in [([[0, 0, 0], [0, 0, 0], [0, 0, 0]], [0, 0, 0]), ([[-1, -1, -1], [0, 0, 0], [1, 1, 1]], [0, 0, 0]), ([[-2, 0, 0], [2, 0, 0], [-2, 0, 0]], [0, 0, 0])]: ric.construct_b_matrix(None, np.array(coords, dtype=np.float64)) res = ric.get_val_eckarts() self.assertListEqual(list(res), list(ref))
def test_val_0D_1(self): """Values without PBC""" masses = np.array([1.] * 2) ric = RIC() ric.add_stretch([1, 2]) ric.setup(masses) for coords, ref in [([[-1, 0, 0], [1, 0, 0]], 2), ([[0, -1, 0], [0, 1, 0]], 2), ([[0, 0, -1], [0, 0, 1]], 2), ([[1, 1, 1], [-1, -1, -1]], 2 * np.sqrt(3))]: ric.construct_b_matrix(None, np.array(coords, dtype=np.float64)) res = ric.get_val_stretches()[0] self.assertAlmostEqual(res, ref)
def test_val_3D_1(self): """Values with PBC in an orthogonal box""" masses = np.array([1.] * 2) hmat = np.array([[5., 0., 0.], [0., 6., 0.], [0., 0., 7.]]) ric = RIC() ric.add_stretch([1, 2]) ric.setup(masses) for coords, ref in [([[-1, 0, 0], [1, 0, 0]], 2), ([[0, -1, 0], [0, 1, 0]], 2), ([[0, 0, -1], [0, 0, 1]], 2), ([[1, 1, 1], [-1, -1, -1]], 2 * np.sqrt(3))]: ric.construct_b_matrix(hmat, np.array(coords, dtype=np.float64)) res = ric.get_val_stretches()[0] self.assertAlmostEqual(res, ref)
def test_7(self): masses = np.array(range(1, 2), dtype=np.float64) ric = RIC() ric.add_eckart(rots=[False, 0, 'a']) ric.setup(masses) self.assertListEqual(list(ric._ric.ric_def_eckart_rots), [3]) self.assertListEqual(list(ric._ric.ric_ibr_eckart_rots), [4]) self.assertEqual(ric.num_ric, 4) self.assertEqual(ric.num_stretch, 0) self.assertEqual(ric.num_in_bend, 0) 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, 4)
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_grad_0D_rand(self): masses = np.array([1.]*4) ric = RIC() ric.add_out_bend([1,2,3,4]) ric.setup(masses) def func(x): ric.construct_b_matrix(None,np.reshape(x,(-1,3))) res = np.copy(ric.get_val_out_bends()) return res grad = nd.Gradient(func,step_nom=[0.01]*12) for coords in 2*np.random.random((10,4,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-9)
def test_values_1(self): masses = np.array([1.]*4) hmat = np.array([[0.,0.,0.]]*3) for coords, ref in [ ([[0,0,0],[0,0, 1],[-1,-1,0],[1,-1,0]], 0), ([[0,0,0],[0,1, 1],[-1,-1,0],[1,-1,0]], 45), ([[0,0,0],[0,1, 0],[-1,-1,0],[1,-1,0]], 90), ([[0,0,0],[0,1,-1],[-1,-1,0],[1,-1,0]], 135), ([[0,0,0],[0,0,-1],[-1,-1,0],[1,-1,0]], 180), ]: ric = RIC() ric.add_out_bend([1,2,3,4]) ric.setup(masses) ric.construct_b_matrix(hmat,np.array(coords,dtype=np.float64)) res = ric.get_val_out_bends()[0] self.assertAlmostEqual(res,np.radians(ref)) del ric
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_1(self): masses = np.array([1.] * 2) ric = RIC() ric.add_stretch([1, 2]) ric.setup(masses) def func(x): ric.construct_b_matrix(None, np.reshape(x, (2, 3))) return ric.get_val_stretches()[0] grad = nd.Gradient(func, step_nom=[0.01] * 6) for coords in [[[-1, 0, 0], [1, 0, 0]], [[0, -1, 0], [0, 1, 0]], [[0, 0, -1], [0, 0, 1]], [[2, 2, 2], [-1, -1, -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-12)
def test_1(self): ric = RIC() ric.add_stretch([1, 2]) 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_)), 1.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_)), 1.e-14)
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_5(self): masses = np.array(range(1, 14), dtype=np.float64) ric = RIC() ric.add_lin_bend([1, 2, 3], 'xy') ric.add_lin_bend([4, 5, 6], 'xz') ric.add_lin_bend([7, 8, 9], 'yz') ric.add_lin_bend([10, 11, 12], 13) ric.setup(masses) self.assertListEqual(list(ric._ric.ric_def_lin_bends[:, 0]), [1, 2, 3]) self.assertListEqual(list(ric._ric.ric_def_lin_bends[:, 1]), [1, 2, 3]) self.assertListEqual(list(ric._ric.ric_def_lin_bends[:, 2]), [4, 5, 6]) self.assertListEqual(list(ric._ric.ric_def_lin_bends[:, 3]), [4, 5, 6]) self.assertListEqual(list(ric._ric.ric_def_lin_bends[:, 4]), [7, 8, 9]) self.assertListEqual(list(ric._ric.ric_def_lin_bends[:, 5]), [7, 8, 9]) self.assertListEqual(list(ric._ric.ric_def_lin_bends[:, 6]), [10, 11, 12]) self.assertListEqual(list(ric._ric.ric_def_lin_bends[:, 7]), [10, 11, 12]) self.assertListEqual(list(ric._ric.ric_ibr_lin_bends), [1, 2, 3, 4, 5, 6, 7, 8]) self.assertListEqual(list(ric._ric.ric_lin_bend_inds), [0, 0, 0, 0, 0, 0, 13, -13]) self.assertListEqual(list(ric._ric.ric_lin_bend_axes[:, 0]), [1, 0, 0]) self.assertListEqual(list(ric._ric.ric_lin_bend_axes[:, 1]), [0, 1, 0]) self.assertListEqual(list(ric._ric.ric_lin_bend_axes[:, 2]), [1, 0, 0]) self.assertListEqual(list(ric._ric.ric_lin_bend_axes[:, 3]), [0, 0, 1]) self.assertListEqual(list(ric._ric.ric_lin_bend_axes[:, 4]), [0, 1, 0]) self.assertListEqual(list(ric._ric.ric_lin_bend_axes[:, 5]), [0, 0, 1]) self.assertListEqual(list(ric._ric.ric_lin_bend_axes[:, 6]), [0, 0, 0]) self.assertListEqual(list(ric._ric.ric_lin_bend_axes[:, 7]), [0, 0, 0]) self.assertEqual(ric.num_ric, 8) self.assertEqual(ric.num_stretch, 0) self.assertEqual(ric.num_in_bend, 0) self.assertEqual(ric.num_out_bend, 0) self.assertEqual(ric.num_lin_bend, 8) 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_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_grad_0D_1(self): masses = np.array([1., 2., 1.]) ric = RIC() ric.add_eckart(trans=[True] * 3, rots=[False] * 3) ric.setup(masses) def func(x): ric.construct_b_matrix(None, np.reshape(x, (3, 3))) return np.copy(ric.get_val_eckarts()) jac = nd.Jacobian(func, step_nom=[0.01] * 9) for coords in [[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[-1, -1, -1], [0, 0, 0], [1, 1, 1]], [[-2, 0, 0], [2, 0, 0], [-2, 0, 0]]]: coords = np.array(coords, dtype=np.float64) ref = jac(coords.flatten()) res = ric.construct_b_matrix(None, coords) #print ref #print res self.assertLess(np.max(np.abs(ref - res)), 1.e-12)
def test_grad_0D_1(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 [[[1, 0, 0], [0, 0, 0], [1, 1, 0]], [[1, 0, 0], [0, 0, 0], [0, 1, 0]], [[1, 0, 0], [0, 0, 0], [-1, 1, 0]], [[0, 1, 2], [5, 4, 3], [6, 7, 8]]]: 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_val_0D_yz(self): """Values without PBC""" masses = np.array([1.] * 3) ric = RIC() ric.add_lin_bend([1, 2, 3], 'yz') ric.setup(masses) for coords, ref in [ ([[-1, 0, 0], [0, 0, 0], [1, 0, 0]], [0, 0]), # Ref ([[-1, -1, -1], [0, 0, 0], [1, 1, 1]], [0, 0]), # Diagonal ([[0, 0, 0], [1, 1, 1], [2, 2, 2]], [0, 0]), # Offset ([[-3, 0, 0], [0, 0, 0], [5, 0, 0]], [0, 0]), # Longer ([[-1, 0, 0], [0, 0, 0], [1, -1, 0]], [0, 45]), # -y ([[-1, 0, 0], [0, 0, 0], [1, 1, 0]], [0, -45]), # +y ([[-1, 0, 0], [0, 0, 0], [1, 0, -1]], [-45, 0]), # -z ([[-1, 0, 0], [0, 0, 0], [1, 0, 1]], [45, 0]), # +z ]: ric.construct_b_matrix(None, np.array(coords, dtype=np.float64)) res = ric.get_val_lin_bends() self.assertAlmostEqual(res[0], np.radians(ref[0])) self.assertAlmostEqual(res[1], np.radians(ref[1]))
def test_6(self): masses = np.array(range(1, 10), dtype=np.float64) ric = RIC() ric.add_torsion([1, 0, 0, 0, 0, 2, 3, 4, 0, 0, 0, 0]) ric.add_torsion([3, 4, 0, 0, 0, 5, 6, 7, 8, 9, 0, 0], ivals=[2, 3]) ric.setup(masses) self.assertListEqual(list(ric._ric.ric_def_torsions[:, 0]), [1, 0, 0, 0, 0, 2, 3, 4, 0, 0, 0, 0]) self.assertListEqual(list(ric._ric.ric_def_torsions[:, 1]), [3, 4, 0, 0, 0, 5, 6, 7, 8, 9, 0, 0]) self.assertListEqual(list(ric._ric.ric_torsion_ivals[:, 0]), [1, 1]) self.assertListEqual(list(ric._ric.ric_torsion_ivals[:, 1]), [2, 3]) self.assertListEqual(list(ric._ric.ric_ibr_torsions), [1, 2]) self.assertEqual(ric.num_ric, 2) self.assertEqual(ric.num_stretch, 0) self.assertEqual(ric.num_in_bend, 0) self.assertEqual(ric.num_out_bend, 0) self.assertEqual(ric.num_lin_bend, 0) self.assertEqual(ric.num_torsion, 2) self.assertEqual(ric.num_eckart, 0)
def test_grad_0D_ind(self): masses = np.array([1.] * 4) ric = RIC() ric.add_lin_bend([1, 2, 3], 4) ric.setup(masses) def func(x): ric.construct_b_matrix(None, np.reshape(x, (-1, 3))) res = np.copy(ric.get_val_lin_bends()) return res jac = nd.Jacobian(func, step_nom=[0.01] * 12) # This is buggy! def func0(x): ric.construct_b_matrix(None, np.reshape(x, (-1, 3))) res = np.copy(ric.get_val_lin_bends()) return res[0] def func1(x): ric.construct_b_matrix(None, np.reshape(x, (-1, 3))) res = np.copy(ric.get_val_lin_bends()) return res[1] grad0 = nd.Gradient(func0, step_nom=[0.01] * 12) grad1 = nd.Gradient(func1, step_nom=[0.01] * 12) for coords in [ [[-1, 0, 0], [.1, 0, 0], [1, 0, 0], [0, 1, 0]], # x-y [[-1, 0, 0], [.1, 0, 0], [1, 0, 0], [0, 0, 1]], # x-z [[-1, 0, 0], [.1, 0, 0], [1, 0, 0], [0, 1, 1]], # x-yz [[-1, 0, 0], [.1, 0, 0], [1, 0, 0], [1, 1, 1]], # x-xyz [[0, -1, 0], [0, .1, 0], [0, 1, 0], [2, 0, 0]], # y-x [[0, -1, 0], [0, .1, 0], [0, 1, 0], [0, 0, 2]], # y-z [[0, -1, 0], [0, .1, 0], [0, 1, 0], [0, 2, 2]], # y-yz [[0, -1, 0], [0, .1, 0], [0, 1, 0], [2, 2, 2]], # y-xyz [[0, 0, -1], [0, 0, .1], [0, 0, 1], [3, 0, 0]], # z-x [[0, 0, -1], [0, 0, .1], [0, 0, 1], [0, 3, 0]], # z-y [[0, 0, -1], [0, 0, .1], [0, 0, 1], [3, 3, 0]], # z-xy [[0, 0, -1], [0, 0, .1], [0, 0, 1], [3, 3, 3]], # z-xyz ]: coords = np.array(coords, dtype=np.float64) res = ric.construct_b_matrix(None, coords) # HACK: disable the axes updates _inds = np.copy(ric._ric.ric_lin_bend_inds) ric._ric.ric_lin_bend_inds[:] = 0 #ref = jac(coords.flatten()) ref0 = grad0(coords.flatten()) ref1 = grad1(coords.flatten()) # HACK: enbale the axes updates ric._ric.ric_lin_bend_inds[:] = _inds #print coords #print ref #print ref0 #print ref1 #print res #self.assertLess(np.max(np.abs(ref-res)),1.e-8) self.assertLess(np.max(np.abs(ref0 - res[0])), 1.e-8) self.assertLess(np.max(np.abs(ref1 - res[1])), 1.e-8)