gaussian_beams.py 6.32 KB
 Daniel Brown committed Dec 05, 2013 1 2 3 import pykat.exceptions as pkex import numpy import math  4 import copy  5 from pykat.SIfloat import SIfloat  Daniel Brown committed Dec 05, 2013 6 7 8 9 10 11 12 13 14 15 16 17  class gauss_param(object): """ Gaussian beam complex parameter gauss_param is effectively a complex number with extra functionality to determine beam parameters. defaults to 1064e-9m for wavelength and refractive index 1 usage: q = gauss_param(w0=w0, z=z) q = gauss_param(z=z, zr=zr)  Andreas Freise committed Jan 16, 2014 18  q = gauss_param(w=w, rc=rc)  19  q = gauss_param(q=a) # where a is a complex number  Daniel Brown committed Dec 05, 2013 20 21 22 23 24 25 26 27  or change default wavelength and refractive index with: q = gauss_param(wavelength, nr, w0=w0, zr=zr) """ def __init__(self, wavelength=1064e-9, nr=1, *args, **kwargs): self.__q = None  Daniel Brown committed May 02, 2014 28 29 30  print "wavelength", wavelength, kwargs self.__lambda = SIfloat(wavelength) self.__nr = SIfloat(nr)  Daniel Brown committed Dec 05, 2013 31 32  if len(args) == 1:  Daniel Brown committed Mar 02, 2014 33  self.__q = complex(args[0])  34 35 36 37 38  elif len(kwargs) == 1: if "q" in kwargs: self.__q = complex(kwargs["q"]) else:  Andreas Freise committed Jan 16, 2014 39  raise pkex.BasePyKatException("Must specify: z and w0 or z and zr or rc and w or q, to define the beam parameter")  40   Daniel Brown committed Dec 05, 2013 41 42 43  elif len(kwargs) == 2: if "w0" in kwargs and "z" in kwargs:  44  q = SIfloat(kwargs["z"]) + 1j *float(math.pi*SIfloat(kwargs["w0"])**2/(self.__lambda/self.__nr) )  Daniel Brown committed Dec 05, 2013 45  elif "z" in kwargs and "zr" in kwargs:  46  q = SIfloat(kwargs["z"]) + 1j *SIfloat(kwargs["zr"])  Andreas Freise committed Jan 16, 2014 47  elif "rc" in kwargs and "w" in kwargs:  48  one_q = 1 / SIfloat(kwargs["rc"]) - 1j * self.__lamda / (math.pi * self.__nr * SIfloat(kwargs["w"])**2)  Daniel Brown committed Dec 05, 2013 49 50  q = 1/one_q else:  Andreas Freise committed Jan 16, 2014 51  raise pkex.BasePyKatException("Must specify: z and w0 or z and zr or rc and w or q, to define the beam parameter")  Daniel Brown committed Dec 05, 2013 52 53 54 55 56 57 58  self.__q = q else: raise pkex.BasePyKatException("Incorrect usage for gauss_param constructor") @property def wavelength(self): return self.__lambda  Daniel Brown committed May 02, 2014 59 60  @wavelength.setter def wavelength(self,value): self.__lambda = SIfloat(value)  Daniel Brown committed Dec 05, 2013 61 62 63 64 65 66 67 68 69 70 71 72 73 74  @property def nr(self): return self.__nr @property def q(self): return self.__q @property def z(self): return self.__q.real @property def zr(self): return self.__q.imag @property  Andreas Freise committed Jan 09, 2014 75  def w(self):  Andreas Freise committed Jan 16, 2014 76 77  return abs(self.__q)*math.sqrt(self.__lambda / (self.__nr * math.pi * self.__q.imag)) #return self.w0 * math.sqrt(1 + (self.__q.real/self.__q.imag)**2)  Daniel Brown committed Dec 05, 2013 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102  @property def w0(self): return math.sqrt(self.__q.imag * self.__lambda / (self.__nr * math.pi)) @property def Rc(self): if self.__q.real != 0: return abs(self.__q) / self.__q.real else: return float("inf") def conjugate(self): return gauss_param(self.__lambda, self.__nr, self.__q.conjugate()) def __complex__(self): return self.__q def __str__(self): return str(self.__q) def __mul__(self, a): return gauss_param(self.__lambda, self.__nr, self.__q * complex(a)) def __imul__(self, a):  Daniel Brown committed Dec 15, 2013 103  self.__q *= complex(a)  Daniel Brown committed Dec 05, 2013 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123  return self __rmul__ = __mul__ def __add__(self, a): return gauss_param(self.__lambda, self.__nr, self.__q + complex(a)) def __iadd__(self, a): self.__q += complex(a) return self __radd__ = __add__ def __sub__(self, a): return gauss_param(self.__lambda, self.__nr, self.__q - complex(a)) def __isub__(self, a): self.__q -= complex(a) return self  Daniel Brown committed Feb 12, 2014 124 125  def __rsub__(self, a): return gauss_param(self.__lambda, self.__nr, complex(a) - self.__q)  Daniel Brown committed Dec 05, 2013 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140  def __div__(self, a): return gauss_param(self.__lambda, self.__nr, self.__q / complex(a)) def __idiv__(self, a): self.__q /= complex(a) return self def __pow__(self, q): return gauss_param(self.__lambda, self.__nr, self.__q**q) def __neg__(self, q): return gauss_param(self.__lambda, self.__nr, -self.__q) def __eq__(self, q):  Daniel Brown committed Dec 11, 2013 141 142 143 144 145 146 147 148 149 150  return complex(q) == self.__q @property def real(self): return self.__q.real @real.setter def real(self, value): self.__q.real = SIfloat(value) @property def imag(self): return self.__q.imag @imag.setter  151  def imag(self, value): self.__q.imag = SIfloat(value)  Andreas Freise committed Mar 02, 2014 152 153 154 155  # reverse beam direction def reverse(self): self.__q = -1.0 * self.__q.real + 1j * self.__q.imag  156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196  class HG_gauss_beam(object): def __init__(self, qx, qy=None, n=0, m=0): self._qx = copy.deepcopy(qx) self._2pi_qrt = math.pow(2.0/math.pi, 0.25) if qy == None: self._q0 = copy.deepcopy(qx) else: self._q0 = copy.deepcopy(qy) self.n = n self.m = m self._calc_constants() @property def n(self): return self._n @n.setter def n(self,value): self._n = float(value) self._calc_constants() @property def m(self): return self._m @m.setter def m(self,value): self._m = float(value) self._calc_constants() def _calc_constants(self): self.__xpre_const = math.pow(2.0/math.pi, 0.25) self.__xpre_const *= math.sqrt(1/(2**self._n * math.factorial(self._n))) self.__xpre_const *= math.sqrt(1j*self._qx.imag / self._qx) self.__xpre_const *= math.pow((1j*self._qx.imag * self._qx.conjugate)/(-1j*self._qx.imag * self._qx), self._n/2.0) self.__ypre_const = math.pow(2.0/math.pi, 0.25) self.__ypre_const *= math.sqrt(1/(2**self._m * math.factorial(self._m))) self.__ypre_const *= math.sqrt(1j*self._qy.imag / self._qy) self.__ypre_const *= math.pow((1j*self._qy.imag * self._qy.conjugate)/(-1j*self._qy.imag * self._qy), self._m/2.0)  Daniel Brown committed Dec 15, 2013 197 198 199  def Unm(self, n, m, x, y): # need to finish... return self.__xpre_const  200   Andreas Freise committed Jan 09, 2014 201