Пример #1
0
    def test_kpointlist(self):
        """Test KpointList."""
        lattice = self.lattice

        frac_coords = [0, 0, 0, 1 / 2, 1 / 2, 1 / 2, 1 / 3, 1 / 3, 1 / 3]
        weights = [0.1, 0.2, 0.7]

        klist = KpointList(lattice, frac_coords, weights=weights)

        self.serialize_with_pickle(klist, protocols=[-1])
        self.assertMSONable(klist, test_if_subclass=False)

        assert klist.sum_weights() == 1
        assert len(klist) == 3

        for i, kpoint in enumerate(klist):
            assert kpoint in klist
            assert klist.count(kpoint) == 1
            assert klist.find(kpoint) == i

        # Changing the weight of the Kpoint object should change the weights of klist.
        for kpoint in klist:
            kpoint.set_weight(1.0)
        assert np.all(klist.weights == 1.0)

        # Test find_closest
        iclose, kclose, dist = klist.find_closest([0, 0, 0])
        assert iclose == 0 and dist == 0.

        iclose, kclose, dist = klist.find_closest(
            Kpoint([0.001, 0.002, 0.003], klist.reciprocal_lattice))
        assert iclose == 0
        self.assert_almost_equal(dist, 0.001984943324127921)

        frac_coords = [0, 0, 0, 1 / 2, 1 / 3, 1 / 3]
        other_klist = KpointList(lattice, frac_coords)

        # Test __add__
        add_klist = klist + other_klist

        for k in itertools.chain(klist, other_klist):
            assert k in add_klist

        assert add_klist.count([0, 0, 0]) == 2

        # Remove duplicated k-points.
        add_klist = add_klist.remove_duplicated()
        self.assertTrue(add_klist.count([0, 0, 0]) == 1)
        self.assertTrue(len(add_klist) == 4)
        self.assertTrue(add_klist == add_klist.remove_duplicated())
Пример #2
0
    def __init__(self, filepath):
        super(Fold2BlochNcfile, self).__init__(filepath)
        self.reader = ElectronsReader(filepath)

        # Initialize the electron bands from file.
        # Spectral weights are dimensioned with `nss`
        # Fortran arrays.
        # nctkarr_t("reduced_coordinates_of_unfolded_kpoints", "dp", "number_of_reduced_dimensions, nk_unfolded")
        # nctkarr_t("unfolded_eigenvalues", "dp", "max_number_of_states, nk_unfolded, number_of_spins")
        # nctkarr_t("spectral_weights", "dp", "max_number_of_states, nk_unfolded, nsppol_times_nspinor")
        self._ebands = self.reader.read_ebands()
        self.nss = max(self.nsppol, self.nspinor)
        self.fold_matrix = self.reader.read_value("fold_matrix")

        # Compute direct lattice of the primitive cell from fold_matrix.
        if is_diagonal(self.fold_matrix):
            folds = np.diagonal(self.fold_matrix)
            self.pc_lattice = Lattice(
                (self.structure.lattice.matrix.T * (1.0 / folds)).T.copy())
        else:
            raise NotImplementedError("non diagonal fold_matrix: %s" %
                                      str(self.fold_matrix))

        # Read fold2bloch output data.
        self.uf_kfrac_coords = self.reader.read_value(
            "reduced_coordinates_of_unfolded_kpoints")
        self.uf_kpoints = KpointList(self.pc_lattice.reciprocal_lattice,
                                     self.uf_kfrac_coords)
        self.uf_nkpt = len(self.uf_kpoints)
Пример #3
0
    def __init__(self, filepath):
        super(ScrReader, self).__init__(filepath)

        # Read and store important quantities.
        self.structure = self.read_structure()
        qred_coords = self.read_value("qpoints_dielectric_function")
        self.qpoints = KpointList(self.structure.reciprocal_lattice,
                                  qred_coords)
        self.wpts = self.read_value("frequencies_dielectric_function",
                                    cmode="c")
