Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
finesse
pykat
Commits
94f811e6
Commit
94f811e6
authored
Dec 16, 2013
by
Daniel Brown
Browse files
EOL changes
parent
6b2e8649
Changes
14
Expand all
Hide whitespace changes
Inline
Side-by-side
bin/parse.kat
deleted
100644 → 0
View file @
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
bin/test_plot.py
View file @
94f811e6
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
m m1 0.5 0.5 0 n2 n3
s s2 10 1 n3 n4
m m2 0.5 0.5 0 n4 n5
s s3 10 1 n5 n6
"""
#kat = finesse.kat(katexe='/Users/adf/work/bin/kat')
kat
=
finesse
.
kat
()
kat
.
parseCommands
(
code
)
kat
.
add
(
cavity
(
'cav1'
,
'm1'
,
'n3'
,
'm2'
,
'n4'
))
kat
.
add
(
photodiode
(
'pd_cav'
,
'n4'
))
kat
.
add
(
photodiode
(
'pd_ref'
,
'n2'
))
kat
.
add
(
photodiode
(
'pd_trs'
,
'n5'
))
kat
.
add
(
xaxis
(
"lin"
,
[
0
,
360
],
kat
.
m2
,
kat
.
m2
.
phi
,
100
))
kat
.
m1
.
Rcx
=
-
1000.0
kat
.
m1
.
Rcy
=
-
1000.0
kat
.
m2
.
Rcx
=
1000.0
kat
.
m2
.
Rcy
=
1000.0
kat
.
maxtem
=
0
out
=
kat
.
run
(
printout
=
0
,
printerr
=
0
)
pl
.
figure
()
pl
.
plot
(
out
.
x
,
out
[
"pd_cav"
])
pl
.
xlabel
(
out
.
xlabel
)
pl
.
ylabel
(
"Intensity [W]"
)
pl
.
legend
(
out
.
ylabels
)
pl
.
show
()
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
m m1 0.5 0.5 0 n2 n3
s s2 10 1 n3 n4
m m2 0.5 0.5 0 n4 n5
s s3 10 1 n5 n6
"""
#kat = finesse.kat(katexe='/Users/adf/work/bin/kat')
kat
=
finesse
.
kat
()
kat
.
parseCommands
(
code
)
kat
.
add
(
cavity
(
'cav1'
,
'm1'
,
'n3'
,
'm2'
,
'n4'
))
kat
.
add
(
photodiode
(
'pd_cav'
,
'n4'
))
kat
.
add
(
photodiode
(
'pd_ref'
,
'n2'
))
kat
.
add
(
photodiode
(
'pd_trs'
,
'n5'
))
kat
.
add
(
xaxis
(
"lin"
,
[
0
,
360
],
kat
.
m2
,
kat
.
m2
.
phi
,
100
))
kat
.
m1
.
Rcx
=
-
1000.0
kat
.
m1
.
Rcy
=
-
1000.0
kat
.
m2
.
Rcx
=
1000.0
kat
.
m2
.
Rcy
=
1000.0
kat
.
maxtem
=
0
out
=
kat
.
run
(
printout
=
0
,
printerr
=
0
)
pl
.
figure
()
pl
.
plot
(
out
.
x
,
out
[
"pd_cav"
])
pl
.
xlabel
(
out
.
xlabel
)
pl
.
ylabel
(
"Intensity [W]"
)
pl
.
legend
(
out
.
ylabels
)
pl
.
show
()
pykat/colours.py
View file @
94f811e6
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 28 10:43:18 2013
@author: Daniel
"""
class
colours
:
HEADER
=
'
\033
[95m'
OKBLUE
=
'
\033
[94m'
OKGREEN
=
'
\033
[92m'
WARNING
=
'
\033
[93m'
FAIL
=
'
\033
[91m'
ENDC
=
'
\033
[0m'
def
disable
(
self
):
self
.
HEADER
=
''
self
.
OKBLUE
=
''
self
.
OKGREEN
=
''
self
.
WARNING
=
''
self
.
FAIL
=
''
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 28 10:43:18 2013
@author: Daniel
"""
class
colours
:
HEADER
=
'
\033
[95m'
OKBLUE
=
'
\033
[94m'
OKGREEN
=
'
\033
[92m'
WARNING
=
'
\033
[93m'
FAIL
=
'
\033
[91m'
ENDC
=
'
\033
[0m'
def
disable
(
self
):
self
.
HEADER
=
''
self
.
OKBLUE
=
''
self
.
OKGREEN
=
''
self
.
WARNING
=
''
self
.
FAIL
=
''
self
.
ENDC
=
''
\ No newline at end of file
pykat/commands.py
View file @
94f811e6
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 28 11:58:09 2013
@author: Daniel
"""
import
numpy
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
):
self
.
tag
=
None
def
getFinesseText
(
self
):
""" Base class for individual finesse optical components """
raise
NotImplementedError
(
"This function is not implemented"
)
@
staticmethod
def
parseFinesseText
(
text
):
raise
NotImplementedError
(
"This function is not implemented"
)
def
_on_kat_add
(
self
,
kat
):
"""
Called when this component has been added to a kat object
"""
self
.
_kat
=
kat
class
cavity
(
Command
):
def
__init__
(
self
,
name
,
c1
,
n1
,
c2
,
n2
):
self
.
__name
=
name
self
.
__c1
=
c1
self
.
__c2
=
c2
self
.
__n1
=
n1
self
.
__n2
=
n2
def
getFinesseText
(
self
):
return
'cav {0} {1} {2} {3} {4}'
.
format
(
self
.
__name
,
self
.
__c1
,
self
.
__n1
,
self
.
__c2
,
self
.
__n2
);
class
gauss
(
object
):
@
staticmethod
def
parseFinesseText
(
text
,
kat
):
values
=
text
.
split
(
" "
)
if
not
values
[
0
].
startswith
(
"gauss"
):
raise
exceptions
.
RuntimeError
(
"'{0}' not a valid Finesse gauss command"
.
format
(
text
))
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"
:
scale
=
Scale
.
logarithmic
elif
isinstance
(
scale
,
str
):
# 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
))
if
scale
!=
Scale
.
linear
and
scale
!=
Scale
.
logarithmic
:
raise
exceptions
.
ValueError
(
"scale is not Scale.linear or Scale.logarithmic"
)
self
.
scale
=
scale
if
numpy
.
size
(
limits
)
!=
2
:
raise
exceptions
.
ValueError
(
"limits input should be a 2x1 vector of limits for the xaxis"
)
self
.
limits
=
numpy
.
array
(
SIfloat
(
limits
)).
astype
(
float
)
if
steps
<=
0
:
raise
exceptions
.
ValueError
(
"steps value should be > 0"
)
self
.
steps
=
int
(
steps
)
# if entered component is a string then just store and use that
if
isinstance
(
comp
,
str
):
self
.
__comp
=
comp
elif
not
isinstance
(
comp
,
Component
):
raise
exceptions
.
ValueError
(
"{0} has not been added"
.
format
(
comp
))
else
:
self
.
__comp
=
comp
if
isinstance
(
param
,
str
):
self
.
__param
=
param
elif
not
isinstance
(
param
,
Param
)
:
raise
exceptions
.
ValueError
(
"param argument is not of type Param"
)
else
:
self
.
__param
=
param
@
staticmethod
def
parseFinesseText
(
text
):
values
=
text
.
split
(
" "
)
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
],
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
'{axis_type} {0} {1} {2} {3} {4} {5}'
.
format
(
comp_name
,
param_name
,
self
.
scale
,
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"
)
self
.
x
=
putter
(
"x2"
)
self
.
mx
=
putter
(
"mx2"
)
@
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
))
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 28 11:58:09 2013
@author: Daniel
"""
import
numpy
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
):
self
.
tag
=
None
def
getFinesseText
(
self
):
""" Base class for individual finesse optical components """
raise
NotImplementedError
(
"This function is not implemented"
)
@
staticmethod
def
parseFinesseText
(
text
):
raise
NotImplementedError
(
"This function is not implemented"
)
def
_on_kat_add
(
self
,
kat
):
"""
Called when this component has been added to a kat object
"""
self
.
_kat
=
kat
class
cavity
(
Command
):
def
__init__
(
self
,
name
,
c1
,
n1
,
c2
,
n2
):
self
.
__name
=
name
self
.
__c1
=
c1
self
.
__c2
=
c2
self
.
__n1
=
n1
self
.
__n2
=
n2
def
getFinesseText
(
self
):
return
'cav {0} {1} {2} {3} {4}'
.
format
(
self
.
__name
,
self
.
__c1
,
self
.
__n1
,
self
.
__c2
,
self
.
__n2
);
class
gauss
(
object
):
@
staticmethod
def
parseFinesseText
(
text
,
kat
):
values
=
text
.
split
(
" "
)
if
not
values
[
0
].
startswith
(
"gauss"
):
raise
exceptions
.
RuntimeError
(
"'{0}' not a valid Finesse gauss command"
.
format
(
text
))
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"
:
scale
=
Scale
.
logarithmic
elif
isinstance
(
scale
,
str
):
# 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
))
if
scale
!=
Scale
.
linear
and
scale
!=
Scale
.
logarithmic
:
raise
exceptions
.
ValueError
(
"scale is not Scale.linear or Scale.logarithmic"
)
self
.
scale
=
scale
if
numpy
.
size
(
limits
)
!=
2
:
raise
exceptions
.
ValueError
(
"limits input should be a 2x1 vector of limits for the xaxis"
)
self
.
limits
=
numpy
.
array
(
SIfloat
(
limits
)).
astype
(
float
)
if
steps
<=
0
:
raise
exceptions
.
ValueError
(
"steps value should be > 0"
)
self
.
steps
=
int
(
steps
)
# if entered component is a string then just store and use that
if
isinstance
(
comp
,
str
):
self
.
__comp
=
comp
elif
not
isinstance
(
comp
,
Component
):
raise
exceptions
.
ValueError
(
"{0} has not been added"
.
format
(
comp
))
else
:
self
.
__comp
=
comp
if
isinstance
(
param
,
str
):
self
.
__param
=
param
elif
not
isinstance
(
param
,
Param
)
:
raise
exceptions
.
ValueError
(
"param argument is not of type Param"
)
else
:
self
.
__param
=
param
@
staticmethod
def
parseFinesseText
(
text
):
values
=
text
.
split
(
" "
)
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
],
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
'{axis_type} {0} {1} {2} {3} {4} {5}'
.
format
(
comp_name
,
param_name
,
self
.
scale
,
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"
)
self
.
x
=
putter
(
"x2"
)
self
.
mx
=
putter
(
"mx2"
)
@
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
pykat/components.py
View file @
94f811e6
This diff is collapsed.
Click to expand it.
pykat/detectors.py
View file @
94f811e6
This diff is collapsed.
Click to expand it.
pykat/exceptions.py
View file @
94f811e6
import
exceptions
class
BasePyKatException
(
Exception
):
def
__init__
(
self
,
msg
):
self
.
__msg
=
msg
def
__str__
(
self
):
return
self
.
__msg
class
FinesseParse
(
BasePyKatException
)
:
def
__init__
(
self
,
msg
):
BasePyKatException
.
__init__
(
self
,
"Error parsing Finesse input
\n
{0}"
.
format
(
msg
))
class
MissingFinesseEnvVar
(
BasePyKatException
)
:
def
__init__
(
self
):
BasePyKatException
.
__init__
(
self
,
"The environment variable FINESSE_DIR was not defined"
)
class
MissingFinesse
(
BasePyKatException
)
:
def
__init__
(
self
):
BasePyKatException
.
__init__
(
self
,
"Could not find the finesse executable 'kat' in '{0}',"
\
"or you do not have the permissions to run it."
\
.
format
(
os
.
environ
.
get
(
'FINESSE_DIR'
)))
class
FinesseRunError
(
BasePyKatException
)
:
def
__init__
(
self
,
err
,
kat
):
self
.
__err
=
err
self
.
__kat
=
kat
BasePyKatException
.
__init__
(
self
,
"Finesse returned an error running {1}: {0}"
.
format
(
self
.
__err
,
self
.
__kat
))
import
exceptions
class
BasePyKatException
(
Exception
):
def
__init__
(
self
,
msg
):
self
.
__msg
=
msg
def
__str__
(
self
):
return
self
.
__msg
class
FinesseParse
(
BasePyKatException
)
:
def
__init__
(
self
,
msg
):
BasePyKatException
.
__init__
(
self
,
"Error parsing Finesse input
\n
{0}"
.
format
(
msg
))
class
MissingFinesseEnvVar
(
BasePyKatException
)
:
def
__init__
(
self
):
BasePyKatException
.
__init__
(
self
,
"The environment variable FINESSE_DIR was not defined"
)
class
MissingFinesse
(
BasePyKatException
)
:
def
__init__
(
self
):
BasePyKatException
.
__init__
(
self
,
"Could not find the finesse executable 'kat' in '{0}',"
\
"or you do not have the permissions to run it."
\
.
format
(
os
.
environ
.
get
(
'FINESSE_DIR'
)))
class
FinesseRunError
(
BasePyKatException
)
:
def
__init__
(
self
,
err
,
kat
):
self
.
__err
=
err
self
.
__kat
=
kat
BasePyKatException
.
__init__
(
self
,
"Finesse returned an error running {1}: {0}"
.
format
(
self
.
__err
,
self
.
__kat
))
pykat/finesse.py
View file @
94f811e6
This diff is collapsed.
Click to expand it.
pykat/gui/graphics.py
View file @
94f811e6
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 01 09:13:03 2013
@author: Daniel
"""
from
PyQt4.QtGui
import
*
from
PyQt4.Qt
import
*
from
PyQt4
import
QtSvg
from
PyQt4.QtSvg
import
QGraphicsSvgItem
import
pykat.components
import
exceptions
nsize
=
10
class
NodeQGraphicItem
(
QGraphicsRectItem
):
def
__init__
(
self
,
node
,
x
,
y
,
*
args
,
**
kwargs
):
QGraphicsRectItem
.
__init__
(
self
,
*
args
,
**
kwargs
)
self
.
__node
=
node
self
.
setPos
(
x
,
y
)
item
=
QGraphicsTextItem
(
node
.
name
,
self
)
rect
=
item
.
boundingRect
()
item
.
setPos
(
-
0.5
*
rect
.
width
(),
0
)
self
.
setAcceptHoverEvents
(
True
)
self
.
marked
=
False
@
property
def
node
(
self
):
return
self
.
__node
def
refresh
(
self
):
if
not
self
.
marked
:
if
self
.
__node
.
isConnected
():
self
.
setBrush
(
QBrush
(
Qt
.
red
))
else
:
self
.
setBrush
(
QBrush
(
Qt
.
green
))
else
:
self
.
setBrush
(
QBrush
(
Qt
.
yellow
))
class
SpaceQGraphicsItem
(
QGraphicsLineItem
):
def
__init__
(
self
,
spaceComponent
):
QGraphicsLineItem
.
__init__
(
self
)
self
.
__n1
=
None
self
.
__n2
=
None
self
.
__space
=
spaceComponent
item
=
QGraphicsTextItem
(
self
.
__space
.
name
,
self
)
rect
=
item
.
boundingRect
()
item
.
setPos
(
-
0.5
*
rect
.
width
(),
0
*
rect
.
height
())
self
.
refresh
()
@
property
def
space
(
self
):
return
self
.
__space
def
refresh
(
self
):
nodes
=
self
.
__space
.
nodes
conn
=
nodes
[
0
].
amIConnected
(
self
.
__space
)
x1
=
0
y1
=
0
x2
=
0
y2
=
0
if
conn
[
0
]:
if
conn
[
1
]
!=
None
:
if
self
.
__n1
is
not
None
:
# i.e. we have a node graphic item but now it is connected to something
self
.
__n1
.
scene
().
removeItem
(
self
.
__n1
)
self
.
__n1
=
None
# now check if a connected component was returned too
if
conn
[
1
]
!=
None
:
# so this node should be attached to something
# in this case we get the position of their node
# and draw the the line from their
itm
=
conn
[
1
].
getQGraphicsItem
()
x1
=
itm
.
x
()
+
itm
.
nodedx
[
conn
[
2
]][
0
]
y1
=
itm
.
y
()
+
itm
.
nodedx
[
conn
[
2
]][
1
]
else
:
if
self
.
__n1
==
None
:
self
.
__n1
=
NodeQGraphicItem
(
nodes
[
0
],
0
,
0
,
-
nsize
/
2
,
-
nsize
/
2
,
nsize
,
nsize
,
self
)
self
.
__n1
.
setPen
(
QPen
(
Qt
.
black
,
1
))
self
.
__n1
.
setVisible
(
True
)
self
.
__n1
.
setBrush
(
QBrush
(
Qt
.
green
))
p
=
self
.
__n1
.
pos
()
x1
=
self
.
x
()
+
p
.
x
()
y1
=
self
.
y
()
+
p
.
y
()
conn
=
nodes
[
1
].
amIConnected
(
self
.
__space
)
if
conn
[
0
]:
if
conn
[
1
]
!=
None
:
if
self
.
__n2
is
not
None
:
# i.e. we have a node graphic item but now it is connected to something