diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed8ebf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ \ No newline at end of file diff --git a/color.py b/color.py new file mode 100644 index 0000000..9680a6e --- /dev/null +++ b/color.py @@ -0,0 +1,45 @@ +import numpy as np + + +def wavelength_to_rgb(wavelength, gamma=0.8): + wavelength *= 10**9 + if wavelength >= 380 and wavelength <= 750: + attenuation = 1.0 + if wavelength < 420: + attenuation = 0.3 + 0.7 * (wavelength - 380) / (420 - 380) + elif wavelength > 700: + attenuation = 0.3 + 0.7 * (750 - wavelength) / (750 - 700) + + if wavelength >= 380 and wavelength < 440: + R = -(wavelength - 440) / (440 - 380) + G = 0.0 + B = 1.0 + elif wavelength >= 440 and wavelength < 490: + R = 0.0 + G = (wavelength - 440) / (490 - 440) + B = 1.0 + elif wavelength >= 490 and wavelength < 510: + R = 0.0 + G = 1.0 + B = -(wavelength - 510) / (510 - 490) + elif wavelength >= 510 and wavelength < 580: + R = (wavelength - 510) / (580 - 510) + G = 1.0 + B = 0.0 + elif wavelength >= 580 and wavelength < 645: + R = 1.0 + G = -(wavelength - 645) / (645 - 580) + B = 0.0 + elif wavelength >= 645 and wavelength <= 750: + R = 1.0 + G = 0.0 + B = 0.0 + else: + R = G = B = 0.0 + + R = (R * attenuation) ** gamma + G = (G * attenuation) ** gamma + B = (B * attenuation) ** gamma + else: + R = G = B = 0.0 + return (R, G, B) diff --git a/main.py b/main.py new file mode 100644 index 0000000..dbd0b23 --- /dev/null +++ b/main.py @@ -0,0 +1,69 @@ +import numpy as np +from mpl_toolkits.mplot3d import Axes3D +import matplotlib.pyplot as plt +from spectro import LightWaveVec, LightDir, diffracted_light +from math import pi, acos, cos, sqrt + +from color import wavelength_to_rgb + +wavelen_min_nm = 380 +wavelen_max_nm = 750 +wavelen_step_nm = 1 +wavelens = [] +for i in range(wavelen_min_nm, wavelen_max_nm, wavelen_step_nm): + wavelens.append(i * 10**-9) + +beta_i = pi/2 +gamma_i = pi/4 +grating_c = 1400 * 10**-9 +cos_alpha_i = sqrt(1 - cos(beta_i)**2 - cos(gamma_i)**2) +alpha_i = acos(cos_alpha_i) +diffraction_orders = [0, 1] + +colorlist = [] + +sorrow_light = [] + +for wavelen in wavelens: + light_i = LightWaveVec(pi * 2 / wavelen, + LightDir(alpha_i, beta_i, gamma_i)) + + """ + """ + colorlist.append((0-cos_alpha_i, 0-cos(beta_i), 0-cos(gamma_i))) + sorrow_light.append([ + -cos_alpha_i, + -cos(beta_i), + -cos(gamma_i), + cos_alpha_i, + cos(beta_i), + cos(gamma_i), + ]) + for m in diffraction_orders: + light_r_list = diffracted_light(light_i, m, grating_c) + if (len(light_r_list) == 2): + print("FOUND 2 SOLUTIONS WHEN m IS ", m, " WAVELEN IS ", wavelen) + elif (len(light_r_list) == 0): + print("FOUND NO SOLUTION WHEN m IS ", m, " WAVELEN IS ", wavelen) + for light_r in light_r_list: + + colorlist.append(wavelength_to_rgb(wavelen)) + sorrow_light .append([0, 0, 0, + cos(light_r.direction.alpha), + cos(light_r.direction.beta), + cos(light_r.direction.gamma), + ]) + +X, Y, Z, U, V, W = zip(*sorrow_light) + +fig = plt.figure() +ax = fig.add_subplot(111, projection="3d") +ax.quiver(X, Y, Z, U, V, W, colors=colorlist, arrow_length_ratio=0) + +ax.set_xlim([-1, 1]) +ax.set_ylim([-1, 1]) +ax.set_zlim([-1, 1]) +ax.set_xlabel('X Label') +ax.set_ylabel('Y Label') +ax.set_zlabel('Z Label') +plt.show() diff --git a/spectro.py b/spectro.py index 9550efb..bfec21b 100644 --- a/spectro.py +++ b/spectro.py @@ -24,26 +24,37 @@ class LightWaveVec: def diffracted_light(incident_light, diffraction_order, grating_constant, is_reflected_grating=True): ans = [] + in_alpha = incident_light.direction.alpha 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(cos_gamma): - if (math.fabs(cos_gamma) <= 1): # to ensure diffracted light can be solved + # I KNOW IT'S NOT OK NOW + def try_diffract(cos_gamma): + + # FLOAT ERROR + if math.fabs(cos_gamma) <= 1 + 1e-12: + cos_gamma = max(-1.0, min(1.0, cos_gamma)) + + if (math.fabs(cos_gamma) <= 1): # to ensure diffracted light can be solved dif_gamma = math.acos(cos_gamma) cos2_alpha = 1 - math.cos(dif_beta)**2 - math.cos(dif_gamma)**2 + # FLOAT ERROR + if cos2_alpha >= -1e-12: + cos2_alpha = max(cos2_alpha, 0.0) + 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))) + math.sqrt(cos2_alpha), math.cos(in_alpha))) else: dif_alpha = math.acos(math.copysign( - math.sqrt(cos2_alpha), math.cos(dif_beta))) + math.sqrt(cos2_alpha), math.cos(in_alpha))) ans.append(LightWaveVec(dif_wavenum, LightDir( dif_alpha, @@ -58,25 +69,33 @@ def diffracted_light(incident_light, diffraction_order, grating_constant, is_ref / 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) + # when diffraction_order is 0, cos_gamma_2 equals to cos_gamma_1 + if (diffraction_order != 0): + 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 +def wavelen(gamma_i, gamma_r, diffraction_order, grating_constant): + return math.fabs(grating_constant + / diffraction_order + * (math.cos(gamma_r) - math.cos(gamma_i))) + + """ 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(2*math.pi/(370*10 ^ -9), +light_i = LightWaveVec(2*math.pi/(370 * 10**-9), LightDir(alpha_i, beta_i, gamma_i)) k = 1 -d = 1400 * 10 ^ -9 +d = 1400 * 10**-9 light_r = LightWaveVec.diffracted_light(light_i, k, d) for l in light_r: