예제 #1
0
    def test_parse_structures_single_cluster(self) -> None:
        c1 = Cluster(cost=0.0,
                     molecules=[Molecule(coordinates=np.zeros(shape=(2, 3)), particle_names=["H", "He"]),
                                Molecule(coordinates=np.zeros(shape=(3, 3)), particle_names=["H", "H", "He"])])

        output = self.writer._parse_structures(c1)
        self.assertListEqual(output[0][0][0].tolist(), np.zeros(shape=(5, 3)).tolist())  # Check coords are correct
예제 #2
0
    def test_get_mass(self) -> None:
        mol1 = Molecule(np.array([[0, 0, 0], [1, 1, 1]]), ["H", "H"])
        mol2 = Molecule(np.array([[0, 0, 0], [1, 1, 1]]), ["C", "H"])

        self.assertEqual(2.02, get_mass(mol1))
        self.assertEqual(13.02, get_mass(mol2))

        mol2.masses = None
        self.assertEqual(13.02, get_mass(mol2))
예제 #3
0
    def test__get_coord_labels_1_cluster(self) -> None:
        c1 = Cluster(cost=0.0,
                     molecules=[Molecule(coordinates=np.zeros(shape=(2, 3)), particle_names=["H", "He"]),
                                Molecule(coordinates=np.zeros(shape=(3, 3)), particle_names=["H", "H", "He"])])

        cluster_list = [c1]
        coord_labels = self.writer._get_coord_labels(cluster_list)

        self.assertListEqual(list(coord_labels[0][0][0]), [0., 0., 0.])
        self.assertTrue("He" in coord_labels[0][1])
예제 #4
0
    def test_graph_to_fragments__good_graph(self):

        mol_list = [Molecule(coordinates=np.zeros(3, 3), particle_names=["H", "H", "H"]),
                    Molecule(coordinates=np.ones(3, 3)*12, particle_names=["O", "O", "O"]),
                    ]
        IDfrags = IdentifyFragments(molecule_list=mol_list)

        mol_G = nx.Graph()

        mol_G.add_nodes_from([])
        print(IDfrags.graph_to_fragments())
예제 #5
0
    def test_parse_structures_list_clusters(self) -> None:
        c1 = Cluster(cost=0.0,
                     molecules=[Molecule(coordinates=np.zeros(shape=(2, 3)), particle_names=["H", "He"]),
                                Molecule(coordinates=np.zeros(shape=(3, 3)), particle_names=["H", "H", "He"])])

        c2 = Cluster(cost=9.0,
                     molecules=[Molecule(coordinates=np.ones(shape=(2, 3)), particle_names=["B", "Be"]),
                                Molecule(coordinates=np.ones(shape=(3, 3)), particle_names=["Be", "B", "Be"])])
        cluster_list = [c1, c2]

        output = self.writer._parse_structures(cluster_list)
        self.assertListEqual(output[0][1][0].tolist(), np.zeros(shape=(5, 3)).tolist())  # Check coords are correct
        self.assertListEqual(output[1], [0, 9])
예제 #6
0
    def test_insert_clusters(self) -> None:
        c1 = Cluster(cost=-44.44, molecules=[Molecule(np.zeros(3))])
        c2 = Cluster(cost=-44.14, molecules=[Molecule(np.zeros(3))])

        clusters = [c1, c2]

        no_comparison_database = Database(new_database=True)

        self.log.debug(f"{no_comparison_database}, {type(no_comparison_database)}")

        self.assertIsInstance(no_comparison_database.fast_insert_clusters(clusters), type(None))

        self.assertEqual(no_comparison_database.minima[0].cost, -44.44)
        self.assertEqual(no_comparison_database.minima[1].cost, -44.14)
예제 #7
0
    def test_get_clusters_by_cost(self) -> None:
        self.database.add_new_cluster(cost=-100.5,
                                      molecules=[Molecule(np.zeros(3)),
                                                 Molecule(np.ones(3))])
        self.database.add_new_cluster(cost=-11.5,
                                      molecules=[Molecule(np.zeros(3)),
                                                 Molecule(np.ones(3))])
        self.database.add_new_cluster(cost=-12.5,
                                      molecules=[Molecule(np.zeros(3)),
                                                 Molecule(np.zeros(3))])
        self.database.add_new_cluster(cost=-13.1,
                                      molecules=[Molecule(np.zeros(3)),
                                                 Molecule(np.ones(3))])

        clusters1 = self.database.get_clusters_by_cost(cost_max=-13.0)
        self.assertEqual(-100.5, clusters1[0].cost)
        self.assertEqual(-13.1, clusters1[-1].cost)

        clusters2 = self.database.get_clusters_by_cost(cost_min=-12.6, cost_max=-11.4)

        self.assertEqual(-12.5, clusters2[0].cost)
        self.assertEqual(-11.5, clusters2[1].cost)

        # Test that cost_min > cost_max raises an error
        with self.assertRaises(ValueError):
            self.database.get_clusters_by_cost(cost_max=-1.0, cost_min=2.0)
예제 #8
0
    def test_no_comparison(self) -> None:
        c1 = Cluster(cost=-44.94, molecules=[Molecule(np.zeros(3))])

        no_comparison_database = Database(new_database=True)

        self.assertIsInstance(no_comparison_database.add_new_cluster(cost=-44.44, molecules=[Molecule(np.zeros(3))]),
                              Cluster)

        self.assertIsInstance(no_comparison_database.insert_cluster(c1), Cluster)
예제 #9
0
    def test_get_clusters_by_id(self) -> None:
        c1 = self.database.add_new_cluster(cost=-3.5,
                                           molecules=[Molecule(np.zeros(3)),
                                                      Molecule(np.ones(3))])
        c2 = self.database.add_new_cluster(cost=-3.6,
                                           molecules=[Molecule(np.zeros(3)),
                                                      Molecule(np.ones(3))])
        _id = c1.id()

        _ids = [_id, c2.id()]

        ret1 = self.database.get_clusters_by_id(_id)

        self.assertIsInstance(ret1, Cluster)
        self.assertEqual(ret1.cost, -3.5)

        ret2 = self.database.get_clusters_by_id(_ids)
        self.assertIsInstance(ret2, list)
        self.assertEqual(ret2[1].cost, -3.6)
예제 #10
0
    def test_cluster_id_equality_and_hash(self) -> None:
        c1 = self.database.add_new_cluster(cost=-1.5,
                                           molecules=[Molecule(np.zeros(3)),
                                                      Molecule(np.ones(3))])
        c2 = self.database.add_new_cluster(cost=-1.6,
                                           molecules=[Molecule(np.zeros(3)),
                                                      Molecule(np.ones(3))])

        # check id
        self.assertIsInstance(c1.id(), int)

        # Check equality vs instances of Cluster and integers
        c1_id = c1.id()
        self.assertFalse(c1 == c2)
        self.assertTrue(c1 == c1)
        self.assertTrue(c1 == c1_id)
        self.assertFalse(c2 == c1_id)

        # Check hashing function
        self.assertIsInstance(c1.__hash__(), int)
예제 #11
0
    def test_global_minimum(self) -> None:

        self.database.add_new_cluster(cost=-100.5,
                                      molecules=[Molecule(np.zeros(3)),
                                                 Molecule(np.ones(3))])
        self.database.add_new_cluster(cost=-100.1,
                                      molecules=[Molecule(np.zeros(3)),
                                                 Molecule(np.ones(3))])

        # Add clusters very close to the GM to test that cluster comparison is being called
        self.database.add_new_cluster(cost=-100.1+1e-7,
                                      molecules=[Molecule(np.zeros(3)),
                                                 Molecule(np.ones(3))])
        self.database.add_new_cluster(cost=-100.1-1e-7,
                                      molecules=[Molecule(np.zeros(3)),
                                                 Molecule(np.ones(3))])
        self.database.add_new_cluster(cost=-100.1-7e-7,
                                      molecules=[Molecule(np.zeros(3)),
                                                 Molecule(np.ones(3))])

        global_minimum = self.database.get_global_minimum()
        self.assertEqual(global_minimum.cost, -100.5)
예제 #12
0
    def test_database_creation_and_loading(self) -> None:
        self.assertTrue(self.database.is_bmpga_database)
        self.assertTrue(os.path.isfile(self.db_location))

        cluster1 = self.database.add_new_cluster(cost=5.0,
                                                 molecules=[Molecule(np.zeros(3))])

        c1_id = cluster1.id()
        # check that we can load database from disk
        database2 = Database(db=self.db_location)
        self.assertTrue(database2.is_bmpga_database)

        answer = database2.get_clusters_by_id(c1_id)

        self.log.debug(answer)

        self.assertIsInstance(answer, Cluster)
        self.assertEqual(answer.cost, 5.0)
예제 #13
0
    def __init__(self, max_quenches: int = 10) -> None:

        self.max_quenches = max_quenches
        self.total_quenches = 0

        self.calls = 0
        self.cluster = Cluster(cost=0.0,
                               molecules=[
                                   Molecule(coordinates=np.array([[0., 0., 0.],
                                                                  [1., 1.,
                                                                   1.]]),
                                            particle_names=["LJ", "LJ"])
                               ])
        self.log = logging.getLogger(__name__)
        self.log.debug("Started DummyGA")
        self.all_jobs = [
            dumps(["minimize", copy.deepcopy(self.cluster)]),
            dumps(["energy", copy.deepcopy(self.cluster)]),
            dumps(["random", copy.deepcopy(self.cluster)])
        ] * 5
예제 #14
0
 def __init__(self, name):
     self.atoms, self.coordinates, self.charges, self.sigma, self.epsilon = self.read_json(name=name)
     self.molecule = Molecule(coordinates=copy.deepcopy(self.coordinates),
                              particle_names=copy.deepcopy(self.atoms))
예제 #15
0
 def setUpClass(cls) -> None:
     """Sets up some cluster objects for testing"""
     cls.cluster1 = Cluster(cost=-1.0, molecules=[Molecule(np.array([[-1., -1., -1.], [1., 1., 1.]]), ["H", "H"])])
     cls.cluster2 = Cluster(cost=-1.0, molecules=[Molecule(np.array([[-1., -1., -1.], [1., 1., 1.]]), ["H", "H"])])
     cls.cluster3 = Cluster(cost=-1.0, molecules=[Molecule(np.array([[-1., -1., -1.], [1., 1., 1.]]), ["H", "H"])])
     cls.log = logging.getLogger(__name__)
예제 #16
0
 def test_insert_cluster(self) -> None:
     test_cluster = Cluster(cost=-44.44, molecules=[Molecule(np.zeros(3))])
     self.database.insert_cluster(test_cluster)
예제 #17
0
 def test_add_cluster(self) -> None:
     out = self.database.add_new_cluster(cost=-10, molecules=[Molecule(np.zeros(3)), Molecule(np.ones(3))])
     self.assertIsInstance(out, Cluster)