def test_minimise(self):
        if self.executable is None:
            self.skipTest("Executable not found. Minimise test skipped.")

        testdir = "test_dftb"
        if os.path.exists(testdir):
            shutil.rmtree(testdir)
        os.mkdir(testdir)

        clus1 = Cluster(molecules=[
            Molecule(particle_names=["H", "H", "O"],
                     coordinates=np.array([[0.0, 0.0, 1.0], [0.0, 0.0, -1.0],
                                           [0.0, 0.0, 0.0]])),
            Molecule(particle_names=["H", "H", "O"],
                     coordinates=np.array([[2.0, 0.0, 1.0], [2.0, 0.0, -1.0],
                                           [2.0, 0.0, 0.0]]))
        ])

        pot = DeMonDFTBPotential(minimize_template=self.test_data_path +
                                 "/dftb_in.hsd",
                                 energy_template="NONE",
                                 work_dir=".",
                                 run_string=self.executable)

        clus2 = pot.minimize(clus1, dir_name=testdir)

        coords, mol_ids, atom_names = clus2.get_particle_positions()

        self.assertListEqual(atom_names, ["H", "H", "O", "H", "H", "O"])

        if os.path.exists(testdir):
            shutil.rmtree(testdir)
Beispiel #2
0
    def test_lj_energy_r1(self) -> None:

        c1 = Cluster(cost=0.0, molecules=[Molecule(coordinates=np.array([[0.0, 0.0, 0.0]]), particle_names=["LJ"]),
                                          Molecule(coordinates=np.array([[1.0, 0.0, 0.0]]), particle_names=["LJ"])])
        # First we check that we get V=0 at r=1
        self.assertEqual(0.0, self.LJ_6_12.get_energy(c1))
        self.assertEqual(0.0, self.LJ_15_30.get_energy(c1))
Beispiel #3
0
    def test_molecular_rotations(self) -> None:
        """
        Tests for correct applications of molecular

        """
        mol = Molecule(np.array([np.zeros(3), np.ones(3)]), ["H", "H"])

        # Rotate by pi about the xy axis
        mol.rotate(np.array([1, 1, 0]), np.pi)
        self.assertTrue(
            check_list_almost_equal(mol.coordinates, [[0, 0, 1], [1, 1, 0]]))

        # Rotate back to original positions
        mol.rotate(np.array([1, 1, 0]), np.pi)
        self.assertTrue(
            check_list_almost_equal(mol.coordinates, [[0, 0, 0], [1, 1, 1]]))

        # Rotate about x
        mol.rotate(np.array([1, 0, 0]), np.pi)
        self.assertTrue(
            check_list_almost_equal(mol.coordinates, [[0, 1, 1], [1, 0, 0]]))

        # Rotate about xz
        mol.rotate(np.array([1, 0, 1]), np.pi)
        self.assertTrue(
            check_list_almost_equal(mol.coordinates, [[1, 0, 0], [0, 1, 1]]))
Beispiel #4
0
    def test_SGD_runs(self):

        writer = XYZWriter()
        pot = NewLJ(5)
        c1 = Cluster(molecules=[
            Molecule(coordinates=np.array(
                [[0.566474720473, 0.05189774298450, 0.03347914367068]]),
                     particle_names=["Cl"]),
            Molecule(coordinates=np.array(
                [[-0.010999390189086, 1.01397142828, -1.00418537828]]),
                     particle_names=["Cl"]),
            Molecule(coordinates=np.array(
                [[-0.555475330284, 0.0341308287337, 0.0623354819510]]),
                     particle_names=["Cl"]),
            Molecule(coordinates=np.array(
                [[-0.39523644062, 2.8659668824697, 0.3990951103299]]),
                     particle_names=["Cl"]),
            Molecule(coordinates=np.array(
                [[-0.39523644062, 0.8659668824697, 0.3990951103299]]),
                     particle_names=["Cl"])
        ], )
        sgd = SGD(potential=pot)

        c_ret = sgd(c1)
        writer.write(c_ret, "test_data/out.xyz")
        self.assertIsInstance(c_ret, Cluster)
        self.assertAlmostEqual(pot.get_energy(c_ret), -9.10385, places=3)
