Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@syhaffert
Copy link
Collaborator

I added code to calculate the Fresnel transmission and reflection coefficients for isotropic media.

@codecov
Copy link

codecov bot commented Jan 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.14%. Comparing base (6cd4fc2) to head (d73be96).
Report is 6 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #255      +/-   ##
==========================================
+ Coverage   83.11%   83.14%   +0.03%     
==========================================
  Files         104      104              
  Lines        7765     7779      +14     
==========================================
+ Hits         6454     6468      +14     
  Misses       1311     1311              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ehpor ehpor force-pushed the fresnel_coefficients branch from 92b9917 to d843923 Compare June 27, 2025 05:34
@ehpor
Copy link
Owner

ehpor commented Jun 27, 2025

Small test script:

import hcipy
import matplotlib.pyplot as plt
import numpy as np

n1 = 1.5
n2 = 1
angle_deg = np.linspace(0, 90, 1000)
angle = np.radians(angle_deg)

rs, rp = hcipy.fresnel_reflection_coefficients(n1, n2, angle)
ts, tp = hcipy.fresnel_transmission_coefficients(n1, n2, angle)

# Magnitudes and phases
def mag_phase(x):
    return np.abs(x), np.unwrap(np.angle(x))

mag_rs, phase_rs = mag_phase(rs)
mag_rp, phase_rp = mag_phase(rp)
mag_ts, phase_ts = mag_phase(ts)
mag_tp, phase_tp = mag_phase(tp)

# Plotting
fig, axs = plt.subplots(2, 2, figsize=(12, 8), sharex=True)

axs[0, 0].plot(angle_deg, mag_rs, label='|r_s|')
axs[0, 0].plot(angle_deg, mag_rp, label='|r_p|')
axs[0, 0].set_ylabel('Reflection Coefficient Magnitude')
axs[0, 0].legend()
axs[0, 0].grid()

axs[0, 1].plot(angle_deg, np.degrees(phase_rs), label='arg(r_s)')
axs[0, 1].plot(angle_deg, np.degrees(phase_rp), label='arg(r_p)')
axs[0, 1].set_ylabel('Reflection Coefficient Phase (deg)')
axs[0, 1].legend()
axs[0, 1].grid()

axs[1, 0].plot(angle_deg, mag_ts, label='|t_s|')
axs[1, 0].plot(angle_deg, mag_tp, label='|t_p|')
axs[1, 0].set_xlabel('Angle of Incidence (deg)')
axs[1, 0].set_ylabel('Transmission Coefficient Magnitude')
axs[1, 0].legend()
axs[1, 0].grid()

axs[1, 1].plot(angle_deg, np.degrees(phase_ts), label='arg(t_s)')
axs[1, 1].plot(angle_deg, np.degrees(phase_tp), label='arg(t_p)')
axs[1, 1].set_xlabel('Angle of Incidence (deg)')
axs[1, 1].set_ylabel('Transmission Coefficient Phase (deg)')
axs[1, 1].legend()
axs[1, 1].grid()

plt.tight_layout()
plt.show()

image

@ehpor
Copy link
Owner

ehpor commented Jun 27, 2025

@syhaffert Does this look correct?

@syhaffert
Copy link
Collaborator Author

syhaffert commented Jul 1, 2025

I checked for power conservation using the following code block in your script. And the power is conserved to machine precision. I also see the correct behavior when we travel from lower density to higher density (n1 =1 and n2=1.5). So, yes I think everything is correct.

import hcipy
import matplotlib.pyplot as plt
import numpy as np

n1 = 1.0
n2 = 1.5
angle_deg = np.linspace(0, 90, 1000)
angle = np.radians(angle_deg)

rs, rp = hcipy.fresnel_reflection_coefficients(n1, n2, angle)
ts, tp = hcipy.fresnel_transmission_coefficients(n1, n2, angle)

# Calculate the refracted angle
sin_t = n1 / n2 * np.sin(angle)
cos_t = np.real(np.sqrt(1 - sin_t**2 + 0j))
Ts = abs(ts)**2 * n2 / n1 * cos_t / np.cos(angle)
Rs = abs(rs)**2

Tp = abs(tp)**2 * n2 / n1 * cos_t / np.cos(angle)
Rp = abs(rp)**2

# Magnitudes and phases
def mag_phase(x):
    return np.abs(x), np.angle(x)

mag_rs, phase_rs = mag_phase(rs)
mag_rp, phase_rp = mag_phase(rp)
mag_ts, phase_ts = mag_phase(ts)
mag_tp, phase_tp = mag_phase(tp)

fig, axs1 = plt.subplots(1, 2, figsize=(12, 4))
axs1[0].plot(angle_deg, Ts, label='Ts')
axs1[0].plot(angle_deg, Rs, label='Rs')
axs1[0].plot(angle_deg, Tp, label='Tp', ls='--', color='C0')
axs1[0].plot(angle_deg, Rp, label='Rp', ls='--', color='C1')
axs1[0].legend()
axs1[0].grid()
axs1[0].set_xlabel('Angle of Incidence (deg)')
axs1[0].set_ylabel('Power transmission')
axs1[0].set_ylim(-0.03, 1.03)

axs1[1].plot(angle_deg, Ts + Rs, label='Ts + Rs')
axs1[1].plot(angle_deg, Tp + Rp, label='Tp + Rp', ls='--')
axs1[1].legend()
axs1[1].grid()
axs1[1].set_xlabel('Angle of Incidence (deg)')
axs1[1].set_ylabel('Power transmission')
axs1[1].set_ylim(-0.03, 1.03)

# Plotting
fig, axs = plt.subplots(2, 2, figsize=(12, 8), sharex=True)

axs[0, 0].plot(angle_deg, mag_rs, label='|r_s|')
axs[0, 0].plot(angle_deg, mag_rp, label='|r_p|')
axs[0, 0].set_ylabel('Reflection Coefficient Magnitude')
axs[0, 0].legend()
axs[0, 0].grid()

axs[0, 1].plot(angle_deg, np.degrees(phase_rs), label='arg(r_s)')
axs[0, 1].plot(angle_deg, np.degrees(phase_rp), label='arg(r_p)')
axs[0, 1].set_ylabel('Reflection Coefficient Phase (deg)')
axs[0, 1].legend()
axs[0, 1].grid()

axs[1, 0].plot(angle_deg, mag_ts, label='|t_s|')
axs[1, 0].plot(angle_deg, mag_tp, label='|t_p|')
axs[1, 0].set_xlabel('Angle of Incidence (deg)')
axs[1, 0].set_ylabel('Transmission Coefficient Magnitude')
axs[1, 0].legend()
axs[1, 0].grid()

axs[1, 1].plot(angle_deg, np.degrees(phase_ts), label='arg(t_s)')
axs[1, 1].plot(angle_deg, np.degrees(phase_tp), label='arg(t_p)')
axs[1, 1].set_xlabel('Angle of Incidence (deg)')
axs[1, 1].set_ylabel('Transmission Coefficient Phase (deg)')
axs[1, 1].legend()
axs[1, 1].grid()

plt.tight_layout()
plt.show()

@ehpor ehpor force-pushed the fresnel_coefficients branch from 0bdf4ba to d73be96 Compare July 1, 2025 08:53
@ehpor ehpor enabled auto-merge July 1, 2025 08:53
@ehpor ehpor merged commit ac31947 into master Jul 1, 2025
16 checks passed
@ehpor ehpor deleted the fresnel_coefficients branch July 7, 2025 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants