def setup_aatopology(self): water_sites, ref = water(self.xyzfile) rbsystem = RBTopology() rbsystem.add_sites(water_sites) rbsystem.finalize_setup() #print len(rbsystem.sites), len(rbsystem.indices) print "I have %d water molecules in the system" % len(rbsystem.sites) rbcoords = rbsystem.coords_adapter(np.zeros(len(rbsystem.sites)*6)) for site, com in zip(rbsystem.sites, rbcoords.posRigid): com[:] = ref.coords[site.atom_indices[0]] - site.atom_positions[0] pot = LJ(eps=0.1550, sig=3.1536) # get the flattened coordinate array print "The initial energy is", pot.getEnergy(ref.coords.flatten()) rbpot = rigidbody.RBPotentialWrapper(rbsystem, pot) print "rbpot.getEnergy(rbcoords.coords)", rbpot.getEnergy(rbcoords.coords) e, g = rbpot.getEnergyGradient(rbcoords.coords) g_n = rbpot.NumericalDerivative(rbcoords.coords, eps=1e-4) cg = rbsystem.coords_adapter(g-g_n) # coords = rbpot.getCoords() # nrigid = rbcoords.size / 6 # print "nrigid", nrigid self.potential = rbpot self.nrigid = len(rbsystem.sites) self.render_scale = 0.3 self.atom_types = rbsystem.get_atomtypes()
class TestAAMeasure(unittest.TestCase): def setUp(self): self.nrigid = 10 self.topology = RBTopology() self.topology.add_sites( [create_tetrahedron() for _ in range(self.nrigid)]) self.topology.finalize_setup() self.measure = MeasureAngleAxisCluster(self.topology) 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_align_bad_input(self): x1 = np.array([random_aa() for _ in range(2 * self.nrigid)]).ravel() x2 = list(x1) with self.assertRaises(TypeError): self.measure.align(x1, x2) def test_symmetries(self): tet = create_tetrahedron() print(tet.symmetries) self.assertEqual(len(tet.symmetries), 12) 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)
class TestAAMeasure(unittest.TestCase): def setUp(self): self.nrigid = 10 self.topology = RBTopology() self.topology.add_sites([create_tetrahedron() for _ in range(self.nrigid)]) self.topology.finalize_setup() self.measure = MeasureAngleAxisCluster(self.topology) 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_align_bad_input(self): x1 = np.array([random_aa() for _ in range(2*self.nrigid)]).ravel() x2 = list(x1) with self.assertRaises(TypeError): self.measure.align(x1, x2) def test_symmetries(self): tet = create_tetrahedron() print(tet.symmetries) self.assertEqual(len(tet.symmetries), 12) 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 make_atom_indices_cpp_topology(self, atom_indices=None): sites = [make_otp() for _ in xrange(self.nrigid)] if atom_indices is None: atom_indices = np.array(range(self.nrigid * 3), dtype=int) np.random.shuffle(atom_indices) i = 0 for site in sites: site.atom_indices = atom_indices[i:i + site.get_natoms()].copy() i += site.get_natoms() topology = RBTopology() topology.add_sites(sites) topology.finalize_setup(use_cpp=False) return topology, atom_indices
def make_atom_indices_cpp_topology(self, atom_indices=None): sites = [make_otp() for _ in xrange(self.nrigid)] if atom_indices is None: atom_indices = np.array(range(self.nrigid*3), dtype=int) np.random.shuffle(atom_indices) i = 0 for site in sites: site.atom_indices = atom_indices[i:i+site.get_natoms()].copy() i += site.get_natoms() topology = RBTopology() topology.add_sites(sites) topology.finalize_setup(use_cpp=False) return topology, atom_indices
def setup_aatopology(self): """this sets up the topology for the whole rigid body system""" topology = RBTopology() topology.add_sites([self.make_otp() for _ in xrange(self.nrigid)]) self.render_scale = 0.2 self.atom_types = topology.get_atomtypes() self.draw_bonds = [] for i in xrange(self.nrigid): self.draw_bonds.append((3*i, 3*i+1)) self.draw_bonds.append((3*i, 3*i+2)) topology.finalize_setup() return topology
class TestAAMindist(unittest.TestCase): def setUp(self): #GMIN.initialize() # self.pot = GMINPotential(GMIN) self.nrigid = 10 self.water = create_water() self.topology = RBTopology() self.topology.add_sites( [deepcopy(self.water) for _ in range(self.nrigid)]) self.topology.finalize_setup() # def test_zeroev(self): # x = self.pot.getCoords() # zev = self.topology.zeroEV(x) # # eps = 1e-5 # for dx in zev: # print "ev test", (self.pot.getEnergy(x) - self.pot.getEnergy(x + eps*dx))/eps # # dx = np.random.random(x.shape) # dx/=np.linalg.norm(dx) # print "ev test", (self.pot.getEnergy(x) - self.pot.getEnergy(x + eps*dx))/eps def test_distance(self): for i in range(100): coords1 = np.random.random(6 * self.nrigid) * 4 coords2 = np.random.random(6 * self.nrigid) * 4 coords1[:3 * self.nrigid] = 0 coords2[:3 * self.nrigid] = 0 measure1 = am.MeasureAngleAxisCluster(self.topology) measure2 = am.MeasureRigidBodyCluster(self.topology) #print self.assertAlmostEqual(measure1.get_dist(coords1, coords2), measure2.get_dist(coords1, coords2))
class TestAATransform(unittest.TestCase): def setUp(self): self.nrigid = 10 self.topology = RBTopology() self.topology.add_sites([create_water() for _ in range(self.nrigid)]) self.topology.finalize_setup() self.transform = TransformAngleAxisCluster(self.topology) 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)
class TestAAMindist(unittest.TestCase): def setUp(self): #GMIN.initialize() # self.pot = GMINPotential(GMIN) self.nrigid = 10 self.water = create_water() self.topology = RBTopology() self.topology.add_sites([deepcopy(self.water) for _ in range(self.nrigid)]) self.topology.finalize_setup() # def test_zeroev(self): # x = self.pot.getCoords() # zev = self.topology.zeroEV(x) # # eps = 1e-5 # for dx in zev: # print "ev test", (self.pot.getEnergy(x) - self.pot.getEnergy(x + eps*dx))/eps # # dx = np.random.random(x.shape) # dx/=np.linalg.norm(dx) # print "ev test", (self.pot.getEnergy(x) - self.pot.getEnergy(x + eps*dx))/eps def test_distance(self): for i in range(100): coords1 = np.random.random(6*self.nrigid)*4 coords2 = np.random.random(6*self.nrigid)*4 coords1[:3*self.nrigid]=0 coords2[:3*self.nrigid]=0 measure1 = am.MeasureAngleAxisCluster(self.topology) measure2 = am.MeasureRigidBodyCluster(self.topology) #print self.assertAlmostEqual(measure1.get_dist(coords1, coords2), measure2.get_dist(coords1, coords2))
class TestAATransform(unittest.TestCase): def setUp(self): self.nrigid = 10 self.topology = RBTopology() self.topology.add_sites([create_water() for _ in range(self.nrigid)]) self.topology.finalize_setup() self.transform = TransformAngleAxisCluster(self.topology) 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)
class TestOTPExplicit(unittest.TestCase): def setUp(self): nrigid = 3 self.topology = RBTopology() self.topology.add_sites([make_otp() for i in range(nrigid)]) self.topology.finalize_setup() cartesian_potential = LJ() self.pot = RBPotentialWrapper(self.topology, cartesian_potential) self.x0 = _x03 self.x0 = np.array(self.x0) self.e0 = -17.3387670023 assert nrigid * 6 == self.x0.size self.x0atomistic = _x03_atomistic self.nrigid = nrigid def test_energy(self): e = self.pot.getEnergy(self.x0) self.assertAlmostEqual(e, self.e0, delta=1e-4) def test_energy_gradient(self): e = self.pot.getEnergy(self.x0) gnum = self.pot.NumericalDerivative(self.x0) e2, g = self.pot.getEnergyGradient(self.x0) self.assertAlmostEqual(e, e2, delta=1e-4) for i in range(g.size): self.assertAlmostEqual(g[i], gnum[i], 2) def test_to_atomistic(self): xatom = self.topology.to_atomistic(self.x0).flatten() for i in range(xatom.size): self.assertAlmostEqual(xatom[i], self.x0atomistic[i], 2) def test_site_to_atomistic(self): rf = make_otp() p = np.array([1., 2, 3]) p /= np.linalg.norm(p) com = np.array([4., 5, 6]) print("otp to atomistic") print(rf.to_atomistic(com, p)) print("otp transform grad") g = np.array(list(range(9)), dtype=float).reshape([-1, 3]) print(g.reshape(-1)) print(rf.transform_grad(p, g)) def test_to_atomistic2(self): x0 = np.array(list(range(self.nrigid * 6)), dtype=float) x2 = x0.reshape([-1, 3]) for p in x2[self.nrigid:, :]: p /= np.linalg.norm(p) atomistic = self.topology.to_atomistic(x0).flatten() from pele.potentials import LJ lj = LJ() e, g = lj.getEnergyGradient(atomistic.reshape(-1)) grb = self.topology.transform_gradient(x0, g) rbpot = RBPotentialWrapper(self.topology, lj) print(rbpot.getEnergy(x0))
class TestOTPExplicit(unittest.TestCase): def make_otp(self): """this constructs a single OTP molecule""" otp = RigidFragment() otp.add_atom("O", np.array([0.0, -2. / 3 * np.sin(7. * pi / 24.), 0.0]), 1.) otp.add_atom( "O", np.array([cos(7. * pi / 24.), 1. / 3. * sin(7. * pi / 24.), 0.0]), 1.) otp.add_atom( "O", np.array([-cos(7. * pi / 24.), 1. / 3. * sin(7. * pi / 24), 0.0]), 1.) otp.finalize_setup() return otp def setUp(self): nrigid = 3 self.topology = RBTopology() self.topology.add_sites([self.make_otp() for i in xrange(nrigid)]) self.topology.finalize_setup() cartesian_potential = LJ() self.pot = RBPotentialWrapper(self.topology, cartesian_potential) self.x0 = _x03 self.x0 = np.array(self.x0) self.e0 = -17.3387670023 assert nrigid * 6 == self.x0.size self.x0atomistic = _x03_atomistic self.nrigid = nrigid def test_energy(self): e = self.pot.getEnergy(self.x0) self.assertAlmostEqual(e, self.e0, delta=1e-4) def test_energy_gradient(self): e = self.pot.getEnergy(self.x0) gnum = self.pot.NumericalDerivative(self.x0) e2, g = self.pot.getEnergyGradient(self.x0) self.assertAlmostEqual(e, e2, delta=1e-4) for i in xrange(g.size): self.assertAlmostEqual(g[i], gnum[i], 2) def test_to_atomistic(self): xatom = self.topology.to_atomistic(self.x0).flatten() for i in xrange(xatom.size): self.assertAlmostEqual(xatom[i], self.x0atomistic[i], 2) def test_site_to_atomistic(self): rf = self.make_otp() p = np.array([1., 2, 3]) p /= np.linalg.norm(p) com = np.array([4., 5, 6]) print "otp to atomistic" print rf.to_atomistic(com, p) print "otp transform grad" g = np.array(range(9), dtype=float).reshape([-1, 3]) print g.reshape(-1) print rf.transform_grad(p, g) def test_to_atomistic2(self): x0 = np.array(range(self.nrigid * 6), dtype=float) x2 = x0.reshape([-1, 3]) for p in x2[self.nrigid:, :]: p /= np.linalg.norm(p) atomistic = self.topology.to_atomistic(x0).flatten() from pele.potentials import LJ lj = LJ() e, g = lj.getEnergyGradient(atomistic.reshape(-1)) grb = self.topology.transform_gradient(x0, g) rbpot = RBPotentialWrapper(self.topology, lj) print rbpot.getEnergy(x0)
def make_normal_cpp_topology(self): sites = [make_otp() for _ in xrange(self.nrigid)] normal_topology = RBTopology() normal_topology.add_sites(sites) normal_topology.finalize_setup() return normal_topology
class TestOTPExplicit(unittest.TestCase): def setUp(self): nrigid = 3 self.topology = RBTopology() self.topology.add_sites([make_otp() for i in range(nrigid)]) self.topology.finalize_setup() cartesian_potential = LJ() self.pot = RBPotentialWrapper(self.topology, cartesian_potential) self.x0 = _x03 self.x0 = np.array(self.x0) self.e0 = -17.3387670023 assert nrigid * 6 == self.x0.size self.x0atomistic = _x03_atomistic self.nrigid = nrigid def test_energy(self): e = self.pot.getEnergy(self.x0) self.assertAlmostEqual(e, self.e0, delta=1e-4) def test_energy_gradient(self): e = self.pot.getEnergy(self.x0) gnum = self.pot.NumericalDerivative(self.x0) e2, g = self.pot.getEnergyGradient(self.x0) self.assertAlmostEqual(e, e2, delta=1e-4) for i in range(g.size): self.assertAlmostEqual(g[i], gnum[i], 2) def test_to_atomistic(self): xatom = self.topology.to_atomistic(self.x0).flatten() for i in range(xatom.size): self.assertAlmostEqual(xatom[i], self.x0atomistic[i], 2) def test_site_to_atomistic(self): rf = make_otp() p = np.array([1., 2, 3]) p /= np.linalg.norm(p) com = np.array([4., 5, 6]) print("otp to atomistic") print(rf.to_atomistic(com, p)) print("otp transform grad") g = np.array(list(range(9)), dtype=float).reshape([-1,3]) print(g.reshape(-1)) print(rf.transform_grad(p, g)) def test_to_atomistic2(self): x0 = np.array(list(range(self.nrigid * 6)), dtype=float) x2 = x0.reshape([-1,3]) for p in x2[self.nrigid:,:]: p /= np.linalg.norm(p) atomistic = self.topology.to_atomistic(x0).flatten() from pele.potentials import LJ lj = LJ() e, g = lj.getEnergyGradient(atomistic.reshape(-1)) grb = self.topology.transform_gradient(x0, g) rbpot = RBPotentialWrapper(self.topology, lj) print(rbpot.getEnergy(x0))
class TestOTPExplicit(unittest.TestCase): def make_otp(self): """this constructs a single OTP molecule""" otp = RigidFragment() otp.add_atom("O", np.array([0.0, -2./3 * np.sin( 7.*pi/24.), 0.0]), 1.) otp.add_atom("O", np.array([cos( 7.*pi/24.), 1./3. * sin( 7.* pi/24.), 0.0]), 1.) otp.add_atom("O", np.array([-cos( 7.* pi/24.), 1./3. * sin( 7.*pi/24), 0.0]), 1.) otp.finalize_setup() return otp def setUp(self): nrigid = 3 self.topology = RBTopology() self.topology.add_sites([self.make_otp() for i in xrange(nrigid)]) self.topology.finalize_setup() cartesian_potential = LJ() self.pot = RBPotentialWrapper(self.topology, cartesian_potential) self.x0 = _x03 self.x0 = np.array(self.x0) self.e0 = -17.3387670023 assert nrigid * 6 == self.x0.size self.x0atomistic = _x03_atomistic self.nrigid = nrigid def test_energy(self): e = self.pot.getEnergy(self.x0) self.assertAlmostEqual(e, self.e0, delta=1e-4) def test_energy_gradient(self): e = self.pot.getEnergy(self.x0) gnum = self.pot.NumericalDerivative(self.x0) e2, g = self.pot.getEnergyGradient(self.x0) self.assertAlmostEqual(e, e2, delta=1e-4) for i in xrange(g.size): self.assertAlmostEqual(g[i], gnum[i], 2) def test_to_atomistic(self): xatom = self.topology.to_atomistic(self.x0).flatten() for i in xrange(xatom.size): self.assertAlmostEqual(xatom[i], self.x0atomistic[i], 2) def test_site_to_atomistic(self): rf = self.make_otp() p = np.array([1., 2, 3]) p /= np.linalg.norm(p) com = np.array([4., 5, 6]) print "otp to atomistic" print rf.to_atomistic(com, p) print "otp transform grad" g = np.array(range(9), dtype=float).reshape([-1,3]) print g.reshape(-1) print rf.transform_grad(p, g) def test_to_atomistic2(self): x0 = np.array(range(self.nrigid * 6), dtype=float) x2 = x0.reshape([-1,3]) for p in x2[self.nrigid:,:]: p /= np.linalg.norm(p) atomistic = self.topology.to_atomistic(x0).flatten() from pele.potentials import LJ lj = LJ() e, g = lj.getEnergyGradient(atomistic.reshape(-1)) grb = self.topology.transform_gradient(x0, g) rbpot = RBPotentialWrapper(self.topology, lj) print rbpot.getEnergy(x0)