Commit 7450f30c authored by Daniel Brown's avatar Daniel Brown
Browse files

Adding lkat test

parent ab06a7ed
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, which involves generating a ctypes wrapper using The command I use to generate the wrapper is:
/Users/ddb/svn/ctypesgen-read-only/ -o ./kat.h
You then also need to build 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
# return our cost function
return -1 *
minimize(Fmin, [maxphi.value], method='Nelder-Mead')
maxphi.value = m1.phi
print "Process: Maximum power =",
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
maxtem 2
attr m1 Rc -1000
attr m2 Rc 1000
cav c1 m1 n3 m2 n4
kat = pykat.finesse.kat()
maxphi = Value('d', 100)
p = kat.getProcess(callback, maxphi=maxphi)
print "Host: Received maximum phi =", maxphi.value
......@@ -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
pykat_web = ""
def f__lkat_process(callback, cmd, kwargs):
lkat = ctypes.PyDLL("")
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
callback(lkat, **kwargs)
except Exception as ex:
print "Exception caught in python: ", ex.message
# This should always be called no matter what
class katRun(object):
def __init__(self):
self.runDateTime =
......@@ -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.
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