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

"""
Abstract class for defects
"""
from __future__ import division

__author__ = "<NAME>"

import abc 

from pymatgen.core.sites import PeriodicSite
from pymatgen.symmetry.finder import SymmetryFinder

#from pymatgen.core.structure import PeriodicSize
#from pymatgen.core.structure_modifier import SuperCellMaker

class Defect:
    """
    Abstract class for point defects
    """
    __metaclass__ = abc.ABCMeta
    
    @abc.abstractmethod
    def enumerate_uniq_defectsites(self):
        """
        Enumerates all the unique defects
        """
        print 'Not implemented'
    
    @abc.abstractmethod
    def make_supercells_with_defects(self, scaling_matrix):
        """
        Generate the supercell with input multipliers and create the defect
        """ 
        print 'Not implemented'
    
    
class Vacancy(Defect):
    """
    Subclass of Defect to generate vacancies
    """
    def __init__(self, inp_structure):
        """
        Given a structure, generate the unique vacancy sites
        
        Args:
            inp_structure:
                pymatgen.core.Structure
        """
        
        self._structure = inp_structure
        symm_finder = SymmetryFinder(self._structure)
        symm_structure = symm_finder.get_symmetrized_structure()
        equiv_site_seq = symm_structure.equivalent_sites
        self._uniq_vac_sites = []
        for equiv_sites in equiv_site_seq:
            self._uniq_vac_sites.append(equiv_sites[0])
        
        
    def enumerate_uniq_defectsites(self):
        """
        Enumerate the unique defect sites
        
        Returns:
            List of unique defect sites

        """
        return self._uniq_vac_sites
    
    def _supercell_with_defect(self, scaling_matrix, defect_site):
        sc = self._structure.copy()
        sc.make_supercell(scaling_matrix)
        oldf_coords = defect_site.frac_coords
        coords = defect_site.lattice.get_cartesian_coords(oldf_coords)
        newf_coords = sc.lattice.get_fractional_coords(coords)
        sc_defect_site = PeriodicSite(defect_site.species_and_occu, newf_coords,
                                      sc.lattice,
                                      properties=defect_site.properties)
        for i in range(len(sc.sites)):
            if sc_defect_site == sc.sites[i]:
                sc.remove(i)
                return sc
        
    def make_supercells_with_defects(self, scaling_matrix):
        """
        Returns sequence of supercells containing unique defects
        """
        sc_with_vac = []
        for uniq_defect_site in self.enumerate():
            sc_with_vac.append(self._supercell_with_defect(scaling_matrix, 
                                                           uniq_defect_site))
        return sc_with_vac
             
        
class Interstitial(Defect):
    """
    Subclass of Defect to generate interstitials
    """
    def __init__(self, inp_structure ):
        """
        Given a structure, generate unique interstitial sites
        
        Args:
            inp_structure:
                pymatgen.core.Structure
        """
        
        self._structure = inp_structure
        self._uniq_interstitial_sites = []
        #symm_finder = SymmetryFinder(self._structure)
        #Use Zeo++ to analyze the void space and identify the void space
    
    def enumerate_uniq_defectsites(self):
        return self._uniq_interstitial_sites

    def add_interstitial(self, site, element):
        pass

    def make_supercells_with_defects(self, scaling_matrix, element):
        pass<commit_msg>Interstitial defect generation implemented<commit_after>#!/usr/bin/env python

"""
Abstract class for defects
"""
from __future__ import division


import abc 

from pymatgen.core.sites import PeriodicSite
from pymatgen.symmetry.finder import SymmetryFinder

#from pymatgen.core.structure import PeriodicSize
#from pymatgen.core.structure_modifier import SuperCellMaker

class Defect:
    """
    Abstract class for point defects
    """
    __metaclass__ = abc.ABCMeta
    
    @abc.abstractmethod
    def enumerate_uniq_defectsites(self):
        """
        Enumerates all the unique defects
        """
        print 'Not implemented'
    
    @abc.abstractmethod
    def make_supercells_with_defects(self, scaling_matrix):
        """
        Generate the supercell with input multipliers and create the defect
        To create unit cell with defect pass unit matrix.
        """ 
        print 'Not implemented'
    
    
