diff --git a/examples/lkat_optimisation.py b/examples/lkat_optimisation.py new file mode 100644 index 0000000000000000000000000000000000000000..3ad08c991276efc11410614e719d44c361be330a --- /dev/null +++ b/examples/lkat_optimisation.py @@ -0,0 +1,84 @@ +""" +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 diff --git a/pykat/finesse.py b/pykat/finesse.py index f19c003d0e71f6d9403a14afd1f73a32790c58c1..fd4f0b4872e7046e6bfb641f76250bc095660c99 100644 --- a/pykat/finesse.py +++ b/pykat/finesse.py @@ -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.