Commit 94f811e6 authored by Daniel Brown's avatar Daniel Brown
Browse files

EOL changes

parent 6b2e8649
l l1 1 0 0 n1
s s1 10 1 n1 n2
m m1 0.5 0.5 0 n2 n3
s s2 10 1 n3 n4
m m2 0.5 0.5 0 n4 dump
pd PD1 n2
xaxis m1 phi lin 0 360 1000
from pykat import finesse from pykat import finesse
from pykat.detectors import * from pykat.detectors import *
from pykat.components import * from pykat.components import *
from pykat.commands import * from pykat.commands import *
from pykat.structs import * from pykat.structs import *
import numpy as np import numpy as np
import pylab as pl import pylab as pl
code = """ code = """
l l1 1 0 0 n1 l l1 1 0 0 n1
s s1 10 1 n1 n2 s s1 10 1 n1 n2
m m1 0.5 0.5 0 n2 n3 m m1 0.5 0.5 0 n2 n3
s s2 10 1 n3 n4 s s2 10 1 n3 n4
m m2 0.5 0.5 0 n4 n5 m m2 0.5 0.5 0 n4 n5
s s3 10 1 n5 n6 s s3 10 1 n5 n6
""" """
#kat = finesse.kat(katexe='/Users/adf/work/bin/kat') #kat = finesse.kat(katexe='/Users/adf/work/bin/kat')
kat = finesse.kat() kat = finesse.kat()
kat.parseCommands(code) kat.parseCommands(code)
kat.add(cavity('cav1', 'm1', 'n3', 'm2', 'n4')) kat.add(cavity('cav1', 'm1', 'n3', 'm2', 'n4'))
kat.add(photodiode('pd_cav','n4')) kat.add(photodiode('pd_cav','n4'))
kat.add(photodiode('pd_ref','n2')) kat.add(photodiode('pd_ref','n2'))
kat.add(photodiode('pd_trs','n5')) kat.add(photodiode('pd_trs','n5'))
kat.add(xaxis("lin", [0, 360], kat.m2, kat.m2.phi, 100)) kat.add(xaxis("lin", [0, 360], kat.m2, kat.m2.phi, 100))
kat.m1.Rcx = -1000.0 kat.m1.Rcx = -1000.0
kat.m1.Rcy = -1000.0 kat.m1.Rcy = -1000.0
kat.m2.Rcx = 1000.0 kat.m2.Rcx = 1000.0
kat.m2.Rcy = 1000.0 kat.m2.Rcy = 1000.0
kat.maxtem = 0 kat.maxtem = 0
out = kat.run(printout=0,printerr=0) out = kat.run(printout=0,printerr=0)
pl.figure() pl.figure()
pl.plot(out.x, out["pd_cav"]) pl.plot(out.x, out["pd_cav"])
pl.xlabel(out.xlabel) pl.xlabel(out.xlabel)
pl.ylabel("Intensity [W]") pl.ylabel("Intensity [W]")
pl.legend(out.ylabels) pl.legend(out.ylabels)
pl.show() pl.show()
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" """
Created on Mon Jan 28 10:43:18 2013 Created on Mon Jan 28 10:43:18 2013
@author: Daniel @author: Daniel
""" """
class colours: class colours:
HEADER = '\033[95m' HEADER = '\033[95m'
OKBLUE = '\033[94m' OKBLUE = '\033[94m'
OKGREEN = '\033[92m' OKGREEN = '\033[92m'
WARNING = '\033[93m' WARNING = '\033[93m'
FAIL = '\033[91m' FAIL = '\033[91m'
ENDC = '\033[0m' ENDC = '\033[0m'
def disable(self): def disable(self):
self.HEADER = '' self.HEADER = ''
self.OKBLUE = '' self.OKBLUE = ''
self.OKGREEN = '' self.OKGREEN = ''
self.WARNING = '' self.WARNING = ''
self.FAIL = '' self.FAIL = ''
self.ENDC = '' self.ENDC = ''
\ No newline at end of file
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" """
Created on Mon Jan 28 11:58:09 2013 Created on Mon Jan 28 11:58:09 2013
@author: Daniel @author: Daniel
""" """
import numpy import numpy
from numpy import min,max from numpy import min,max
import exceptions import exceptions
from components import * from components import *
from structs import * from structs import *
from pykat.param import Param, putter from pykat.param import Param, putter
class Command(object): class Command(object):
def __init__(self): def __init__(self):
self.tag = None self.tag = None
def getFinesseText(self): def getFinesseText(self):
""" Base class for individual finesse optical components """ """ Base class for individual finesse optical components """
raise NotImplementedError("This function is not implemented") raise NotImplementedError("This function is not implemented")
@staticmethod @staticmethod
def parseFinesseText(text): def parseFinesseText(text):
raise NotImplementedError("This function is not implemented") raise NotImplementedError("This function is not implemented")
def _on_kat_add(self, kat): def _on_kat_add(self, kat):
""" """
Called when this component has been added to a kat object Called when this component has been added to a kat object
""" """
self._kat = kat self._kat = kat
class cavity(Command): class cavity(Command):
def __init__(self, name, c1, n1, c2, n2): def __init__(self, name, c1, n1, c2, n2):
self.__name = name self.__name = name
self.__c1 = c1 self.__c1 = c1
self.__c2 = c2 self.__c2 = c2
self.__n1 = n1 self.__n1 = n1
self.__n2 = n2 self.__n2 = n2
def getFinesseText(self): def getFinesseText(self):
return 'cav {0} {1} {2} {3} {4}'.format(self.__name, self.__c1, self.__n1, self.__c2, self.__n2); return 'cav {0} {1} {2} {3} {4}'.format(self.__name, self.__c1, self.__n1, self.__c2, self.__n2);
class gauss(object): class gauss(object):
@staticmethod @staticmethod
def parseFinesseText(text, kat): def parseFinesseText(text, kat):
values = text.split(" ") values = text.split(" ")
if not values[0].startswith("gauss"): if not values[0].startswith("gauss"):
raise exceptions.RuntimeError("'{0}' not a valid Finesse gauss command".format(text)) raise exceptions.RuntimeError("'{0}' not a valid Finesse gauss command".format(text))
class xaxis(Command): class xaxis(Command):
def __init__(self, scale, limits, comp, param, steps, axis_type="xaxis"): def __init__(self, scale, limits, comp, param, steps, axis_type="xaxis"):
self._axis_type = axis_type self._axis_type = axis_type
self.x = putter("x1") self.x = putter("x1")
self.mx = putter("mx1") self.mx = putter("mx1")
if scale == "lin": if scale == "lin":
scale = Scale.linear scale = Scale.linear
elif scale == "log": elif scale == "log":
scale = Scale.logarithmic scale = Scale.logarithmic
elif isinstance(scale, str): elif isinstance(scale, str):
# else we have a string but not a recognisable one # else we have a string but not a recognisable one
raise exceptions.ValueError("scale argument '{0}' is not valid, must be 'lin' or 'log'".format(scale)) raise exceptions.ValueError("scale argument '{0}' is not valid, must be 'lin' or 'log'".format(scale))
if scale != Scale.linear and scale != Scale.logarithmic: if scale != Scale.linear and scale != Scale.logarithmic:
raise exceptions.ValueError("scale is not Scale.linear or Scale.logarithmic") raise exceptions.ValueError("scale is not Scale.linear or Scale.logarithmic")
self.scale = scale self.scale = scale
if numpy.size(limits) != 2 : if numpy.size(limits) != 2 :
raise exceptions.ValueError("limits input should be a 2x1 vector of limits for the xaxis") raise exceptions.ValueError("limits input should be a 2x1 vector of limits for the xaxis")
self.limits = numpy.array(SIfloat(limits)).astype(float) self.limits = numpy.array(SIfloat(limits)).astype(float)
if steps <= 0 : if steps <= 0 :
raise exceptions.ValueError("steps value should be > 0") raise exceptions.ValueError("steps value should be > 0")
self.steps = int(steps) self.steps = int(steps)
# if entered component is a string then just store and use that # if entered component is a string then just store and use that
if isinstance(comp, str): if isinstance(comp, str):
self.__comp = comp self.__comp = comp
elif not isinstance(comp, Component): elif not isinstance(comp, Component):
raise exceptions.ValueError("{0} has not been added".format(comp)) raise exceptions.ValueError("{0} has not been added".format(comp))
else: else:
self.__comp = comp self.__comp = comp
if isinstance(param, str): if isinstance(param, str):
self.__param = param self.__param = param
elif not isinstance(param, Param) : elif not isinstance(param, Param) :
raise exceptions.ValueError("param argument is not of type Param") raise exceptions.ValueError("param argument is not of type Param")
else: else:
self.__param = param self.__param = param
@staticmethod @staticmethod
def parseFinesseText(text): def parseFinesseText(text):
values = text.split(" ") values = text.split(" ")
if values[0] != "xaxis" and values[0] != "xaxis*": if values[0] != "xaxis" and values[0] != "xaxis*":
raise exceptions.RuntimeError("'{0}' not a valid Finesse xaxis command".format(text)) raise exceptions.RuntimeError("'{0}' not a valid Finesse xaxis command".format(text))
axis_type = values[0] axis_type = values[0]
values.pop(0) # remove initial value values.pop(0) # remove initial value
if len(values) != 6: if len(values) != 6:
raise exceptions.RuntimeError("xaxis Finesse code format incorrect '{0}'".format(text)) 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], axis_type=axis_type) return xaxis(values[2], [values[3], values[4]], values[0], values[1], values[5], axis_type=axis_type)
def getFinesseText(self): def getFinesseText(self):
# store either the component name of the string provided # store either the component name of the string provided
comp_name = self.__comp.name if isinstance(self.__comp, Component) else self.__comp 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 param_name = self.__param.name if isinstance(self.__param, Param) else self.__param
return '{axis_type} {0} {1} {2} {3} {4} {5}'.format( return '{axis_type} {0} {1} {2} {3} {4} {5}'.format(
comp_name, param_name, self.scale, comp_name, param_name, self.scale,
min(self.limits), max(self.limits), self.steps, axis_type=self._axis_type); min(self.limits), max(self.limits), self.steps, axis_type=self._axis_type);
class x2axis(xaxis): class x2axis(xaxis):
def __init__(self, scale, limits, comp, param, steps): def __init__(self, scale, limits, comp, param, steps):
xaxis.__init__(self, scale, limits, comp, param, steps, axis_type="x2axis") xaxis.__init__(self, scale, limits, comp, param, steps, axis_type="x2axis")
self.x = putter("x2") self.x = putter("x2")
self.mx = putter("mx2") self.mx = putter("mx2")
@staticmethod @staticmethod
def parseFinesseText(text): def parseFinesseText(text):
values = text.split(" ") values = text.split(" ")
if values[0] != "x2axis" and values[0] != "x2axis*": if values[0] != "x2axis" and values[0] != "x2axis*":
raise exceptions.RuntimeError("'{0}' not a valid Finesse xaxis command".format(text)) raise exceptions.RuntimeError("'{0}' not a valid Finesse xaxis command".format(text))
axis_type = values[0] axis_type = values[0]
values.pop(0) # remove initial value values.pop(0) # remove initial value
if len(values) != 6: if len(values) != 6:
raise exceptions.RuntimeError("xaxis Finesse code format incorrect '{0}'".format(text)) 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]) return x2axis(values[2], [values[3], values[4]], values[0], values[1], values[5])
\ No newline at end of file
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" """
Created on Mon Jan 28 11:10:01 2013 Created on Mon Jan 28 11:10:01 2013
@author: Daniel @author: Daniel
""" """
import exceptions import exceptions
import pykat.exceptions as pkex import pykat.exceptions as pkex
import pykat import pykat
from pykat.node_network import * from pykat.node_network import *
from pykat.exceptions import * from pykat.exceptions import *
import abc import abc
import pykat.gui.resources import pykat.gui.resources
import pykat.gui.graphics import pykat.gui.graphics
from pykat.gui.graphics import * from pykat.gui.graphics import *
from pykat.SIfloat import * from pykat.SIfloat import *
from pykat.param import Param, AttrParam from pykat.param import Param, AttrParam
next_component_id = 1 next_component_id = 1
class NodeGaussSetter(object): class NodeGaussSetter(object):
def __init__(self, component, node): def __init__(self, component, node):
self.__comp = component self.__comp = component
self.__node = node self.__node = node
@property @property
def node(self): def node(self):
return self.__node return self.__node
@property @property
def q(self): def q(self):
return self.__node.qx return self.__node.qx
@q.setter @q.setter
def q(self, value): def q(self, value):
self.__node.setGauss(self.__comp, complex(value)) self.__node.setGauss(self.__comp, complex(value))
@property @property
def qx(self): def qx(self):
return self.__node.qx return self.__node.qx
@qx.setter @qx.setter
def qx(self, value): def qx(self, value):
self.__node.setGauss(self.__comp, complex(value)) self.__node.setGauss(self.__comp, complex(value))
@property @property
def qy(self): def qy(self):
return self.__node.qy return self.__node.qy
@qy.setter @qy.setter
def qy(self, value): def qy(self, value):
self.__node.setGauss(self.__comp, self.qx, complex(value)) self.__node.setGauss(self.__comp, self.qx, complex(value))
class Component(object): class Component(object):
__metaclass__ = abc.ABCMeta __metaclass__ = abc.ABCMeta
def __init__(self, name): def __init__(self, name):
self.__name = name self.__name = name
self._svgItem = None self._svgItem = None
self._requested_node_names = [] self._requested_node_names = []
self._kat = None self._kat = None
self.tag = None self.tag = None
self._params = [] self._params = []
# store a unique ID for this component # store a unique ID for this component
global next_component_id global next_component_id
self.__id = next_component_id self.__id = next_component_id
next_component_id += 1 next_component_id += 1
# This creates an instance specific class for the component # This creates an instance specific class for the component
# this enables us to add properties to instances rather than # this enables us to add properties to instances rather than
# all classes # all classes
cls = type(self) cls = type(self)
self.__class__ = type(cls.__name__, (cls,), {}) self.__class__ = type(cls.__name__, (cls,), {})
def _register_param(self, param): def _register_param(self, param):
self._params.append(param) self._params.append(param)
def _on_kat_add(self, kat): def _on_kat_add(self, kat):
""" """
Called when this component has been added to a kat object. Called when this component has been added to a kat object.
kat is the finesse.kat object which it now belongs to and kat is the finesse.kat object which it now belongs to and
node_array is an array specific to this object which contains node_array is an array specific to this object which contains
references to the nodes that are attached to it. references to the nodes that are attached to it.
""" """
if self._kat != None: if self._kat != None:
raise pkex.BasePyKatException("Component has already been added to a finesse.kat object") raise pkex.BasePyKatException("Component has already been added to a finesse.kat object")
self._kat = kat self._kat = kat
kat.nodes.registerComponentNodes(self, self._requested_node_names, self.__on_node_change) kat.nodes.registerComponentNodes(self, self._requested_node_names, self.__on_node_change)
def __on_node_change(self): def __on_node_change(self):
# need to update the node gauss parameter setter members # need to update the node gauss parameter setter members
self.__update_node_setters() self.__update_node_setters()
def __update_node_setters(self): def __update_node_setters(self):
# check if any node setters have already been added. If so we # check if any node setters have already been added. If so we
# need to remove them. This function should get called if the nodes # need to remove them. This function should get called if the nodes
# are updated, either by some function call or the GUI # are updated, either by some function call or the GUI
key_rm = [k for k in self.__dict__ if k.startswith("__nodesetter_", 0, 13)] key_rm = [k for k in self.__dict__ if k.startswith("__nodesetter_", 0, 13)]
# now we have a list of which to remove # now we have a list of which to remove
for key in key_rm: for key in key_rm:
ns = self.__dict__[key] ns = self.__dict__[key]
delattr(self, '__nodesetter_' + ns.node.name) delattr(self, '__nodesetter_' + ns.node.name)
delattr(self.__class__, ns.node.name) delattr(self.__class__, ns.node.name)
for node in self.nodes: for node in self.nodes:
if type(node) != pykat.node_network.DumpNode: if type(node) != pykat.node_network.DumpNode:
ns = NodeGaussSetter(self, node) ns = NodeGaussSetter(self, node)
self.__add_node_setter(ns) self.__add_node_setter(ns)
def __add_node_setter(self, ns): def __add_node_setter(self, ns):
if not isinstance(ns, NodeGaussSetter): if not isinstance(ns, NodeGaussSetter):
raise exceptions.ValueError("Argument is not of type NodeGaussSetter")