Beispiel #5
0
    def test_DefineSystem_unequal_molecules_and_numbers(self) -> None:
        mol1 = Molecule(coordinates=np.array([[1., 1., 1.], [0., 0., 0.]]),
                        particle_names=["D", "H"])
        mol2 = Molecule(coordinates=np.array([[1., 1., 1.], [0., 0., 0.]]),
                        particle_names=["He", "He"])

        with self.assertRaises(AssertionError):
            DefineSystem([2], molecules=[mol1, mol2])
Beispiel #6
0
    def test_minimise_dimer(self) -> None:
        c1 = Cluster(cost=0.0, molecules=[Molecule(coordinates=np.array([[0.0, 0.0, 0.0]]),
                                                   particle_names=["LJ"]),
                                          Molecule(coordinates=np.array([[2**(1./6.0), 0.0, 0.0]]),
                                                   particle_names=["LJ"])])

        min1 = self.LJ_6_12.minimize(c1)

        self.assertTrue(min1['success'])
        self.assertAlmostEqual((2**(1./6.)), magnitude(min1['coordinates'][0], min1['coordinates'][1]))
Beispiel #7
0
    def test_DefineSystem_init_with_molecules(self) -> None:

        mol1 = Molecule(coordinates=np.zeros(shape=(2, 3)),
                        particle_names=["D", "H"])
        mol2 = Molecule(coordinates=np.ones(shape=(2, 3)),
                        particle_names=["He", "He"])

        system = DefineSystem([1, 1], molecules=[mol1, mol2])
        self.assertListEqual(list(system.initial_molecules[0].coordinates[0]),
                             [0, 0, 0])
        self.assertEqual(system.initial_molecules[1].name, "HeHe")
Beispiel #8
0
    def test_minimize_3LJ(self) -> None:

        c1 = Cluster(cost=0.0, molecules=[Molecule(coordinates=np.array([[0.0, 0.0, 0.0]]),
                                                   particle_names=["LJ"]),
                                          Molecule(coordinates=np.array([[1.0, 0.0, 0.0]]),
                                                   particle_names=["LJ"]),
                                          Molecule(coordinates=np.array([[0.5, 0.5, 0.0]]),
                                                   particle_names=["LJ"])])

        min2 = self.LJ_6_12.minimize(c1)

        self.assertAlmostEqual(-3, min2['energy'])
Beispiel #9
0
    def test_DefineSystem_get_random_cluster(self) -> None:
        mol1 = Molecule(coordinates=np.array([[1., 1., 1.], [0., 0., 0.]]),
                        particle_names=["D", "H"])
        mol2 = Molecule(coordinates=np.array([[1., 1., 1.], [0., 0., 0.]]),
                        particle_names=["He", "He"])

        system = DefineSystem([1, 2], molecules=[mol1, mol2])
        self.assertListEqual(list(system.initial_molecules[1].coordinates[1]),
                             [0, 0, 0])
        self.assertEqual(system.initial_molecules[1].name, "HeHe")

        self.assertIsInstance(system.get_random_cluster(), Cluster)
Beispiel #10
0
    def test_RST_mutate_good(self) -> None:

        c1 = Cluster(cost=9.0,
                     molecules=[Molecule(coordinates=np.random.uniform(low=10, size=(2, 3)),
                                         particle_names=["B", "Be"]),
                                Molecule(coordinates=np.random.uniform(low=10, size=(3, 3)),
                                         particle_names=["Be", "B", "Be"])])

        mutation = RandomSingleTranslation()
        new_cluster = mutation.mutate(copy.deepcopy(c1))

        self.assertIsInstance(new_cluster, Cluster)
        self.assertFalse(np.allclose(new_cluster.get_molecular_positions()[0], c1.get_molecular_positions()[0]))