Пример #4
0
    def test_kpointlist(self):
        """Test KpointList."""
        lattice = self.lattice

        frac_coords = [0, 0, 0, 1/2, 1/2, 1/2, 1/3, 1/3, 1/3]
        weights = [0.1, 0.2, 0.7]

        klist = KpointList(lattice, frac_coords, weights=weights)

        self.assertTrue(klist.sum_weights() == 1)
        self.assertTrue(len(klist) == 3)

        for i, kpoint in enumerate(klist):
            self.assertTrue(kpoint in klist)
            self.assertTrue(klist.count(kpoint) == 1)
            self.assertTrue(klist.find(kpoint) == i)

        # Changing the weight of the Kpoint object should change the weights of klist.
        for kpoint in klist: kpoint.set_weight(1.0)
        self.assertTrue(np.all(klist.weights == 1.0))

        frac_coords = [0, 0, 0, 1/2, 1/3, 1/3]
                                                                  
        other_klist = KpointList(lattice, frac_coords)

        # Test __add__
        add_klist = klist + other_klist 

        for k in itertools.chain(klist, other_klist):
            self.assertTrue(k in add_klist)

        self.assertTrue(add_klist.count([0,0,0]) == 2)

        # Remove duplicated k-points.
        add_klist = add_klist.remove_duplicated()
        self.assertTrue(add_klist.count([0,0,0]) == 1)
        self.assertTrue(len(add_klist) == 4)
        self.assertTrue(add_klist == add_klist.remove_duplicated())
Пример #5
0
    def from_file(cls, filepath):
        """Create the object from a netCDF file."""
        with PHBST_Reader(filepath) as r:
            structure = r.read_structure()

            # Build the list of q-points
            qpoints = KpointList(structure.reciprocal_lattice,
                                 frac_coords=r.read_qredcoords(),
                                 weights=r.read_qweights(),
                                 names=None)

            return cls(structure=structure,
                       qpoints=qpoints,
                       phfreqs=r.read_phfreqs(),
                       phdispl_cart=r.read_phdispl_cart())
Пример #6
0
    def __init__(self, filepath):
        super(DdbFile, self).__init__(filepath)

        self._header = self._parse_header()

        self._structure = Structure.from_abivars(**self.header)
        # Add Spacegroup (needed in guessed_ngkpt)
        # FIXME: has_timerev is always True
        spgid, has_timerev, h = 0, True, self.header
        self._structure.set_spacegroup(SpaceGroup(spgid, h.symrel, h.tnons, h.symafm, has_timerev))

        frac_coords = self._read_qpoints()
        self._qpoints = KpointList(self.structure.reciprocal_lattice, frac_coords, weights=None, names=None)

        # Guess q-mesh
        self._guessed_ngqpt = self._guess_ngqpt()
Пример #7
0
    def __init__(self, filepath):
        super(ScrReader, self).__init__(filepath)

        # Read and store important quantities.
        self.structure = self.read_structure()
        qfrac_coords = self.read_value("qpoints_dielectric_function")
        self.kpoints = KpointList(self.structure.reciprocal_lattice,
                                  qfrac_coords)
        self.wpoints = self.read_value("frequencies_dielectric_function",
                                       cmode="c")
        self.ng = self.read_dimvalue(
            "number_of_coefficients_dielectric_function")

        # Find number of real/imaginary frequencies.
        self.nw = len(self.wpoints)
        self.nrew = self.nw
        self.nimw = 0
        for i, w in enumerate(self.wpoints):
            if np.iscomplex(w):
                self.nrew = i
                break

        self.nimw = self.nw - self.nrew
        if self.nimw and not np.all(np.iscomplex(
                self.wpoints[self.nrew + 1:])):
            raise ValueError(
                "wpoints should contained real points packed in the first positions\n"
                "followed by imaginary points but got: %s" % str(self.wpoints))

        # Define self.netcdf_name from the data available on file.
        nfound = 0
        netcdf_names = [
            "polarizability", "dielectric_function",
            "inverse_dielectric_function"
        ]
        for tryname in netcdf_names:
            if tryname in self.rootgrp.variables:
                self.netcdf_name = tryname
                nfound += 1

        if nfound == 0:
            raise RuntimeError("Cannot find `%s` in netcdf file" %
                               str(netcdf_names))
        if nfound > 1:
            raise RuntimeError(
                "Find multiple netcdf arrays (%s) in netcdf file!" %
                str(netcdf_names))
Пример #8
0
 def qpoints(self):
     """List of q-points (ndarray)."""
     # Read the fractional coordinates and convert them to KpointList.
     return KpointList(self.structure.reciprocal_lattice,
                       frac_coords=self.read_value("qpoints"))
Пример #9
0
 def qpoints(self):
     return KpointList(self.structure.reciprocal_lattice,
                       frac_coords=self.reader.read_value("qpts"))
