/
tmm.py
executable file
·107 lines (78 loc) · 3.79 KB
/
tmm.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
from solcore.structure import Layer, Junction, TunnelJunction
import solcore.analytic_solar_cells as ASC
from solcore.absorption_calculator import calculate_rat, OptiStack, calculate_absorption_profile
import numpy as np
import types
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
def solve_tmm(solar_cell, options):
""" Calculates the reflection, transmission and absorption of a solar cell object using the transfer matrix method
:param solar_cell:
:param options:
:return:
"""
wl = options.wavelength
# We include the shadowing losses
initial = (1 - solar_cell.shading) if hasattr(solar_cell, 'shading') else 1
# Now we calculate the absorbed and transmitted light. We first get all the relevant parameters from the objects
widths = []
offset = 0
all_layers = []
for j, layer_object in enumerate(solar_cell):
# Attenuation due to absorption in the AR coatings or any layer in the front that is not part of the junction
if type(layer_object) is Layer:
all_layers.append(layer_object)
widths.append(layer_object.width)
# For each junction, and layer within the junction, we get the absorption coefficient and the layer width.
elif type(layer_object) in [TunnelJunction, Junction]:
junction_width = 0
try:
for i, layer in enumerate(layer_object):
all_layers.append(layer)
junction_width += layer.width
widths.append(layer.width)
solar_cell[j].width = junction_width
except TypeError as err:
print('ERROR in "solar_cell_solver: TMM solver":\n'
'\tNo layers found in Junction or TunnelJunction objects.')
raise err
solar_cell[j].offset = offset
offset += layer_object.width
# With all the information, we create the optical stack
no_back_reflexion = options.no_back_reflexion if 'no_back_reflexion' in options.keys() else True
stack = OptiStack(all_layers, no_back_reflexion=no_back_reflexion)
dist = np.logspace(0, np.log10(offset*1e9), int(300*np.log10(offset*1e9)))
position = options.position if 'position' in options.keys() else dist
print('Calculating RAT...')
RAT = calculate_rat(stack, wl * 1e9, coherent=True)
print('Calculating absorption profile...')
out = calculate_absorption_profile(stack, wl * 1e9, dist=position)
# With all this information, we are ready to calculate the differential absorption function
diff_absorption, all_absorbed = calculate_absorption_tmm(out)
# Each building block (layer or junction) needs to have access to the absorbed light in its region.
# We update each object with that information.
for j in range(len(solar_cell)):
solar_cell[j].diff_absorption = diff_absorption
solar_cell[j].absorbed = types.MethodType(absorbed, solar_cell[j])
solar_cell.reflected = RAT['R'] * initial
solar_cell.transmitted = (1-RAT['R']-all_absorbed) * initial
solar_cell.absorbed = all_absorbed * initial
def absorbed(self, z):
out = self.diff_absorption(self.offset + z) * (z < self.width)
return out.T
def calculate_absorption_tmm(tmm_out):
all_z = tmm_out['position']*1e-9
all_abs = tmm_out['absorption']/1e-9
def diff_absorption(z):
idx = all_z.searchsorted(z)
idx = np.where(idx <= len(all_z)-2, idx, len(all_z)-2)
try:
z1 = all_z[idx]
z2 = all_z[idx + 1]
f = (z - z1) / (z2 - z1)
out = f * all_abs[:, idx] + (1 - f) * all_abs[:, idx + 1]
except IndexError:
out = all_abs[:, idx]
return out
all_absorbed = np.trapz(diff_absorption(all_z), all_z)
return diff_absorption, all_absorbed