codexpast-exam-of-the-materials-science-course-of-the-university-of-cambridge2017paper-1-diffraction.py
past-exam-of-the-materials-science-course-of-the-university-of-cambridge/2017/paper-1-diffraction.py
"""Original diffraction access diagrams; Python 3.14, numpy, matplotlib.
Outputs paper-1-diffraction.png in cwd. No source images are used.
"""
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
fig=plt.figure(figsize=(10,8),layout="constrained")
a=fig.add_subplot(221,projection="3d")
for v,name,col in [([1,0,0],"[100] face normal","#222222"),([1,1,1],"[111] 54.74 deg","#a24871"),([1,1,0],"[220] 45 deg","#a86c1c"),([6,4,0],"[640] 33.69 deg","#336fa5")]:
v=np.array(v,dtype=float);v/=np.linalg.norm(v)
a.quiver(0,0,0,*v,color=col,arrow_length_ratio=.12)
a.text(*(v*1.08),name,color=col,fontsize=8)
a.set(xlim=(0,1.3),ylim=(0,1.2),zlim=(0,1),xlabel="[100]",ylabel="[010]",zlabel="[001]",title="Plane normals relative to cut face")
a.view_init(elev=24,azim=-55)
def ray(ax,start,end,col):ax.annotate("",xy=end,xytext=start,arrowprops=dict(arrowstyle="->",lw=2,color=col))
b=fig.add_subplot(222)
b.axhspan(-.42,0,color="#dddddd");b.axhline(0,color="black")
t=np.deg2rad(15.962)
ray(b,(-1.15*np.cos(t),1.15*np.sin(t)),(0,0),"#336fa5")
ray(b,(0,0),(1.15*np.cos(t),1.15*np.sin(t)),"#b63e3e")
b.annotate("[200] normal",xy=(0,.85),xytext=(.1,.8),fontsize=9)
b.plot([0,0],[0,.8],ls="--",color="black")
b.text(-1.13,.49,"incident");b.text(.48,.49,"diffracted")
b.text(-.95,-.25,"(100) face parallel to (200)",fontsize=9)
b.text(-.95,-.62,"theta = 15.96 deg; scattering = 31.92 deg",fontsize=9)
b.set(xlim=(-1.3,1.3),ylim=(-.75,1),aspect="equal",title="Symmetric front-face reflection")
b.axis("off")
c=fig.add_subplot(223)
t=np.deg2rad(22.887)
u=np.array([1,-1])/np.sqrt(2);n=np.array([1,1])/np.sqrt(2)
poly=np.array([-.95*u-.12*n,.95*u-.12*n,.95*u+.12*n,-.95*u+.12*n])
c.add_patch(Polygon(poly,facecolor="#dddddd",edgecolor="#555555"))
c.plot([-1,1],[0,0],color="#a86c1c",ls="--",label="(220) plane")
ray(c,(-1.15*np.cos(t),1.15*np.sin(t)),(0,0),"#336fa5")
ray(c,(0,0),(1.15*np.cos(t),1.15*np.sin(t)),"#b63e3e")
c.text(-1.2,.76,"incident",fontsize=9);c.text(.65,.76,"exit",fontsize=9)
c.text(-1.2,-.95,"Surface tilt 45 deg exceeds Bragg angle 22.89 deg",fontsize=8.5)
c.text(-1.2,-1.12,"Rays approach/leave opposite slab sides",fontsize=8.5)
c.text(.58,.06,"(220) plane",color="#a86c1c",fontsize=9)
c.set(xlim=(-1.35,1.35),ylim=(-1.25,1),aspect="equal",title="(220) orientation: a transmission path")
c.axis("off")
d=fig.add_subplot(224)
d.axvspan(130,180,color="#eee0dd",label="beyond 130 deg example limit")
for pos,label in [(27.556,"111"),(31.924,"200"),(45.773,"220"),(165.072,"640")]:
d.vlines(pos,0,1,color="#336fa5",lw=2)
d.text(pos,.99,label,rotation=90,ha="right",va="top",fontsize=10)
nom=2*np.rad2deg(np.arcsin(.154*3/(2*.56)))
d.plot(nom,.07,"x",color="#b63e3e",ms=8)
d.text(nom+3,.12,"300 absent",fontsize=9)
d.set(xlim=(10,180),ylim=(0,1.12),yticks=[],xlabel="Scattering angle 2 theta (degrees)",title="Cu K-alpha: angle and extinction tests")
d.text(62,.62,"Marks give positions only,\nnot predicted intensities",fontsize=9)
d.legend(loc="upper center",bbox_to_anchor=(.5,-.2),fontsize=8,frameon=False)
fig.savefig("paper-1-diffraction.png",dpi=155,facecolor="white")