def test(self):
        # Make two simple structures.
        structure1 = Cell()
        structure1.add_atom(Atom([0, 0, 0], 0))
        structure1.set_type_name(0, "Al")

        entries = []
        entry1 = CrystalStructureEntry(structure1, name="Al", radii=None)
        entries.append(entry1)

        structure2 = Cell()
        structure2.add_atom(Atom([0, 0, 0], 0))
        structure2.set_type_name(0, "Ni")
        structure2.add_atom(Atom([0, 0.5, 0], 1))
        structure2.set_type_name(1, "Al")
        structure2.add_atom(Atom([0, 0, 0.5], 1))
        entry2 = CrystalStructureEntry(structure2, name="NiAl2", radii=None)
        entries.append(entry2)

        # Create feature generator.
        gen = PRDFAttributeGenerator()
        gen.set_cut_off_distance(3.0)
        gen.set_n_points(5)
        gen.set_elements(entries)

        # Add extra element H.
        gen.add_element(name="H")

        # Generate features.
        features = gen.generate_features(entries)

        # Test results.
        self.assertEqual(3 * 3 * 5, features.shape[1])
        self.assertEqual(3 * 3 * 5, len(features.values[0]))
        self.assertAlmostEqual(0, sum(features.values[0][0 : 4 * 5]),
                                delta=1e-6) # First 4 PRDFs are H-X.
        self.assertTrue(max(features.values[0][4 * 5 : 5 * 5]) > 0)
        self.assertAlmostEqual(0, sum(features.values[0][6 * 5 : 9 * 5]),
                                delta=1e-6)  # Only Al in structure.

        self.assertEqual(3 * 3 * 5, len(features.values[1]))
        self.assertAlmostEqual(0, sum(features.values[1][0: 4 * 5]),
                                delta=1e-6)  # First 4 PRDFs are H-X.
        self.assertTrue(max(features.values[1][4 * 5: 5 * 5]) > 0)
        self.assertTrue(max(features.values[1][5 * 5: 6 * 5]) > 0)
        self.assertAlmostEqual(0, sum(features.values[1][6 * 5: 7 * 5]),
                                delta=1e-6)  # Only Al in structure.
        self.assertTrue(max(features.values[1][7 * 5: 8 * 5]) > 0)
        self.assertTrue(max(features.values[1][8 * 5: 9 * 5]) > 0)
Exemplo n.º 2
0
    def test_results(self):
        # Structure of a B2 crystal.
        structure = Cell()
        structure.add_atom(Atom([0, 0, 0], 0))
        structure.add_atom(Atom([0.5, 0.5, 0.5], 1))
        structure.set_type_name(0, "Al")
        structure.set_type_name(1, "Ni")

        entry = CrystalStructureEntry(structure, name="B2", radii=None)
        entries = [entry]

        # Create feature generator.
        gen = ChemicalOrderingAttributeGenerator()
        gen.set_weighted(False)
        gen.set_shells([1, 2])

        # Generate features.
        features = gen.generate_features(entries)

        # Test results.
        self.assertAlmostEqual(0.142857, features.values[0][0], delta=1e-6)
        self.assertAlmostEqual(0.04, features.values[0][1], delta=1e-6)

        # Now with weights.
        gen.set_weighted(True)
        features = gen.generate_features(entries)

        # Test results.
        self.assertAlmostEqual(0.551982, features.values[0][0], delta=1e-6)
        self.assertAlmostEqual(0.253856, features.values[0][1], delta=1e-6)
Exemplo n.º 3
0
def load_crystal_structures():
    """Load and return crystal structure entries.
    From Magpie https://bitbucket.org/wolverton/magpie

    =================   ======================
    length                                  18
    header                    formation_energy
    molecules rep.                 composition
    Features                                 0
    Returns                             1 list
    =================   ======================

    Returns
    -------
    list
        The list of crystal structure entries from CrystalStructureEntry class.

    Examples
    --------
    >>> from chemml.datasets import load_crystal_structures
    >>> entries = load_crystal_structures()
    >>> print(len(entries))
    18
    """
    DATA_PATH = pkg_resources.resource_filename(
        'chemml', os.path.join('datasets', 'data', 'magpie_python_test'))
    from chemml.chem.magpie_python import CrystalStructureEntry
    entries = CrystalStructureEntry.import_structures_list(DATA_PATH)
    return entries
    def test_results2(self):
        # Create a B1-HHe structure.
        structure = Cell()
        basis = np.zeros((3, 3))
        basis[0] = np.array([0, 0.5, 0.5])
        basis[1] = np.array([0.5, 0, 0.5])
        basis[2] = np.array([0.5, 0.5, 0])
        structure.set_basis(basis=basis)
        structure.add_atom(Atom([0, 0, 0], 0))
        structure.add_atom(Atom([0.5, 0.5, 0.5], 1))
        structure.set_type_name(0, "H")
        structure.set_type_name(1, "He")

        entries = [CrystalStructureEntry(structure, name="B1-HHe", radii=None)]

        # Get the feature generator.
        gen = self.get_generator()
        gen.clear_elemental_properties()
        gen.add_elemental_property("Number")

        # Generate features.
        features = gen.generate_features(entries)

        # Test the results.
        self.assertEqual(self.expected_count(), features.shape[1])
        np_tst.assert_array_almost_equal([1, 0, 1, 1, 0, 0, 0, 0, 0, 0],
                                         features.values[0])
    def test_results2(self):
        # Create a L12-H3He structure.
        # Structure of  L12.
        structure = Cell()
        structure.add_atom(Atom([0, 0, 0], 1))
        structure.add_atom(Atom([0.5, 0.5, 0], 0))
        structure.add_atom(Atom([0.5, 0, 0.5], 0))
        structure.add_atom(Atom([0, 0.5, 0.5], 0))
        structure.set_type_name(0, "H")
        structure.set_type_name(1, "He")

        entries = [
            CrystalStructureEntry(structure, name="L12-HHe", radii=None)
        ]

        # Get the feature generator.
        gen = self.get_generator()
        gen.clear_shells()
        gen.clear_elemental_properties()
        gen.add_shell(1)
        gen.add_elemental_property("Number")

        # Generate features.
        features = gen.generate_features(entries)

        # Test the results.
        self.assertEqual(5, features.shape[1])
        np_tst.assert_array_almost_equal(
            [0.166666667, 0.083333333, 0, 2.0 / 9, 2.0 / 9],
            features.values[0])
    def test(self):
        # Make two simple structures.
        structure1 = Cell()
        structure1.add_atom(Atom([0, 0, 0], 0))
        structure1.set_type_name(0, "Al")

        entries = []
        entry1 = CrystalStructureEntry(structure1, name="Al", radii=None)
        entries.append(entry1)

        structure2 = Cell()
        structure2.add_atom(Atom([0, 0, 0], 0))
        structure2.set_type_name(0, "Ni")
        structure2.add_atom(Atom([0, 0.5, 0], 1))
        structure2.set_type_name(1, "Al")
        structure2.add_atom(Atom([0, 0, 0.5], 1))
        entry2 = CrystalStructureEntry(structure2, name="NiAl2", radii=None)
        entries.append(entry2)

        # Create feature generator.
        gen = CoulombMatrixAttributeGenerator()
        gen.set_n_eigenvalues(10)

        # Generate features.
        features = gen.generate_features(entries)

        # Test results.
        self.assertEqual(10, features.shape[1])

        self.assertNotAlmostEqual(0, features.values[0][0], delta=1e-6)
        for i in range(1, 10):
            self.assertAlmostEqual(0, features.values[0][i], delta=1e-6)

        self.assertNotAlmostEqual(0, features.values[1][0], delta=1e-6)
        self.assertNotAlmostEqual(0, features.values[1][1], delta=1e-6)
        self.assertNotAlmostEqual(0, features.values[1][2], delta=1e-6)
        for i in range(3, 10):
            self.assertAlmostEqual(0, features.values[1][i], delta=1e-6)
Exemplo n.º 7
0
    def test(self):
        structure = Cell()
        structure.set_basis(lengths=[3.2, 3.2, 3.2], angles=[90, 90, 90])
        structure.add_atom(Atom([0, 0, 0], 0))
        structure.add_atom(Atom([0.5, 0.5, 0.5], 1))
        structure.set_type_name(0, "Ni")
        structure.set_type_name(1, "Al")

        entry = CrystalStructureEntry(structure, name="B2-NiAl", radii=None)
        entries = [entry]

        # Create feature generator.
        gen = APRDFAttributeGenerator()
        gen.set_cut_off_distance(3.2)
        gen.set_num_points(2)
        gen.set_smoothing_parameter(100)
        gen.add_elemental_property("Number")

        # Generate features.
        features = gen.generate_features(entries)
        self.assertEqual(2, len(features.columns))

        ap_rdf = features.values

        # Assemble known contributors.
        # [0] -> Number of neighbors * P_i * P_j
        # [1] -> Bond distance
        contributors = []
        contributors.append([2 * 8 * 13 * 28, 3.2 * math.sqrt(3) / 2])  # A-B
        #  1st NN.
        contributors.append([6 * 13 * 13, 3.2 * 1])  # A-A 2nd NN.
        contributors.append([6 * 28 * 28, 3.2 * 1])  # B-B 2nd NN.
        contributors.append([8 * 13 * 13, 3.2 * math.sqrt(3)])  # A-A 3rd NN.
        contributors.append([8 * 28 * 28, 3.2 * math.sqrt(3)])  # B-B 3rd NN.

        eval_dist = [1.6, 3.2]
        expected_ap_rdf = [
            sum([c[0] * math.exp(-100 * (c[1] - r)**2)
                 for c in contributors]) / 2 for r in eval_dist
        ]
        np_tst.assert_array_almost_equal(expected_ap_rdf, ap_rdf[0])
Exemplo n.º 8
0
    def test_results(self):
        # Create Dataset.
        dataset = []

        # Create primitive cell for B2-AlNi.
        structure1 = Cell()
        structure1.set_basis(lengths=[2.88, 2.88, 2.88], angles=[90, 90, 90])
        structure1.add_atom(Atom([0, 0, 0], 0))
        structure1.add_atom(Atom([0.5, 0.5, 0.5], 1))
        structure1.set_type_name(0, "Al")
        structure1.set_type_name(1, "Ni")
        entry1 = CrystalStructureEntry(structure1,
                                       name="Primitive",
                                       radii=None)
        dataset.append(entry1)

        # Create Scaled Cell.
        structure2 = Cell()
        structure2.set_basis(lengths=[3.0, 3.0, 3.0], angles=[90, 90, 90])
        structure2.add_atom(Atom([0, 0, 0], 0))
        structure2.add_atom(Atom([0.5, 0.5, 0.5], 1))
        structure2.set_type_name(0, "Al")
        structure2.set_type_name(1, "Ni")
        entry2 = CrystalStructureEntry(structure2, name="Scaled", radii=None)
        dataset.append(entry2)

        # Create a cell where A & B are swapped.
        structure3 = Cell()
        structure3.set_basis(lengths=[3.0, 3.0, 3.0], angles=[90, 90, 90])
        structure3.add_atom(Atom([0, 0, 0], 0))
        structure3.add_atom(Atom([0.5, 0.5, 0.5], 1))
        structure3.set_type_name(0, "Al")
        structure3.set_type_name(1, "Ni")
        entry3 = CrystalStructureEntry(structure3, name="Scaled", radii=None)
        dataset.append(entry3)

        # Create a 2x1x1 supercell.
        structure4 = Cell()
        structure4.set_basis(lengths=[6.0, 3.0, 3.0], angles=[90, 90, 90])
        structure4.add_atom(Atom([0, 0, 0], 0))
        structure4.add_atom(Atom([0.5, 0, 0], 0))
        structure4.add_atom(Atom([0.25, 0.5, 0.5], 1))
        structure4.add_atom(Atom([0.75, 0.5, 0.5], 1))
        structure4.set_type_name(0, "Ni")
        structure4.set_type_name(1, "Al")
        entry4 = CrystalStructureEntry(structure4,
                                       name="Primitive",
                                       radii=None)
        dataset.append(entry4)

        # Generate features.
        gen = self.get_generator()
        features = gen.generate_features(dataset)

        # Make sure the correct number were generated.
        ec = self.expected_count()
        self.assertEqual(ec, features.shape[1])
        for i in range(len(dataset)):
            self.assertEqual(ec, len(features.values[i]))

        # Make sure scaling doesn't effect it.
        for i in range(ec):
            self.assertAlmostEqual(features.values[0][i],
                                   features.values[1][i],
                                   delta=1e-6)

        # Make sure its permutationally-invariant.
        for i in range(ec):
            self.assertAlmostEqual(features.values[0][i],
                                   features.values[2][i],
                                   delta=1e-6)

        # Make sure it passes supercell.
        for i in range(ec):
            self.assertAlmostEqual(features.values[0][i],
                                   features.values[3][i],
                                   delta=1e-6)