def test_transform_gradient(self): tai, atom_indices = self.make_atom_indices_python_topology() tnorm = self.make_normal_python_topology() coords = self.get_random_rbcoords() aai = tai.to_atomistic(coords.copy()) anorm = tnorm.to_atomistic(coords.copy()) lj = LJ() # atomistic_coords = np.random.uniform(-1, 1, 3*len(atom_indices)) # e, atomistic_grad = lj.getEnergyGradient(atomistic_coords) e1, gai = lj.getEnergyGradient(aai.ravel()) e2, gnorm = lj.getEnergyGradient(anorm.ravel()) self.assertAlmostEqual(e1, e2) assert_arrays_almost_equal(self, gai.reshape(-1,3)[atom_indices], gnorm.reshape(-1,3)) rb_gai = tai.transform_gradient(coords.copy(), gai) rb_gnorm = tnorm.transform_gradient(coords.copy(), gnorm) assert_arrays_almost_equal(self, rb_gai, rb_gnorm)
def test_cpp_align(self): x1 = np.array([random_aa() for _ in range(2*self.nrigid)]).ravel() x2 = np.array([random_aa() for _ in range(2*self.nrigid)]).ravel() x1_cpp = x1.copy() x2_cpp = x2.copy() x1_python = x1.copy() x2_python = x2.copy() ret = self.measure._align_pythonic(x1_python, x2_python) self.assertIsNone(ret) ret = self.measure.align(x1_cpp, x2_cpp) self.assertIsNone(ret) assert_arrays_almost_equal(self, x1_python, x1) assert_arrays_almost_equal(self, x1_cpp, x1) # assert that the center of mass coordinates didn't change assert_arrays_almost_equal(self, x2_python[:3*self.nrigid], x2[:3*self.nrigid]) assert_arrays_almost_equal(self, x2_cpp[:3*self.nrigid], x2[:3*self.nrigid]) # assert that x2 actually changed max_change = np.max(np.abs(x2_cpp - x2)) self.assertGreater(max_change, 1e-3) assert_arrays_almost_equal(self, x2_python, x2_cpp)
def test_cpp_align(self): x1 = np.array([random_aa() for _ in range(2 * self.nrigid)]).ravel() x2 = np.array([random_aa() for _ in range(2 * self.nrigid)]).ravel() x1_cpp = x1.copy() x2_cpp = x2.copy() x1_python = x1.copy() x2_python = x2.copy() ret = self.measure._align_pythonic(x1_python, x2_python) self.assertIsNone(ret) ret = self.measure.align(x1_cpp, x2_cpp) self.assertIsNone(ret) assert_arrays_almost_equal(self, x1_python, x1) assert_arrays_almost_equal(self, x1_cpp, x1) # assert that the center of mass coordinates didn't change assert_arrays_almost_equal(self, x2_python[:3 * self.nrigid], x2[:3 * self.nrigid]) assert_arrays_almost_equal(self, x2_cpp[:3 * self.nrigid], x2[:3 * self.nrigid]) # assert that x2 actually changed max_change = np.max(np.abs(x2_cpp - x2)) self.assertGreater(max_change, 1e-3) assert_arrays_almost_equal(self, x2_python, x2_cpp)
def test_compare(self): dkear, rot1 = findrotation_kearsley(self.x1, self.x2, align_com=True) dkab, rot2 = kabsch_wrap(self.x1, self.x2, align_com=True) qkear = mx2q(rot1) qkab = mx2q(rot2) self.assertAlmostEqual(dkear, dkab, places=4) assert_arrays_almost_equal(self, qkear, qkab)
def test_align_exact(self): x1 = np.array([random_aa() for _ in range(2 * self.nrigid)]).ravel() x2 = x1.copy() tet = create_tetrahedron() mx = tet.symmetries[2].copy() p = x2[-3:].copy() x2[-3:] = rotations.rotate_aa(rotations.mx2aa(mx), p) self.measure._align_pythonic(x1, x2) assert_arrays_almost_equal(self, x1, x2)
def test_align_exact(self): x1 = np.array([random_aa() for _ in range(2*self.nrigid)]).ravel() x2 = x1.copy() tet = create_tetrahedron() mx = tet.symmetries[2].copy() p = x2[-3:].copy() x2[-3:] = rotations.rotate_aa(rotations.mx2aa(mx), p) self.measure._align_pythonic(x1, x2) assert_arrays_almost_equal(self, x1, x2)
def test_to_atomistic(self): tai, atom_indices = self.make_atom_indices_python_topology() tnorm = self.make_normal_python_topology() coords = self.get_random_rbcoords() a1 = tai.to_atomistic(coords.copy()) a2 = tnorm.to_atomistic(coords) a1perm = a1[atom_indices] assert_arrays_almost_equal(self, a1perm, a2)
def test1(self): # plot_pot() opt = FindTransitionState(self.x0, self.pot, orthogZeroEigs=None, # iprint=1, # verbosity=10, event=print_event, # tol=1e-3, # lowestEigenvectorQuenchParams=dict(iprint=1, events=[print_event]) ) ret = opt.run() self.assertTrue(ret.success) assert_arrays_almost_equal(self, ret.coords, self.xts, places=3) self.assertAlmostEqual(ret.energy, self.ets, delta=1e-3) self.assertLess(ret.rms, 1e-3) self.assertEqual(ret.nfev + 1, self.pot.nfev)
def test1(self): rot = q2mx(random_q()) x2 = self.x1.copy() self.transform.rotate(x2, rot) x1 = self.x1 dist, mx = self.findrot(x1, x2) self.assertLess(dist, 1e-4) self.transform.translate(x2, -self.measure.get_com(x2)) self.transform.rotate(x2, mx) self.transform.translate(x2, self.measure.get_com(x1)) assert_arrays_almost_equal(self, x1, x2)
def test_from_near_minimum(self): print "\n\nstarting from a minimum" x0 = np.array([.6, .1]) opt = FindTransitionState(x0, self.pot, orthogZeroEigs=None, iprint=1, verbosity=10, # event=print_event, # tol=1e-3, # lowestEigenvectorQuenchParams=dict(iprint=1, events=[print_event]) ) ret = opt.run() print ret self.assertTrue(ret.success) assert_arrays_almost_equal(self, ret.coords, self.xts, places=3)
def test_from_near_minimum(self): print("\n\nstarting from a minimum") x0 = np.array([.6, .1]) opt = FindTransitionState(x0, self.pot, orthogZeroEigs=None, iprint=1, verbosity=10, # event=print_event, # tol=1e-3, # lowestEigenvectorQuenchParams=dict(iprint=1, events=[print_event]) ) ret = opt.run() print(ret) self.assertTrue(ret.success) assert_arrays_almost_equal(self, ret.coords, self.xts, places=3)
def test2(self): rot = q2mx(random_q()) dx = .01 x2 = self.x1.copy() x2[0] += .01 self.transform.rotate(x2, rot) x1old = self.x1.copy() x2old = x2.copy() dist, mx = self.findrot(self.x1, x2) # check it didn't change the arrays assert_arrays_almost_equal(self, x1old, self.x1) assert_arrays_almost_equal(self, x2old, x2) self.assertLess(dist, dx)
def test_cpp_rotate(self): x0 = np.array([random_aa() for _ in range(2 * self.nrigid)]).ravel() aa = random_aa() mx = aa2mx(aa) x0_rotated = x0.copy() self.transform.rotate(x0_rotated, mx) x0_rotated_python = x0.copy() self.transform._rotate_python(x0_rotated_python, mx) assert_arrays_almost_equal(self, x0_rotated, x0_rotated_python) mx_reverse = aa2mx(-aa) self.transform.rotate(x0_rotated, mx_reverse) assert_arrays_almost_equal(self, x0, x0_rotated)
def test_cpp_rotate(self): x0 = np.array([random_aa() for _ in range(2*self.nrigid)]).ravel() aa = random_aa() mx = aa2mx(aa) x0_rotated = x0.copy() self.transform.rotate(x0_rotated, mx) x0_rotated_python = x0.copy() self.transform._rotate_python(x0_rotated_python, mx) assert_arrays_almost_equal(self, x0_rotated, x0_rotated_python) mx_reverse = aa2mx(-aa) self.transform.rotate(x0_rotated, mx_reverse) assert_arrays_almost_equal(self, x0, x0_rotated)
def test_cpp_potential(self): python_topology, atom_indices = self.make_atom_indices_python_topology() cpp_topology, atom_indices = self.make_atom_indices_cpp_topology(atom_indices=atom_indices.copy()) lj = LJ() pot_python = pythonRBPotentialWrapper(python_topology, lj) pot_cpp = pythonRBPotentialWrapper(cpp_topology, lj) coords = self.get_random_rbcoords() e_python = pot_python.getEnergy(coords.copy()) e_cpp = pot_cpp.getEnergy(coords.copy()) self.assertAlmostEqual(e_python, e_cpp) e1, g_python = pot_python.getEnergyGradient(coords.copy()) e2, g_cpp = pot_cpp.getEnergyGradient(coords.copy()) assert_arrays_almost_equal(self, g_python, g_cpp)
def test_potential(self): topology, atom_indices = self.make_atom_indices_python_topology() normal_topology = self.make_normal_python_topology() lj = LJ() pot = pythonRBPotentialWrapper(topology, lj) pot_normal = pythonRBPotentialWrapper(normal_topology, lj) coords = self.get_random_rbcoords() energy_atom_indices = pot.getEnergy(coords.copy()) energy_normal = pot_normal.getEnergy(coords.copy()) self.assertAlmostEqual(energy_atom_indices, energy_normal) e1, gei = pot.getEnergyGradient(coords.copy()) e2, gnorm = pot_normal.getEnergyGradient(coords.copy()) assert_arrays_almost_equal(self, gei, gnorm)
def test_cpp_potential(self): python_topology, atom_indices = self.make_atom_indices_python_topology( ) cpp_topology, atom_indices = self.make_atom_indices_cpp_topology( atom_indices=atom_indices.copy()) lj = LJ() pot_python = pythonRBPotentialWrapper(python_topology, lj) pot_cpp = pythonRBPotentialWrapper(cpp_topology, lj) coords = self.get_random_rbcoords() e_python = pot_python.getEnergy(coords.copy()) e_cpp = pot_cpp.getEnergy(coords.copy()) self.assertAlmostEqual(e_python, e_cpp) e1, g_python = pot_python.getEnergyGradient(coords.copy()) e2, g_cpp = pot_cpp.getEnergyGradient(coords.copy()) assert_arrays_almost_equal(self, g_python, g_cpp)
def test1(self): current_dir = os.path.dirname(__file__) db = Database() converter = OptimDBConverter( db, mindata=os.path.join(current_dir, "min.data"), tsdata=os.path.join(current_dir, "ts.data"), pointsmin=os.path.join(current_dir, "points.min"), pointsts=os.path.join(current_dir, "points.ts"), endianness="<") converter.convert() mdata = tempfile.NamedTemporaryFile(delete=True) tsdata = tempfile.NamedTemporaryFile(delete=True) pm = tempfile.NamedTemporaryFile(delete=True) pts = tempfile.NamedTemporaryFile(delete=True) print mdata.name, tsdata.name writer = WritePathsampleDB(db, mindata=mdata.name, tsdata=tsdata.name, pointsmin=pm.name, pointsts=pts.name, endianness="<") writer.write_db() d1 = np.genfromtxt(os.path.join(current_dir, "min.data"))[:, :3] d2 = np.genfromtxt(mdata.name)[:, :3] _base_test.assert_arrays_almost_equal(self, d1, d2) d1 = np.genfromtxt(os.path.join(current_dir, "ts.data")).reshape(-1, 8)[:, :5] d2 = np.genfromtxt(tsdata.name).reshape(-1, 8)[:, :5] print d1, d2 _base_test.assert_arrays_almost_equal(self, d1, d2) self.assertEqual(sha1_of_file(os.path.join(current_dir, "points.min")), sha1_of_file(pm.name)) self.assertEqual(sha1_of_file(os.path.join(current_dir, "points.ts")), sha1_of_file(pts.name))
def test2(self): from pele.utils.tests.test_disconnectivity_graph import create_random_database db = create_random_database(nmin=20, nts=15) delete = False mdata = tempfile.NamedTemporaryFile(delete=delete, suffix=".min.data") tsdata = tempfile.NamedTemporaryFile(delete=delete, suffix=".ts.data") pm = tempfile.NamedTemporaryFile(delete=delete) pts = tempfile.NamedTemporaryFile(delete=delete) print mdata.name, tsdata.name writer = WritePathsampleDB(db, mindata=mdata.name, tsdata=tsdata.name, pointsmin=pm.name, pointsts=pts.name, endianness="<") writer.write_db() newdb = Database() reader = OptimDBConverter(newdb, mindata=mdata.name, tsdata=tsdata.name, pointsmin=pm.name, pointsts=pts.name, endianness="<") reader.convert() for m1, m2 in izip(db.minima(), newdb.minima()): self.assertAlmostEqual(m1.energy, m2.energy) _base_test.assert_arrays_almost_equal(self, m1.coords, m2.coords) for ts1, ts2 in izip(db.transition_states(), newdb.transition_states()): self.assertAlmostEqual(ts1.energy, ts2.energy) _base_test.assert_arrays_almost_equal(self, ts1.coords, ts2.coords) self.assertAlmostEqual(ts1.minimum1.energy, ts2.minimum1.energy) self.assertAlmostEqual(ts1.minimum2.energy, ts2.minimum2.energy)
def test1(self): current_dir = os.path.dirname(__file__) db = Database() converter = OptimDBConverter(db, mindata=os.path.join(current_dir, "min.data"), tsdata=os.path.join(current_dir, "ts.data"), pointsmin=os.path.join(current_dir, "points.min"), pointsts=os.path.join(current_dir, "points.ts"), endianness="<") converter.convert() mdata = tempfile.NamedTemporaryFile(delete=True) tsdata = tempfile.NamedTemporaryFile(delete=True) pm = tempfile.NamedTemporaryFile(delete=True) pts = tempfile.NamedTemporaryFile(delete=True) print(mdata.name, tsdata.name) writer = WritePathsampleDB(db, mindata=mdata.name, tsdata=tsdata.name, pointsmin=pm.name, pointsts=pts.name, endianness="<") writer.write_db() d1 = np.genfromtxt(os.path.join(current_dir, "min.data"))[:,:3] d2 = np.genfromtxt(mdata.name)[:,:3] _base_test.assert_arrays_almost_equal(self, d1, d2) d1 = np.genfromtxt(os.path.join(current_dir, "ts.data")).reshape(-1,8)[:,:5] d2 = np.genfromtxt(tsdata.name).reshape(-1,8)[:,:5] print(d1, d2) _base_test.assert_arrays_almost_equal(self, d1, d2) self.assertEqual(sha1_of_file(os.path.join(current_dir, "points.min")), sha1_of_file(pm.name)) self.assertEqual(sha1_of_file(os.path.join(current_dir, "points.ts")), sha1_of_file(pts.name))
def test2(self): from pele.utils.tests.test_disconnectivity_graph import create_random_database db = create_random_database(nmin=20, nts=15) delete=False mdata = tempfile.NamedTemporaryFile(delete=delete, suffix=".min.data") tsdata = tempfile.NamedTemporaryFile(delete=delete, suffix=".ts.data") pm = tempfile.NamedTemporaryFile(delete=delete) pts = tempfile.NamedTemporaryFile(delete=delete) print(mdata.name, tsdata.name) writer = WritePathsampleDB(db, mindata=mdata.name, tsdata=tsdata.name, pointsmin=pm.name, pointsts=pts.name, endianness="<") writer.write_db() newdb = Database() reader = OptimDBConverter(newdb, mindata=mdata.name, tsdata=tsdata.name, pointsmin=pm.name, pointsts=pts.name, endianness="<") reader.convert() for m1, m2 in zip(db.minima(), newdb.minima()): self.assertAlmostEqual(m1.energy, m2.energy) _base_test.assert_arrays_almost_equal(self, m1.coords, m2.coords) for ts1, ts2 in zip(db.transition_states(), newdb.transition_states()): self.assertAlmostEqual(ts1.energy, ts2.energy) _base_test.assert_arrays_almost_equal(self, ts1.coords, ts2.coords) self.assertAlmostEqual(ts1.minimum1.energy, ts2.minimum1.energy) self.assertAlmostEqual(ts1.minimum2.energy, ts2.minimum2.energy)
def test_transform_gradient(self): tai, atom_indices = self.make_atom_indices_python_topology() tnorm = self.make_normal_python_topology() coords = self.get_random_rbcoords() aai = tai.to_atomistic(coords.copy()) anorm = tnorm.to_atomistic(coords.copy()) lj = LJ() # atomistic_coords = np.random.uniform(-1, 1, 3*len(atom_indices)) # e, atomistic_grad = lj.getEnergyGradient(atomistic_coords) e1, gai = lj.getEnergyGradient(aai.ravel()) e2, gnorm = lj.getEnergyGradient(anorm.ravel()) self.assertAlmostEqual(e1, e2) assert_arrays_almost_equal(self, gai.reshape(-1, 3)[atom_indices], gnorm.reshape(-1, 3)) rb_gai = tai.transform_gradient(coords.copy(), gai) rb_gnorm = tnorm.transform_gradient(coords.copy(), gnorm) assert_arrays_almost_equal(self, rb_gai, rb_gnorm)
def test_wrapper(self): ret = findTransitionState(self.x0, self.pot, orthogZeroEigs=None) self.assertTrue(ret.success) assert_arrays_almost_equal(self, ret.coords, self.xts, places=3)