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
7450f30c
Commit
7450f30c
authored
Mar 03, 2014
by
Daniel Brown
Browse files
Adding lkat test
parent
ab06a7ed
Changes
2
Hide whitespace changes
Inline
Side-by-side
examples/lkat_optimisation.py
0 → 100644
View file @
7450f30c
"""
This example is a new feature regarding the use of Finesse as a shared library rather than
as an executable. It is still very experimental! The shared library is accessed using
multiprocessing, which spawns an entire new process to run Finesse in. To use this requires
firstly generating pylibkat.py, which involves generating a ctypes wrapper using
https://code.google.com/p/ctypesgen/. The command I use to generate the wrapper is:
/Users/ddb/svn/ctypesgen-read-only/ctypesgen.py -o pylibkat.py ./kat.h
You then also need to build libkat.so shared library from the python_shared branch of Finesse.
Once the above is done the following script may or may not work...
"""
import
ctypes
import
pylibkat
import
scipy
from
scipy.optimize
import
minimize
import
pylab
import
time
import
numpy
as
np
from
multiprocessing
import
Process
,
Manager
,
Value
import
pykat
def
callback
(
lkat
,
maxphi
):
"""
This callback will be run in a completly different process to that which this
script started. So we can't simply pass values back and forth. The callback
arguments are:
lkat - The handle to the finesse instance
maxphi - a shared object between this and the main process, see multiprocessing.Value
"""
print
"Entering callback..."
# first we need to get a handle on the internals of Finesse
inter
=
pylibkat
.
interferometer
.
in_dll
(
lkat
,
"inter"
)
m1
=
inter
.
mirror_list
[
0
]
m2
=
inter
.
mirror_list
[
1
]
circ
=
inter
.
output_data_list
[
0
]
def
Fmin
(
x
):
# change any variables we are interested in
m1
.
phi
=
x
[
0
]
# now step the simulation forward
lkat
.
_pykat_step
()
# return our cost function
return
-
1
*
circ
.
re
minimize
(
Fmin
,
[
maxphi
.
value
],
method
=
'Nelder-Mead'
)
maxphi
.
value
=
m1
.
phi
print
"Process: Maximum power ="
,
circ
.
re
print
"Process: Mirror tuning ="
,
m1
.
phi
cmd
=
"""
l l1 1 0 n1
s s1 1 n1 n2
m m1 0.99 0.01 0 n2 n3
s s2 100 n3 n4
m m2 0.99 0.01 0 n4 n5
pd circ n3
noxaxis
maxtem 2
attr m1 Rc -1000
attr m2 Rc 1000
cav c1 m1 n3 m2 n4
"""
kat
=
pykat
.
finesse
.
kat
()
kat
.
parseCommands
(
cmd
)
maxphi
=
Value
(
'd'
,
100
)
p
=
kat
.
getProcess
(
callback
,
maxphi
=
maxphi
)
p
.
start
()
p
.
join
()
print
"Host: Received maximum phi ="
,
maxphi
.
value
pykat/finesse.py
View file @
7450f30c
...
...
@@ -35,7 +35,7 @@ import warnings
import
re
import
itertools
import
ctypes
import
collections
from
collections
import
namedtuple
,
OrderedDict
...
...
@@ -52,10 +52,34 @@ import pykat.exceptions as pkex
from
PyQt4.QtCore
import
QCoreApplication
from
PyQt4.QtGui
import
QApplication
from
multiprocessing
import
Process
,
Manager
NO_GUI
=
False
NO_BLOCK
=
"NO_BLOCK"
pykat_web
=
"www.gwoptics.org/pykat"
def
f__lkat_process
(
callback
,
cmd
,
kwargs
):
"""
"""
lkat
=
ctypes
.
PyDLL
(
"libkat.so.0"
)
try
:
lkat
.
_pykat_preInit
()
# must always be called, sets up
# exception handling and such no simulation
# specifc code here
# reads in the kat.ini and setups up other parts
lkat
.
_pykat_init
()
lkat
.
_pykat_setup
(
cmd
)
callback
(
lkat
,
**
kwargs
)
except
Exception
as
ex
:
print
"Exception caught in python: "
,
ex
.
message
finally
:
# This should always be called no matter what
lkat
.
_pykat_finish
(
0
)
class
katRun
(
object
):
def
__init__
(
self
):
self
.
runDateTime
=
datetime
.
datetime
.
now
()
...
...
@@ -576,7 +600,16 @@ class kat(object):
except
pkex
.
BasePyKatException
as
ex
:
print
ex
def
getProcess
(
self
,
callback
,
**
kwargs
):
"""
"""
cmd
=
"
\n
"
.
join
(
self
.
generateKatScript
())
return
Process
(
target
=
f__lkat_process
,
args
=
(
callback
,
cmd
,
kwargs
))
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.
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment