def get_3rdO_corr(defect, charge=None, n=100, epsilon=1e0):
    """ Function returns scaled 3rd order image charge correction
    Reference: S. Lany and A. Zunger, Phys. Rev. B 78, 235104 (2008)
               S. Lany and A. Zunger, Model. Simul. Mater. Sci. Eng. 17, 0842002 (2009)
               [Eq. 6, 7 and 11]
                                                                                                                         
    Parameters
        defect = pylada.vasp.Extract object
        charge = charge of point defect. Default 1e0 elementary charge
        epsilon = dimensionless relative permittivity

    Returns
        scaled (1-1/epsilon) third image correction in eV

    Note:
        1. Function is adopted from Haowei Peng's version in pylada.defects modules
    """

    from quantities import elementary_charge, eV, pi, angstrom
    from pylada.physics import a0, Ry

    ## Pgraf's port of the old c-version. (Haowei comments could be faster)
    ## Anuj_05/22/18: Modified to "third_order" in pylada.crystal.defects
    from pylada.crystal.defects import third_order

    if charge is None: charge = 1e0
    elif charge == 0: return 0e0 * eV
    if hasattr(charge, "units"):
        charge = float(charge.rescale(elementary_charge))
    if hasattr(epsilon, "units"): epsilon = float(epsilon.simplified)

    structure = defect.structure

    cell = (structure.cell * structure.scale).rescale(a0)

    #Anuj_05/22/18: modified to "third_order"
    scaled_3rdO = third_order(cell, n) * (4e0*pi/3e0)* Ry.rescale(eV) * charge * charge \
        *(1e0- 1e0/epsilon) / epsilon

    return scaled_3rdO
def thirdO(latt_vec_array, charge, n):
    """ Function returns 3rd order image charge correction, same as LZ fortran script
    Reference: S. Lany and A. Zunger, Phys. Rev. B 78, 235104 (2008)
               S. Lany and A. Zunger, Model. Simul. Mater. Sci. Eng. 17, 0842002 (2009)
               [Eq. 6, 7]

    Parameters
        defect = pylada.vasp.Extract object
        charge = charge of point defect. Default 1e0 elementary charge
        n = precision in integral of Eq. 7 (LZ 2009), larger the better

    Returns
        third image correction in eV
    """

    cell_scale = 1.0  # SKW: In notebook workflow cell parameters are converted to Cartesians and units of Angstroms
    cell = (latt_vec_array * cell_scale) * angstrom.rescale(a0)

    #Anuj_05/22/18:modified to "third_order"
    thirdO = third_order(
        cell, n) * (4e0 * pi / 3e0) * Ry.rescale(eV) * charge * charge

    return thirdO
def thirdO(defect, charge=None, n=100):
    """ Function returns 3rd order image charge correction, same as LZ fortran script
    Reference: S. Lany and A. Zunger, Phys. Rev. B 78, 235104 (2008)
               S. Lany and A. Zunger, Model. Simul. Mater. Sci. Eng. 17, 0842002 (2009)
               [Eq. 6, 7]

    Parameters
        defect = pylada.vasp.Extract object
        charge = charge of point defect. Default 1e0 elementary charge
        n = precision in integral of Eq. 7 (LZ 2009), larger the better

    Returns
        third image correction in eV
    """

    from quantities import elementary_charge, eV, pi, angstrom
    from pylada.physics import a0, Ry

    ## Pgraf's port of the old c-version. Could be faster
    ## Anuj_05/22/18: modified to "third_order" in pylada.crystal.defects
    from pylada.crystal.defects import third_order

    if charge is None: charge = 1e0
    elif charge == 0: return 0e0 * eV
    if hasattr(charge, "units"):
        charge = float(charge.rescale(elementary_charge))

    structure = defect.structure

    cell = (structure.cell * structure.scale).rescale(a0)

    #Anuj_05/22/18:modified to "third_order"
    thirdO = third_order(
        cell, n) * (4e0 * pi / 3e0) * Ry.rescale(eV) * charge * charge

    return thirdO