# Path: twod_materials/utils.py
# def is_converged(directory):
#     """
#     Check if a relaxation has converged.
# 
#     Args:
#         directory (str): path to directory to check.
# 
#     Returns:
#         boolean. Whether or not the job is converged.
#     """
# 
#     try:
#         if Vasprun('{}/vasprun.xml'.format(directory)).converged:
#             return True
#         else:
#             return False
# 
#     except:
#         return False
# 
# Path: twod_materials/stability/startup.py
# PACKAGE_PATH = twod_materials.__file__.replace('__init__.pyc', '')
# PACKAGE_PATH = PACKAGE_PATH.replace('__init__.py', '')
# PACKAGE_PATH = '/'.join(PACKAGE_PATH.split('/')[:-2])
# INCAR_DICT = {
#     '@class': 'Incar', '@module': 'pymatgen.io.vasp.inputs', 'AGGAC': 0.0,
#     'EDIFF': 1e-04, 'GGA': 'Bo', 'IBRION': 2, 'ISIF': 3, 'ISMEAR': 1,
#     'LAECHG': True, 'LCHARG': True, 'LREAL': 'Auto', 'LUSE_VDW': True,
#     'NPAR': 4, 'NSW': 50, 'PARAM1': 0.1833333333, 'PARAM2': 0.22,
#     'PREC': 'Accurate', 'ENCUT': 500, 'SIGMA': 0.1, 'LVTOT': True,
#     'LVHAR': True, 'ALGO': 'Fast', 'ISPIN': 2
#     }
#     MPR = MPRester(os.environ['MP_API'])
#     MPR = MPRester(config_vars['mp_api'])
# VASP = config_vars['normal_binary']
# VASP_2D = config_vars['twod_binary']
# VDW_KERNEL = config_vars['vdw_kernel']
#     QUEUE = config_vars['queue_system'].lower()
#     QUEUE = 'slurm'
#     QUEUE = 'pbs'
# def relax(dim=2, submit=True, force_overwrite=False):
# 
# Path: twod_materials/stability/analysis.py
# PACKAGE_PATH = twod_materials.__file__.replace('__init__.pyc', '')
# PACKAGE_PATH = PACKAGE_PATH.replace('__init__.py', '')
# PACKAGE_PATH = '/'.join(PACKAGE_PATH.split('/')[:-2])
#     MPR = MPRester(os.environ['MP_API'])
#     MPR = MPRester(config_vars['mp_api'])
# def get_competing_phases():
# def get_hull_distance(competing_phase_directory='../competing_phases'):
# def plot_hull_distances(hull_distances, fmt='pdf'):


import os
import time
from twod_materials.utils import is_converged
from twod_materials.stability.startup import relax, relax_competing_species
from twod_materials.stability.analysis import (get_competing_species,
                                               get_hull_distances,
                                               plot_hull_distances)
from __future__ import print_function, division, unicode_literals





INTERVAL = 360  # Seconds between convergence checks

directories = [dir for dir in os.listdir(os.getcwd()) if os.path.isdir(dir)
               and dir not in ['all_competitors']]

if __name__ == '__main__':

    competing_species = get_competing_species(directories)
    relax_competing_species(competing_species)

    for directory in directories:
        os.chdir(directory)
        relax()
        os.chdir('../')

    loop = True
    while loop:
        print('>> Checking convergence')
        finished_2d, finished_3d = [], []

        for directory in directories:
            if is_converged(directory):
                finished_2d.append(directory)
        for directory in competing_species:

# Path: twod_materials/utils.py
# def is_converged(directory):
#     """
#     Check if a relaxation has converged.
# 
#     Args:
#         directory (str): path to directory to check.
# 
#     Returns:
#         boolean. Whether or not the job is converged.
#     """
# 
#     try:
#         if Vasprun('{}/vasprun.xml'.format(directory)).converged:
#             return True
#         else:
#             return False
# 
#     except:
#         return False
# 
# Path: twod_materials/stability/startup.py
# PACKAGE_PATH = twod_materials.__file__.replace('__init__.pyc', '')
# PACKAGE_PATH = PACKAGE_PATH.replace('__init__.py', '')
# PACKAGE_PATH = '/'.join(PACKAGE_PATH.split('/')[:-2])
# INCAR_DICT = {
#     '@class': 'Incar', '@module': 'pymatgen.io.vasp.inputs', 'AGGAC': 0.0,
#     'EDIFF': 1e-04, 'GGA': 'Bo', 'IBRION': 2, 'ISIF': 3, 'ISMEAR': 1,
#     'LAECHG': True, 'LCHARG': True, 'LREAL': 'Auto', 'LUSE_VDW': True,
#     'NPAR': 4, 'NSW': 50, 'PARAM1': 0.1833333333, 'PARAM2': 0.22,
#     'PREC': 'Accurate', 'ENCUT': 500, 'SIGMA': 0.1, 'LVTOT': True,
#     'LVHAR': True, 'ALGO': 'Fast', 'ISPIN': 2
#     }
#     MPR = MPRester(os.environ['MP_API'])
#     MPR = MPRester(config_vars['mp_api'])
# VASP = config_vars['normal_binary']
# VASP_2D = config_vars['twod_binary']
# VDW_KERNEL = config_vars['vdw_kernel']
#     QUEUE = config_vars['queue_system'].lower()
#     QUEUE = 'slurm'
#     QUEUE = 'pbs'
# def relax(dim=2, submit=True, force_overwrite=False):
# 
# Path: twod_materials/stability/analysis.py
# PACKAGE_PATH = twod_materials.__file__.replace('__init__.pyc', '')
# PACKAGE_PATH = PACKAGE_PATH.replace('__init__.py', '')
# PACKAGE_PATH = '/'.join(PACKAGE_PATH.split('/')[:-2])
#     MPR = MPRester(os.environ['MP_API'])
#     MPR = MPRester(config_vars['mp_api'])
# def get_competing_phases():
# def get_hull_distance(competing_phase_directory='../competing_phases'):
# def plot_hull_distances(hull_distances, fmt='pdf'):


# Path: examples/thermodynamic_stability.py
import os
import time
from twod_materials.utils import is_converged
from twod_materials.stability.startup import relax, relax_competing_species
from twod_materials.stability.analysis import (get_competing_species,
                                               get_hull_distances,
                                               plot_hull_distances)

from __future__ import print_function, division, unicode_literals





INTERVAL = 360  # Seconds between convergence checks

directories = [dir for dir in os.listdir(os.getcwd()) if os.path.isdir(dir)
               and dir not in ['all_competitors']]

if __name__ == '__main__':

    competing_species = get_competing_species(directories)
    relax_competing_species(competing_species)

    for directory in directories:
        os.chdir(directory)
        relax()
        os.chdir('../')

    loop = True
    while loop:
        print('>> Checking convergence')
        finished_2d, finished_3d = [], []

        for directory in directories:
            if is_converged(directory):
                finished_2d.append(directory)
        for directory in competing_species:

            if is_converged('all_competitors/{}'.format(directory[0])):