Пример #1
0
def parse_castep_file(file_name, ir_or_raman):
    """
    Read frequencies from a <>.castep file

    @param file_name - file path of the file to read
    @return the frequencies, infra red and raman intensities and weights of frequency blocks
    """

    file_data = {}

    # Get Regex strings from load_helper
    header_regex = re.compile(load_helper.CASTEP_HEADER_REGEX)
    data_regex = re.compile(load_helper.CASTEP_DATA_REGEX)
    bond_regex = re.compile(load_helper.CASTEP_BOND_REGEX)

    block_count = 0
    frequencies, ir_intensities, raman_intensities, weights, q_vectors, bonds = [], [], [], [], [], []
    data_lists = (frequencies, ir_intensities, raman_intensities)
    with open(file_name, 'rU') as f_handle:
        file_data.update(_parse_castep_file_header(f_handle))


        while True:
            line = f_handle.readline()
            # Check we've reached the end of file
            if not line:
                break

            # Check if we've found a block of frequencies
            header_match = header_regex.match(line)
            if header_match:
                block_count += 1
                weight, q_vector = load_helper._parse_block_header(header_match, block_count)
                weights.append(weight)
                q_vectors.append(q_vector)

                # Move file pointer forward to start of intensity data
                _find_castep_freq_block(f_handle, data_regex)

                # Parse block of frequencies
                for line_data in _parse_castep_freq_block(f_handle, file_data['num_branches'], ir_or_raman):
                    for data_list, item in zip(data_lists, line_data):
                        data_list.append(item)

            # Check if we've found a bond
            bond_match = bond_regex.match(line)
            if bond_match:
                bonds.append(_parse_castep_bond(bond_match))

    frequencies = np.asarray(frequencies)
    ir_intensities = np.asarray(ir_intensities)
    raman_intensities = np.asarray(raman_intensities)
    warray = np.repeat(weights, file_data['num_branches'])

    file_data.update({
        'frequencies': frequencies,
        'ir_intensities': ir_intensities,
        'raman_intensities': raman_intensities,
        'weights': warray,
        'q_vectors':q_vectors
        })

    if len(bonds) > 0:
        file_data['bonds'] = bonds

    return file_data
Пример #2
0
def parse_phonon_file(file_name, record_eigenvectors):
    """
    Read frequencies from a <>.phonon file

    @param file_name - file path of the file to read
    @return the frequencies, infra red and raman intensities and weights of frequency blocks
    """
    file_data = {}

    # Get regex strings from load_helper
    header_regex = re.compile(load_helper.PHONON_HEADER_REGEX)
    eigenvectors_regex = re.compile(load_helper.PHONON_EIGENVEC_REGEX)

    block_count = 0
    frequencies, ir_intensities, raman_intensities, weights, q_vectors, eigenvectors = [], [], [], [], [], []
    data_lists = (frequencies, ir_intensities, raman_intensities)
    with open(file_name, 'rU') as f_handle:
        file_data.update(_parse_phonon_file_header(f_handle))

        while True:
            line = f_handle.readline()
            # Check we've reached the end of file
            if not line:
                break

            # Check if we've found a block of frequencies
            header_match = header_regex.match(line)
            if header_match:
                block_count += 1

                weight, q_vector = load_helper._parse_block_header(
                    header_match, block_count)
                weights.append(weight)
                q_vectors.append(q_vector)

                # Parse block of frequencies
                for line_data in _parse_phonon_freq_block(
                        f_handle, file_data['num_branches']):
                    for data_list, item in zip(data_lists, line_data):
                        data_list.append(item)

            vector_match = eigenvectors_regex.match(line)
            if vector_match:
                if record_eigenvectors:
                    # Parse eigenvectors for partial dos
                    vectors = _parse_phonon_eigenvectors(
                        f_handle, file_data['num_ions'],
                        file_data['num_branches'])
                    eigenvectors.append(vectors)
                else:
                    # Skip over eigenvectors
                    for _ in range(file_data['num_ions'] *
                                   file_data['num_branches']):
                        line = f_handle.readline()
                        if not line:
                            raise IOError(
                                "Bad file format. Uexpectedly reached end of file."
                            )

    frequencies = np.asarray(frequencies)
    ir_intensities = np.asarray(ir_intensities)
    raman_intensities = np.asarray(raman_intensities)
    warray = np.repeat(weights, file_data['num_branches'])
    eigenvectors = np.asarray(eigenvectors)

    file_data.update({
        'frequencies': frequencies,
        'ir_intensities': ir_intensities,
        'raman_intensities': raman_intensities,
        'weights': warray,
        'q_vectors': q_vectors,
        'eigenvectors': eigenvectors
    })

    return file_data, element_isotope
