Skip to content

Commit

Permalink
codacy changes
Browse files Browse the repository at this point in the history
  • Loading branch information
phoebe-p committed Sep 21, 2024
1 parent 9ff868f commit f86e2cf
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 39 deletions.
9 changes: 9 additions & 0 deletions docs/Examples/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ Full list of GitHub examples
These examples are available in the `GitHub repository <https://github.com/qpv-research-group/rayflare/tree/devel/examples>`_; a list
is provided here to give an overview of what is covered in each example.

TMM
#####

1.

Ray-tracing
#############

RCWA
######

.. _solcore-education: https://qpv-research-group.github.io/solcore-education/solcore-workshop-2/schedule.html
1 change: 0 additions & 1 deletion examples/analytical_rt_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from rayflare.textures import regular_pyramids, planar_surface
from rayflare.ray_tracing import rt_structure
from solcore import material
import seaborn as sns
from rayflare.options import default_options
from solcore.structure import Layer
from time import time
Expand Down
3 changes: 0 additions & 3 deletions examples/phong_scattering_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
d = 100e-6
# setting up Solcore materials
Air = material("Air")()

from solcore.absorption_calculator import download_db, search_db

Si = material("Si")()
SiN = material("Si3N4")()

Expand Down
148 changes: 125 additions & 23 deletions examples/rt_pyramids.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
#!/usr/bin/env python
# coding: utf-8

# # Ray-tracing with regular pyramids
#
# This example shows how to define a structure, and the form of the calculation outputs, for a silicon wafer with pyramid texturing on the front surface and a planar rear surface, and compares the results with those of a well-known wafer ray-tracer (PVLighthouse). The absorption profile is also calculated automatically.

# In[25]:


import numpy as np

from rayflare.ray_tracing import rt_structure
Expand All @@ -11,15 +21,23 @@
import matplotlib.pyplot as plt
import seaborn as sns


# We define the relevant materials. In this case, we are going to compare the results at the end with ray-tracing results from PVLighthouse's wafer ray tracer, so we want to make sure we are using exactly the same optical constant data. For this, we will use Solcore's ability to download and pull materials from the refractiveindex.info database. First we download the database (if you've already done this previously you will be prompted to ask if you would like to re-download it) and then we search it for the Green 2008 silicon optical constant data. We then define a Solcore material by referencing this search result (for this version of the database, it is the entry with page id 566). We then set some options: the number of rays to trace at each wavelengths, how many x/y points to scan across, the wavelengths, and that the computation should be done in parallel (by default this will use all the available cores).

# In[26]:


# setting up some colours for plotting
pal = sns.color_palette("husl", 4)


# setting up Solcore materials
Air = material("Air")()

from solcore.absorption_calculator import download_db, search_db
from solcore.absorption_calculator import search_db

# download refractiveindex.info database if necessary
# from solcore.absorption_calculator import download_db
# download_db()
Si_Green2008 = search_db("Green-2008")[0][0]
Si = material(str(Si_Green2008), nk_db=True)()
Expand All @@ -36,46 +54,130 @@
options.depth_spacing_bulk = si("0.1um")
options.parallel = True


# Define the structure: the front is made of inverted regular triangles (upright=False) with an opening angle of 55 degrees and a size of 2 microns. The back surface is planar. The rt_structure class takes a list of textures and a list of materials; there must always be one more entry in the list of textures than the number of materials (the incidence and transmission media are specified separately). We then calculate the reflection, transmission, and absorption per layer. The absorption profile will also be calculated because for ray-tracing this requires essentially no additional work, since it only uses Beer-Lambert like absorption.

# In[27]:


flat_surf = planar_surface(size=2) # pyramid size in microns
triangle_surf = regular_pyramids(55, upright=False, size=2,
analytical=True, phong=False,
phong_options=[25, True])
triangle_surf = regular_pyramids(55, upright=False, size=2)

# set up ray-tracing options
rtstr = rt_structure(
textures=[triangle_surf, flat_surf],
materials=[Si],
widths=[si("100um")],
widths=[si("300um")],
incidence=Air,
transmission=Air,
)

result = rtstr.calculate(options)

plt.figure()
plt.plot(options.wavelength*1e9, result['R'], label='R')
plt.plot(options.wavelength*1e9, result['A_per_layer'], label='A_bulk')
plt.plot(options.wavelength*1e9, result['T'], label='T')

# Plot results, and compare them with PVLighthouse ray-tracing results for the same structure.

# In[28]:


PVlighthouse = np.loadtxt("data/RAT_data_300um_2um_55.csv", delimiter=",", skiprows=1)

