-
Notifications
You must be signed in to change notification settings - Fork 11
/
test_ray_tracing.py
323 lines (242 loc) · 10.4 KB
/
test_ray_tracing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
from pytest import approx
import numpy as np
import itertools
def test_parallel():
from rayflare.ray_tracing import rt_structure
from rayflare.textures import regular_pyramids
from rayflare.options import default_options
from solcore import material
from solcore import si
Air = material("Air")()
Si = material("Si")()
GaAs = material("GaAs")()
Ge = material("Ge")()
triangle_surf = regular_pyramids(30)
options = default_options()
options.wavelength = np.linspace(700, 1700, 5) * 1e-9
options.theta_in = 45 * np.pi / 180
options.nx = 5
options.ny = 5
options.pol = "p"
options.n_rays = 3000
options.depth_spacing = 1e-6
options.parallel = True
rtstr = rt_structure(
textures=[triangle_surf, triangle_surf, triangle_surf, triangle_surf],
materials=[GaAs, Si, Ge],
widths=[si("100um"), si("70um"), si("50um")],
incidence=Air,
transmission=Air,
)
result_new = rtstr.calculate(options)
options.parallel = False
rtstr = rt_structure(
textures=[triangle_surf, triangle_surf, triangle_surf, triangle_surf],
materials=[GaAs, Si, Ge],
widths=[si("100um"), si("70um"), si("50um")],
incidence=Air,
transmission=Air,
)
result_old = rtstr.calculate(options)
assert sorted(list(result_old.keys())) == sorted(list(result_new.keys()))
rel_error = 0.3 # Monte Carlo (random) simulations so will never be exactly equal)
assert result_new["R"] == approx(result_old["R"], rel=rel_error)
assert result_new["R0"] == approx(result_old["R0"], rel=rel_error)
assert result_new["A_per_layer"] == approx(result_old["A_per_layer"], rel=rel_error)
assert result_new["profile"] == approx(result_old["profile"], rel=rel_error)
assert np.nanmean(result_new["thetas"], 1) == approx(np.nanmean(result_old["thetas"], 1), rel=rel_error)
# assert np.nanmean(result_new['phis'], 1) == approx(np.nanmean(result_old['phis'], 1), rel=rel_error)
assert np.nanmean(result_new["n_interactions"], 1) == approx(
np.nanmean(result_old["n_interactions"], 1), rel=rel_error
)
assert np.nanmean(result_new["n_passes"], 1) == approx(np.nanmean(result_old["n_passes"], 1), rel=rel_error)
assert result_new["R"] + result_new["T"] + np.sum(result_new["A_per_layer"], 1) == approx(1, rel=options.I_thresh)
assert result_old["R"] + result_old["T"] + np.sum(result_old["A_per_layer"], 1) == approx(1, rel=options.I_thresh)
def test_flip():
from rayflare.ray_tracing import rt_structure
from rayflare.textures import regular_pyramids
from rayflare.options import default_options
from solcore import material
from solcore import si
Air = material("Air")()
Si = material("Si")()
GaAs = material("GaAs")()
Ge = material("Ge")()
triangle_surf = regular_pyramids(70)
options = default_options()
options.wavelength = np.linspace(700, 1700, 8) * 1e-9
options.nx = 5
options.ny = 5
options.pol = "s"
options.n_rays = 200
options.parallel = True
rtstr_1 = rt_structure(
textures=[triangle_surf, triangle_surf, triangle_surf, triangle_surf],
materials=[GaAs, Si, Ge],
widths=[si("100um"), si("70um"), si("50um")],
incidence=Air,
transmission=Air,
)
result_up = rtstr_1.calculate(options)
options.initial_direction = -1
options.initial_material = 4
triangle_surf = regular_pyramids(70, upright=False)
rtstr_2 = rt_structure(
textures=[triangle_surf, triangle_surf, triangle_surf, triangle_surf],
materials=[Ge, Si, GaAs],
widths=[si("50um"), si("70um"), si("100um")],
incidence=Air,
transmission=Air,
)
result_down = rtstr_2.calculate(options)
abs_error = 0.1
assert result_up["R"] + result_up["T"] + np.sum(result_up["A_per_layer"], 1) == approx(1, rel=options.I_thresh)
assert result_down["R"] + result_down["T"] + np.sum(result_down["A_per_layer"], 1) == approx(
1, rel=options.I_thresh
)
assert result_up["T"] == approx(result_down["R"], abs=abs_error)
assert result_up["A_per_layer"][:, 0] == approx(result_down["A_per_layer"][:, 2], abs=abs_error)
assert result_up["A_per_layer"][:, 1] == approx(result_down["A_per_layer"][:, 1], abs=abs_error)
assert result_up["A_per_layer"][:, 2] == approx(result_down["A_per_layer"][:, 0], abs=abs_error)
def test_periodic():
from rayflare.ray_tracing import rt_structure
from rayflare.textures import regular_pyramids
from rayflare.options import default_options
from solcore import material
Air = material("Air")()
Si = material("Si")()
triangle_surf = regular_pyramids(60)
options = default_options()
options.wavelength = np.linspace(700, 1700, 8) * 1e-9
options.nx = 5
options.ny = 5
options.pol = "s"
options.n_rays = 200
options.parallel = True
options.periodic = True
rtstr_1 = rt_structure(textures=[triangle_surf], materials=[], widths=[], incidence=Air, transmission=Si)
result_periodic = rtstr_1.calculate(options)
options.periodic = False
rtstr_2 = rt_structure(textures=[triangle_surf], materials=[], widths=[], incidence=Air, transmission=Si)
result_single = rtstr_2.calculate(options)
assert result_periodic["R"] + result_periodic["T"] + np.sum(result_periodic["A_per_layer"], 1) == approx(
1, rel=options.I_thresh
)
assert result_single["R"] + result_single["T"] + np.sum(result_single["A_per_layer"], 1) == approx(
1, rel=options.I_thresh
)
assert np.all(result_periodic["R"] >= result_single["R"])
assert np.all(result_single["R"] == 0)
def test_interface_absorption():
from rayflare.ray_tracing import rt_structure
from solcore import material
from solcore.structure import Layer
from rayflare.options import default_options
from rayflare.textures import planar_surface
opts = default_options()
opts.project_name = "method_comparison_test_prof"
opts.n_rays = 1000
opts.parallel = False
# opts.nx = 5
# opts.ny = 5
thetas_in = np.linspace(0, 1.2, 3)
pol_in = ["s", "p", "u"]
d_Ge = 50e-6
d_Si = 30e-6
GaAs = material("GaAs")()
Air = material("Air")()
Si = material("Si")()
Ge = material("Ge")()
MgF2 = material("MgF2")()
Ta2O5 = material("TaOx1")()
ITO = material("ITO2")()
Ag = material("Ag")()
front_layers = [Layer(100e-9, MgF2), Layer(50e-9, Ta2O5), Layer(502e-9, GaAs)]
back_layers = [Layer(100e-9, ITO), Layer(50e-9, Ag)]
front_surf = planar_surface(interface_layers=front_layers, prof_layers=[3]) # pyramid size in microns
back_surf = planar_surface(interface_layers=back_layers, prof_layers=[1, 2]) # pyramid size in microns
middle_surf = planar_surface()
opts.wavelengths = np.linspace(300, 1900, 5) * 1e-9
opts.depth_spacing = 1e-9
opts.depth_spacing_bulk = 1e-8
# opts.parallel = False
rtstr = rt_structure([front_surf, middle_surf, back_surf], [Si, Ge], [d_Si, d_Ge], Air, Air, opts, use_TMM=True)
# import matplotlib.pyplot as plt
for angle_pol in itertools.product(thetas_in, pol_in):
print(angle_pol)
opts.theta_in = angle_pol[0]
opts.pol_in = angle_pol[1]
rt_res = rtstr.calculate(opts)
total_A = np.concatenate(
(
rt_res["A_per_layer"],
rt_res["A_per_interface"][0],
rt_res["A_per_interface"][2],
rt_res["R"][:, None],
rt_res["T"][:, None],
),
axis=1,
)
prof_int_front = np.trapz(rt_res["interface_profiles"][0], dx=1, axis=1)
prof_int_back = np.trapz(rt_res["interface_profiles"][2], dx=1, axis=1)
prof_int_bulk = np.trapz(rt_res["profile"], dx=10, axis=1)
assert prof_int_bulk == approx(np.sum(rt_res["A_per_layer"], 1), abs=0.01)
assert prof_int_front == approx(np.sum(rt_res["A_per_interface"][0], 1), abs=0.01)
assert prof_int_back == approx(np.sum(rt_res["A_per_interface"][2], 1), abs=0.01)
# plt.figure()
# plt.plot(opts.wavelengths*1e9, total_A)
# plt.legend(["Si", "Ge", "MGF2", "Ta2O5", "GaAs", "ITO", "Ag", "R", "T"])
# plt.show()
#
# plt.figure()
# plt.plot(rt_res["interface_profiles"][0].T)
# plt.show()
#
# plt.figure()
# plt.plot(rt_res["interface_profiles"][2].T)
# plt.show()
assert np.sum(total_A, 1) == approx(1, abs=opts.I_thresh)
def test_random_position():
from rayflare.ray_tracing import rt_structure
from rayflare.textures import regular_pyramids
from solcore import material
from rayflare.options import default_options
options = default_options()
options.n_rays = 20000
options.nx = 50
options.ny = 50
options.wavelength = np.array([500e-9])
Si = material("Si")()
Air = material("Air")()
triangle_surf = regular_pyramids()
rtstr_1 = rt_structure([triangle_surf], [], [], Air, Si)
options.random_ray_position = False
res_regular = rtstr_1.calculate(options)
options.random_ray_position = True
res_random = rtstr_1.calculate(options)
assert res_regular["R"] == approx(res_random["R"], rel=0.05)
assert res_regular["T"] == approx(res_random["T"], rel=0.05)
assert np.mean(res_regular["n_interactions"]) == approx(np.mean(res_random["n_interactions"]), rel=0.05)
def test_inverted():
from rayflare.ray_tracing import rt_structure
from rayflare.textures import regular_pyramids
from solcore import material
from rayflare.options import default_options
options = default_options()
options.n_rays = 20000
options.nx = 50
options.ny = 50
options.wavelength = np.array([500e-9])
Si = material("Si")()
Air = material("Air")()
triangle_surf = regular_pyramids(elevation_angle=40, upright=True)
triangle_surf_inverted = regular_pyramids(elevation_angle=40, upright=False)
rtstr_1 = rt_structure([triangle_surf], [], [], Air, Si)
res_upright = rtstr_1.calculate(options)
rtstr_2 = rt_structure([triangle_surf_inverted], [], [], Si, Air)
options.initial_material = 1
options.initial_direction = -1
res_inverted = rtstr_2.calculate(options)
assert res_inverted["R"] == approx(res_upright["T"], rel=0.03)
assert res_inverted["T"] == approx(res_upright["R"], rel=0.03)
assert np.mean(res_inverted["n_interactions"]) == approx(np.mean(res_upright["n_interactions"]), rel=0.03)