# Path: pyDFTutils/ase_utils/kpoints.py
# def get_ir_kpts(atoms, mesh):
#     """
#     Gamma-centered IR kpoints. mesh : [nk1,nk2,nk3].
#     """
#     lattice = atoms.get_cell()
#     positions = atoms.get_scaled_positions()
#     numbers = atoms.get_atomic_numbers()
# 
#     cell = (lattice, positions, numbers)
#     mapping, grid = spglib.get_ir_reciprocal_mesh(
#         mesh, cell, is_shift=[0, 0, 0])
#     #print("%3d ->%3d %s" % (1, mapping[0], grid[0].astype(float) / mesh))
#     #print("Number of ir-kpoints: %d" % len(np.unique(mapping)))
#     #print(grid[np.unique(mapping)] / np.array(mesh, dtype=float))
#     return grid[np.unique(mapping)] / np.array(mesh, dtype=float)
# 
# def cubic_kpath(npoints=500,name=True):
#     """
#     return the kpoint path for cubic
#     Parameters:
#     ----------------
#     npoints: int
#         number of points.
# 
#     Returns:
#     -----------------
#     kpts:
#         kpoints
#     xs:
#         x cordinates for plot
#     xspecial:
#         x cordinates for special k points
#     """
#     special_path = special_paths['cubic']
#     points = special_points['cubic']
#     paths = parse_path_string(special_path)
#     special_kpts = [points[k] for k in paths[0]]
#     kpts, xs, xspecial = bandpath(
#         special_kpts, cell=np.eye(3), npoints=npoints)
#     if not name:
#         return kpts, xs, xspecial
#     else:
#         return kpts, xs, xspecial,special_path


import os
import shutil
import numpy as np
import subprocess
import spglib
from glob import glob
from os.path import join, isfile, islink
from ase.atoms import Atoms
from ase.data import atomic_numbers
from ase.units import Bohr, Hartree, fs, Ha, eV
from ase.data import chemical_symbols
from ase.io.abinit import read_abinit
from ase.io.vasp import write_vasp
from ase.calculators.calculator import FileIOCalculator, Parameters, kpts2mp, \
    ReadError,all_changes,Calculator
from pyDFTutils.ase_utils.kpoints import get_ir_kpts, cubic_kpath
from string import Template
from ase.dft.kpoints import special_paths, special_points, parse_path_string, bandpath
from collections import Counter
        # else: dataset2: Gamma phonon
        if efield or strain:
            self.add_dataset(
                iscf=-3,
                getwfk=1,
                kptopt=2,
                rfelfd=2,
                nqpt=1,
                rfphon=0,
                tolvrs=0,
                tolwfr=tolwfr * 10,
                qpt='0 0 0')
            self.add_dataset(
                getwfk=1,
                getddk=self.ndtset,
                kptopt=2,
                rfelfd=3 * int(efield),
                rfstrs=3 * strain,
                nqpt=1,
                iscf=7,
                qpt='0 0 0',
                rfphon=1,
                rfatpol="%s %s" % (1, len(atoms)),
                rfdir='1 1 1',
                tolvrs=tolvrs,
                toldfe=0,
                prtwf=prtwf,
                prtbbb=prtbbb)
        # phonon
        if isinstance(qpts[0], int):

# Path: pyDFTutils/ase_utils/kpoints.py
# def get_ir_kpts(atoms, mesh):
#     """
#     Gamma-centered IR kpoints. mesh : [nk1,nk2,nk3].
#     """
#     lattice = atoms.get_cell()
#     positions = atoms.get_scaled_positions()
#     numbers = atoms.get_atomic_numbers()
# 
#     cell = (lattice, positions, numbers)
#     mapping, grid = spglib.get_ir_reciprocal_mesh(
#         mesh, cell, is_shift=[0, 0, 0])
#     #print("%3d ->%3d %s" % (1, mapping[0], grid[0].astype(float) / mesh))
#     #print("Number of ir-kpoints: %d" % len(np.unique(mapping)))
#     #print(grid[np.unique(mapping)] / np.array(mesh, dtype=float))
#     return grid[np.unique(mapping)] / np.array(mesh, dtype=float)
# 
# def cubic_kpath(npoints=500,name=True):
#     """
#     return the kpoint path for cubic
#     Parameters:
#     ----------------
#     npoints: int
#         number of points.
# 
#     Returns:
#     -----------------
#     kpts:
#         kpoints
#     xs:
#         x cordinates for plot
#     xspecial:
#         x cordinates for special k points
#     """
#     special_path = special_paths['cubic']
#     points = special_points['cubic']
#     paths = parse_path_string(special_path)
#     special_kpts = [points[k] for k in paths[0]]
#     kpts, xs, xspecial = bandpath(
#         special_kpts, cell=np.eye(3), npoints=npoints)
#     if not name:
#         return kpts, xs, xspecial
#     else:
#         return kpts, xs, xspecial,special_path


# Path: pyDFTutils/abinit/myabinit_e.py
import os
import shutil
import numpy as np
import subprocess
import spglib
from glob import glob
from os.path import join, isfile, islink
from ase.atoms import Atoms
from ase.data import atomic_numbers
from ase.units import Bohr, Hartree, fs, Ha, eV
from ase.data import chemical_symbols
from ase.io.abinit import read_abinit
from ase.io.vasp import write_vasp
from ase.calculators.calculator import FileIOCalculator, Parameters, kpts2mp, \
    ReadError,all_changes,Calculator
from pyDFTutils.ase_utils.kpoints import get_ir_kpts, cubic_kpath
from string import Template
from ase.dft.kpoints import special_paths, special_points, parse_path_string, bandpath
from collections import Counter

        # else: dataset2: Gamma phonon
        if efield or strain:
            self.add_dataset(
                iscf=-3,
                getwfk=1,
                kptopt=2,
                rfelfd=2,
                nqpt=1,
                rfphon=0,
                tolvrs=0,
                tolwfr=tolwfr * 10,
                qpt='0 0 0')
            self.add_dataset(
                getwfk=1,
                getddk=self.ndtset,
                kptopt=2,
                rfelfd=3 * int(efield),
                rfstrs=3 * strain,
                nqpt=1,
                iscf=7,
                qpt='0 0 0',
                rfphon=1,
                rfatpol="%s %s" % (1, len(atoms)),
                rfdir='1 1 1',
                tolvrs=tolvrs,
                toldfe=0,
                prtwf=prtwf,
                prtbbb=prtbbb)
        # phonon
        if isinstance(qpts[0], int):

            qpoints = get_ir_kpts(atoms, qpts)