Пример #3
0
def parse_castep_file(file_name, ir_or_raman):
    """
    Read frequencies from a <>.castep file

    @param file_name - file path of the file to read
    @return the frequencies, infra red and raman intensities and weights of frequency blocks
    """

    file_data = {}

    # Get Regex strings from load_helper
    header_regex = re.compile(load_helper.CASTEP_HEADER_REGEX)
    data_regex = re.compile(load_helper.CASTEP_DATA_REGEX)
    bond_regex = re.compile(load_helper.CASTEP_BOND_REGEX)

    block_count = 0
    frequencies, ir_intensities, raman_intensities, weights, q_vectors, bonds = [], [], [], [], [], []
    data_lists = (frequencies, ir_intensities, raman_intensities)
    with open(file_name, 'rU') as f_handle:
        file_data.update(_parse_castep_file_header(f_handle))

        while True:
            line = f_handle.readline()
            # Check we've reached the end of file
            if not line:
                break

            # Check if we've found a block of frequencies
            header_match = header_regex.match(line)
            if header_match:
                block_count += 1
                weight, q_vector = load_helper._parse_block_header(header_match, block_count)
                weights.append(weight)
                q_vectors.append(q_vector)

                # Move file pointer forward to start of intensity data
                _find_castep_freq_block(f_handle, data_regex)

                # Parse block of frequencies
                for line_data in _parse_castep_freq_block(f_handle, file_data['num_branches'], ir_or_raman):
                    for data_list, item in zip(data_lists, line_data):
                        data_list.append(item)

            # Check if we've found a bond
            bond_match = bond_regex.match(line)
            if bond_match:
                bonds.append(_parse_castep_bond(bond_match))

    frequencies = np.asarray(frequencies)
    ir_intensities = np.asarray(ir_intensities)
    raman_intensities = np.asarray(raman_intensities)
    warray = np.repeat(weights, file_data['num_branches'])

    file_data.update({
        'frequencies': frequencies,
        'ir_intensities': ir_intensities,
        'raman_intensities': raman_intensities,
        'weights': warray,
        'q_vectors':q_vectors
        })

    if len(bonds) > 0:
        file_data['bonds'] = bonds

    return file_data
Пример #4
0
def parse_phonon_file(file_name, record_eigenvectors):
    """
    Read frequencies from a <>.phonon file

    @param file_name - file path of the file to read
    @return the frequencies, infra red and raman intensities and weights of frequency blocks
    """
    file_data = {}

    # Get regex strings from load_helper
    header_regex = re.compile(load_helper.PHONON_HEADER_REGEX)
    eigenvectors_regex = re.compile(load_helper.PHONON_EIGENVEC_REGEX)

    block_count = 0
    frequencies, ir_intensities, raman_intensities, weights, q_vectors, eigenvectors = [], [], [], [], [], []
    data_lists = (frequencies, ir_intensities, raman_intensities)
    with open(file_name, 'rU') as f_handle:
        file_data.update(_parse_phonon_file_header(f_handle))

        while True:
            line = f_handle.readline()
            # Check we've reached the end of file
            if not line:
                break

            # Check if we've found a block of frequencies
            header_match = header_regex.match(line)
            if header_match:
                block_count += 1

                weight, q_vector = load_helper._parse_block_header(header_match, block_count)
                weights.append(weight)
                q_vectors.append(q_vector)

                # Parse block of frequencies
                for line_data in _parse_phonon_freq_block(f_handle,
                                                          file_data['num_branches']):
                    for data_list, item in zip(data_lists, line_data):
                        data_list.append(item)

            vector_match = eigenvectors_regex.match(line)
            if vector_match:
                if record_eigenvectors:
                    # Parse eigenvectors for partial dos
                    vectors = _parse_phonon_eigenvectors(f_handle,
                                                         file_data['num_ions'],
                                                         file_data['num_branches'])
                    eigenvectors.append(vectors)
                else:
                    # Skip over eigenvectors
                    for _ in range(file_data['num_ions'] * file_data['num_branches']):
                        line = f_handle.readline()
                        if not line:
                            raise IOError("Bad file format. Uexpectedly reached end of file.")

    frequencies = np.asarray(frequencies)
    ir_intensities = np.asarray(ir_intensities)
    raman_intensities = np.asarray(raman_intensities)
    warray = np.repeat(weights, file_data['num_branches'])
    eigenvectors = np.asarray(eigenvectors)

    file_data.update({
        'frequencies': frequencies,
        'ir_intensities': ir_intensities,
        'raman_intensities': raman_intensities,
        'weights': warray,
        'q_vectors':q_vectors,
        'eigenvectors': eigenvectors
        })

    return file_data, element_isotope