<commit_before>#!/usr/bin/python
import unittest
import os

from pymatgen.io.vaspio import Poscar, Potcar, Kpoints, Incar, Vasprun, Outcar
from pymatgen.core.lattice import Lattice
from pymatgen.core.structure import Composition, Structure
from numpy import array

module_dir = os.path.dirname(os.path.abspath(__file__))

class  PoscarTest(unittest.TestCase):
    
    def test_init(self):
        filepath = os.path.join(module_dir, 'vasp_testfiles','POSCAR')
        poscar = Poscar.from_file(filepath)
        comp = poscar.struct.composition
        self.assertEqual(comp,Composition.from_formula("Fe4P4O16"))

        si = 14
        coords = list()
        coords.append(array([0,0,0]))
        coords.append(array([0.75,0.5,0.75]))

        #Silicon structure for testing.
        latt = Lattice(array([[ 3.8401979337, 0.00, 0.00],[1.9200989668, 3.3257101909, 0.00],[0.00,-2.2171384943,3.1355090603]]))
        struct = Structure(latt,[si,si],coords)
        poscar = Poscar(struct)
        expected_str = '''Si2
1.0
3.840198 0.000000 0.000000
1.920099 3.325710 0.000000
0.000000 -2.217138 3.135509
Si
2
direct
0.000000 0.000000 0.000000 Si
0.750000 0.500000 0.750000 Si'''
        
        self.assertEquals(str(poscar), expected_str, "Wrong POSCAR output!")

class  IncarTest(unittest.TestCase):
    
    def test_init(self):
        filepath = os.path.join(module_dir, 'vasp_testfiles','INCAR')
        incar = Incar.from_file(filepath)
        incar["LDAU"] = "T"
        self.assertEqual(incar["ALGO"],"Damped","Wrong Algo")
        self.assertEqual(float(incar["EDIFF"]),1e-4,"Wrong EDIFF")
    
    def test_from_structure(self):
        filepath = os.path.join(module_dir,'vasp_testfiles','POSCAR')
        poscar = Poscar.from_file(filepath)
        incar = Incar.from_structure(poscar.struct)
        self.assertEqual(incar['LDAUU'], [5.3, 0, 0])
        si = 14
        coords = list()
        coords.append(array([0,0,0]))
        coords.append(array([0.75,0.5,0.75]))

        #Silicon structure for testing.
        latt = Lattice(array([[ 3.8401979337, 0.00, 0.00],[1.9200989668, 3.3257101909, 0.00],[0.00,-2.2171384943,3.1355090603]]))
        struct = Structure(latt,[si,si],coords)
        incar = Incar.from_structure(struct)
        self.assertNotIn("LDAU", incar)
        
        
    def test_diff(self):
        filepath1 = os.path.join(module_dir, 'vasp_testfiles','INCAR')
        incar1 = Incar.from_file(filepath1)
        filepath2 = os.path.join(module_dir, 'vasp_testfiles','INCAR.2')
        incar2 = Incar.from_file(filepath2)        
        self.assertEqual(incar1.diff(incar2), {'Same': {'IBRION': 2, 'PREC': 'Accurate', 'ISIF': 3, 'LMAXMIX': 4, 'LREAL': 'Auto', 'ISPIN': 2, 'LORBIT': '11', 'SIGMA': 0.05}, 'Different': {'MAGMOM': {'INCAR1': [6, -6, -6, 6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6], 'INCAR2': 'Default'}, 'NKRED': {'INCAR1': '2', 'INCAR2': 'Default'}, 'ENCUTFOCK': {'INCAR1': '0', 'INCAR2': 'Default'}, 'NUPDOWN': {'INCAR1': '0', 'INCAR2': 'Default'}, 'EDIFF': {'INCAR1': '1E-4', 'INCAR2': 0.0001}, 'HFSCREEN': {'INCAR1': '0.207', 'INCAR2': 'Default'}, 'LSCALU': {'INCAR1': '.FALSE.', 'INCAR2': 'Default'}, 'SYSTEM': {'INCAR1': 'id=[0] dblock_code=[97763-ICSD] formula=[Li Mn (P O4)] sg_name=[P n m a]', 'INCAR2': 'id=[91090] dblock_code=[20070929235612LiNiO-59.53134651-VASP] formula=[Li3 Ni3 O6] sg_name=[R-3m]'}, 'ENCUT': {'INCAR1': '500', 'INCAR2': 'Default'}, 'NSIM': {'INCAR1': '1', 'INCAR2': 'Default'}, 'LCHARG': {'INCAR1': '.TRUE.', 'INCAR2': 'Default'}, 'LPLANE': {'INCAR1': '.TRUE.', 'INCAR2': 'Default'}, 'ALGO': {'INCAR1': 'Damped', 'INCAR2': 'Fast'}, 'LHFCALC': {'INCAR1': '.TRUE.', 'INCAR2': 'Default'}, 'TIME': {'INCAR1': '0.4', 'INCAR2': 'Default'}, 'ISMEAR': {'INCAR1': 0, 'INCAR2': -5}, 'LWAVE': {'INCAR1': True, 'INCAR2': False}, 'NPAR': {'INCAR1': 8, 'INCAR2': 1}, 'NSW': {'INCAR1': 99, 'INCAR2': 51}, 'ISPIND': {'INCAR1': '2', 'INCAR2': 'Default'}}})       
        
class  KpointsTest(unittest.TestCase):
    
    def test_init(self):
        filepath = os.path.join(module_dir, 'vasp_testfiles','KPOINTS.auto')
        kpoints = Kpoints.from_file(filepath)
        self.assertEqual(kpoints.kpts,[[10]],"Wrong kpoint lattice read")
        filepath = os.path.join(module_dir, 'vasp_testfiles','KPOINTS.cartesian')
        kpoints = Kpoints.from_file(filepath)
        self.assertEqual(kpoints.kpts,[[0.25, 0, 0], [0,0.25,0], [0,0,0.25]],"Wrong kpoint lattice read")
        self.assertEqual(kpoints.kpts_shift,[0.5, 0.5, 0.5],"Wrong kpoint shift read")
        
        filepath = os.path.join(module_dir, 'vasp_testfiles','KPOINTS')
        kpoints = Kpoints.from_file(filepath)
        self.assertEqual(kpoints.kpts,[[2,4,6]],"Wrong kpoint lattice read")
    
    def test_from_structure(self):
        filepath = os.path.join(module_dir,'vasp_testfiles','POSCAR')
        poscar = Poscar.from_file(filepath)
        kpoints = Kpoints.from_structure(poscar.struct)
        self.assertIsNotNone(kpoints)<commit_msg>Fix unittest for new incar diff code/<commit_after>#!/usr/bin/python
import unittest
import os

from pymatgen.io.vaspio import Poscar, Potcar, Kpoints, Incar, Vasprun, Outcar
from pymatgen.core.lattice import Lattice
from pymatgen.core.structure import Composition, Structure
from numpy import array

module_dir = os.path.dirname(os.path.abspath(__file__))

class  PoscarTest(unittest.TestCase):
    
    def test_init(self):
        filepath = os.path.join(module_dir, 'vasp_testfiles','POSCAR')
        poscar = Poscar.from_file(filepath)
        comp = poscar.struct.composition
        self.assertEqual(comp,Composition.from_formula("Fe4P4O16"))

        si = 14
        coords = list()
        coords.append(array([0,0,0]))
        coords.append(array([0.75,0.5,0.75]))

        #Silicon structure for testing.
        latt = Lattice(array([[ 3.8401979337, 0.00, 0.00],[1.9200989668, 3.3257101909, 0.00],[0.00,-2.2171384943,3.1355090603]]))
        struct = Structure(latt,[si,si],coords)
        poscar = Poscar(struct)
        expected_str = '''Si2
1.0
3.840198 0.000000 0.000000
1.920099 3.325710 0.000000
0.000000 -2.217138 3.135509
Si
2
direct
0.000000 0.000000 0.000000 Si
0.750000 0.500000 0.750000 Si'''
        
        self.assertEquals(str(poscar), expected_str, "Wrong POSCAR output!")

