Commit 7d3f7954 authored by Daniel Brown's avatar Daniel Brown
Browse files

Merge branch 'master' of gitmaster.atlas.aei.uni-hannover.de:pykat/pykat

parents a83ab33a 72a5c650
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import matplotlib
BACKEND = 'Qt4Agg'
matplotlib.use(BACKEND)
import pylab as pl
from pykat.utilities.optics.gaussian_beams import HG_beam, beam_param
from pykat.fft.fft import *
import numpy as np
import scipy
def main():
# wavelength
Lambda = 1064.0E-9
# distance to propagate/focal length of lens
D = 10
# mix coefficients
c1 = 0.7
c2 = 0.3
# mode indices
n1 = 2
m1 = 3
n2 = 1
m2 = 0
######## Generate Grid stucture required for FFT propagation ####
xpoints = 512
ypoints = 512
xsize = 0.05
ysize = 0.05
# Apply offset such that the center of the beam lies in the
# center of a grid tile
xoffset = -0.5*xsize/xpoints
yoffset = -0.5*ysize/ypoints
shape = grid(xpoints, ypoints, xsize, ysize, xoffset, yoffset)
x = shape.xaxis
y = shape.yaxis
######## Generates a mixture of fields ################
gx = beam_param(w0=2e-3, z=0)
gy = beam_param(w0=2e-3, z=0)
beam = HG_beam(gx,gy,n1,m1)
field1 = beam.Unm(x,y)
beam2 = HG_beam(gx,gy,n2,m2)
field2 = beam2.Unm(x,y)
global field, laser1, laser2
field = np.sqrt(c1)*field1 + np.sqrt(c2)*field2
####### Apply phase plate #######################################
laser1 = field*(np.conjugate(field1))
laser2 = field*(np.conjugate(field2))
####### Propagates the field by FFT ##############################
laser1 = FFT_propagate(laser1,shape,Lambda,D,1)
laser2 = FFT_propagate(laser2,shape,Lambda,D,1)
f=D
#laser1 = apply_lens(laser1, shape, Lambda, f)
#laser2 = apply_lens(laser2, shape, Lambda, f)
laser1 = apply_thin_lens(laser1, shape, Lambda, f)
laser2 = apply_thin_lens(laser2, shape, Lambda, f)
laser1 = FFT_propagate(laser1,shape,Lambda,D,1)
laser2 = FFT_propagate(laser2,shape,Lambda,D,1)
# midpoint computation for even number of points only!
midx=(xpoints)//2
midy=(ypoints)//2
coef1 = np.abs(laser1[midx,midy])
coef2 = np.abs(laser2[midx,midy])
ratio = (coef1/coef2)**2
pc2 = 1/(1+ratio)
pc1 = pc2*ratio
print("c1 {0}, coef1 {1}, error {3} (raw output {2})".format(c1, pc1, coef1, np.abs(c1-pc1)))
print("c2 {0}, coef2 {1}, error {3} (raw output {2})".format(c2, pc2, coef2, np.abs(c2-pc2)))
# plot hand tuned for certain ranges and sizes, not automtically scaled
fig=pl.figure(110)
fig.clear()
off1=xpoints/10
off2=xpoints/6
pl.subplot(1, 3, 1)
pl.imshow(abs(field))
pl.xlim(midx-off1,midx+off1)
pl.ylim(midy-off1,midy+off1)
pl.draw()
pl.subplot(1, 3, 2)
pl.imshow(abs(laser1))
pl.xlim(midx-off2,midx+off2)
pl.ylim(midy-off2,midy+off2)
pl.draw()
pl.subplot(1, 3, 3)
pl.imshow(abs(laser2))
pl.xlim(midx-off2,midx+off2)
pl.ylim(midy-off2,midy+off2)
pl.draw()
if in_ipython():
pl.show(block=0)
else:
pl.show(block=1)
# testing if the script is run from within ipython
def in_ipython():
try:
__IPYTHON__
except NameError:
return False
else:
return True
if __name__ == '__main__':
main()
"""
---------------------------------------------------------
Simple example to show the use of the utility functions:
- cavity_w1w2_Rc1Rc2(Lambda, L, w1, w2)
- cavity_info(Lambda, L, Rc1, Rc2)
Andreas Freise 01.12.2014
http://www.gwoptics.org/pykat/
---------------------------------------------------------
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from pykat.optics.fabry_perot import *
import numpy as np
def main():
# wavelength
Lambda = 1064.0E-9
L = 3994.515
print("Advanced LIGO")
# attr ITMY Rc -1940.7
# attr ETMY Rc 2242.4
w1=0.053
w2=0.062
[g1, g2, Rc1, Rc2] = cavity_w1w2_Rc1Rc2(Lambda, L, w1, w2)
print("w1 = {0}, w2 = {1}".format(w1,w2))
print("g1 = {0}, Rc1 = {1}".format(g1,Rc1))
print("g2 = {0}, Rc2 = {1}".format(g2,Rc2))
print("g1*g2 = {0}".format(g1*g2))
print("A+")
# attr ITMX Rc -1852.9
# attr ETMX Rc 2174.3
w1=0.08
w2=0.094
[g1, g2, Rc1, Rc2] = cavity_w1w2_Rc1Rc2(Lambda, L, w1, w2)
print("w1 = {0}, w2 = {1}".format(w1,w2))
print("g1 = {0}, Rc1 = {1}".format(g1,Rc1))
print("g2 = {0}, Rc2 = {1}".format(g2,Rc2))
print("g1*g2 = {0}".format(g1*g2))
# cross check
[zr,w0,z1,wc1,wc2]=cavity_info(Lambda, L, Rc1, Rc2)
print("(cross check: w1 = {0}, w2 = {1})".format(wc1,wc2))
if __name__ == '__main__':
main()
You can redistribute and/or modify this library under the terms of the
GNU LGPL license or BSD license (or both).
---
progressbar - Text progress bar library for python.
Copyright (C) 2005 Nilton Volpato
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
---
progressbar - Text progress bar library for python
Copyright (c) 2008 Nilton Volpato
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
a. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
b. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
c. Neither the name of the author nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
Text progress bar library for Python.
A text progress bar is typically used to display the progress of a long
running operation, providing a visual cue that processing is underway.
The ProgressBar class manages the current progress, and the format of the line
is given by a number of widgets. A widget is an object that may display
differently depending on the state of the progress bar. There are three types
of widgets:
- a string, which always shows itself
- a ProgressBarWidget, which may return a different value every time its
update method is called
- a ProgressBarWidgetHFill, which is like ProgressBarWidget, except it
expands to fill the remaining width of the line.
The progressbar module is very easy to use, yet very powerful. It will also
automatically enable features like auto-resizing when the system supports it.
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# progressbar - Text progress bar library for Python.
# Copyright (c) 2005 Nilton Volpato
# Copyright (c) 2012 Rick van Hattem
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
'''Text progress bar library for Python.
A text progress bar is typically used to display the progress of a long
running operation, providing a visual cue that processing is underway.
The ProgressBar class manages the current progress, and the format of the line
is given by a number of widgets. A widget is an object that may display
differently depending on the state of the progress bar. There are three types
of widgets:
- a string, which always shows itself
- a ProgressBarWidget, which may return a different value every time its
update method is called
- a ProgressBarWidgetHFill, which is like ProgressBarWidget, except it
expands to fill the remaining width of the line.
The progressbar module is very easy to use, yet very powerful. It will also
automatically enable features like auto-resizing when the system supports it.
'''
from __future__ import division, absolute_import, with_statement
import math
import os
import signal
import sys
import time
from datetime import date
try:
from fcntl import ioctl
from array import array
import termios
except ImportError: # pragma: no cover
pass
try:
from cStringIO import StringIO
except ImportError: # pragma: no cover
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
from pykat.external.progressbar.widgets import *
__author__ = 'Rick van Hattem'
__author_email__ = 'Rick.van.Hattem@Fawo.nl'
__date__ = str(date.today())
__version__ = '2.7.3'
class UnknownLength:
pass
class ProgressBar(object):
'''The ProgressBar class which updates and prints the bar.
A common way of using it is like:
>>> pbar = ProgressBar().start()
>>> for i in range(100):
... pbar.update(i+1)
... # do something
...
>>> pbar.finish()
You can also use a ProgressBar as an iterator:
>>> progress = ProgressBar()
>>> some_iterable = range(100)
>>> for i in progress(some_iterable):
... # do something
... pass
...
Since the progress bar is incredibly customizable you can specify
different widgets of any type in any order. You can even write your own
widgets! However, since there are already a good number of widgets you
should probably play around with them before moving on to create your own
widgets.
The term_width parameter represents the current terminal width. If the
parameter is set to an integer then the progress bar will use that,
otherwise it will attempt to determine the terminal width falling back to
80 columns if the width cannot be determined.
When implementing a widget's update method you are passed a reference to
the current progress bar. As a result, you have access to the
ProgressBar's methods and attributes. Although there is nothing preventing
you from changing the ProgressBar you should treat it as read only.
Useful methods and attributes include (Public API):
- currval: current progress (0 <= currval <= maxval)
- maxval: maximum (and final) value
- finished: True if the bar has finished (reached 100%)
- start_time: the time when start() method of ProgressBar was called
- seconds_elapsed: seconds elapsed since start_time and last call to
update
- percentage(): progress in percent [0..100]
'''
_DEFAULT_MAXVAL = 100
_DEFAULT_TERMSIZE = 80
def __init__(self, maxval=None, widgets=None, term_width=None, poll=0.1,
left_justify=True, fd=sys.stderr, redirect_stderr=False,
redirect_stdout=False):
'''Initializes a progress bar with sane defaults'''
if widgets is None:
# Don't share widgets with any other progress bars
widgets = self.default_widgets()
self.maxval = maxval
self.widgets = widgets
self.fd = fd
self.left_justify = left_justify
self.redirect_stderr = redirect_stderr
self.redirect_stdout = redirect_stdout
self.signal_set = False
if term_width is not None:
self.term_width = term_width
else:
try:
self._handle_resize()
signal.signal(signal.SIGWINCH, self._handle_resize)
self.signal_set = True
except (SystemExit, KeyboardInterrupt): # pragma: no cover
raise
except: # pragma: no cover
self.term_width = self._env_size()
self.__iterable = None
self._update_widgets()
self.currval = 0
self.finished = False
self.last_update_time = None
self.poll = poll
self.seconds_elapsed = 0
self.start_time = None
self.update_interval = 1
def default_widgets(self):
return [
Percentage(), ' (', SimpleProgress(), ')', ' ', Bar(), ' ',
Timer(), ' ', AdaptiveETA(),
]
def __call__(self, iterable, maxval=None):
'Use a ProgressBar to iterate through an iterable'
if maxval is None:
try:
self.maxval = len(iterable)
except:
if self.maxval is None:
self.maxval = UnknownLength
else:
self.maxval = maxval
self.__iterable = iter(iterable)
return self
def __iter__(self):
return self
def __next__(self):
try:
value = next(self.__iterable)
if self.start_time is None:
self.start()
else:
self.update(self.currval + 1)
return value
except StopIteration:
self.finish()
raise
def __exit__(self, exc_type, exc_value, traceback):
self.finish()
def __enter__(self):
return self.start()
# Create an alias so that Python 2.x won't complain about not being
# an iterator.
next = __next__
def __iadd__(self, value):
'Updates the ProgressBar by adding a new value.'
self.update(self.currval + value)
return self
def _env_size(self):
'Tries to find the term_width from the environment.'
return int(os.environ.get('COLUMNS', self._DEFAULT_TERMSIZE)) - 1
def _handle_resize(self, signum=None, frame=None):
'Tries to catch resize signals sent from the terminal.'
h, w = array('h', ioctl(self.fd, termios.TIOCGWINSZ, '\0' * 8))[:2]
self.term_width = w
def percentage(self):
'Returns the progress as a percentage.'
return self.currval * 100.0 / (self.maxval or 1)
percent = property(percentage)
def _format_widgets(self):
result = []
expanding = []
width = self.term_width
for index, widget in enumerate(self.widgets):
if isinstance(widget, WidgetHFill):
result.append(widget)
expanding.insert(0, index)
else:
widget = format_updatable(widget, self)
result.append(widget)
width -= len(widget)
count = len(expanding)
while count:
portion = max(int(math.ceil(width * 1. / count)), 0)
index = expanding.pop()
count -= 1
widget = result[index].update(self, portion)
width -= len(widget)
result[index] = widget
return result
def _format_line(self):
'Joins the widgets and justifies the line'
widgets = ''.join(self._format_widgets())
if self.left_justify:
return widgets.ljust(self.term_width)
else:
return widgets.rjust(self.term_width)
def _need_update(self):
'Returns whether the ProgressBar should redraw the line.'
if self.currval >= self.next_update or self.finished:
return True
delta = time.time() - self.last_update_time
return self._time_sensitive and delta > self.poll
def _update_widgets(self):
'Checks all widgets for the time sensitive bit'
self._time_sensitive = any(getattr(w, 'TIME_SENSITIVE', False)
for w in self.widgets)
def update(self, value=None):
'Updates the ProgressBar to a new value.'
if value is not None and value is not UnknownLength:
if (self.maxval is not UnknownLength
and not 0 <= value <= self.maxval
and not value < self.currval):
raise ValueError('Value out of range')
self.currval = value
if self.start_time is None:
self.start()
self.update(value)
if not self._need_update():
return
if self.redirect_stderr and sys.stderr.tell():
self.fd.write('\r' + ' ' * self.term_width + '\r')
self._stderr.write(sys.stderr.getvalue())
self._stderr.flush()
sys.stderr = StringIO()
if self.redirect_stdout and sys.stdout.tell():
self.fd.write('\r' + ' ' * self.term_width + '\r')
self._stdout.write(sys.stdout.getvalue())
self._stdout.flush()
sys.stdout = StringIO()
now = time.time()
self.seconds_elapsed = now - self.start_time
self.next_update = self.currval + self.update_interval
self.fd.write('\r' + self._format_line())
self.last_update_time = now
def start(self):
'''Starts measuring time, and prints the bar at 0%.
It returns self so you can use it like this:
>>> pbar = ProgressBar().start()
>>> for i in range(100):
... # do something
... pbar.update(i+1)
...
>>> pbar.finish()
'''
if self.redirect_stderr:
self._stderr = sys.stderr
sys.stderr = StringIO()
if self.redirect_stdout:
self._stdout = sys.stdout
sys.stdout = StringIO()
if self.maxval is None:
self.maxval = self._DEFAULT_MAXVAL
self.num_intervals = max(100, self.term_width)
self.next_update = 0
if self.maxval is not UnknownLength:
if self.maxval < 0:
raise ValueError('Value out of range')
self.update_interval = self.maxval / self.num_intervals
self.start_time = self.last_update_time = time.time()