class Vacancy(Defect):
    """
    Subclass of Defect to generate vacancies
    """
    def __init__(self, inp_structure):
        """
        Given a structure, generate the unique vacancy sites
        
        Args:
            inp_structure:
                pymatgen.core.Structure
        """
        
        self._structure = inp_structure
        symm_finder = SymmetryFinder(self._structure)
        symm_structure = symm_finder.get_symmetrized_structure()
        equiv_site_seq = symm_structure.equivalent_sites
        self._uniq_vac_sites = []
        for equiv_sites in equiv_site_seq:
            self._uniq_vac_sites.append(equiv_sites[0])
        
        
    def enumerate_uniq_defectsites(self):
        """
        Enumerate the unique defect sites
        
        Returns:
            List of unique defect sites

        """
        return self._uniq_vac_sites
    
    def _supercell_with_defect(self, scaling_matrix, defect_site):
        sc = self._structure.copy()
        sc.make_supercell(scaling_matrix)
        oldf_coords = defect_site.frac_coords
        coords = defect_site.lattice.get_cartesian_coords(oldf_coords)
        newf_coords = sc.lattice.get_fractional_coords(coords)
        sc_defect_site = PeriodicSite(defect_site.species_and_occu, newf_coords,
                                      sc.lattice,
                                      properties=defect_site.properties)
        for i in range(len(sc.sites)):
            if sc_defect_site == sc.sites[i]:
                sc.remove(i)
                return sc
        
    def make_supercells_with_defects(self, scaling_matrix):
        """
        Returns sequence of supercells in pymatgen.core.structure.Structure
        format, with each supercell containing unique vacancy
        """
        sc_with_vac = []
        for uniq_defect_site in self.enumerate():
            sc_with_vac.append(self._supercell_with_defect(scaling_matrix, 
                                                           uniq_defect_site))
        return sc_with_vac
             
        
class Interstitial(Defect):
    """
    Subclass of Defect to generate interstitials
    """
    def __init__(self, structure):
        """
        Given a structure, generate unique interstitial sites
        
        Args:
            structure:
                pymatgen.core.Structure
        """
        
        self._structure = structure
        self._uniq_interstitial_sites = []

        #Use Zeo++ to obtain the voronoi nodes. The unique voronoi nodes
        #are possible candidates for interstitial sites
        possibleInterstitialSites = _get_zeo_uniq_voronoi_nodes(self._structure)
        
        #Do futher processing on possibleInterstitialSites to obtain 
        #interstitial sites
        self._uniq_interstitial_sites = possibleInterstitialSites
        
        
    def enumerate_uniq_defectsites(self):
        return self._uniq_interstitial_sites

            
    def _supercell_with_defect(self, scaling_matrix, defect_site, element):
        sc = self._structure.copy()
        sc.make_supercell(scaling_matrix)
        oldf_coords = defect_site.frac_coords
        coords = defect_site.lattice.get_cartesian_coords(oldf_coords)
        newf_coords = sc.lattice.get_fractional_coords(coords)
        sc_defect_site = PeriodicSite(defect_site.species_and_occu, newf_coords,
                                      sc.lattice,
                                      properties=defect_site.properties)
        for i in range(len(sc.sites)):
            if sc_defect_site == sc.sites[i]:
                sc.replace(i, element)
                return sc
           

    def make_supercells_with_defects(self, scaling_matrix, element):
        """
        Returns sequence of supercells in pymatgen.core.structure.Structure
        format, with each supercell containing unique interstitial
        """
        sc_with_interstitial = []
        for uniq_defect_site in self.enumerate():
            sc_with_interstitial.append(self._supercell_with_defect(
                                    scaling_matrix, uniq_defect_site, element))
        return sc_with_interstitial

def _get_zeo_uniq_voronoi_nodes(structure):
    """
    Obtain uniq voronoi nodes using Zeo++
    """
    from pymatgen.io.cssrio import Cssr
    from zeo.netstorage import AtomNetwork, VoronoiNetwork
    from voronoi_node_symmetry import uniq_voronoi_nodes
    cssrStruct = Cssr(structure)
    name = "tmp"
    tmpCssrFile = name+".cssr"
    tmpVoroXyzFile = name+"_voro.xyz"
    cssrStruct.write_file(tmpCssrFile)
    # Use pymatgen radii for elements in future
    atmnet = AtomNetwork(tmpCssrFile)
    vornet = atmnet.perform_voronoi_decomposition()
    tmpProbeRad = 0.1
    vornet.analyze_writeto_xyz(name, tmpProbeRad, atmnet)
    return uniq_voronoi_nodes(tmpCssrFile, tmpVoroXyzFile)
    
    
{'fraction_non_alphanumeric': 0.06654638081086818, 'fraction_numerical': 0.0004245383145828911, 'mean_word_length': 2.1663306451612905, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3083454', 'n_tokens_mistral': 2842, 'n_tokens_neox': 2675, 'n_words': 562}