<commit_before>#!/usr/bin/env python

EMC_VERSION='2.0b1'

diff_d2 = []
diff_d2.append([0.0, 0.0, 0.0]); # 0
diff_d2.append([-1.0, 0.0, 0.0]); diff_d2.append([1.0, 0.0, 0.0]);  # dx  1-2
diff_d2.append([0.0, -1.0, 0.0]); diff_d2.append([0.0, 1.0, 0.0])   # dy  3-4
diff_d2.append([0.0, 0.0, -1.0]); diff_d2.append([0.0, 0.0, 1.0])   # dz  5-6
diff_d2.append([-1.0, -1.0, 0.0]); diff_d2.append([1.0, 1.0, 0.0]); diff_d2.append([1.0, -1.0, 0.0]); diff_d2.append([-1.0, 1.0, 0.0]); # dxdy 7-10
diff_d2.append([-1.0, 0.0, -1.0]); diff_d2.append([1.0, 0.0, 1.0]); diff_d2.append([1.0, 0.0, -1.0]); diff_d2.append([-1.0, 0.0, 1.0]); # dxdz 11-14
diff_d2.append([0.0, -1.0, -1.0]); diff_d2.append([0.0, 1.0, 1.0]); diff_d2.append([0.0, 1.0, -1.0]); diff_d2.append([0.0, -1.0, 1.0]); # dydz 15-18

Bohr = 0.5291772

def MAT_m_VEC(m, v):
    p = [ 0.0 for i in range(len(v)) ]
    for i in range(len(m)):
        assert len(v) == len(m[i]), 'Length of the matrix row is not equal to the length of the vector'
        p[i] = sum( [ m[i][j]*v[j] for j in range(len(v)) ] )
    return p

def T(m):
    p = [[ m[i][j] for i in range(len( m[j] )) ] for j in range(len( m )) ]
    return p

def N(v):
    max_ = 0.
    for item in v:
        if abs(item) > abs(max_): max_ = item

    return [ item/max_ for item in v ]
def DET_3X3(m):
    assert len(m) == 3, 'Matrix should be of the size 3 by 3'
    return m[0][0]*m[1][1]*m[2][2] + m[1][0]*m[2][1]*m[0][2] + m[2][0]*m[0][1]*m[1][2] - \
           m[0][2]*m[1][1]*m[2][0] - m[2][1]*m[1][2]*m[0][0] - m[2][2]*m[0][1]*m[1][0]

def IS_SYMMETRIC(m):
    for i in range(len(m)):
        for j in range(len(m[i])):
            if m[i][j] != m[j][i]: return False # automatically checks square-shape

    return True

def EIGVALUES_SYMMETRIC_3X3(m):
    # follows http://en.wikipedia.org/wiki/Eigenvalue_algorithm#3.C3.973_matrices
    import math

    assert IS_SYMMETRIC(m), 'Supplied matrix is not symmetric'

    eigs = [ 0.0 for i in range(3) ]
    n = [[0.0 for i in range(3)] for j in range(3)]
    identity = [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]

    p = m[0][1]**2 + m[0][2]**2 + m[1][2]**2
    if p == 0.0:
        # m is diagonal.
        eigs = sorted( [ m[i][i] for i in range(3) ] ) # ascending order
    else:
        q = sum([ m[i][i] for i in range(3) ])/3.0
        p = (m[0][0] - q)**2 + (m[1][1] - q)**2 + (m[2][2] - q)**2 + 2.0*p
        p = math.sqrt(p/6.0)

        for i in range(3):
            for j in range(3):
                n[i][j] = (1.0/p) * (m[i][j] - q*identity[i][j])

        r = DET_3X3(n)/2.0

        if r <= -1.:
            phi = math.pi/3.0
        elif r >= 1.:
            phi = 0.0
        else:
            phi = math.acos(r)/3.0

        eigs[0] = q + 2.0*p*math.cos(phi)
        eigs[1] = q + 2.0*p*math.cos(phi + math.pi * (2.0/3.0))
        eigs[2] = 3.0*q - eigs[0] - eigs[1]

        eigs = sorted(eigs) # I'm aware of the list.sort() method

    return eigs

def cart2frac(basis, v):
    import numpy as np
    return np.dot(v, np.linalg.inv(basis)).tolist()

def fd_effmass(e, stepsize, order=2, debug=False):
    m = [[0.0 for i in range(3)] for j in range(3)]
    m[0][0] = (e[1] - 2.0*e[0] + e[2])/stepsize**2
    m[1][1] = (e[3] - 2.0*e[0] + e[4])/stepsize**2
    m[2][2] = (e[5] - 2.0*e[0] + e[6])/stepsize**2

    m[0][1] = (e[7] + e[8] - e[9] - e[10])/(4*stepsize**2)
    m[0][2] = (e[11] + e[12] - e[13] - e[14])/(4*stepsize**2)
    m[1][2] = (e[15] + e[16] - e[17] - e[18])/(4*stepsize**2)

    # symmetrize
    m[1][0] = m[0][1]
    m[2][0] = m[0][2]
    m[2][1] = m[1][2]

    if debug:
        print 'Assembling effective mass tensor...'
        print ''
        for i in range(len(m)):
            print '%7.5f %7.5f %7.5f' % (m[i][0], m[i][1], m[i][2])

    if debug: print ''
    return m

