jones: removed scanrefangle, scanplateangle
[physicspy.git] / physicspy / optics / jones.py
blob9ab1353597b9195d65b8c371888b502f56866647
1 #!/usr/bin/env python
2 from __future__ import division
3 from numpy import sqrt, cos, sin, arctan, exp, abs, pi, conj
4 from scipy import array, dot, sum
6 class JonesVector:
7 """ A Jones vector class to represent polarized EM waves """
8 def __init__(self,Jarray=array([1,0])):
9 self.Jx = Jarray[0]
10 self.Jy = Jarray[1]
12 def size(self):
13 """ Jones vector size """
14 return sqrt(dot(self.toArray().conj(),self.toArray()).real)
16 def normalize(self):
17 """ Normalized Jones vector """
18 result = self
19 try:
20 size = result.size()
21 if size == 0:
22 raise Exception('Zero-sized Jones vector cannot be normalized')
23 result.Jx /= size
24 result.Jy /= size
25 except Exception as inst:
26 print "Error: ",inst
27 finally:
28 return result
30 def toArray(self):
31 """ Convert into array format """
32 return array([self.Jx, self.Jy])
34 def rotate(self,phi):
35 """ Rotated Jones vector
37 Argument:
38 phi - rotation angle in radians (clockwise is positive)
39 """
40 R = array([[cos(phi), sin(phi)], \
41 [-sin(phi), cos(phi)]])
42 return JonesVector(dot(R, self.toArray()))
44 def waveplate(self,G):
45 """ Waveplate with arbitrary retardance
46 Slow axis (or "c axis") is along X
48 Argument:
49 G - retartandance in phase units
50 (e.g. one wavelength retardance is G = 2 * pi)
51 """
52 W0 = array([[exp(-1j*G/2), 0], \
53 [0, exp(1j*G/2)]])
54 return JonesVector(dot(W0, self.toArray()))
56 def waveplateRot(self,phi,G):
57 """ Waveplate matrix with arbitrary rotation
59 Arguments:
60 phi - rotation angle in radians
61 (clockwise is positive)
62 G - retardance in phase units
63 (e.g. one wavelength retardance is G = 2 * pi)
64 """
65 return self.rotate(phi).waveplate(G).rotate(-phi)
67 def pol(self,phi):
68 """ Polarizer matrix """
69 P = array([[cos(phi)**2, cos(phi)*sin(phi)], \
70 [sin(phi)*cos(phi), sin(phi)**2]])
71 return JonesVector(dot(P, self.toArray()))
73 def mirrormetal(self,n,k,th):
74 """ Reflection off a metal mirror
75 Incoming and reflected beams are assumed to be in the X plane
76 """
77 dr = mphase(n,k,th);
78 W0 = array([[dr[3]*exp(-1j*dr[1]), 0],\
79 [0, dr[2]*exp(-1j*dr[0])]])
80 return JonesVector(dot(W0, self.toArray()))
82 def intensity(self):
83 """ Intensity from electric field vector """
84 return real(self.Jx)**2 + real(self.Jy)**2
87 def mphase(n,k,th):
88 """ Calculate phase shift and reflectance of a metal in the s and p directions"""
89 u = sqrt(0.5 *((n**2 - k**2 - sin(th)**2) + sqrt( (n**2 - k**2 - sin(th)**2)**2 + 4*n**2*k**2 )))
90 v = sqrt(0.5*(-(n**2 - k**2 - sin(th)**2) + sqrt( (n**2 - k**2 - sin(th)**2)**2 + 4*n**2*k**2 )))
91 ds = arctan(2*v*cos(th)/(u**2+v**2-cos(th)**2));
92 dp = arctan(2*v*cos(th)*(n**2-k**2-2*u**2)/(u**2+v**2-(n**2+k**2)**2*cos(th)**2));
93 if(dp < 0):
94 dp = dp+pi;
95 rs = abs((cos(th) - (u+v*1j))/(cos(th) + (u+v*1j)))
96 rp = abs(((n**2 + k**2)*cos(th) - (u+v*1j))/((n**2 + k**2)*cos(th) + (u+v*1j)));
97 return array([ds, dp, rs, rp])