Skip to content

Commit f0847fc

Browse files
committed
Slim down all and make it more Pythonic. Easy to use now with example for octupoles.
1 parent e243dbe commit f0847fc

File tree

7 files changed

+2360
-46
lines changed

7 files changed

+2360
-46
lines changed

Detuning.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1+
from __future__ import division
2+
13
import numpy as np
24

35

4-
class Detuning:
5-
def __call__(self,jx,jy):
6-
raise NotImplemented;
6+
class Detuning(object):
7+
8+
def __call__(self, jx, jy):
9+
raise NotImplemented
710

811

912
class LinearDetuning(Detuning):
10-
_startTune = 0.31;
11-
_slopex = 0.0;
12-
_slopey = 0.0;
1313

14-
def __init__(self, startTune, slopex, slopey):
14+
def __init__(self, startTune=0.31, slopex=0, slopey=0):
1515
self._startTune = startTune
1616
self._slopex = slopex
1717
self._slopey = slopey
@@ -21,11 +21,8 @@ def __call__(self, jx, jy):
2121

2222

2323
class FootprintDetuning(Detuning):
24-
_footprint = None;
25-
_plane = None;
26-
2724
# 0 for H and 1 for V
28-
def __init__(self, footprint, plane=0):
25+
def __init__(self, footprint=None, plane=0):
2926
self._footprint = footprint
3027
self._plane = plane
3128

Dispersion.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
class Dispersion:
2-
_detuning = None;
3-
_distribution = None;
4-
_Q = 0.0;
5-
_epsilon = 1E-6;
1+
from __future__ import division
62

7-
def __init__(self, distribution, detuning, Q, epsilon=1E-6):
3+
4+
class Dispersion(object):
5+
6+
def __init__(self, distribution=None, detuning=None, Q=0, epsilon=1E-6):
87
self._distribution = distribution
98
self._detuning = detuning
109
self._Q = Q
1110
self._epsilon = epsilon
1211

13-
def setEpsilon(self,epsilon):
12+
def setEpsilon(self, epsilon):
1413
self._epsilon = epsilon
1514

1615
def getEpsilon(self):
1716
return self._epsilon
1817

1918
def getValue(self, jx, jy):
20-
2119
# DI = (
2220
# (jx * self._distribution.getDJx(jx,jy)) /
2321
# (complex(self._Q - self._detuning(jx,jy), self._epsilon))

Distribution.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
23
from abc import ABCMeta, abstractmethod
34

45

@@ -25,10 +26,10 @@ class BiGaussian(Distribution):
2526

2627
def getValue(self, jx, jy):
2728
'''Return pdf of normal bi-Gaussian'''
28-
return np.exp(-(jx + jy));
29+
return np.exp(-(jx + jy))
2930

3031
def getDJx(self, jx, jy):
31-
return -self.getValue(jx, jy);
32+
return -self.getValue(jx, jy)
3233

3334
def getDJy(self, jx, jy):
34-
return -self.getValue(jx, jy);
35+
return -self.getValue(jx, jy)

Integrator.py

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import division
22

3+
import warnings
34
import numpy as np
45
from scipy.integrate import dblquad, simps
56

@@ -25,13 +26,13 @@ def integrate(self): pass
2526

2627

2728
class Boundary:
28-
_boundary = 0.0;
29+
_boundary = 0.0
2930

30-
def __init__(self,boundary):
31-
self._boundary = boundary;
31+
def __init__(self, boundary):
32+
self._boundary = boundary
3233

33-
def __call__(self,x):
34-
return self._boundary;
34+
def __call__(self, x):
35+
return self._boundary
3536

3637

3738
class DblquadIntegrator(Integrator):
@@ -50,10 +51,12 @@ def integrate(self, Q):
5051
rDispersion = RealDispersion(self.distribution, self.detuning, Q)
5152
iDispersion = ImaginaryDispersion(self.distribution, self.detuning, Q)
5253

53-
realPart, realErr = dblquad(rDispersion, self.minJx, self.maxJx,
54-
self.minJy, self.maxJy)
55-
imagPart, imagErr = dblquad(iDispersion, self.minJx, self.maxJx,
56-
self.minJy, self.maxJy)
54+
realPart, realErr = dblquad(rDispersion,
55+
self.minJx, self.maxJx,
56+
self.minJy, self.maxJy)
57+
imagPart, imagErr = dblquad(iDispersion,
58+
self.minJx, self.maxJx,
59+
self.minJy, self.maxJy)
5760

5861
return -1.0/complex(realPart, imagPart)
5962

@@ -64,17 +67,23 @@ def __init__(self, *args, **kwargs):
6467

6568
super(SimpsonIntegrator, self).__init__(*args, **kwargs)
6669

67-
if 'n_steps' not in kwargs: kwargs['n_steps'] = 2000
68-
self.jx = np.linspace(self.minJx, self.maxJx, kwargs['n_steps'])
69-
self.jy = np.linspace(self.minJy, self.maxJy, kwargs['n_steps'])
70+
if 'n_steps' not in kwargs:
71+
kwargs['n_steps'] = 1000
72+
n_steps = kwargs['n_steps']
73+
self.jx = np.linspace(self.minJx, self.maxJx, n_steps)
74+
self.jy = np.linspace(self.minJy, self.maxJy, n_steps)
7075
self.JX, self.JY = np.meshgrid(self.jx, self.jy)
7176

