Added a image to show how diffracted light overlaps

This commit is contained in:
starcells committed 2026-11-15 12:41:59 +08:00
1 parent f94ef31434
commit 76d5f09732
8 files changed
+181 -4

No files matched your search

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 144 KiB

+135
View File
@@ -0,0 +1,135 @@
# -*- 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()
+2 -2
View File
@@ -16,7 +16,7 @@ raw_target_deg = [pi/2, pi*2/3]
"""
raw_source_deg = [pi / 2, pi / 3] # beta=90°, gamma=120°
raw_target_deg = [pi / 2, pi / 6] # beta=90°, gamma=100°
diffraction_order = 1
diffraction_order = -2
def calc_degs(vec):
@@ -78,7 +78,7 @@ Ar_deltas = [
Hg_deltas = [48, 63, 265, 775, 880] # Hg
H_deltas = [-406, 300, 611, 765]
full_deltas = range(delta_min, delta_max)
deltas = full_deltas
deltas = Hg_deltas
for i in deltas:
delta_z = i / 100
+42
View File
@@ -0,0 +1,42 @@
import matplotlib.pyplot as plt
# 1. 准备数据
orders = ["1", "2", "3", "4", "5"]
intensities = [1.000, 0.309, 0.0075, 0.0381, 0.0359]
# 2. 设置中文字体和图形大小
plt.rcParams["font.sans-serif"] = ["SimHei", "Arial Unicode MS", "sans-serif"]
plt.rcParams["axes.unicode_minus"] = False
plt.rcParams["mathtext.fontset"] = "cm" # 使用 LaTeX 风格的数学字体
fig, ax = plt.subplots(figsize=(8, 4), dpi=150)
# 3. 绘制柱状图
bars = ax.bar(orders, intensities, color="#4A5568", edgecolor="black", width=0.6)
# 4. 设置坐标轴和样式
ax.set_title("级次与相对强度关系", fontsize=14, pad=15)
ax.set_xlabel("m", fontsize=12)
# 【核心修改点】加上 r 前缀和 $ 符号
ax.set_ylabel(r"$(I_m / I_1)$", fontsize=12, rotation=0, labelpad=25)
# 5. 在柱子上方添加数值标签
for bar in bars:
yval = bar.get_height()
ax.text(
bar.get_x() + bar.get_width() / 2,
yval + 0.02,
f"{yval:.4f}" if yval < 0.1 else f"{yval:.3f}",
ha="center",
va="bottom",
fontsize=10,
)
# 6. 优化视觉呈现
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.set_ylim(0, 1.2)
plt.tight_layout()
plt.show()
+2 -2
View File
@@ -8,18 +8,18 @@ from color import wavelength_to_rgb
wavelen_min_nm = 380
wavelen_max_nm = 750
wavelen_step_nm = 1
"""
wavelens = [404.66e-9, 435.83e-9, 546.07e-9, 576.96e-9, 579.00e-9]
"""
wavelens = []
for i in range(wavelen_min_nm, wavelen_max_nm, wavelen_step_nm):
wavelens.append(i * 10**-9)
"""
light_len = 36
grating_c = 1600 * 10**-9
diffraction_orders = [-2, -1]
diffraction_orders = [-2, -1, 0]
# Order that rended earlier might be covered by other orders
# So lower order should be placed at the tail
Binary file not shown.

After

Width:  |  Height:  |  Size: 139 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB