diff --git a/spectro.py b/spectro.py index 6447517..9550efb 100644 --- a/spectro.py +++ b/spectro.py @@ -6,72 +6,81 @@ class LightDir: beta = -1 gamma = -1 - def __init__(self,a,b,g): + def __init__(self, a, b, g): self.alpha = a self.beta = b self.gamma = g - def another_abs(dir1,dir2): - tmp = 1-math.cos(dir1)**2-math.cos(dir2)**2 - if(tmp > 1 or tmp < 0): - return None - return math.acos(math.sqrt(tmp)) - class LightWaveVec: wavenum = 0.0 - direction = LightDir(-1,-1,-1) + direction = LightDir(-1, -1, -1) - def __init__(self,wave_num,wave_dir): + def __init__(self, wave_num, wave_dir): self.wavenum = wave_num self.direction = wave_dir - def diffracted_light(incident_light, diffraction_order, grating_constant): - ans = [] - dif_beta = incident_light.direction.beta - dif_wavenum = incident_light.wavenum +def diffracted_light(incident_light, diffraction_order, grating_constant, is_reflected_grating=True): + ans = [] - # 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 + dif_beta = incident_light.direction.beta + dif_wavenum = incident_light.wavenum - 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 num of gamma - math.acos(math.sqrt(tmp_alpha)), - dif_beta, - -dif_gamma - ))) + # 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 - # 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) + def try_diffract(cos_gamma): + if (math.fabs(cos_gamma) <= 1): # to ensure diffracted light can be solved - tmp_gamma_2 = math.cos(incident_light.direction.gamma) - \ - diffraction_order * \ - (1 / incident_light.wavenum) \ - / grating_constant - try_diffract(tmp_gamma_2) + dif_gamma = math.acos(cos_gamma) + cos2_alpha = 1 - math.cos(dif_beta)**2 - math.cos(dif_gamma)**2 - return ans + if (cos2_alpha >= 0): # to ensure diffracted light can be solved + + if (is_reflected_grating): + dif_alpha = math.acos(- math.copysign( + math.sqrt(cos2_alpha), math.cos(dif_beta))) + else: + dif_alpha = math.acos(math.copysign( + math.sqrt(cos2_alpha), math.cos(dif_beta))) + + ans.append(LightWaveVec(dif_wavenum, LightDir( + dif_alpha, + dif_beta, + dif_gamma + ))) + + # wavelen = 2pi/wavenum + cos_gamma_1 = math.cos(incident_light.direction.gamma) + \ + diffraction_order \ + * (2*math.pi / incident_light.wavenum) \ + / grating_constant + try_diffract(cos_gamma_1) + + cos_gamma_2 = math.cos(incident_light.direction.gamma) - \ + diffraction_order \ + * (2*math.pi / incident_light.wavenum) \ + / grating_constant + try_diffract(cos_gamma_2) + + return ans + + +""" +example: beta_i = 51.9086765665 / 180 * math.pi gamma_i = 104.1539440993 / 180 * math.pi -alpha_i = LightDir.another_abs(beta_i,gamma_i) -light_i = LightWaveVec(1/(370*10^-9),LightDir(alpha_i,beta_i,gamma_i)) +alpha_i = LightDir.another_abs(beta_i, gamma_i) +light_i = LightWaveVec(2*math.pi/(370*10 ^ -9), + LightDir(alpha_i, beta_i, gamma_i)) k = 1 -d = 1400*10^-9 -light_r = LightWaveVec.diffracted_light(light_i,k,d) +d = 1400 * 10 ^ -9 +light_r = LightWaveVec.diffracted_light(light_i, k, d) for l in light_r: - print("alpha_x:",math.cos(l.direction.alpha) ) - print("beta_y:",math.cos(l.direction.beta) ) - print("gamma_z:",math.cos(l.direction.gamma) ) \ No newline at end of file + print("alpha_x:", math.cos(l.direction.alpha)) + print("beta_y:", math.cos(l.direction.beta)) + print("gamma_z:", math.cos(l.direction.gamma)) +"""