"""3次元自由電子モデルの状態密度（スピン縮退2を含む）。"""
import numpy as np
import matplotlib.pyplot as plt
from scipy.constants import electron_mass, electron_volt, hbar


def dos(E, EC=0, meff=1):
    """E, EC: eV、meff: 自由電子質量に対する比。

    状態密度を states/(eV m^3) で返す。Eは数値または配列。
    """
    mass = meff * electron_mass
    energy = np.maximum(np.asarray(E) - EC, 0) * electron_volt  # J
    # Jあたりの状態密度を計算し、eVあたりに変換する。
    return (1 / (2 * np.pi**2) * (2 * mass / hbar**2)**1.5
            * np.sqrt(energy) * electron_volt)


def plot(E, D):
    plt.figure()
    plt.plot(E, D)
    plt.xlabel("Energy (eV)")
    plt.ylabel("DOS (states / (eV m$^3$))")
    plt.title("3D free-electron density of states")
    plt.grid()
    plt.tight_layout()
    plt.show()


def main():
    EC = 0
    Emin, Emax = -1, 2
    E = np.linspace(Emin, Emax, 501)
    D = dos(E, EC)
    plot(E, D)


if __name__ == "__main__":
    main()