Beispiel #11
0
 def setUpClass(cls) -> None:
     """Sets up some test initial_molecules"""
     cls.mol1 = Molecule(coordinates=np.array([[0.0, 0.0, 0.0],
                                               [1.0, 1.0, 1.0]]),
                         particle_names=["C", "H"])
     cls.mol2 = Molecule(coordinates=np.array([[-1.0, -1.0, -1.0],
                                               [1.0, 1.0, 1.0]]),
                         particle_names=["H", "H"])
     cls.mol3 = Molecule(
         coordinates=np.array([[-1.0, -1.0, -1.0], [1.0, 1.0, 1.0]]))
     cls.mol4 = Molecule(coordinates=np.array([[0.0, 0.0, 0.0],
                                               [1.0, 1.0, 1.0]]),
                         particle_names=["C", "H"])
     cls.mol5 = Molecule(coordinates=np.array([[0.0, 0.0, 0.0],
                                               [1.0, -1.0, 1.0]]),
                         particle_names=["H", "H"])
     cls.mol6 = Molecule(coordinates=np.array([[0.0, 0.0, 0.0],
                                               [1.0, -1.0, 1.0],
                                               [-1.0, 1.0, -1.0]]),
                         particle_names=["H", "H", "H"])
     cls.mol7 = Molecule(coordinates=np.array([[0.0, 0.0, 0.0],
                                               [1.0, -1.0, 1.0],
                                               [-1.0, 1.0, -1.0]]),
                         masses=[1.6, 1.3, 1.2])
     cls.mol8 = Molecule(coordinates=np.array([[0.0, 0.0, 0.0],
                                               [-1.0, 1.0, -1.0]]),
                         particle_names=["H", "C"])
Beispiel #12
0
    def test_lj_energy_r_min(self) -> None:
        # Then check we get v~-1 @ r~r_{min} (r_min = 2**(1./exp_attractive))
        c1 = Cluster(cost=0.0, molecules=[Molecule(coordinates=np.array([[0.0, 0.0, 0.0]]),
                                                   particle_names=["LJ"]),
                                          Molecule(coordinates=np.array([[2**(1./6.0), 0.0, 0.0]]),
                                                   particle_names=["LJ"])])

        c2 = Cluster(cost=0.0, molecules=[Molecule(coordinates=np.array([[0.0, 0.0, 0.0]]),
                                                   particle_names=["LJ"]),
                                          Molecule(coordinates=np.array([[2 ** (1. / 20.), 0.0, 0.0]]),
                                                   particle_names=["LJ"])])

        self.assertAlmostEqual(-1.0, self.LJ_6_12.get_energy(c1))
        self.assertAlmostEqual(-1.0, self.LJ_15_30.get_energy(c2))
Beispiel #13
0
    def test_get_energy_GLJ_zero(self):
        pot = OPLS_potential(q=[0, 0], eps=[1, 1], sigma=[1, 1])

        # GLJ energy at r_ij=1.0 == 0.0 eV
        clus1 = Cluster(cost=0.0, molecules=[Molecule(coordinates=np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 1.0]]),
                                                      particle_names=["LJ", "LJ"])])

        self.assertEqual(pot.get_energy(clus1), 0.0)

        # At large r, en ~~ 0.0
        clus2 = Cluster(cost=0.0, molecules=[Molecule(coordinates=np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 1e99]]),
                                                      particle_names=["LJ", "LJ"])])

        self.assertEqual(pot.get_energy(clus2), 0)
Beispiel #14
0
    def test_ljc_get_energy_0(self) -> None:
        fast_potential = LJcPotential(2)

        c1 = Cluster(cost=0.0,
                     molecules=[
                         Molecule(coordinates=np.array([[0.0, 0.0, 0.0]]),
                                  particle_names=["LJ"]),
                         Molecule(coordinates=np.array([[1.0, 0.0, 0.0]]),
                                  particle_names=["LJ"])
                     ])

        # First we check that we get U=0 at r=1 and both implementations give the same result
        e_fast_0 = fast_potential.get_energy(c1)
        self.assertEqual(0.0, e_fast_0)
        self.assertEqual(self.ref_potential.get_energy(c1, ), e_fast_0)
