184 lines
3.9 KiB
Python
184 lines
3.9 KiB
Python
from spectro import wavelen
|
|
import matplotlib.pyplot as plt
|
|
from math import pi, cos, acos, sqrt
|
|
from color import wavelength_to_rgb
|
|
|
|
grating_c = 1600 * 10**-9
|
|
|
|
arrow_light = []
|
|
colorlist = []
|
|
|
|
light_len = 30
|
|
|
|
"""
|
|
raw_source_deg = [pi/2, pi*2/3]
|
|
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 = -2
|
|
|
|
|
|
def calc_degs(vec):
|
|
degs = []
|
|
tmp = 0
|
|
for l in vec:
|
|
tmp += l**2
|
|
tmp = sqrt(tmp)
|
|
for l in vec:
|
|
degs.append(acos(l / tmp))
|
|
return degs
|
|
|
|
|
|
delta_min = -800
|
|
delta_max = 1000
|
|
Ne_deltas = [
|
|
-712,
|
|
-641,
|
|
-499,
|
|
-475,
|
|
-433,
|
|
-367,
|
|
-294,
|
|
-282,
|
|
-251,
|
|
-231,
|
|
-207,
|
|
-176,
|
|
-143,
|
|
-130,
|
|
-89,
|
|
-62,
|
|
12,
|
|
25,
|
|
] # Ne
|
|
Ar_deltas = [
|
|
-734,
|
|
-662,
|
|
-263,
|
|
-135,
|
|
-75,
|
|
133,
|
|
183,
|
|
210,
|
|
242,
|
|
289,
|
|
334,
|
|
358,
|
|
381,
|
|
424,
|
|
471,
|
|
513,
|
|
608,
|
|
665,
|
|
702,
|
|
708,
|
|
730,
|
|
] # Ar
|
|
Hg_deltas = [48, 63, 265, 775, 880] # Hg
|
|
H_deltas = [-406, 300, 611, 765]
|
|
full_deltas = range(delta_min, delta_max)
|
|
deltas = Hg_deltas
|
|
|
|
for i in deltas:
|
|
delta_z = i / 100
|
|
|
|
scattered_source = [
|
|
sqrt(1 - cos(raw_source_deg[0]) ** 2 - cos(raw_source_deg[1]) ** 2) * light_len,
|
|
cos(raw_source_deg[0]) * light_len,
|
|
cos(raw_source_deg[1]) * light_len,
|
|
]
|
|
scattered_source_deg = calc_degs(scattered_source)
|
|
arrow_light.append(
|
|
[
|
|
-scattered_source[0],
|
|
-scattered_source[1],
|
|
-scattered_source[2] + delta_z,
|
|
scattered_source[0],
|
|
scattered_source[1],
|
|
scattered_source[2],
|
|
]
|
|
)
|
|
scattered_target = [
|
|
-sqrt(1 - cos(raw_target_deg[0]) ** 2 - cos(raw_target_deg[1]) ** 2)
|
|
* light_len,
|
|
cos(raw_target_deg[0]) * light_len,
|
|
cos(raw_target_deg[1]) * light_len - delta_z,
|
|
]
|
|
scattered_target_deg = calc_degs(scattered_target)
|
|
arrow_light.append(
|
|
[
|
|
0,
|
|
0,
|
|
delta_z,
|
|
scattered_target[0],
|
|
scattered_target[1],
|
|
scattered_target[2],
|
|
]
|
|
)
|
|
|
|
color = wavelength_to_rgb(
|
|
wavelen(
|
|
raw_source_deg[1], scattered_target_deg[2], diffraction_order, grating_c
|
|
)
|
|
)
|
|
|
|
colorlist.append(color)
|
|
colorlist.append(color)
|
|
|
|
|
|
X, Y, Z, U, V, W = zip(*arrow_light)
|
|
|
|
fig = plt.figure(figsize=(10, 9))
|
|
|
|
ax = fig.add_subplot(211, projection="3d")
|
|
ax.quiver(X, Y, Z, U, V, W, colors=colorlist, arrow_length_ratio=0, linewidth=1.5)
|
|
|
|
ax.set_xlim([-light_len, light_len])
|
|
ax.set_ylim([-light_len, light_len])
|
|
ax.set_zlim([-light_len, light_len])
|
|
ax.set_xlabel("X Label")
|
|
ax.set_ylabel("Y Label")
|
|
ax.set_zlabel("Z Label")
|
|
|
|
# ================= 2. 下方离散光谱图 =================
|
|
plt.rcParams["font.sans-serif"] = ["SimHei"]
|
|
plt.rcParams["axes.unicode_minus"] = False
|
|
|
|
ax_spec = fig.add_subplot(212)
|
|
ax_spec.set_title("Spectro")
|
|
|
|
# 【关键修改】按真实数值从小到大排序
|
|
sorted_deltas = sorted(deltas)
|
|
|
|
# 找到这些真实值在原始 deltas 中的原始索引,用来取对应颜色
|
|
# 因为 colorlist 的顺序是和未排序的 deltas 严格对应的
|
|
min_d = min(sorted_deltas)
|
|
max_d = max(sorted_deltas)
|
|
|
|
# 把 X 轴的显示范围稍微留点边距
|
|
margin = (max_d - min_d) * 0.1 if max_d != min_d else 10
|
|
ax_spec.set_xlim(min_d - margin, max_d + margin)
|
|
ax_spec.set_ylim(0, 1)
|
|
|
|
for d in sorted_deltas:
|
|
# 找到当前 d 在原始 deltas 里的位置(原始索引)
|
|
original_idx = deltas.index(d)
|
|
|
|
# 根据原始索引 * 2 取出正确的颜色
|
|
c = colorlist[original_idx * 2]
|
|
|
|
# 用真实的 d 值作为 X 坐标画线
|
|
ax_spec.axvline(x=d, color=c, linewidth=4)
|
|
|
|
# 标注文字
|
|
ax_spec.text(d, 1.05, str(d), ha="center", va="bottom", fontsize=12)
|
|
|
|
ax_spec.set_xticks([])
|
|
ax_spec.set_yticks([])
|
|
ax_spec.set_xlabel("")
|
|
ax_spec.set_ylabel("")
|
|
|
|
plt.tight_layout()
|
|
plt.show()
|