7277
def integrate(self, Q, epsilon=1e-6):
7378

74-
# dd = Dispersion(self.distribution, self.detuning, Q, epsilon=epsilon).getValue(self.JX, self.JY)
75-
dd = np.array([[
76-
Dispersion(self.distribution, self.detuning, Q, epsilon=epsilon).getValue(x, y)
77-
for y in self.jy] for x in self.jx])
79+
dd = Dispersion(
80+
self.distribution, self.detuning, Q, epsilon=epsilon
81+
).getValue(self.JX, self.JY)
82+
# dd = np.array([[
83+
# Dispersion(
84+
# self.distribution, self.detuning, Q, epsilon=epsilon
85+
# ).getValue(x, y)
86+
# for y in self.jy] for x in self.jx])
7887

7988
return -1./simps(simps(dd, self.jx), self.jy)
8089

@@ -85,20 +94,33 @@ def __init__(self, *args, **kwargs):
8594

8695
super(TrapzIntegrator, self).__init__(*args, **kwargs)
8796

88-
if 'n_steps' not in kwargs: kwargs['n_steps'] = 1000
89-
self.jx = np.linspace(self.minJx, self.maxJx, kwargs['n_steps'])
90-
self.jy = np.linspace(self.minJy, self.maxJy, kwargs['n_steps'])
97+
if 'n_steps' not in kwargs:
98+
kwargs['n_steps'] = 1000
99+
n_steps = 1000
100+
self.jx = np.linspace(self.minJx, self.maxJx, n_steps)
101+
self.jy = np.linspace(self.minJy, self.maxJy, n_steps)
91102
self.JX, self.JY = np.meshgrid(self.jx, self.jy)
92103

93104
def integrate(self, Q, epsilon=1e-6):
94105

95-
dd = Dispersion(self.distribution, self.detuning, Q, epsilon=epsilon).getValue(self.JX, self.JY)
106+
dd = Dispersion(
107+
self.distribution, self.detuning, Q, epsilon=epsilon
108+
).getValue(self.JX, self.JY)
109+
96110
return -1./np.trapz(np.trapz(dd, self.jx), self.jy)
97111

98112

99113
class FixedTrapezoidalIntegrator(Integrator):
100114

101-
def __init__(self, distribution=None, detuning=None, minJ=0, maxJ=18, nStep=2000):
115+
def __init__(self, distribution=None, detuning=None,
116+
minJ=0, maxJ=18, nStep=2000):
117+
warnings.simplefilter('always', DeprecationWarning)
118+
warnings.warn('Class "{:s}" '.format(self.__name__) +
119+
'is deprecated and will be replaced in the ' +
120+
'near future.',
121+
category=DeprecationWarning, stacklevel=2)
122+
warnings.simplefilter('default', DeprecationWarning)
123+
102124
self._distribution = distribution
103125
self._detuning = detuning
104126
self._minJx = minJ
@@ -158,7 +180,14 @@ class AdaptiveRectangularIntegrator(Integrator):
158180
_arrayY = None;
159181

160182

161-
def __init__(self,distribution,detuning,minJ=0.0,maxJ=18.0):
183+
def __init__(self, distribution, detuning, minJ=0.0, maxJ=18.0):
184+
warnings.simplefilter('always', DeprecationWarning)
185+
warnings.warn('Class "{:s}" '.format(self.__name__) +
186+
'is deprecated and will be replaced in the ' +
187+
'near future.',
188+
category=DeprecationWarning, stacklevel=2)
189+
warnings.simplefilter('default', DeprecationWarning)
190+
162191
self._distribution = distribution;
163192
self._detuning = detuning;
164193
self._minJx = minJ;

main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import sys, time
44
import numpy as np
55
import matplotlib.pyplot as plt
6+
plt.switch_backend('TkAgg')
67

78
from PySSD.Distribution import BiGaussian
89
from PySSD.Detuning import LinearDetuning
910
from PySSD.Dispersion import Dispersion
10-
from PySSD.Integrator import FixedTrapezoidalIntegrator, SimpsonIntegrator
11+
from PySSD.Integrator import FixedTrapezoidalIntegrator, SimpsonIntegrator, TrapzIntegrator
1112

1213
# from PySSD.Detunign import FootprintDetuning
1314
# from PySSD.Footprint import Footprint, parseDynapTune
@@ -62,8 +63,9 @@ def findQs(detuning, stepSize=5E-5, maxJ=18.0, dJ=0.1, margin=1):
6263
maxJ = 18
6364
Qs = findQs(detuning, stepSize=2e-5, maxJ=maxJ)
6465

65-
# integrator = FixedTrapezoidalIntegrator(distribution, detuning, maxJ=maxJ)
66+
integrator = FixedTrapezoidalIntegrator(distribution, detuning, maxJ=maxJ)
6667
integrator = SimpsonIntegrator(distribution, detuning, maxJ=maxJ)
68+
integrator = TrapzIntegrator(distribution, detuning, maxJ=maxJ)
6769

6870
tuneShifts = []
6971
outputFileName = './Test.sdiag';

0 commit comments

Comments
 (0)