Files
spectro/angle_overlap.py
T

136 lines
5.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""衍射光角度重叠图:各衍射级次在可见光波段内覆盖的衍射角区间随入射角的变化。
坐标约定(与 spectro.py 的方向余弦一致):
光栅位于 yOz 平面,刻线沿 y,周期方向为 z,法线为 +x;
方向角 (α, β, γ) 是光线与 +x / +y / +z 轴的夹角;
γ 是光线与 +z 轴(+z 在光栅平面内)的夹角,它不是入射角、也不是衍射角。
真正的入射角、衍射角自光栅法线量起:
θ_i = π/2 - γ_i , θ_r = π/2 - γ_r .
光栅方程 cos γ_r = cos γ_i + mλ/d 随之写成
sin θ_r = sin θ_i + mλ/d .
"""
import os
import sys
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Patch
plt.rcParams["font.sans-serif"] = ["Microsoft YaHei", "SimHei", "DejaVu Sans"]
plt.rcParams["axes.unicode_minus"] = False
# ---------------- 参数 ----------------
d = 1600e-9 # 光栅常数 1.6 μm
LAM_MIN, LAM_MAX = 380e-9, 750e-9
orders = [-4, -3, -2, -1, 0, 1, 2, 3]
theta_i_deg = np.linspace(10.0, 80.0, 561)
theta_i = np.radians(theta_i_deg)
def band(m):
"""级次 m 在每条 θ_i 上覆盖的 θ_r 区间(无解处为 nan)。
sin θ_r 随 λ 单调,区间端点取 λ 两端;|sin θ_r| 超出 1 的部分截到 ±90°,
端点连续,不会因 λ 采样而出现锯齿。
"""
s1 = np.sin(theta_i) + m * LAM_MIN / d
s2 = np.sin(theta_i) + m * LAM_MAX / d
lo_s, hi_s = np.minimum(s1, s2), np.maximum(s1, s2)
valid = (lo_s <= 1.0) & (hi_s >= -1.0)
lo = np.degrees(np.arcsin(np.clip(lo_s, -1.0, 1.0)))
hi = np.degrees(np.arcsin(np.clip(hi_s, -1.0, 1.0)))
lo[~valid] = np.nan
hi[~valid] = np.nan
return lo, hi
bands = {m: band(m) for m in orders}
FILL = {-4: "#cfe3f5", -3: "#9dc6e8", -2: "#6aa6d6", -1: "#2f6f9f",
1: "#f7c59f", 2: "#f08b6a", 3: "#d1495b"}
EDGE = {-4: "#8fb8dc", -3: "#6aa6d6", -2: "#3f7fac", -1: "#1f4e79",
1: "#e07b39", 2: "#c94f36", 3: "#a4243b"}
fig, ax = plt.subplots(figsize=(12.0, 7.4), dpi=110)
# ---------------- 各衍射级次的角区间 ----------------
for m in orders:
if m == 0:
ax.plot(theta_i_deg, theta_i_deg, ls="--", lw=1.2, color="#666666",
label="m = 0", zorder=4)
continue
lo, hi = bands[m]
ax.fill_between(theta_i_deg, lo, hi, color=FILL[m], alpha=0.55,
linewidth=0, zorder=2)
ax.plot(theta_i_deg, lo, color=EDGE[m], lw=1.1, zorder=3)
ax.plot(theta_i_deg, hi, color=EDGE[m], lw=1.1, zorder=3)
# ---------------- 相邻级次的重叠区 ----------------
# 只有当两个级次在该 θ_i 下都存在时,区间相交才算重叠。
pairs = [(a, b) for a, b in zip(orders[:-1], orders[1:]) if a != 0 and b != 0]
OV_COLOR = {(-4, -3): "#8e6bb0", (-3, -2): "#3f9d8f", (-2, -1): "#5b8fc9",
(1, 2): "#e08a3c", (2, 3): "#c0392b"}
legend_extra = []
for a, b in pairs:
lo_a, hi_a = bands[a]
lo_b, hi_b = bands[b]
both = np.isfinite(lo_a) & np.isfinite(lo_b)
lo = np.where(both, np.maximum(lo_a, lo_b), np.nan)
hi = np.where(both, np.minimum(hi_a, hi_b), np.nan)
mask = both & np.isfinite(lo) & np.isfinite(hi) & (hi > lo)
if not mask.any():
continue
ax.fill_between(theta_i_deg, lo, hi, where=mask, facecolor=OV_COLOR[(a, b)],
alpha=0.40, hatch="///", edgecolor=OV_COLOR[(a, b)],
linewidth=0, zorder=5)
legend_extra.append((f"{a:+d} / {b:+d}", OV_COLOR[(a, b)]))
# ---------------- 级次标签 ----------------
for m in orders:
if m == 0:
continue
lo, hi = bands[m]
idx = np.where(np.isfinite(lo))[0]
if idx.size == 0:
continue
j = idx[0] + (idx[-1] - idx[0]) // 2
y = 0.5 * (lo[j] + hi[j])
y = min(max(y, -86.0), 86.0)
ax.text(theta_i_deg[j], y, f"m = {m:+d}", ha="center", va="center",
fontsize=10.5, color=EDGE[m], fontweight="bold", zorder=6,
bbox=dict(boxstyle="round,pad=0.18", fc="white", ec="none"))
# ---------------- 坐标轴 ----------------
ax.set_xlim(10, 80)
ax.set_ylim(-90, 90)
ax.set_xlabel(r"入射角 $\theta_i=\pi/2-\gamma_i$ (°)", fontsize=13)
ax.set_ylabel(r"衍射角 $\theta_r=\pi/2-\gamma_r$ (°)", fontsize=13)
ax.set_title("各级衍射光的角度范围与重叠", fontsize=15, pad=12)
ax.axhline(0, color="#999999", lw=0.8, ls=":", zorder=1)
ax.grid(alpha=0.18, zorder=0)
# ---------------- 图例(级次已标在图上,这里只需解释重叠区与 m=0) ----------------
handles = [Patch(facecolor="white", edgecolor=c, hatch="///",
label=f"{t} 重叠") for t, c in legend_extra]
handles.append(plt.Line2D([], [], ls="--", color="#666666", label="m = 0"))
ax.legend(handles=handles, loc="upper center", bbox_to_anchor=(0.5, -0.10),
ncol=len(handles), fontsize=9.5, frameon=False)
fig.tight_layout()
out = sys.argv[1] if len(sys.argv) > 1 else os.path.join(
os.path.dirname(os.path.abspath(__file__)), "angle_overlap.png")
fig.savefig(out, bbox_inches="tight")
print("saved", out)
# ---------------- 数值自检 ----------------
print("\n重叠判据: 级次 m 与 m+1 有重叠 <=> λ_max >= (m+1)/m * λ_min")
lam_lo, lam_hi = LAM_MIN * 1e9, LAM_MAX * 1e9
for a, b in pairs:
need = b / a * lam_lo if a > 0 else (a / b) * lam_lo
print(f" m={a:+d}/{b:+d}: 需要 λ_max >= {abs(need):6.1f} nm -> "
f"{'重叠' if lam_hi >= abs(need) else '不重叠'}")
plt.show()