<commit_before># coding: utf-8

from __future__ import unicode_literals, division

"""
Created on Jun 1, 2012
"""


__author__ = "<NAME>, <NAME>"
__copyright__ = "Copyright 2012, The Materials Project"
__version__ = "0.1"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__date__ = "Jun 1, 2012"

import unittest
import os
import glob
import shutil
import datetime

from custodian.vasp.handlers import VaspErrorHandler, \
    UnconvergedErrorHandler, MeshSymmetryErrorHandler, WalltimeHandler, \
    MaxForceErrorHandler, PositiveEnergyErrorHandler, PotimErrorHandler, \
    FrozenJobErrorHandler, AliasingErrorHandler
from pymatgen.io.vasp import Incar, Poscar, Structure


test_dir = os.path.join(os.path.dirname(__file__), "..", "..", "..",
                        'test_files')

cwd = os.getcwd()


def clean_dir():
    for f in glob.glob("error.*.tar.gz"):
        os.remove(f)
    for f in glob.glob("custodian.chk.*.tar.gz"):
        os.remove(f)


class VaspErrorHandlerTest(unittest.TestCase):

    def setUp(self):
        if "VASP_PSP_DIR" not in os.environ:
            os.environ["VASP_PSP_DIR"] = test_dir
        os.chdir(test_dir)
        shutil.copy("INCAR", "INCAR.orig")
        shutil.copy("KPOINTS", "KPOINTS.orig")
        shutil.copy("POSCAR", "POSCAR.orig")
        shutil.copy("CHGCAR", "CHGCAR.orig")

    def test_frozen_job(self):
        h = FrozenJobErrorHandler()
        d = h.correct()
        self.assertEqual(d['errors'], ['Frozen job'])
        self.assertEqual(Incar.from_file("INCAR")['ALGO'], "Normal")

    def test_check_correct(self):
        h = VaspErrorHandler("vasp.teterror")
        h.check()
        d = h.correct()
        self.assertEqual(d["errors"], ['tet'])
        self.assertEqual(d["actions"],
                         [{'action': {'_set': {'ISMEAR': 0}},
                           'dict': 'INCAR'}])
        h = VaspErrorHandler("vasp.sgrcon")
        h.check()
        d = h.correct()
        self.assertEqual(d["errors"], ['rot_matrix'])
        self.assertEqual(set([a["dict"] for a in d["actions"]]),
                         {"KPOINTS"})

        h = VaspErrorHandler("vasp.real_optlay")
        h.check()
        d = h.correct()
        self.assertEqual(d["errors"], ['real_optlay'])
        self.assertEqual(d["actions"],
                         [{'action': {'_set': {'LREAL': False}},
                           'dict': 'INCAR'}])

    def test_mesh_symmetry(self):
        h = MeshSymmetryErrorHandler("vasp.ibzkpt")
        h.check()
        d = h.correct()
        self.assertEqual(d["errors"], ['mesh_symmetry'])
        self.assertEqual(d["actions"],
                         [{'action': {'_set': {'kpoints': [[4, 4, 4]]}},
                           'dict': 'KPOINTS'}])

    def test_dentet(self):
        h = VaspErrorHandler("vasp.dentet")
        h.check()
        d = h.correct()
        self.assertEqual(d["errors"], ['dentet'])
        self.assertEqual(d["actions"],
                         [{'action': {'_set': {'ISMEAR': 0}},
                           'dict': 'INCAR'}])

    def test_brmix(self):
        h = VaspErrorHandler("vasp.brmix")
        self.assertEqual(h.check(), True)
        d = h.correct()
        self.assertEqual(d["errors"], ['brmix'])
        self.assertFalse(os.path.exists("CHGCAR"))

        shutil.copy("INCAR.nelect", "INCAR")
        h = VaspErrorHandler("vasp.brmix")
        self.assertEqual(h.check(), False)
        d = h.correct()
        self.assertEqual(d["errors"], [])

    def test_too_few_bands(self):
        os.chdir(os.path.join(test_dir, "too_few_bands"))
        shutil.copy("INCAR", "INCAR.orig")
        h = VaspErrorHandler("vasp.too_few_bands")
        h.check()
        d = h.correct()
        self.assertEqual(d["errors"], ['too_few_bands'])
        self.assertEqual(d["actions"],
                         [{'action': {'_set': {'NBANDS': 501}},
                           'dict': 'INCAR'}])
        clean_dir()
        shutil.move("INCAR.orig", "INCAR")
        os.chdir(test_dir)

    def test_rot_matrix(self):<commit_msg>fix test for brmix error handler<commit_after># coding: utf-8

from __future__ import unicode_literals, division

"""
Created on Jun 1, 2012
"""


__author__ = "<NAME>, <NAME>"
__copyright__ = "Copyright 2012, The Materials Project"
__version__ = "0.1"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__date__ = "Jun 1, 2012"

import unittest
import os
import glob
import shutil
import datetime