class  IncarTest(unittest.TestCase):
    
    def test_init(self):
        filepath = os.path.join(module_dir, 'vasp_testfiles','INCAR')
        incar = Incar.from_file(filepath)
        incar["LDAU"] = "T"
        self.assertEqual(incar["ALGO"],"Damped","Wrong Algo")
        self.assertEqual(float(incar["EDIFF"]),1e-4,"Wrong EDIFF")
    
    def test_from_structure(self):
        filepath = os.path.join(module_dir,'vasp_testfiles','POSCAR')
        poscar = Poscar.from_file(filepath)
        incar = Incar.from_structure(poscar.struct)
        self.assertEqual(incar['LDAUU'], [5.3, 0, 0])
        si = 14
        coords = list()
        coords.append(array([0,0,0]))
        coords.append(array([0.75,0.5,0.75]))

        #Silicon structure for testing.
        latt = Lattice(array([[ 3.8401979337, 0.00, 0.00],[1.9200989668, 3.3257101909, 0.00],[0.00,-2.2171384943,3.1355090603]]))
        struct = Structure(latt,[si,si],coords)
        incar = Incar.from_structure(struct)
        self.assertNotIn("LDAU", incar)
        
        
    def test_diff(self):
        filepath1 = os.path.join(module_dir, 'vasp_testfiles','INCAR')
        incar1 = Incar.from_file(filepath1)
        filepath2 = os.path.join(module_dir, 'vasp_testfiles','INCAR.2')
        incar2 = Incar.from_file(filepath2)
        self.assertEqual(incar1.diff(incar2), {'Different': {'MAGMOM': {'INCAR1': [6, -6, -6, 6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6], 'INCAR2': 'Default'}, 'NKRED': {'INCAR1': 2, 'INCAR2': 'Default'}, 'ENCUTFOCK': {'INCAR1': 0.0, 'INCAR2': 'Default'}, 'NUPDOWN': {'INCAR1': 0, 'INCAR2': 'Default'}, 'HFSCREEN': {'INCAR1': 0.207, 'INCAR2': 'Default'}, 'LSCALU': {'INCAR1': False, 'INCAR2': 'Default'}, 'SYSTEM': {'INCAR1': 'id=[0] dblock_code=[97763-ICSD] formula=[Li Mn (P O4)] sg_name=[P n m a]', 'INCAR2': 'id=[91090] dblock_code=[20070929235612LiNiO-59.53134651-VASP] formula=[Li3 Ni3 O6] sg_name=[R-3m]'}, 'ENCUT': {'INCAR1': 500, 'INCAR2': 'Default'}, 'NSIM': {'INCAR1': 1, 'INCAR2': 'Default'}, 'LCHARG': {'INCAR1': True, 'INCAR2': 'Default'}, 'LPLANE': {'INCAR1': True, 'INCAR2': 'Default'}, 'ALGO': {'INCAR1': 'Damped', 'INCAR2': 'Fast'}, 'LHFCALC': {'INCAR1': True, 'INCAR2': 'Default'}, 'TIME': {'INCAR1': 0.4, 'INCAR2': 'Default'}, 'ISMEAR': {'INCAR1': 0, 'INCAR2': -5}, 'LWAVE': {'INCAR1': True, 'INCAR2': False}, 'NPAR': {'INCAR1': 8, 'INCAR2': 1}, 'NSW': {'INCAR1': 99, 'INCAR2': 51}, 'ISPIND': {'INCAR1': 2, 'INCAR2': 'Default'}}, 'Same': {'IBRION': 2, 'PREC': 'Accurate', 'ISIF': 3, 'LMAXMIX': 4, 'LREAL': 'Auto', 'ISPIN': 2, 'EDIFF': 0.0001, 'LORBIT': '11', 'SIGMA': 0.05}})

class  KpointsTest(unittest.TestCase):
    
    def test_init(self):
        filepath = os.path.join(module_dir, 'vasp_testfiles','KPOINTS.auto')
        kpoints = Kpoints.from_file(filepath)
        self.assertEqual(kpoints.kpts,[[10]],"Wrong kpoint lattice read")
        filepath = os.path.join(module_dir, 'vasp_testfiles','KPOINTS.cartesian')
        kpoints = Kpoints.from_file(filepath)
        self.assertEqual(kpoints.kpts,[[0.25, 0, 0], [0,0.25,0], [0,0,0.25]],"Wrong kpoint lattice read")
        self.assertEqual(kpoints.kpts_shift,[0.5, 0.5, 0.5],"Wrong kpoint shift read")
        
        filepath = os.path.join(module_dir, 'vasp_testfiles','KPOINTS')
        kpoints = Kpoints.from_file(filepath)
        self.assertEqual(kpoints.kpts,[[2,4,6]],"Wrong kpoint lattice read")
    
    def test_from_structure(self):
        filepath = os.path.join(module_dir,'vasp_testfiles','POSCAR')
        poscar = Poscar.from_file(filepath)
        kpoints = Kpoints.from_structure(poscar.struct)
        self.assertIsNotNone(kpoints)
{'fraction_non_alphanumeric': 0.191546494281452, 'fraction_numerical': 0.09438090502237692, 'mean_word_length': 3.6815642458100557, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6697316', 'n_tokens_mistral': 4556, 'n_tokens_neox': 3927, 'n_words': 718}