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)