Beispiel #15
0
    def test_ljc_get_energy_minimum(self) -> None:
        fast_potential = LJcPotential(2)

        c1 = Cluster(cost=0.0,
                     molecules=[
                         Molecule(coordinates=np.array([[0.0, 0.0, 0.0]]),
                                  particle_names=["LJ"]),
                         Molecule(coordinates=np.array(
                             [[2**(1. / 6.0), 0.0, 0.0]]),
                                  particle_names=["LJ"])
                     ])

        ref_energy = self.ref_potential.get_energy(c1)
        test_energy = fast_potential.get_energy(c1)
        self.assertEqual(ref_energy, test_energy)
Beispiel #16
0
    def test_shake_mutate_good(self) -> None:
        c1 = Cluster(cost=9.0,
                     molecules=[Molecule(coordinates=np.random.uniform(low=10, size=(2, 3)),
                                         particle_names=["B", "Be"]),
                                Molecule(coordinates=np.random.uniform(low=10, size=(3, 3)),
                                         particle_names=["Be", "B", "Be"])])

        mutation = Shake()

        mutated_c1 = mutation.mutate(copy.deepcopy(c1))

        diff = c1.get_particle_positions()[0] - mutated_c1.get_particle_positions()[0]

        self.assertEqual(magnitude(diff[0]), 0.42059300827254525)
        self.assertEqual(magnitude(diff[-1]), 0.4186786088973787)
Beispiel #17
0
    def test_NWChem_potential_minimize(self) -> None:
        c1: Cluster = Cluster(cost=0.0, molecules=[Molecule(coordinates=np.array([[-1.1, 1.1, 0.2]]),
                                                            particle_names=["He"]),
                                                   Molecule(coordinates=np.array([[1.0, 0.0, 0.1]]),
                                                            particle_names=["He"]),
                                                   Molecule(coordinates=np.array([[0.1, -2.0, 0.0]]),
                                                            particle_names=["He"])])

        if shutil.which("nwchem") is None:
            self.skipTest("No NWChem binary found on system")
        else:
            returned_cluster = self.nwchem_potential.minimize(c1)

            returned_coords = returned_cluster.get_particle_positions()
            self.assertListEqual(returned_coords[2], ["He", "He", "He"])
Beispiel #18
0
    def setUpClass(cls) -> None:
        """Sets up an instance of random cluster and a logger"""
        cls.log = logging.getLogger(__name__)
        cls.mutate = RandomCluster(log=cls.log)  # , box_length=10.0)

        cls.c1 = Cluster(cost=0.0,
                         molecules=[Molecule(coordinates=np.array([[1., 1, 1], [0, 0, 0]]),
                                             particle_names=["He", "H"]),
                                    Molecule(coordinates=np.array([[1., -1, -1], [0, 0, 0], [-1, -1, -1]]),
                                             particle_names=["H", "H", "He"])])

        cls.c2 = Cluster(cost=9.0,
                         molecules=[Molecule(coordinates=np.ones(shape=(2, 3)),
                                             particle_names=["B", "Be"]),
                                    Molecule(coordinates=np.zeros(shape=(3, 3)),
                                             particle_names=["Be", "B", "Be"])])
Beispiel #19
0
    def test_setup(self):

        nMOL = 13

        compare = SimpleEnergeticCharacterizer(accuracy=5e-7)

        db_name = f"thresh_lj{nMOL}.db"
        self.files.append(db_name)

        database = Database(db=db_name,
                            new_database=True,
                            compare_clusters=compare)

        system = DefineSystem(molecules=[Molecule(coordinates=np.array([[0.0, 0.0, 0.0]]), particle_names=["LJ"])],
                              numbers_of_molecules=[nMOL], box_length=15)

        # define a mutation scheme
        # probs is normalised inside Mutate so we can just pass realatve probabilites
        mutation = Mutate(mutations=[RandomSingleTranslation(), RandomMultipleTranslations()],
                          relative_probabilities=[1, 1])

        daemon = Pyro4.Daemon()

        ga_args = dict(database=database, min_pool_size=10, max_generations=1000, system=system, convergence_steps=500,
                       mutate=mutation, daemon=daemon, mutation_rate=0.3, max_queue_size=10,)

        thresh_GA = PoolGA(threshold=-41.326801, initial_pool=database, **ga_args)
        thresh_GA.start_threads()
        time.sleep(0.15)
        self.assertFalse(thresh_GA.jobs.empty())
        daemon.shutdown()
