Commit 223ef74d authored by Daniel Brown's avatar Daniel Brown
Browse files

Large amount of changes to the param object which now include being able to...

Large amount of changes to the param object which now include being able to put and set parameters and commands. Also adding the modulator component.
parent 056dd5ed
class Param(float):
def __new__(self,name,value):
return float.__new__(self,value)
def __init__(self,name,value):
self.__name = name
import abc
import pykat.exceptions as pkex
class puttable(object):
"""
Objects that inherit this should be able to have something `put` to it.
Essentially this means you could write Finesse commands like
put this parameter value
"""
__metaclass__ = abc.ABCMeta
def __init__(self, component_name, parameter_name):
self._parameter_name = parameter_name
self._component_name = component_name
self._putter = None
def put(self, var):
if not isinstance(var, putter):
raise pkex.BasePyKatException("var was not something that can be `put` as a value")
if self._putter != None:
self._putter.put_count -= 1
self._putter = var
self._putter.put_count += 1
name = property(lambda self: self.__name)
def getPutFinesseText(self):
rtn = []
# if something is being put to this
if self._putter != None:
rtn.append("put {comp} {param} ${value}".format(comp=self._component_name, param=self._parameter_name, value=self._putter.put_name()))
return rtn
class putter(object):
"""
If an object can be put to something that is puttable it should inherit this
object.
"""
__metaclass__ = abc.ABCMeta
def __init__(self, put_name):
self._put_name = put_name
self.put_count = 0
def put_name(self): return self._put_name
class Beer(object):
def __init__(self, temp):
self.__T = temp
class Param(puttable, putter):
def __init__(self, name, owner, value):
self._name = name
self._owner = owner
self._value = value
putter.__init__(self,"var_{0}_{1}".format(owner.name, name))
puttable.__init__(self, owner.name, name)
@property
def temp(self):
return Param('Beer Temperature', self.__T)
def name(self): return self._name
@temp.setter
def temp(self,value):
self.__T = float(value)
@property
def value(self): return self._value
@value.setter
def value(self, value):
self._value = value
def __float__(self): return self.value
def getFinesseText(self):
rtn = []
rtn.extend(self.getPutFinesseText())
# if this parameter is being put somewhere then we need to
# set it as a variable
if self.put_count > 0:
rtn.append("set {put_name} {comp} {param}".format(put_name=self.put_name(), comp=self._owner.name, param=self.name))
return rtn
def __mul__(self, a):
return float(self) * a
def __imul__(self, a):
return self.value * float(a)
__rmul__ = __mul__
def __add__(self, a):
return self.value + flota(a)
def __iadd__(self, a):
return self.value + float(a)
__radd__ = __add__
def __sub__(self, a):
return self.value - float(a)
def __isub__(self, a):
return self.value - float(a)
__rsub__ = __sub__
def __div__(self, a):
return self.value / float(a)
def __idiv__(self, a):
return self.value / complex(a)
def __pow__(self, q):
return self.value**q
b = Beer(100)
print b.temp.name, b.temp.__class__
print b.temp * 4.5
print b.temp.name, b.temp.__class__
def __neg__(self):
return -self.value
def __eq__(self, q):
return float(q) == self.value
class Beer(object):
def __init__(self, temp, name):
self._name = name
self._T = Param('T', self, temp)
@property
def name(self): return self._name
@property
def T(self): return self._T
@T.setter
def T(self,value): self._T.value = float(value)
b.temp = 101
b = Beer(1,"b")
c = Beer(2,"c")
print b.temp.name, b.temp.__class__
c.T.put(b.T)
print b.temp
\ No newline at end of file
print c.T.getFinesseText()
print b.T.getFinesseText()
\ No newline at end of file
from pykat import finesse
from pykat.detectors import *
from pykat.components import *
from pykat.commands import *
from pykat.structs import *
import numpy as np
import pylab as pl
code = """
l l1 1 0 0 n1
s s1 10 1 n1 n2
mod eom 10 0.1 1 am 0 n2 n3
"""
kat = finesse.kat()
kat.parseCommands(code)
kat.add(photodiode('pd_ref','n3', num_demods=1, demods=[10,0]))
kat.add(xaxis("lin", [0, 1000], "pd_ref", "f1", 100))
out = kat.run(printout=0, printerr=0)
pl.figure()
pl.plot(out.x, out["pd_ref"])
pl.xlabel(out.xlabel)
pl.ylabel("Intensity [W]")
pl.legend(out.ylabels)
pl.show()
......@@ -9,6 +9,7 @@ from numpy import min,max
import exceptions
from components import *
from structs import *
from pykat.param import Param, putter
class Command(object):
def __init__(self):
......@@ -46,40 +47,15 @@ class gauss(object):
if not values[0].startswith("gauss"):
raise exceptions.RuntimeError("'{0}' not a valid Finesse gauss command".format(text))
class attr():
@staticmethod
def parseFinesseText(text, kat):
values = text.split(" ")
if values[0] != "attr":
raise exceptions.RuntimeError("'{0}' not a valid Finesse attr command".format(text))
values.pop(0) # remove initial value
if len(values) < 3:
raise exceptions.RuntimeError("attr Finesse code format incorrect '{0}'".format(text))
comp = None
comp_name = values[0]
values.pop(0)
for c in kat.getComponents():
if c.name == comp_name:
comp = c
break
if comp == None:
raise
# can list multiple attributes per command
class xaxis(Command):
def __init__(self, scale, limits, comp, param, steps, axis_type="xaxis"):
self._axis_type = axis_type
self.x = putter("x1")
self.mx = putter("mx1")
if scale == "lin":
scale = Scale.linear
elif scale == "log":
......@@ -146,6 +122,8 @@ class xaxis(Command):
class x2axis(xaxis):
def __init__(self, scale, limits, comp, param, steps):
xaxis.__init__(self, scale, limits, comp, param, steps, axis_type="x2axis")
self.x = putter("x2")
self.mx = putter("mx2")
@staticmethod
def parseFinesseText(text):
......
This diff is collapsed.
......@@ -56,14 +56,6 @@ class photodiode(Detector):
if values==None:
values = []
list.__init__(self,[SIfloat(value) for value in values])
"""
def __setitem__(self, key, value):
list.__setitem__(self,key, SIfloat(value))
def append(self, value):
list.append(self,SIfloat(value))
"""
class __Phi(list):
def __convertValue(self, value):
......@@ -82,20 +74,12 @@ class photodiode(Detector):
return list.__getitem__(self,key)
else:
return float(list.__getitem__(self,key))
"""
def __setitem__(self,key,value):
list.__setitem__(self,key, self.__convertValue(value))
def append(self, value):
list.append(self,self.__convertValue(value))
"""
@property
def f(self): return self.__f
@property
def phi(self): return self.__phi
def __init__(self, name, node, senstype="", num_demods=0, demods=[]):
Detector.__init__(self, name, node)
......@@ -109,10 +93,6 @@ class photodiode(Detector):
# Every second element into phi (starting at 2)
self.__phi = self.__Phi(demods[1::2])
"""
for i in demods[1::2]:
self.__phi.append(i)
"""
@staticmethod
def parseFinesseText(text):
......
import exceptions
class BasePyKatException(Exception):
def __init__(self, msg):
self.__msg = msg
......
......@@ -34,14 +34,14 @@ import pykat
import warnings
import re
import pykat.exceptions as pkex
from pykat.node_network import NodeNetwork
from pykat.detectors import Detector
from pykat.components import Component
from pykat.commands import Command, xaxis
from pykat.gui.gui import pyKatGUI
import pykat.exceptions as pkex
from PyQt4.QtCore import QCoreApplication
from PyQt4.QtGui import QApplication
......@@ -265,6 +265,8 @@ class kat(object):
obj = pykat.components.isolator.parseFinesseText(line)
elif(first[0:4] == "lens"):
obj = pykat.components.lens.parseFinesseText(line)
elif(first[0:3] == "mod"):
obj = pykat.components.modulator.parseFinesseText(line)
elif(first[0:2] == "pd"):
obj = pykat.detectors.photodiode.parseFinesseText(line)
elif(first == "xaxis" or first == "xaxis*"):
......@@ -310,12 +312,12 @@ class kat(object):
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
It returns a katRun or katRun2D object which is populated with the various
data from the simulation run.
"""
try:
if not hasattr(self, "xaxis"):
if not hasattr(self, "xaxis") and self.noxaxis != None and self.noxaxis == False:
raise pkex.BasePyKatException("No xaxis was defined")
if len(self.__katdir) == 0:
......@@ -404,7 +406,7 @@ class kat(object):
r.runDateTime = datetime.datetime.now()
if p.returncode != 0:
raise FinesseRunError(err, katfile.name)
raise pkex.FinesseRunError(err, katfile.name)
if printout == 1: print out
if printerr == 1: print err
......@@ -467,7 +469,7 @@ class kat(object):
else:
return r
except FinesseRunError as fe:
except pkex.FinesseRunError as fe:
print fe
finally:
print ""
......@@ -505,7 +507,7 @@ class kat(object):
obj._on_kat_add(self)
except BasePyKatException as ex:
except pkex.BasePyKatException as ex:
print ex
def readOutFile(self, filename):
......
from pykat.SIfloat import SIfloat
import abc
import pykat.exceptions as pkex
class putable(object):
"""
Objects that inherit this should be able to have something `put` to it.
Essentially this means you could write Finesse commands like
put this parameter value
"""
__metaclass__ = abc.ABCMeta
def __init__(self, component_name, parameter_name, isPutable=True):
self._parameter_name = parameter_name
self._component_name = component_name
self._putter = None
self._isPutable = isPutable
@property
def isPutable(self): return self._isPutable
def put(self, var):
print "put"
if not isinstance(var, putter):
raise pkex.BasePyKatException("var was not something that can be `put` as a value")
if self._putter != None:
self._putter.put_count -= 1
self._putter = var
self._putter.put_count += 1
def _getPutFinesseText(self):
rtn = []
# if something is being put to this
if self._putter != None:
rtn.append("put {comp} {param} ${value}".format(comp=self._component_name, param=self._parameter_name, value=self._putter.put_name()))
return rtn
class putter(object):
"""
If an object can be put to something that is putable it should inherit this
object.
"""
def __init__(self, put_name, isPutter=True):
self._put_name = put_name
self.put_count = 0
self._isPutter = isPutter
@property
def isPutter(self): return self._isPutter
def put_name(self): return self._put_name
class Param(putable, putter):
def __init__(self, name, owner, value, isPutable=True, isPutter=True, isTunable=True, var_name=None):
self._name = name
self._owner = owner
self._value = value
self._isPutter = isPutter
self._isTunable = isTunable
self._owner._register_param(self)
if isPutter:
if var_name == None:
var_name = "var_{0}_{1}".format(owner.name, name)
putter.__init__(self, var_name, isPutter)
if isPutable:
putable.__init__(self, owner.name, name, isPutable)
@property
def name(self): return self._name
@property
def isTuneable(self): return self._isTunable
@property
def value(self): return self._value
@value.setter
def value(self, value):
self._value = value
def __str__(self): return str(self.value)
def __float__(self): return self.value
def getFinesseText(self):
rtn = []
if self.isPutable: rtn.extend(self._getPutFinesseText())
# if this parameter is being put somewhere then we need to
# set it as a variable
if self.isPutter and self.put_count > 0:
rtn.append("set {put_name} {comp} {param}".format(put_name=self.put_name(), comp=self._owner.name, param=self.name))
return rtn
def __mul__(self, a):
return float(self) * a
def __imul__(self, a):
return self.value * float(a)
__rmul__ = __mul__
def __add__(self, a):
return self.value + flota(a)
def __iadd__(self, a):
return self.value + float(a)
__radd__ = __add__
def __sub__(self, a):
return self.value - float(a)
def __isub__(self, a):
return self.value - float(a)
__rsub__ = __sub__
def __div__(self, a):
return self.value / float(a)
def __idiv__(self, a):
return self.value / complex(a)
def __pow__(self, q):
return self.value**q
def __neg__(self):
return -self.value
def __eq__(self, q):
return float(q) == self.value
def __ne__(self, q):
return float(q) != self.value
def __lt__(self, q):
return float(q) > self.value
def __gt__(self, q):
return float(q) < self.value
class AttrParam(Param):
"""
Certain parameters of a component are set using the Finesse `attr` command.
This inherits directly from a Param object so can be set whether this attribute
is putable or a putter.
If the value pf the parameter is not 0 the attr command will be printed.
"""
def getFinesseText(self):
rtn = []
if self.value != 0:
rtn.append("attr {0} {1} {2}".format(self._owner.name, self.name, self.value))
rtn.extend(super(AttrParam, self).getFinesseText())
return rtn
\ No newline at end of file
......@@ -95,7 +95,7 @@ class gauss_param(object):
return gauss_param(self.__lambda, self.__nr, self.__q * complex(a))
def __imul__(self, a):
self.__q += complex(a)
self.__q *= complex(a)
return self
__rmul__ = __mul__
......@@ -184,7 +184,8 @@ class HG_gauss_beam(object):
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
def Unm(self, n, m, x, y):
# need to finish...
return self.__xpre_const
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment