Esempio n. 1
0
 def setUp(self):
     np.random.seed(0)
     self.nmol = 5
     self.boxvec = np.array([5,5,5])
     self.rcut = 2.5
     self.system = OTPBulk(self.nmol, self.boxvec, self.rcut)
     pot = self.system.get_potential()
     self.db = self.system.create_database()
     self.m1 = self.db.addMinimum(pot.getEnergy(_x1), _x1)
     self.m2 = self.db.addMinimum(pot.getEnergy(_x2), _x2)
     self.m3 = self.db.addMinimum(pot.getEnergy(_x3), _x3)
     self.m4 = self.db.addMinimum(pot.getEnergy(_x4), _x4)                
Esempio n. 2
0
class TestOTPBulk(unittest.TestCase):
    def setUp(self):
        np.random.seed(0)
        self.nmol = 5
        self.boxvec = np.array([5,5,5])
        self.rcut = 2.5
        self.system = OTPBulk(self.nmol, self.boxvec, self.rcut)
        pot = self.system.get_potential()
        self.db = self.system.create_database()
        self.m1 = self.db.addMinimum(pot.getEnergy(_x1), _x1)
        self.m2 = self.db.addMinimum(pot.getEnergy(_x2), _x2)
        self.m3 = self.db.addMinimum(pot.getEnergy(_x3), _x3)
        self.m4 = self.db.addMinimum(pot.getEnergy(_x4), _x4)                
            
    def test_energy(self):
        self.assertAlmostEqual(self.m3.energy, -34.6758148083, 5) 
        self.assertAlmostEqual(self.m4.energy, -33.7606288716, 5)               
            
    def test_periodic_distance(self):
        x0 = self.system.get_random_configuration()
        shift = np.zeros(self.nmol*6)
        for i in range(3*self.nmol):
            shift[i] = self.boxvec[i%3]/2+1
        x1 = x0 + shift
        
        self.assertLess(sqrt(self.system.aatopology.distance_squared(x0,x1)), 
                        np.linalg.norm((self.boxvec//2+1))*len(self.system.aatopology.sites))
 
        shift = np.zeros(self.nmol*6)    
        for i in range(3*self.nmol):
            shift[i] = -self.boxvec[i%3]
        x1 = x0 + shift
        
        self.assertLess(self.system.aatopology.distance_squared(x0,x1), 1e-8)     
    
    def test1(self):
        pot = self.system.get_potential()
        self.assertLess(np.linalg.norm(pot.getGradient(self.m1.coords)), .1)
        self.assertLess(np.linalg.norm(pot.getGradient(self.m2.coords)), .1)
        
    def test_random_configuration(self):
        n_tests = 100
        fail_count = 0
        x = []
        for i in range(n_tests):
            x.append(self.system.get_random_configuration())
            for j in range(i):
                if(i == j):
                    continue
                if (np.linalg.norm(x[i]-x[j]) < 1e-10):
                    fail_count += 1
                    print("Failing configurations:")
                    print(x[i])
                    print(x[j])
                    print("Difference")
                    print(x[i]-x[j])
                    print("Norm")
                    print(np.linalg.norm(x[i]-x[j]))
        if fail_count > 0:
            print("Failed {} times".format(fail_count))
        self.assertEqual(fail_count, 0)
    
    def test_distance_measure(self):
        n_tests = 100
        fail_count = 0
        for i in range(n_tests):
            x1 = self.system.get_random_configuration()
            x2 = self.system.get_random_configuration()
            dist1 = sqrt(self.system.aatopology.distance_squared(x1, x2))
            x1at = self.system.aatopology.to_atomistic(x1)
            x2at = self.system.aatopology.to_atomistic(x2)
            dist2 = np.linalg.norm(x1at - x2at)
            if(dist1-dist2>1e-13):
                fail_count+=1
                print("Failed.")
                print("distance measure:", dist1)
                print("atomistic cartesian distance:", dist2)
        self.assertEqual(fail_count, 0)
            
        
    
    def test_basinhopping(self):
        db = self.system.create_database()
        bh = self.system.get_basinhopping(db)
        bh.setPrinting(ostream=None)
        bh.run(5)
        self.assertGreaterEqual(db.number_of_minima(), 1)

    def test_double_ended_connect(self):
        connect = self.system.get_double_ended_connect(self.m3, self.m4, self.db)
        connect.connect()
        self.assertTrue(connect.success())