Applications

Band gap estimation by charge doping

Fit the total energy as a function of the total charge with a quadratic and estimate the band gap from its second derivative.

Category
Applications
APIs used
total_chargeget_potential_energynumpy.polynomial.Polynomial
Script
band_gap.py

AOF-DFT does not compute bands extending over the whole system, but the band gap can be estimated from the energy change on electron and hole doping. The total energy per formula unit is computed for charges q per formula unit from −1 to +1 and fitted with a quadratic E(q)=aq2+bq+c; its second derivative 2a corresponds to the band gap.

band_gap.pyDownload
"""
Advance/OF-DFT example: band gap estimated from the total energy of doped systems.

The charge per formula unit is varied and the energy per formula unit is fitted
with a quadratic function. Its second derivative corresponds to the band gap.

Copyright (C) 2026 AdvanceSoft Corporation, All rights reserved.
"""

import matplotlib.pyplot as plt
import numpy as np
from ase.build import bulk
from numpy.polynomial import Polynomial

from aofdft.calculator import AOFDFTCalculator

VPS_DIR = "path/to/DFT_DATA19_AS/VPS"  # edit these two paths for your environment
PAO_DIR = "path/to/DFT_DATA19_AS/PAO"

# 2x2x2 repetitions of the primitive cell (16 atoms, 8 formula units)
materials = {
    "AlSb": bulk("AlSb", "zincblende", a=6.14).repeat((2, 2, 2)),
    "AlP" : bulk("AlP", "zincblende", a=5.46).repeat((2, 2, 2)),
    # you can add more materials
    # "CdSe": bulk("CdSe", "wurtzite", a=4.30, c=7.01).repeat((2, 2, 2)),
    # "CsCl": bulk("CsCl", "cesiumchloride", a=4.12).repeat((2, 2, 2)),
}

charges = np.arange(-1.0, 1.1, 0.5)  # charge per formula unit: -1.0 ... +1.0

for name, atoms in materials.items():
    print(f"--- {name} ---")
    n_units  = len(atoms) // 2  # number of formula units in the cell
    energies = []

    for charge in charges:
        atoms.calc = AOFDFTCalculator(
            console      = False,
            logging_file = f"{name}_{charge:+.1f}.log",
            kin_type     = "AS26",
            kin_model    = "Crystal26",
            vps_dir      = VPS_DIR,
            pao_dir      = PAO_DIR,
            total_charge = charge * n_units,  # negative: electron doping, positive: hole doping
        )
        energies.append(atoms.get_potential_energy() / n_units)  # per formula unit
        print(f"charge = {charge:+.1f}  E = {energies[-1]:.4f} eV")

    # energy relative to the neutral system, fitted by E = a q^2 + b q + c
    relative = np.array(energies) - energies[len(charges) // 2]
    fit      = Polynomial.fit(charges, relative, deg=2)
    c, b, a  = fit.convert().coef

    # the second derivative of the quadratic function corresponds to the band gap
    print(f"Band gap of {name}: {2.0 * a:.2f} eV\n")

    q = np.linspace(-1.0, 1.0, 100)
    plt.scatter(charges, relative, label=f"{name} ($E_g$ = {2.0 * a:.2f} eV)")
    plt.plot(q, fit(q), linestyle="--")

plt.xlabel("Charge per formula unit (e)")
plt.ylabel("Relative energy (eV)")
plt.legend()
plt.tight_layout()
plt.savefig("band_gap_EvsCharge.png", dpi=150)
print("Saved band_gap_EvsCharge.png")

Example output

text
--- AlSb ---
charge = -1.0  E = -280.7054 eV
charge = -0.5  E = -278.3154 eV
charge = +0.0  E = -275.7067 eV
charge = +0.5  E = -272.8815 eV
charge = +1.0  E = -269.8410 eV
Band gap of AlSb: 0.87 eV

--- AlP ---
charge = -1.0  E = -263.9126 eV
charge = -0.5  E = -261.2678 eV
charge = +0.0  E = -258.3360 eV
charge = +0.5  E = -255.1064 eV
charge = +1.0  E = -251.5717 eV
Band gap of AlP: 1.19 eV

Saved band_gap_EvsCharge.png

Comparison with the case study

The values below are reported in the case study Band Gap Analysis. The qualitative ordering of the band gaps is reproduced correctly.

MaterialAOF-DFT (eV)KS-DFT / OpenMX (eV)KS-DFT / Materials Project (eV)
CdSe0.270.100.56
AlSb0.800.831.23
AlP1.651.751.63
CsCl6.093.614.99

The AOF-DFT values in the table were calculated with the primitive cells. They do not agree with the output of this example, which uses 16-atom cells (AlSb 0.87 eV, AlP 1.19 eV).

Relative energy versus total charge with quadratic fits (from the case study).
Relative energy versus total charge with quadratic fits (from the case study).

Notes

  • total_charge is the net charge of the system: negative for electron doping, positive for hole doping. Fractional values are allowed. The script multiplies the charge per formula unit by the number of formula units in the cell (8). The printed energies are also per formula unit.
  • The cells are 2×2×2 repetitions of the primitive cells (16 atoms). With v1.0.0 the results change when the cell has a short period, as the primitive cell (2 atoms) does.
  • The fit uses energies relative to the neutral system, with NumPy's Polynomial.fit.
  • Energies at different charges are compared with one another, so keep the cutoff energy and other settings identical.