Beispiel #20
0
    def test_ljc_minimizer(self) -> None:
        """Test that the minimizer returns the expected result"""
        fast_potential = LJcPotential(2)

        c1 = Cluster(cost=0.0,
                     molecules=[
                         Molecule(coordinates=np.array([[0.0, 0.0, 0.0]]),
                                  particle_names=["LJ"]),
                         Molecule(coordinates=np.array([[1.0, 0.0, 0.0]]),
                                  particle_names=["LJ"])
                     ])

        min_ljc_small = fast_potential.minimize(cluster=c1)
        pos = min_ljc_small.get_particle_positions()
        coords = pos[0]
        min_dist_2_part = np.linalg.norm(coords[0] - coords[1])
        self.assertAlmostEqual((2**(1. / 6.)), min_dist_2_part)
Beispiel #21
0
    def test_get_energy_coulombic_unlike_charges(self):

        pot = OPLS_potential(q=[1, -1], eps=[0, 0], sigma=[1, 1])

        clus = Cluster(cost=0.0, molecules=[Molecule(coordinates=np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 0.1]]),
                                                     particle_names=["LJ", "LJ"])])

        self.assertLess(pot.get_energy(clus), 0)
Beispiel #22
0
    def test_get_energy_GLJ_minimum(self):
        # GLJ energy at r_ij~1.112 ~~ -1.0 eV

        pot = OPLS_potential(q=[0, 0], eps=[1, 1], sigma=[1, 1])

        clus = Cluster(cost=0.0, molecules=[Molecule(coordinates=np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 2**(1/6)]]),
                                                     particle_names=["LJ", "LJ"])])
        self.assertEqual(pot.get_energy(clus), -1)
Beispiel #23
0
    def test_ljc_get_jacobian_1(self) -> None:
        fast_potential = LJcPotential(2)

        c1 = Cluster(cost=0.0,
                     molecules=[
                         Molecule(coordinates=np.array([[0.0, 0.0, 0.0]]),
                                  particle_names=["LJ"]),
                         Molecule(coordinates=np.array(
                             [[2**(1 / 6.), 0.0, 0.0]]),
                                  particle_names=["LJ"])
                     ])

        test_jac = fast_potential.get_jacobian(
            c1.get_particle_positions()[0].flatten())

        # print(ref_jac, test_jac)
        self.assertEqual(
            np.array([-12.0, 0.0, 0.0, 12.0, 0.0, 0.0]).all(), test_jac.all())
Beispiel #24
0
    def test_simple_case(self):
        mol1 = Molecule(coordinates=np.array([[1.0, 0.0, 0.0], [0.0, 1., 0.0],
                                              [0.0, 0.0,
                                               1.0], [-1.0, 0.0, 0.0],
                                              [0.0, -1.0, 0.0],
                                              [0.0, 0.0, -1.0]]),
                        masses=[1., 1., 1., 1., 1., 1.])

        print(gyration_tensor(mol1.coordinates, mol1.masses))
Beispiel #25
0
    def test_get_energy_GLJ_close_contact(self):
        # At small r, en >> 0.0

        pot = OPLS_potential(q=[0, 0], eps=[1, 1], sigma=[1, 1])

        clus = Cluster(cost=0.0, molecules=[Molecule(coordinates=np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 0.1]]),
                                                     particle_names=["LJ", "LJ"])])

        self.assertGreater(pot.get_energy(clus), 1e3)