def generate_kpoints(kpt_frac, stepsize, prg, basis, debug=False):
    import numpy as np
    import sys

    # working in the reciprocal space
    basis_r = (np.linalg.inv( T(basis) )* 2*np.pi).tolist()

    k_c = MAT_m_VEC(T(basis_r), kpt_frac)
    if debug: print kpt_r_cart

    if prg == 'V':
        stepsize = stepsize*(1/Bohr) # [1/A]

    kpoints = []
    for i in range(len(diff_d2)):
        k_c_ = [ k_c[j] + diff_d2[i][j]*stepsize for j in range(3) ] # getting displaced k points in Cartesian coordinates
        k_f = cart2frac(basis_r, k_c_)
        kpoints.append( [k_f[0], k_f[1], k_f[2]] )

    return kpoints

def parse_EIGENVAL_VASP(eigenval_fh, band, diff2_size, debug=False):
    import sys

    ev2h = 1.0/27.21138505
    eigenval_fh.seek(0) # just in case
    eigenval_fh.readline()
    eigenval_fh.readline()
    eigenval_fh.readline()
    eigenval_fh.readline()<commit_msg>only 1 numpy dependence remained: linalg.eigh<commit_after>#!/usr/bin/env python

EMC_VERSION='2.0b1'

diff_d2 = []
diff_d2.append([0.0, 0.0, 0.0]); # 0
diff_d2.append([-1.0, 0.0, 0.0]); diff_d2.append([1.0, 0.0, 0.0]);  # dx  1-2
diff_d2.append([0.0, -1.0, 0.0]); diff_d2.append([0.0, 1.0, 0.0])   # dy  3-4
diff_d2.append([0.0, 0.0, -1.0]); diff_d2.append([0.0, 0.0, 1.0])   # dz  5-6
diff_d2.append([-1.0, -1.0, 0.0]); diff_d2.append([1.0, 1.0, 0.0]); diff_d2.append([1.0, -1.0, 0.0]); diff_d2.append([-1.0, 1.0, 0.0]); # dxdy 7-10
diff_d2.append([-1.0, 0.0, -1.0]); diff_d2.append([1.0, 0.0, 1.0]); diff_d2.append([1.0, 0.0, -1.0]); diff_d2.append([-1.0, 0.0, 1.0]); # dxdz 11-14
diff_d2.append([0.0, -1.0, -1.0]); diff_d2.append([0.0, 1.0, 1.0]); diff_d2.append([0.0, 1.0, -1.0]); diff_d2.append([0.0, -1.0, 1.0]); # dydz 15-18

Bohr = 0.5291772

def MAT_m_VEC(m, v):
    p = [ 0.0 for i in range(len(v)) ]
    for i in range(len(m)):
        assert len(v) == len(m[i]), 'Length of the matrix row is not equal to the length of the vector'
        p[i] = sum( [ m[i][j]*v[j] for j in range(len(v)) ] )
    return p

def T(m):
    p = [[ m[i][j] for i in range(len( m[j] )) ] for j in range(len( m )) ]
    return p

def N(v):
    max_ = 0.
    for item in v:
        if abs(item) > abs(max_): max_ = item

    return [ item/max_ for item in v ]

def DET_3X3(m):
    assert len(m) == 3, 'Matrix should be of the size 3 by 3'
    return m[0][0]*m[1][1]*m[2][2] + m[1][0]*m[2][1]*m[0][2] + m[2][0]*m[0][1]*m[1][2] - \
           m[0][2]*m[1][1]*m[2][0] - m[2][1]*m[1][2]*m[0][0] - m[2][2]*m[0][1]*m[1][0]

def SCALE_ADJOINT_3X3(m, s):
    a = [[0.0 for i in range(3)] for j in range(3)]

    a[0][0] = (s) * (m[1][1] * m[2][2] - m[1][2] * m[2][1])
    a[1][0] = (s) * (m[1][2] * m[2][0] - m[1][0] * m[2][2])
    a[2][0] = (s) * (m[1][0] * m[2][1] - m[1][1] * m[2][0])

    a[0][1] = (s) * (m[0][2] * m[2][1] - m[0][1] * m[2][2])
    a[1][1] = (s) * (m[0][0] * m[2][2] - m[0][2] * m[2][0])
    a[2][1] = (s) * (m[0][1] * m[2][0] - m[0][0] * m[2][1])

    a[0][2] = (s) * (m[0][1] * m[1][2] - m[0][2] * m[1][1])
    a[1][2] = (s) * (m[0][2] * m[1][0] - m[0][0] * m[1][2])
    a[2][2] = (s) * (m[0][0] * m[1][1] - m[0][1] * m[1][0])

    return a

def INVERT_3X3(m):
    tmp = 1.0/DET_3X3(m)
    return SCALE_ADJOINT_3X3(m, tmp)

def IS_SYMMETRIC(m):
    for i in range(len(m)):
        for j in range(len(m[i])):
            if m[i][j] != m[j][i]: return False # automatically checks square-shape

    return True

def EIGVALUES_SYMMETRIC_3X3(m):
    # follows http://en.wikipedia.org/wiki/Eigenvalue_algorithm#3.C3.973_matrices
    import math

    assert IS_SYMMETRIC(m), 'Supplied matrix is not symmetric'

    eigs = [ 0.0 for i in range(3) ]
    n = [[0.0 for i in range(3)] for j in range(3)]
    identity = [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]

    p = m[0][1]**2 + m[0][2]**2 + m[1][2]**2
    if p == 0.0:
        # m is diagonal.
        eigs = sorted( [ m[i][i] for i in range(3) ] ) # ascending order
    else:
        q = sum([ m[i][i] for i in range(3) ])/3.0
        p = (m[0][0] - q)**2 + (m[1][1] - q)**2 + (m[2][2] - q)**2 + 2.0*p
        p = math.sqrt(p/6.0)

        for i in range(3):
            for j in range(3):
                n[i][j] = (1.0/p) * (m[i][j] - q*identity[i][j])

        r = DET_3X3(n)/2.0

        if r <= -1.:
            phi = math.pi/3.0
        elif r >= 1.:
            phi = 0.0
        else:
            phi = math.acos(r)/3.0

        eigs[0] = q + 2.0*p*math.cos(phi)
        eigs[1] = q + 2.0*p*math.cos(phi + math.pi * (2.0/3.0))
        eigs[2] = 3.0*q - eigs[0] - eigs[1]

        eigs = sorted(eigs) # I'm aware of the list.sort() method

    return eigs

def cart2frac(basis, v):
    return MAT_m_VEC( T(INVERT_3X3(basis)), v )

def fd_effmass(e, stepsize, order=2, debug=False):
    m = [[0.0 for i in range(3)] for j in range(3)]
    m[0][0] = (e[1] - 2.0*e[0] + e[2])/stepsize**2
    m[1][1] = (e[3] - 2.0*e[0] + e[4])/stepsize**2
    m[2][2] = (e[5] - 2.0*e[0] + e[6])/stepsize**2

    m[0][1] = (e[7] + e[8] - e[9] - e[10])/(4*stepsize**2)
    m[0][2] = (e[11] + e[12] - e[13] - e[14])/(4*stepsize**2)
    m[1][2] = (e[15] + e[16] - e[17] - e[18])/(4*stepsize**2)

    # symmetrize
    m[1][0] = m[0][1]
    m[2][0] = m[0][2]
    m[2][1] = m[1][2]

    if debug:
        print 'Assembling effective mass tensor...'
        print ''
        for i in range(len(m)):
            print '%7.5f %7.5f %7.5f' % (m[i][0], m[i][1], m[i][2])

    if debug: print ''
    return m

def generate_kpoints(kpt_frac, stepsize, prg, basis, debug=False):
    from math import pi
    #
    # working in the reciprocal space
    m = INVERT_3X3(T(basis))
    basis_r = [[ m[i][j]*2.0*pi for j in range(3) ] for i in range(3) ]
    #
    k_c = MAT_m_VEC(T(basis_r), kpt_frac)
    if debug: print kpt_r_cart
    #
    if prg == 'V':
        stepsize = stepsize*(1/Bohr) # [1/A]
    #
    kpoints = []
    for i in range(len(diff_d2)):
        k_c_ = [ k_c[j] + diff_d2[i][j]*stepsize for j in range(3) ] # getting displaced k points in Cartesian coordinates
        k_f = cart2frac(basis_r, k_c_)
        kpoints.append( [k_f[0], k_f[1], k_f[2]] )
    #
    return kpoints

def parse_EIGENVAL_VASP(eigenval_fh, band, diff2_size, debug=False):
    ev2h = 1.0/27.21138505
    eigenval_fh.seek(0) # just in case
    eigenval_fh.readline()
    eigenval_fh.readline()
    eigenval_fh.readline()
    eigenval_fh.readline()
{'fraction_non_alphanumeric': 0.2386486752683936, 'fraction_numerical': 0.08943169506549788, 'mean_word_length': 2.4211590296495955, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6895845', 'n_tokens_mistral': 5150, 'n_tokens_neox': 4896, 'n_words': 1039}