codexintegrating-sphere-escape-and-survival.py
integrating-sphere-escape-and-survival.py
"""Plot the ideal integrating-sphere encounter model, not a detector calibration.
Tested with Python 3.14, NumPy 2.3.5 and Matplotlib 3.10.7.
Run from the media directory; output is written to the current directory.
A photon begins immediately before its first wall interaction. Ports absorb/exit.
"""
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
fig, axs = plt.subplots(1,2,figsize=(9.6,4.2),facecolor='white')
fig.subplots_adjust(left=.075,right=.98,bottom=.2,top=.81,wspace=.3)
fig.suptitle('Repeated reflections give repeated chances to escape',fontsize=14,y=.96)
f=np.linspace(0,.10,401)
for rho,color in [(0.90,'#8a6c40'),(.96,'#245b9c'),(.99,'#36804b')]:
p=rho*f/(1-rho*(1-f))
axs[0].plot(100*f,p,label=fr'$\rho={rho:.2f}$',color=color,lw=2)
rho,f0=.96,.04
q=rho*(1-f0)
p_escape=rho*f0/(1-q)
p_absorb=(1-rho)/(1-q)
axs[0].scatter([100*f0],[p_escape],color='#245b9c',zorder=4)
axs[0].annotate('4% ports: 49.0% escape',xy=(4,p_escape),xytext=(.8,.72),
fontsize=9,arrowprops={'arrowstyle':'->','color':'#444'})
axs[0].set(xlabel='Total port area / sphere area (%)',ylabel='Eventual escape probability',
xlim=(0,10),ylim=(0,1),title='Ideal diffuse wall, absorbing ports')
axs[0].legend(loc='lower right',fontsize=9)
n=np.arange(0,61)
axs[1].plot(n,p_escape*(1-q**n),label='Escaped by encounter n',color='#245b9c',lw=2)
axs[1].plot(n,p_absorb*(1-q**n),label='Absorbed by encounter n',color='#a35a28',lw=2)
axs[1].plot(n,q**n,label='Still inside after encounter n',color='#63666b',lw=2,ls='--')
axs[1].set(xlabel='Completed wall encounters, n',ylabel='Fraction of initial photon ensemble',
xlim=(0,60),ylim=(0,1),title=r'$\rho=0.96,\ f=0.04,\ q=0.9216$')
axs[1].legend(loc='upper right',fontsize=8.5)
for ax in axs:
ax.grid(alpha=.2);ax.spines[['right','top']].set_visible(False)
fig.text(.5,.035,'Starting at a wall encounter: escaped + absorbed + still inside = 1.',
ha='center',fontsize=10)
fig.savefig('integrating-sphere-escape-and-survival.png',dpi=120,facecolor='white')