def from_string(string):
        """
        Reads a Kpoints object from a KPOINTS string.

        Args:
            string (str): KPOINTS string.

        Returns:
            Kpoints object
        """
        lines = [line.strip() for line in string.splitlines()]

        comment = lines[0]
        num_kpts = int(lines[1].split()[0].strip())
        style = lines[2].lower()[0]

        # Fully automatic KPOINTS
        if style == "a":
            return Kpoints.automatic(int(lines[3]))

        coord_pattern = re.compile(r'^\s*([\d+.\-Ee]+)\s+([\d+.\-Ee]+)\s+'
                                   r'([\d+.\-Ee]+)')

        # Automatic gamma and Monk KPOINTS, with optional shift
        if style == "g" or style == "m":
            kpts = [int(i) for i in lines[3].split()]
            kpts_shift = (0, 0, 0)
            if len(lines) > 4 and coord_pattern.match(lines[4]):
                try:
                    kpts_shift = [float(i) for i in lines[4].split()]
                except ValueError:
                    pass
            return Kpoints.gamma_automatic(kpts, kpts_shift) if style == "g" \
                else Kpoints.monkhorst_automatic(kpts, kpts_shift)

        # Automatic kpoints with basis
        if num_kpts <= 0:
            style = Kpoints.supported_modes.Cartesian if style in "ck" \
                else Kpoints.supported_modes.Reciprocal
            kpts = [[float(j) for j in lines[i].split()] for i in range(3, 6)]
            kpts_shift = [float(i) for i in lines[6].split()]
            return Kpoints(comment=comment, num_kpts=num_kpts, style=style,
                           kpts=kpts, kpts_shift=kpts_shift)

        # Line-mode KPOINTS, usually used with band structures
        if style == "l":
            coord_type = "Cartesian" if lines[3].lower()[0] in "ck" \
                else "Reciprocal"
            style = Kpoints.supported_modes.Line_mode
            kpts = []
            labels = []
            patt = re.compile(r'([e0-9.\-]+)\s+([e0-9.\-]+)\s+([e0-9.\-]+)'
                              r'\s*!*\s*(.*)')
            for i in range(4, len(lines)):
                line = lines[i]
                m = patt.match(line)
                if m:
                    kpts.append([float(m.group(1)), float(m.group(2)),
                                 float(m.group(3))])
                    labels.append(m.group(4).strip())
            return Kpoints(comment=comment, num_kpts=num_kpts, style=style,
                           kpts=kpts, coord_type=coord_type, labels=labels)

        # Assume explicit KPOINTS if all else fails.
        style = Kpoints.supported_modes.Cartesian if style in "ck" \
            else Kpoints.supported_modes.Reciprocal
        kpts = []
        kpts_weights = []
        labels = []
        tet_number = 0
        tet_weight = 0
        tet_connections = None

        for i in range(3, 3 + num_kpts):
            toks = lines[i].split()
            kpts.append([float(j) for j in toks[0:3]])
            kpts_weights.append(float(toks[3]))
            if len(toks) > 4:
                labels.append(toks[4])
            else:
                labels.append(None)
        try:
            # Deal with tetrahedron method
            if lines[3 + num_kpts].strip().lower()[0] == "t":
                toks = lines[4 + num_kpts].split()
                tet_number = int(toks[0])
                tet_weight = float(toks[1])
                tet_connections = []
                for i in range(5 + num_kpts, 5 + num_kpts + tet_number):
                    toks = lines[i].split()
                    tet_connections.append((int(toks[0]),
                                            [int(toks[j])
                                             for j in range(1, 5)]))
        except IndexError:
            pass

        return Kpoints(comment=comment, num_kpts=num_kpts,
                       style=Kpoints.supported_modes[str(style)],
                       kpts=kpts, kpts_weights=kpts_weights,
                       tet_number=tet_number, tet_weight=tet_weight,
                       tet_connections=tet_connections, labels=labels)