Пример #10
0
    def test_kpointlist(self):
        """Test KpointList."""
        lattice = self.lattice

        frac_coords = [0, 0, 0, 1 / 2, 1 / 2, 1 / 2, 1 / 3, 1 / 3, 1 / 3]
        weights = [0.1, 0.2, 0.7]

        klist = KpointList(lattice, frac_coords, weights=weights)
        repr(klist)
        str(klist)

        self.serialize_with_pickle(klist, protocols=[-1])
        self.assertMSONable(klist, test_if_subclass=False)

        self.assert_equal(klist.frac_coords.flatten(), frac_coords)
        self.assert_equal(klist.get_cart_coords(),
                          np.reshape([k.cart_coords for k in klist], (-1, 3)))
        assert klist.sum_weights() == 1
        assert len(klist) == 3

        for i, kpoint in enumerate(klist):
            assert kpoint in klist
            assert klist.count(kpoint) == 1
            assert klist.find(kpoint) == i

        # Changing the weight of the Kpoint object should change the weights of klist.
        for kpoint in klist:
            kpoint.set_weight(1.0)
        assert np.all(klist.weights == 1.0)

        # Test find_closest
        iclose, kclose, dist = klist.find_closest([0, 0, 0])
        assert iclose == 0 and dist == 0.

        iclose, kclose, dist = klist.find_closest(
            Kpoint([0.001, 0.002, 0.003], klist.reciprocal_lattice))
        assert iclose == 0
        self.assert_almost_equal(dist, 0.001984943324127921)

        # Compute mapping k_index --> (k + q)_index, g0
        k2kqg = klist.get_k2kqg_map((0, 0, 0))
        assert all(ikq == ik for ik, (ikq, g0) in k2kqg.items())
        k2kqg = klist.get_k2kqg_map((1 / 2, 1 / 2, 1 / 2))
        assert len(k2kqg) == 2
        assert k2kqg[0][0] == 1 and np.all(k2kqg[0][1] == 0)
        assert k2kqg[1][0] == 0 and np.all(k2kqg[1][1] == 1)

        frac_coords = [0, 0, 0, 1 / 2, 1 / 3, 1 / 3]
        other_klist = KpointList(lattice, frac_coords)

        # Test __add__
        add_klist = klist + other_klist

        for k in itertools.chain(klist, other_klist):
            assert k in add_klist

        assert add_klist.count([0, 0, 0]) == 2

        # Remove duplicated k-points.
        add_klist = add_klist.remove_duplicated()
        assert add_klist.count([0, 0, 0]) == 1
        assert len(add_klist) == 4
        assert add_klist == add_klist.remove_duplicated()
Пример #11
0
    def __init__(self, path):
        self.ks_bands = ElectronBands.from_file(path)
        self.nsppol = self.ks_bands.nsppol

        super(SigresReader, self).__init__(path)

        try:
            self.nomega_r = self.read_dimvalue("nomega_r")
        except self.Error:
            self.nomega_r = 0

        #self.nomega_i = self.read_dim("nomega_i")

        # Save important quantities needed to simplify the API.
        self.structure = self.read_structure()

        self.gwcalctyp = self.read_value("gwcalctyp")
        self.usepawu = self.read_value("usepawu")

        # 1) The K-points of the homogeneous mesh.
        self.ibz = self.ks_bands.kpoints

        # 2) The K-points where QPState corrections have been calculated.
        gwred_coords = self.read_redc_gwkpoints()
        self.gwkpoints = KpointList(self.structure.reciprocal_lattice, gwred_coords)

        # minbnd[nkptgw,nsppol] gives the minimum band index computed
        # Note conversion between Fortran and python convention.
        self.gwbstart_sk = self.read_value("minbnd") - 1
        self.gwbstop_sk = self.read_value("maxbnd")

        # min and Max band index for GW corrections.
        self.min_gwbstart = np.min(self.gwbstart_sk)
        self.max_gwbstart = np.max(self.gwbstart_sk)

        self.min_gwbstop = np.min(self.gwbstop_sk)
        self.max_gwbstop = np.max(self.gwbstop_sk)

        self._egw = self.read_value("egw", cmode="c")

        # Read and save important matrix elements.
        # All these arrays are dimensioned
        # vxcme(b1gw:b2gw,nkibz,nsppol*nsig_ab))
        self._vxcme = self.read_value("vxcme")
        self._sigxme = self.read_value("sigxme")

        self._hhartree = self.read_value("hhartree", cmode="c")

        self._vUme = self.read_value("vUme")
        #if self.usepawu == 0: self._vUme.fill(0.0)

        # Complex arrays
        self._sigcmee0 = self.read_value("sigcmee0", cmode="c")
        self._ze0 = self.read_value("ze0", cmode="c")

        # Frequencies for the spectral function.
        if self.has_spfunc:
            self._omega_r = self.read_value("omega_r")

            self._sigcme = self.read_value("sigcme", cmode="c")
            self._sigxcme = self.read_value("sigxcme", cmode="c")

        # Self-consistent case
        self._en_qp_diago = self.read_value("en_qp_diago")

        # <KS|QPState>
        self._eigvec_qp = self.read_value("eigvec_qp", cmode="c")