fig = plt.figure(figsize=(9, 3.7))
plt.subplot(1, 2, 1)
plt.plot(
options.wavelength * 1e9,
result["R"],
"-o",
color=pal[0],
label=r"R$_{total}$",
fillstyle="none",
)
plt.plot(
options.wavelength * 1e9,
result["R0"],
"-o",
color=pal[1],
label=r"R$_0$",
fillstyle="none",
)
plt.plot(
options.wavelength * 1e9,
result["T"],
"-o",
color=pal[2],
label=r"T",
fillstyle="none",
)
plt.plot(
options.wavelength * 1e9,
result["A_per_layer"][:, 0],
"-o",
color=pal[3],
label=r"A",
fillstyle="none",
)
plt.plot(PVlighthouse[:, 0], PVlighthouse[:, 2], "--", color=pal[0])
plt.plot(PVlighthouse[:, 0], PVlighthouse[:, 9], "--", color=pal[2])
plt.plot(PVlighthouse[:, 0], PVlighthouse[:, 3], "--", color=pal[1])
plt.plot(PVlighthouse[:, 0], PVlighthouse[:, 5], "--", color=pal[3])
plt.title("a)", loc="left")
plt.plot(-1, -1, "-ok", label="RayFlare")
plt.plot(-1, -1, "--k", label="PVLighthouse")
plt.xlabel("Wavelength (nm)")
plt.ylabel("R / A / T")
plt.ylim(0, 1)
plt.xlim(300, 1200)

plt.legend()

A_single_pass = 1 - np.exp(-200e-6 * Si.alpha(options.wavelength))
A_single_pass_PVL = 1 - np.exp(-200e-6 * Si.alpha(PVlighthouse[:, 0] / 1e9))
lambertian = 4 * Si.n(options.wavelength) ** 2

plt.subplot(1, 2, 2)
plt.plot(
options.wavelength * 1e9,
result["A_per_layer"][:, 0] / A_single_pass,
"-k",
label="RayFlare raytracer",
)
plt.plot(
PVlighthouse[:, 0],
PVlighthouse[:, 5] / A_single_pass_PVL,
"--b",
label="PVLighthouse",
)
plt.legend(loc="upper left")
plt.xlabel("Wavelength (nm)")
plt.ylabel("Path length enhancement")
plt.xlim(300, 1200)
plt.title("b)", loc="left")
plt.show()

# make histogram of theta for transmitted rays only

transmitted_indices = [result['thetas'][i] > np.pi/2 for i in range(len(result))]
# Plotting the absorption profile at wavelengths bigger than about 1000 nm:

angle_bins = np.linspace(0, np.pi, 41)
# In[29]:

theta_dist = np.array([np.histogram(result['thetas'][i1],
bins=40, range=(0, np.pi), density=True)[0]
for i1 in range(len(options.wavelength))])

theta_dist = theta_dist[options.wavelength*1e9 > 900]
theta_dist = theta_dist[:, theta_dist.shape[1]//2:]
min_wl = 1000
ind = np.argmin(np.abs(options.wavelength * 1e9 - min_wl))

z_pos = np.arange(0, 300, options.depth_spacing_bulk * 1e6)

plt.figure()
plt.imshow(theta_dist, aspect='auto', cmap='viridis', extent=(np.pi/2, np.pi, 900, options.wavelength[-1]*1e9))
plt.xlabel('Theta (rad)')
plt.ylabel('Wavelength (nm)')
plt.colorbar()
plt.title('Theta distribution of transmitted rays')
plt.show()
pcm = plt.pcolormesh(
z_pos, options.wavelength[ind:] * 1e9, result["profile"][ind:, :], shading="auto"
)
plt.colorbar(pcm)
plt.gca().invert_yaxis()
plt.xlabel(r"Depth ($\mu$m)")
plt.ylabel("Wavelength (nm)")
plt.show()


# In[29]:




5 changes: 0 additions & 5 deletions rayflare/ray_tracing/analytical_rt.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,6 @@ def analytical_start(nks,

while single_direction:

normals = surfaces[surf_index].N

n0 = nks[mat_i]
n1 = nks[next_mat]

I_rem_data = I_remaining.data[0]

R_data, A_data, T_data, R_pol, T_pol, A_int_detail = analytical_per_face(surfaces[surf_index],
Expand Down
3 changes: 1 addition & 2 deletions rayflare/ray_tracing/rt_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
from joblib import Parallel, delayed
from copy import deepcopy
from solcore.state import State
from scipy.spatial.transform import Rotation
from numba import jit

from rayflare.utilities import get_savepath, get_wavelength, process_pol
from rayflare.utilities import get_savepath, get_wavelength
from rayflare.transfer_matrix_method.lookup_table import make_TMM_lookuptable
from rayflare import logger
from .analytical_rt import analytical_start, dummy_prop_rays
Expand Down
6 changes: 1 addition & 5 deletions tests/test_analytical_rt.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def test_total_RAT_TMM():
from rayflare.options import default_options
from solcore.structure import Layer
import numpy as np
from pytest import approx

Si = material("Si")()
Air = material("Air")()
Expand Down Expand Up @@ -88,7 +87,6 @@ def test_total_RAT_TMM():
widths=[50e-6, 100e-6],
incidence=Air, transmission=Air,
use_TMM=True, options=options,
save_location="current",
overwrite=True,
)

Expand Down Expand Up @@ -261,7 +259,7 @@ def test_phong_scattering():
# to do two surfaces to check.

from rayflare.ray_tracing import rt_structure
from rayflare.textures import regular_pyramids, planar_surface
from rayflare.textures import planar_surface
from solcore import material
from rayflare.options import default_options

Expand All @@ -278,8 +276,6 @@ def test_phong_scattering():
max_angle = np.arccos((5e-3)**(1/alpha))

x = np.linspace(0, max_angle, 6)
# analytical power law:
power_law = np.cos(x)**alpha

options.nx = 10
options.ny = 10
Expand Down

0 comments on commit f86e2cf

Please sign in to comment.