def from_string ( string ) : lines = [ line . strip ( ) for line in string . splitlines ( ) ] comment = lines [ 0 ] num_kpts = int ( lines [ 1 ] . split ( ) [ 0 ] . strip ( ) ) style = lines [ 2 ] . lower ( ) [ 0 ] if style == "a" : return Kpoints . automatic ( int ( lines [ 3 ] ) ) coord_pattern = re . compile ( r'^\s*([\d+.\-Ee]+)\s+([\d+.\-Ee]+)\s+' r'([\d+.\-Ee]+)' ) if style == "g" or style == "m" : kpts = [ int ( i ) for i in lines [ 3 ] . split ( ) ] kpts_shift = ( 0 , 0 , 0 ) if len ( lines ) > 4 and coord_pattern . match ( lines [ 4 ] ) : try : kpts_shift = [ float ( i ) for i in lines [ 4 ] . split ( ) ] except ValueError : pass return Kpoints . gamma_automatic ( kpts , kpts_shift ) if style == "g" else Kpoints . monkhorst_automatic ( kpts , kpts_shift ) if num_kpts <= 0 : style = Kpoints . supported_modes . Cartesian if style in "ck" else Kpoints . supported_modes . Reciprocal kpts = [ [ float ( j ) for j in lines [ i ] . split ( ) ] for i in range ( 3 , 6 ) ] kpts_shift = [ float ( i ) for i in lines [ 6 ] . split ( ) ] return Kpoints ( comment = comment , num_kpts = num_kpts , style = style , kpts = kpts , kpts_shift = kpts_shift ) if style == "l" : coord_type = "Cartesian" if lines [ 3 ] . lower ( ) [ 0 ] in "ck" else "Reciprocal" style = Kpoints . supported_modes . Line_mode kpts = [ ] labels = [ ] patt = re . compile ( r'([e0-9.\-]+)\s+([e0-9.\-]+)\s+([e0-9.\-]+)' r'\s*!*\s*(.*)' ) for i in range ( 4 , len ( lines ) ) : line = lines [ i ] m = patt . match ( line ) if m : kpts . append ( [ float ( m . group ( 1 ) ) , float ( m . group ( 2 ) ) , float ( m . group ( 3 ) ) ] ) labels . append ( m . group ( 4 ) . strip ( ) ) return Kpoints ( comment = comment , num_kpts = num_kpts , style = style , kpts = kpts , coord_type = coord_type , labels = labels ) style = Kpoints . supported_modes . Cartesian if style in "ck" else Kpoints . supported_modes . Reciprocal kpts = [ ] kpts_weights = [ ] labels = [ ] tet_number = 0 tet_weight = 0 tet_connections = None for i in range ( 3 , 3 + num_kpts ) : toks = lines [ i ] . split ( ) kpts . append ( [ float ( j ) for j in toks [ 0 : 3 ] ] ) kpts_weights . append ( float ( toks [ 3 ] ) ) if len ( toks ) > 4 : labels . append ( toks [ 4 ] ) else : labels . append ( None ) try : if lines [ 3 + num_kpts ] . strip ( ) . lower ( ) [ 0 ] == "t" : toks = lines [ 4 + num_kpts ] . split ( ) tet_number = int ( toks [ 0 ] ) tet_weight = float ( toks [ 1 ] ) tet_connections = [ ] for i in range ( 5 + num_kpts , 5 + num_kpts + tet_number ) : toks = lines [ i ] . split ( ) tet_connections . append ( ( int ( toks [ 0 ] ) , [ int ( toks [ j ] ) for j in range ( 1 , 5 ) ] ) ) except IndexError : pass return Kpoints ( comment = comment , num_kpts = num_kpts , style = Kpoints . supported_modes [ str ( style ) ] , kpts = kpts , kpts_weights = kpts_weights , tet_number = tet_number , tet_weight = tet_weight , tet_connections = tet_connections , labels = labels )
Reads a Kpoints object from a KPOINTS string.

        Args:
            string (str): KPOINTS string.

        Returns:
            Kpoints object