from IPython.display import Image
Image('/Users/kanhua/Dropbox/Programming/pypvcell examples/si_back_reflector.png')

"""
Explanation: Introduction
This script calculates the potential increase of photocurrent. The question that I want to resolve in this calculation is: does any kind of back reflector help the absorption of crystalline silicon?
Method
I assume we have a 100%-reflectivity mirror attached at the back side of silicon substrate. The structure is like this:
End of explanation
"""


%matplotlib inline
from pypvcell.photocurrent import conv_abs_to_qe,calc_jsc
from pypvcell.illumination import Illumination
from pypvcell.spectrum import Spectrum
import numpy as np
import matplotlib.pyplot as plt


abs_file = "/Users/kanhua/Dropbox/Programming/pypvcell/legacy/si_alpha.csv"

si_alpha = np.loadtxt(abs_file, delimiter=',')
si_alpha_sp = Spectrum(si_alpha[:,0],si_alpha[:,1],'m')

layer_t=np.logspace(-8,-3,num=100)
jsc_baseline=np.zeros(layer_t.shape)

jsc_full_r=np.zeros(layer_t.shape)

it=np.nditer(layer_t,flags=['f_index'])
ill=Illumination("AM1.5g")

def filter_spec(ill):
    ill_a=ill.get_spectrum(to_x_unit='eV',to_photon_flux=True)
    ill_a=ill_a[:,ill_a[0,:]>1.1]
    ill_a=ill_a[:,ill_a[0,:]<1.42]
    ill_a[1,1]=0
    return Spectrum(ill_a[0,:],ill_a[1,:],'eV',y_unit='m**-2',is_spec_density=True,is_photon_flux=False)

#ill=filter_spec(ill)

while not it.finished:

    t=it[0] #thickness of Si layer

    qe=conv_abs_to_qe(si_alpha_sp,t)
    jsc_baseline[it.index]=calc_jsc(ill, qe)

    # Assme 100% reflection on the back side, essentially doubling the thickness of silicon
    qe_full_r=conv_abs_to_qe(si_alpha_sp,t*2)
    jsc_full_r[it.index]=calc_jsc(ill,qe_full_r)

    it.iternext()
it.reset()

"""
Explanation: We use experimental absorption coefficients of crystalline silicon and assume that the absorptivity follows Beer-Lambert's law. Also, we assume that every abosorped photons can be converted into electrons. In this case, having a 100% reflector on the back side of silicon can be thought of as doubling the thickness of silicon substrate.
Calculation
End of explanation
"""


plt.semilogx(layer_t*1e6, jsc_baseline,hold=True,label="Si")
plt.semilogx(layer_t*1e6,jsc_full_r,label="Si+100% mirror")
plt.xlabel("thickness of Si substrate (um)")
plt.ylabel("Jsc (A/m^2)")
plt.legend(loc="best")

"""
Explanation: Photocurrent with and without the back reflector
End of explanation
"""


plt.semilogx(layer_t*1e6,jsc_full_r/jsc_baseline)
plt.xlabel("thickness of Si substrate (um)")
plt.ylabel("Normalized Jsc enhancement")
plt.savefig("jsc_enhancement.pdf")
plt.show()

"""
Explanation: Normlize the Jsc(Si+mirror) by Jsc(Si only)
End of explanation
"""


# more detailed investigation
plt.semilogx(layer_t*1e6,jsc_full_r/jsc_baseline)
plt.xlabel("thickness of Si substrate (um)")
plt.ylabel("Jsc enhancement (2x)")
plt.xlim([100,1000])
plt.ylim([1.0,1.5])
plt.show()

"""
Explanation: We can see that the back reflector can be very effective when the thickness of silicon substrate is thin (< 1um). Silicon substrates with more than 10-um thicknesses cannot be benefited from this structure very well. This is the reason that photonic or plasmonic structure are useful for thin-film or ultra-thin-film silicon cell, but not conventional bulk crystalline silicon cell.
End of explanation
"""


while not it.finished:

    t=it[0] #thickness of Si layer

    qe=conv_abs_to_qe(si_alpha_sp,t)
    jsc_baseline[it.index]=calc_jsc(Illumination("AM1.5g"), qe)

    # Assme 100% reflection on the back side, essentially doubling the thickness of silicon
    qe_full_r=conv_abs_to_qe(si_alpha_sp,t*10)
    jsc_full_r[it.index]=calc_jsc(Illumination("AM1.5g"),qe_full_r)

    it.iternext()
it.reset()

plt.semilogx(layer_t*1e6,jsc_full_r/jsc_baseline)
plt.xlabel("thickness of Si substrate (um)")
plt.ylabel("Jsc enhancement (10x)")
plt.show()

"""
Explanation: More audacious assumption
Assume that somehow we have a novel reflector that can increase the optical absorption length by 10 times.
End of explanation
"""