Beispiel #26
0
    def test_minimize_LJ38(self) -> None:

        lj38_coordinates = np.loadtxt(bmpga.__path__[0]+"/tests/test_data/LJ38.xyz")

        c1 = Cluster(cost=0.0, molecules=[Molecule(coordinates=np.array([coord]),
                                                   particle_names=["LJ"]) for coord in lj38_coordinates])

        min_lj38 = self.LJ_6_12.minimize(c1)

        self.assertAlmostEqual(-173.928427, min_lj38['energy'], places=6)
Beispiel #27
0
    def test_MC_step(self):
        c1 = Cluster(molecules=[
            Molecule(coordinates=np.array([[0.0, 0.0, 0.0]]),
                     particle_names=["LJ"]),
            Molecule(coordinates=np.array([[1.0, 1.0, 1.0]]),
                     particle_names=["LJ"]),
            Molecule(coordinates=np.array([[1.0, -1.0, 1.0]]),
                     particle_names=["LJ"]),
            Molecule(coordinates=np.array([[-1.0, 1.0, 1.0]]),
                     particle_names=["LJ"]),
            Molecule(coordinates=np.array([[-1.0, -1.0, 1.0]]),
                     particle_names=["LJ"])
        ],
                     cost=0.0)

        pot = LJcPotential(5)
        print(pot.get_energy(c1))
        mc = MonteCarlo(potential=pot, temperature=10.0)
        mc.run(c1, 5)
        print(pot.get_energy(c1))
Beispiel #28
0
    def test_minimize_full(self):
        pot = OPLS_potential(q=[0.5, -0.5], eps=[1., 1.], sigma=[1., 1.])

        clus = Cluster(cost=0.0,
                       molecules=[Molecule(coordinates=np.array([[0.1, 0.1, 0.], [0.2, 0.2, 1.122]]),
                                           particle_names=["LJ", "LJ"])])

        res = pot.minimize(clus)
        print(res)
        self.assertTrue(res['success'])
        self.assertLess(res["energy"], -1.0)
Beispiel #29
0
    def test_DH_crossover_same_labels(self) -> None:

        c1 = Cluster(cost=-3.5,
                     molecules=[Molecule(np.array([[0., 0, 0], [0, 4, 0]]), ["C", "H"]),
                                Molecule(np.array([[0., 0, 1], [0, 3, 0]]), ["C", "H"]),
                                Molecule(np.array([[0., 0, 2], [0, 2, 0]]), ["C", "H"]),
                                Molecule(np.array([[0., 0, 3], [0, 1, 0]]), ["C", "H"]),
                                Molecule(np.array([[0., 0, 4], [0, 0, 0]]), ["C", "H"])])

        c2 = Cluster(cost=-3.5,
                     molecules=[Molecule(np.array([[4., 0, 0], [0, 0, 4]]), ["H", "C"]),
                                Molecule(np.array([[3., 0, 1], [1, 0, 3]]), ["H", "C"]),
                                Molecule(np.array([[2., 0, 2], [2, 0, 1]]), ["H", "C"]),
                                Molecule(np.array([[1., 0, 3], [3, 0, 1]]), ["H", "C"]),
                                Molecule(np.array([[0., 2, 4], [4, 0, 0]]), ["H", "C"])])

        child = self.mate([c1, c2], n_crossover=1)

        self.log.debug(child.molecules)
        self.log.debug(child.get_molecular_positions())
        self.log.debug(child.get_particle_positions())
Beispiel #30
0
    def test_ljc_get_energy_LJ38(self) -> None:
        lj38 = Cluster(cost=0.0,
                       molecules=[
                           Molecule(coordinates=np.array([c]),
                                    particle_names=["LJ"])
                           for c in self.lj38_coordinates
                       ])

        fast_potential = LJcPotential(38)
        # Check that both potentials calculate the same correct energy for the LJ38 minimum
        e_fast_38 = fast_potential.get_energy(lj38)
        self.assertAlmostEqual(
            -173.928427, e_fast_38,
            places=6)  # 6dp accuracy as this is reported online
        self.assertEqual(self.ref_potential.get_energy(lj38), e_fast_38)