commit bc625a9e7b335fdb97821260e3e1f4515f80482a Author: StarCells Date: Wed Oct 7 23:27:03 2026 +0800 First commit diff --git a/spectro.py b/spectro.py new file mode 100644 index 0000000..2747015 --- /dev/null +++ b/spectro.py @@ -0,0 +1,49 @@ +import math + + +class LightDir: + alpha = -1 + beta = -1 + gamma = -1 + + +class LightWaveVec: + wavenum = 0.0 + direction = LightDir() + + def diffracted_light(incident_light, diffraction_order, grating_constant): + ans = [] + + dif_beta = incident_light.direction.beta + dif_wavenum = incident_light.wavenum + + # The case of tmp_gamma == 1 or tmp_alpha == 0 are almost invisible in matplotlib + # I guess it's OK to ignore them when they appear as 1.0000001 or -0.000001 + + def try_diffract(tmp_gamma): + if (math.fabs(tmp_gamma) <= 1): # to ensure diffracted light can be solved + dif_gamma = math.acos(tmp_gamma) + tmp_alpha = 1-math.cos(dif_beta)**2 - math.cos(dif_gamma) ** 2 + if (tmp_alpha >= 0): # to ensure diffracted light can be solved + # TODO: EXPLAIN THEIR PHYSICAL MEANING + ans.append(LightWaveVec(dif_wavenum, LightDir( + # cos(alpha) **2 + cos(beta) **2 + cos(gamma) **2 = 1 + # For it's a reflective grating, take the opposite + - math.acos(math.sqrt(tmp_alpha)), + dif_beta, + dif_gamma + ))) + + # wavelen = 1/wavenum + tmp_gamma_1 = math.cos(incident_light.direction.gamma) + diffraction_order * \ + (1 / incident_light.wavenum) / \ + grating_constant + try_diffract(tmp_gamma_1) + + tmp_gamma_2 = math.cos(incident_light.direction.gamma) - \ + diffraction_order * \ + (1 / incident_light.wavenum) \ + / grating_constant + try_diffract(tmp_gamma_2) + + return ans