Ejemplo n.º 1
0
    def from_dict(cls, d: Dict, fmt: str = None, **kwargs):
        """
        Create a Lattice from a dictionary containing the a, b, c, alpha, beta,
        and gamma parameters if fmt is None.

        If fmt == "abivars", the function build a `Lattice` object from a
        dictionary with the Abinit variables `acell` and `rprim` in Bohr.
        If acell is not given, the Abinit default is used i.e. [1,1,1] Bohr

        Example:

            Lattice.from_dict(fmt="abivars", acell=3*[10], rprim=np.eye(3))
        """
        if fmt == "abivars":
            from pymatgen.io.abinit.abiobjects import lattice_from_abivars

            kwargs.update(d)
            return lattice_from_abivars(cls=cls, **kwargs)

        if "matrix" in d:
            return cls(d["matrix"])
        else:
            return cls.from_parameters(
                d["a"], d["b"], d["c"], d["alpha"], d["beta"], d["gamma"]
            )
Ejemplo n.º 2
0
    def test_rprim_acell(self):
        l1 = lattice_from_abivars(acell=3 * [10], rprim=np.eye(3))
        self.assertAlmostEqual(l1.volume, bohr_to_ang ** 3 * 1000)
        assert l1.angles == (90, 90, 90)
        l2 = lattice_from_abivars(acell=3 * [10], angdeg=(90, 90, 90))
        assert l1 == l2

        l2 = lattice_from_abivars(acell=3 * [8], angdeg=(60, 60, 60))
        abi_rprimd = (
            np.reshape(
                [
                    4.6188022,
                    0.0000000,
                    6.5319726,
                    -2.3094011,
                    4.0000000,
                    6.5319726,
                    -2.3094011,
                    -4.0000000,
                    6.5319726,
                ],
                (3, 3),
            )
            * bohr_to_ang
        )
        self.assertArrayAlmostEqual(l2.matrix, abi_rprimd)

        l3 = lattice_from_abivars(acell=[3, 6, 9], angdeg=(30, 40, 50))
        abi_rprimd = (
            np.reshape(
                [
                    3.0000000,
                    0.0000000,
                    0.0000000,
                    3.8567257,
                    4.5962667,
                    0.0000000,
                    6.8944000,
                    4.3895544,
                    3.7681642,
                ],
                (3, 3),
            )
            * bohr_to_ang
        )
        self.assertArrayAlmostEqual(l3.matrix, abi_rprimd)

        with self.assertRaises(ValueError):
            lattice_from_abivars(acell=[1, 1, 1], angdeg=(90, 90, 90), rprim=np.eye(3))
        with self.assertRaises(ValueError):
            lattice_from_abivars(acell=[1, 1, 1], angdeg=(-90, 90, 90))
Ejemplo n.º 3
0
    def from_dict(cls, d, fmt=None, **kwargs):
        """
        Create a Lattice from a dictionary containing the a, b, c, alpha, beta,
        and gamma parameters if fmt is None.

        If fmt == "abivars", the function build a `Lattice` object from a
        dictionary with the Abinit variables `acell` and `rprim` in Bohr.
        If acell is not given, the Abinit default is used i.e. [1,1,1] Bohr

        Example:

            Lattice.from_dict(fmt="abivars", acell=3*[10], rprim=np.eye(3))
        """
        if fmt == "abivars":
            from pymatgen.io.abinit.abiobjects import lattice_from_abivars
            kwargs.update(d)
            return lattice_from_abivars(cls=cls, **kwargs)

        if "matrix" in d:
            return cls(d["matrix"])
        else:
            return cls.from_parameters(d["a"], d["b"], d["c"], d["alpha"],
                                       d["beta"], d["gamma"])