<commit_before>"""
Tight-binding class to create tight-binding models.
"""
from __future__ import print_function, division

from numbers import Integral

import numpy as np


from sids import Atom, Geometry, Quaternion
from .tightbinding import TightBinding


__all__ = ['PhononTightBinding']


class PhononTightBinding(TightBinding):
    """ Phonon tight-binding model with slight modifications """

    # The order of the Energy
    # I.e. whether energy should be in other units than Ry
    # This conversion is made: [eV] ** _E_order
    _E_order = 2

    def correct_Newton(self):
        """
        Sometimes the dynamical matrix does not obey Newtons laws.
        
        We correct the dynamical matrix by imposing zero force.
        
        Correcting for Newton forces the matrix to be finalized.
        """
        from scipy.sparse import lil_matrix

        # Create UC dynamical matrix
        d_sc, S_sc = self.tocsr()
        del S_sc
        d_sc = d_sc.tocoo()
        d_uc = lil_matrix([self.no, self.no], dtype=d_sc.dtype)

        # Convert SC to UC
        for j, i, d in zip(d_sc.row,d_sc.col,d_sc.data):
            d_uc[j, i % self.no] += d
        del d_sc
        d_uc = d_uc.tocsc()

        # we need to correct the dynamical matrix found in GULP
        # This ensures that Newtons laws are obeyed, (i.e. 
        # action == re-action)
        om = np.sqrt(np.array([a.mass for a in self.atoms],np.float64))
        MM = np.empty([len(om)],np.float64)

        for ja in self.geom:

            # Create conversion to force-constant, and revert back
            # after correcting
            MM[:] = om[:] / om[ja]
            jo = ja * 3

            # Unroll...
            D, S = self[jo,jo]
            self[jo,jo  ] = D - d_uc[jo  , ::3].multiply(MM).sum(), S
            D, S = self[jo,jo+1]
            self[jo,jo+1] = D - d_uc[jo  ,1::3].multiply(MM).sum(), S
            D, S = self[jo,jo+2]
            self[jo,jo+2] = D - d_uc[jo  ,2::3].multiply(MM).sum(), S

            D, S = self[jo+1,jo]
            self[jo+1,jo  ] = D - d_uc[jo+1, ::3].multiply(MM).sum(), S
            D, S = self[jo+1,jo+1]<commit_msg>bug: sparse matrix requires tuple, not list<commit_after>"""
Tight-binding class to create tight-binding models.
"""
from __future__ import print_function, division

from numbers import Integral

import numpy as np


from sids import Atom, Geometry, Quaternion
from .tightbinding import TightBinding


__all__ = ['PhononTightBinding']


class PhononTightBinding(TightBinding):
    """ Phonon tight-binding model with slight modifications """

    # The order of the Energy
    # I.e. whether energy should be in other units than Ry
    # This conversion is made: [eV] ** _E_order
    _E_order = 2

    def correct_Newton(self):
        """
        Sometimes the dynamical matrix does not obey Newtons laws.
        
        We correct the dynamical matrix by imposing zero force.
        
        Correcting for Newton forces the matrix to be finalized.
        """
        from scipy.sparse import lil_matrix

        # Create UC dynamical matrix
        d_sc, S_sc = self.tocsr()
        del S_sc
        d_sc = d_sc.tocoo()
        d_uc = lil_matrix( (self.no, self.no), dtype=d_sc.dtype)

        # Convert SC to UC
        for j, i, d in zip(d_sc.row,d_sc.col,d_sc.data):
            d_uc[j, i % self.no] += d
        del d_sc
        d_uc = d_uc.tocsc()

        # we need to correct the dynamical matrix found in GULP
        # This ensures that Newtons laws are obeyed, (i.e. 
        # action == re-action)
        om = np.sqrt(np.array([a.mass for a in self.atoms],np.float64))
        MM = np.empty([len(om)],np.float64)

        for ja in self.geom:

            # Create conversion to force-constant, and revert back
            # after correcting
            MM[:] = om[:] / om[ja]
            jo = ja * 3

            # Unroll...
            D, S = self[jo,jo]
            self[jo,jo  ] = D - d_uc[jo  , ::3].multiply(MM).sum(), S
            D, S = self[jo,jo+1]
            self[jo,jo+1] = D - d_uc[jo  ,1::3].multiply(MM).sum(), S
            D, S = self[jo,jo+2]
            self[jo,jo+2] = D - d_uc[jo  ,2::3].multiply(MM).sum(), S

            D, S = self[jo+1,jo]
            self[jo+1,jo  ] = D - d_uc[jo+1, ::3].multiply(MM).sum(), S
            D, S = self[jo+1,jo+1]
{'fraction_non_alphanumeric': 0.1180794854123593, 'fraction_numerical': 0.009648518263266712, 'mean_word_length': 2.0027586206896553, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4866368', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1432, 'n_words': 430}