diff --git a/bin/test_plot.py b/bin/test_plot.py index eb73899e9481c230d6f39c47682a24fd1c6ea998..f21aa3ce748d4c9d1aae6b015d869050af9d1e1c 100644 --- a/bin/test_plot.py +++ b/bin/test_plot.py @@ -39,7 +39,7 @@ kat.maxtem = 0 out = kat.run(printout=0,printerr=0) pl.figure() -pl.plot(out.x,out.y) +pl.plot(out.x, out["pd_cav"]) pl.xlabel(out.xlabel) pl.ylabel("Intensity [W]") pl.legend(out.ylabels) diff --git a/pykat/commands.py b/pykat/commands.py index 483fa0a67560b0626258ab487081221457b8b2e4..a0d63977d38d69d7ba3c86eab4f0ab7835bcb7b2 100644 --- a/pykat/commands.py +++ b/pykat/commands.py @@ -74,10 +74,11 @@ class attr(): if comp == None: raise # can list multiple attributes per command - + class xaxis(Command): - def __init__(self, scale, limits, comp, param, steps): + def __init__(self, scale, limits, comp, param, steps, axis_type="xaxis"): + self._axis_type = axis_type if scale == "lin": scale = Scale.linear @@ -121,23 +122,43 @@ class xaxis(Command): def parseFinesseText(text): values = text.split(" ") - if values[0] != "xaxis" and values[0] != "xaxis*" and values[0] != "x2axis" and values[0] != "x2axis*": + if values[0] != "xaxis" and values[0] != "xaxis*": raise exceptions.RuntimeError("'{0}' not a valid Finesse xaxis command".format(text)) - + + axis_type = values[0] + values.pop(0) # remove initial value if len(values) != 6: raise exceptions.RuntimeError("xaxis Finesse code format incorrect '{0}'".format(text)) - return xaxis(values[2], [values[3], values[4]], values[0], values[1], values[5]) + return xaxis(values[2], [values[3], values[4]], values[0], values[1], values[5], axis_type=axis_type) def getFinesseText(self): # store either the component name of the string provided comp_name = self.__comp.name if isinstance(self.__comp, Component) else self.__comp param_name = self.__param.name if isinstance(self.__param, Param) else self.__param - return 'xaxis {0} {1} {2} {3} {4} {5}'.format( + return '{axis_type} {0} {1} {2} {3} {4} {5}'.format( comp_name, param_name, self.scale, - min(self.limits), max(self.limits), self.steps); + min(self.limits), max(self.limits), self.steps, axis_type=self._axis_type); +class x2axis(xaxis): + def __init__(self, scale, limits, comp, param, steps): + xaxis.__init__(self, scale, limits, comp, param, steps, axis_type="x2axis") + + @staticmethod + def parseFinesseText(text): + values = text.split(" ") + + if values[0] != "x2axis" and values[0] != "x2axis*": + raise exceptions.RuntimeError("'{0}' not a valid Finesse xaxis command".format(text)) + axis_type = values[0] + + values.pop(0) # remove initial value + + if len(values) != 6: + raise exceptions.RuntimeError("xaxis Finesse code format incorrect '{0}'".format(text)) + + return x2axis(values[2], [values[3], values[4]], values[0], values[1], values[5]) \ No newline at end of file diff --git a/pykat/components.py b/pykat/components.py index e7d1922d63398608ddec5dfd1101e3be2d83f851..87df724edc04092b694f4be388650bafbe1fb23d 100644 --- a/pykat/components.py +++ b/pykat/components.py @@ -345,7 +345,7 @@ class space(Component): self._requested_node_names.append(node1) self._requested_node_names.append(node2) - + self._QItem = None self.__L = SIfloat(L) self.__n = SIfloat(n) diff --git a/pykat/finesse.py b/pykat/finesse.py index 26ea7756e087e86670424c5a71229c3fe557c722..20175f59441d855687d3f190d391e02d34c47618 100644 --- a/pykat/finesse.py +++ b/pykat/finesse.py @@ -66,8 +66,19 @@ class katRun(object): def loadKatRun(filename): with open(filename,'r') as infile: return pickle.load(infile) - - + + def get(self, value): return self[value] + + def __getitem__(self, value): + if value in self.ylabels: + idx = self.ylabels.index(value) + if len(self.y.shape) == 1: + return self.y + else: + return self.y[:, idx] + else: + raise pkex.BasePyKatExeception("No output by the name {0} found", value) + class Block: def __init__(self, name): self.__name = name @@ -213,8 +224,10 @@ class kat(object): obj = pykat.components.lens.parseFinesseText(line) elif(first[0:2] == "pd"): obj = pykat.detectors.photodiode.parseFinesseText(line) - elif(first == "xaxis" or first == "x2axis" or first == "xaxis*" or first == "x2axis*"): + elif(first == "xaxis" or first == "xaxis*"): obj = pykat.commands.xaxis.parseFinesseText(line) + elif(first == "x2axis" or first == "x2axis*"): + obj = pykat.commands.x2axis.parseFinesseText(line) elif(first == "gauss" or first == "gauss*" or first == "gauss**"): after_process.append(line) else: @@ -251,13 +264,17 @@ class kat(object): except BasePyKatException as ex: print ex - def run(self, printout=1, printerr=1, save_output=False, save_kat=False,kat_name=None) : + def run(self, printout=0, printerr=0, save_output=False, save_kat=False,kat_name=None) : """ Runs the current simulation setup that has been built thus far. It returns a katRun object which is populated with the various data from the simulation run. """ try: + print "--------------------------------------------------------------" + start = datetime.datetime.now() + print "Running kat - Started at " + str(start) + r = katRun() r.katScript = "".join(self.generateKatScript()) @@ -295,28 +312,37 @@ class kat(object): katfile.flush() cmd=[kat_exec, '--perl1'] + if self.__time_code: cmd.append('--perf-timing') - cmd.append('--no-backspace') + + cmd.append('--no-backspace') + # set default format so that less repeated numbers are printed to the + # output file, should speed up running and parsing of output files + cmd.append('-format=%.15g') cmd.append(katfile.name) - if self.verbose: - print cmd + + #if self.verbose: + #print cmd + p=subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) err = "" + print "Finesse binary output:" + for line in iter(p.stderr.readline, ""): - err += line - vals = line.split("-") - - if len(vals) == 2: - action = vals[0].strip() - prc = vals[1].strip()[:-1] - - #sys.stdout.write("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b") - sys.stdout.write("\r{0} {1}%".format(action, prc)) - sys.stdout.flush() + #err += line + if len(line) > 0: + if line.rstrip().endswith('%'): + vals = line.split("-") + action = vals[0].strip() + prc = vals[1].strip()[:-1] + sys.stdout.write("\r{0} {1}%".format(action, prc)) + elif line[0] == '*': + sys.stdout.write(line) + [out,errpipe] = p.communicate() # get the version number @@ -339,7 +365,7 @@ class kat(object): [r.x,r.y,hdr] = self.readOutFile(outfile) r.xlabel = hdr[0] - r.ylabels = hdr[1:] + r.ylabels = map(str.strip, hdr[1:]) if save_output: newoutfile = "{0}.out".format(base) @@ -382,9 +408,12 @@ class kat(object): return [r, perfData] else: return r - + except FinesseRunError as fe: print fe + finally: + print "" + print "Finished in " + str(datetime.datetime.now()-start) def add(self, obj): diff --git a/pykat/utilities/optics/gaussian_beams.py b/pykat/utilities/optics/gaussian_beams.py index d0977e5ec32ebdcd0e406e9b9cbc74f643a0b1f3..769bd75eebad6b4659bf4aae80835ad12d45f4d8 100644 --- a/pykat/utilities/optics/gaussian_beams.py +++ b/pykat/utilities/optics/gaussian_beams.py @@ -1,6 +1,7 @@ import pykat.exceptions as pkex import numpy import math +import copy class gauss_param(object): """ @@ -141,4 +142,49 @@ class gauss_param(object): @property def imag(self): return self.__q.imag @imag.setter - def imag(self, value): self.__q.imag = SIfloat(value) \ No newline at end of file + def imag(self, value): self.__q.imag = SIfloat(value) + +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) + + def Unm(self, n, m, x, y): + return self.__xpre_const * special + + \ No newline at end of file