from custodian.vasp.handlers import VaspErrorHandler, \
    UnconvergedErrorHandler, MeshSymmetryErrorHandler, WalltimeHandler, \
    MaxForceErrorHandler, PositiveEnergyErrorHandler, PotimErrorHandler, \
    FrozenJobErrorHandler, AliasingErrorHandler
from pymatgen.io.vasp import Incar, Poscar, Structure, Kpoints, VaspInput


test_dir = os.path.join(os.path.dirname(__file__), "..", "..", "..",
                        'test_files')

cwd = os.getcwd()


def clean_dir():
    for f in glob.glob("error.*.tar.gz"):
        os.remove(f)
    for f in glob.glob("custodian.chk.*.tar.gz"):
        os.remove(f)


class VaspErrorHandlerTest(unittest.TestCase):

    def setUp(self):
        if "VASP_PSP_DIR" not in os.environ:
            os.environ["VASP_PSP_DIR"] = test_dir
        os.chdir(test_dir)
        shutil.copy("INCAR", "INCAR.orig")
        shutil.copy("KPOINTS", "KPOINTS.orig")
        shutil.copy("POSCAR", "POSCAR.orig")
        shutil.copy("CHGCAR", "CHGCAR.orig")

    def test_frozen_job(self):
        h = FrozenJobErrorHandler()
        d = h.correct()
        self.assertEqual(d['errors'], ['Frozen job'])
        self.assertEqual(Incar.from_file("INCAR")['ALGO'], "Normal")

    def test_check_correct(self):
        h = VaspErrorHandler("vasp.teterror")
        h.check()
        d = h.correct()
        self.assertEqual(d["errors"], ['tet'])
        self.assertEqual(d["actions"],
                         [{'action': {'_set': {'ISMEAR': 0}},
                           'dict': 'INCAR'}])
        h = VaspErrorHandler("vasp.sgrcon")
        h.check()
        d = h.correct()
        self.assertEqual(d["errors"], ['rot_matrix'])
        self.assertEqual(set([a["dict"] for a in d["actions"]]),
                         {"KPOINTS"})

        h = VaspErrorHandler("vasp.real_optlay")
        h.check()
        d = h.correct()
        self.assertEqual(d["errors"], ['real_optlay'])
        self.assertEqual(d["actions"],
                         [{'action': {'_set': {'LREAL': False}},
                           'dict': 'INCAR'}])

    def test_mesh_symmetry(self):
        h = MeshSymmetryErrorHandler("vasp.ibzkpt")
        h.check()
        d = h.correct()
        self.assertEqual(d["errors"], ['mesh_symmetry'])
        self.assertEqual(d["actions"],
                         [{'action': {'_set': {'kpoints': [[4, 4, 4]]}},
                           'dict': 'KPOINTS'}])

    def test_dentet(self):
        h = VaspErrorHandler("vasp.dentet")
        h.check()
        d = h.correct()
        self.assertEqual(d["errors"], ['dentet'])
        self.assertEqual(d["actions"],
                         [{'action': {'_set': {'ISMEAR': 0}},
                           'dict': 'INCAR'}])

    def test_brmix(self):
        h = VaspErrorHandler("vasp.brmix")
        self.assertEqual(h.check(), True)
        d = h.correct()
        self.assertEqual(d["errors"], ['brmix'])

        vi = VaspInput.from_directory(".")
        chgcar_exists = os.path.exists("CHGCAR")
        if h.error_count['brmix'] > 1:
            self.assertFalse(chgcar_exists)
        else:
            self.assertTrue(chgcar_exists)
            if vi["KPOINTS"].style == Kpoints.supported_modes.Gamma and vi["KPOINTS"].num_kpts < 1:
                all_kpts_even = all([
                    bool(n % 2 == 0) for n in vi["KPOINTS"].kpts[0]
                ])
                self.assertFalse(all_kpts_even)

        shutil.copy("INCAR.nelect", "INCAR")
        h = VaspErrorHandler("vasp.brmix")
        self.assertEqual(h.check(), False)
        d = h.correct()
        self.assertEqual(d["errors"], [])

    def test_too_few_bands(self):
        os.chdir(os.path.join(test_dir, "too_few_bands"))
        shutil.copy("INCAR", "INCAR.orig")
        h = VaspErrorHandler("vasp.too_few_bands")
        h.check()
        d = h.correct()
        self.assertEqual(d["errors"], ['too_few_bands'])
        self.assertEqual(d["actions"],
                         [{'action': {'_set': {'NBANDS': 501}},
                           'dict': 'INCAR'}])
        clean_dir()
        shutil.move("INCAR.orig", "INCAR")
        os.chdir(test_dir)

    def test_rot_matrix(self):
{'fraction_non_alphanumeric': 0.1625232342007435, 'fraction_numerical': 0.006389405204460966, 'mean_word_length': 2.2896446312571648, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '2255287', 'n_tokens_mistral': 2904, 'n_tokens_neox': 2764, 'n_words': 493}