"""
概要:
    銅(Cu)の比熱の温度依存性を計算しプロットするスクリプトです。

詳細説明:
    低温領域の比熱、高温領域のShomate方程式による比熱、およびDebyeモデルによる比熱を計算し、
    Dulong-Petitの極限値とともにMatplotlibでグラフ化します。定数はNIST Chemistry WebBookなどを
    参考にしています。

関連リンク:
    Cp-T-Cu_usage
"""
# NIST Chemistry WebBook: https://webbook.nist.gov/chemistry/
#    Cu: https://webbook.nist.gov/cgi/cbook.cgi?Formula=Cu&MatchIso=on&NoIon=on&Units=SI&cTC=on

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import quad

# 定数
R = 8.314  # J/mol·K
theta_D = 343.5  # Debye温度 (K)
dulong_petit = 3 * R  # ≈ 24.94 J/mol·K

# 低温比熱パラメータ（mJ → J）
gamma = 0.695e-3  # J/mol·K^2
beta = 0.468e-5   # J/mol·K^4

# Shomate係数
A = 17.72891
B = 28.09870
C = -31.25289
D = 13.97243
E = 0.068611

# Debye積分関数
def debye_integral(x):
    """
    概要:
        Debye比熱モデルにおける被積分関数の値を計算します。

    引数:
        :param x: 無次元化された積分変数。
        :type x: float または numpy.ndarray

    戻り値:
        :returns: 被積分関数の計算結果。
        :rtype: float または numpy.ndarray
    """
    return (x**4 * np.exp(x)) / (np.exp(x) - 1)**2

def debye_heat_capacity(T):
    """
    概要:
        指定された温度におけるDebyeモデルに基づく比熱を計算します。

    詳細説明:
        温度Tが0の場合は0を返します。それ以外の場合は、scipy.integrate.quadを用いて
        debye_integralを積分し、Debye比熱を算出します。

    引数:
        :param T: 絶対温度。
        :type T: float

    戻り値:
        :returns: 計算されたDebye比熱。
        :rtype: float
    """
    if T == 0:
        return 0
    x_max = theta_D / T
    integral, _ = quad(debye_integral, 0, x_max)
    return 9 * R * (T / theta_D)**3 * integral

# 温度範囲
T_low = np.linspace(0.1, 50, 200)
T_high = np.linspace(298, 1356, 300)
T_debye = np.linspace(1, 500, 300)

# 比熱計算
C_low = gamma * T_low + beta * T_low**3
t = T_high / 1000
C_high = A + B*t + C*t**2 + D*t**3 + E/t**2
C_debye = np.array([debye_heat_capacity(T) for T in T_debye])

# プロット
plt.figure(figsize=(10, 6))
plt.plot(T_low, C_low, label='Low-T Heat Capacity (γT + βT³)', color='blue')
plt.plot(T_high, C_high, label='High-T Heat Capacity (Shomate)', color='red')
plt.plot(T_debye, C_debye, label='Debye Model (θ_D = 343.5 K)', color='green', linestyle='--')
plt.axhline(y=dulong_petit, color='gray', linestyle='--', label='Dulong-Petit Limit (~24.94 J/mol·K)')

# ラベル・凡例・タイトル
plt.xlabel('Temperature T (K)')
plt.ylabel('Heat Capacity C (J/mol·K)')
plt.title('Copper Heat Capacity vs Temperature')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()