Exemplo n.º 1
0
    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]))
Exemplo n.º 2
0
  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
Exemplo n.º 3
0
  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)
Exemplo n.º 4
0
    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))
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
    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)
Exemplo n.º 7
0
    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))
Exemplo n.º 8
0
  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
Exemplo n.º 9
0
    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)
Exemplo n.º 10
0
    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)
Exemplo n.º 11
0
    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)
Exemplo n.º 12
0
    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]))
Exemplo n.º 13
0
  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)
Exemplo n.º 14
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)
Exemplo n.º 15
0
    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)
Exemplo n.º 16
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)
Exemplo n.º 17
0
  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)
Exemplo n.º 18
0
    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)
Exemplo n.º 19
0
    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)
Exemplo n.º 20
0
  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)
Exemplo n.º 21
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)