Skip to content

Commit e9b17e2

Browse files
committed
Merge branch 'develop'
2 parents bea9330 + 58245e2 commit e9b17e2

25 files changed

+1201
-97
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*.pyc
2+
*.swp
3+
__pycache__
4+
*.h5
5+
*.sta
6+
*.txt
7+
*.mat

LHC_custom.py

Lines changed: 108 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -2,85 +2,94 @@
22
import numpy as np
33
from scipy.constants import c, e, m_p
44

5+
class EmptyObject(object):
6+
pass
7+
58
class LHC(BasicSynchrotron):
69

710
def __init__(self, n_segments, machine_configuration, optics_dict=None, **kwargs):
811

912

10-
circumference = 26658.8832
11-
longitudinal_mode = 'non-linear'
12-
p_increment = 0.
13-
charge = e
14-
mass = m_p
15-
alpha = 3.225e-04
13+
pp = EmptyObject()
14+
15+
pp.n_segments = n_segments
16+
pp.machine_configuration = machine_configuration
17+
pp.optics_dict = optics_dict
18+
19+
pp.circumference = 26658.8832
20+
pp.longitudinal_mode = 'non-linear'
21+
pp.p_increment = 0.
22+
pp.charge = e
23+
pp.mass = m_p
24+
pp.alpha = 3.225e-04
1625

1726

18-
if machine_configuration =='HLLHC-injection':
19-
alpha_x = 0.
20-
beta_x = 92.7
21-
D_x = 0.
22-
alpha_y = 0.
23-
beta_y = 93.2
24-
D_y = 0.
27+
if pp.machine_configuration =='HLLHC-injection':
28+
pp.alpha_x = 0.
29+
pp.beta_x = 92.7
30+
pp.D_x = 0.
31+
pp.alpha_y = 0.
32+
pp.beta_y = 93.2
33+
pp.D_y = 0.
2534

26-
accQ_x = 62.28
27-
accQ_y = 60.31
35+
pp.accQ_x = 62.28
36+
pp.accQ_y = 60.31
2837

29-
h_RF = 35640
30-
V_RF = 8e6
31-
dphi_RF = 0.
38+
pp.h_RF = 35640
39+
pp.V_RF = 8e6
40+
pp.dphi_RF = 0.
3241

33-
p0 = 450.e9 * e /c
42+
pp.p0 = 450.e9 * e /c
3443

35-
elif machine_configuration =='HLLHC-collision':
36-
alpha_x = 0.
37-
beta_x = 92.7
38-
D_x = 0.
39-
alpha_y = 0.
40-
beta_y = 93.2
41-
D_y = 0.
44+
elif pp.machine_configuration =='HLLHC-collision':
45+
pp.alpha_x = 0.
46+
pp.beta_x = 92.7
47+
pp.D_x = 0.
48+
pp.alpha_y = 0.
49+
pp.beta_y = 93.2
50+
pp.D_y = 0.
4251

43-
accQ_x = 62.31
44-
accQ_y = 60.32
52+
pp.accQ_x = 62.31
53+
pp.accQ_y = 60.32
4554

46-
h_RF = 35640
47-
V_RF = 16e6
48-
dphi_RF = 0.
55+
pp.h_RF = 35640
56+
pp.V_RF = 16e6
57+
pp.dphi_RF = 0.
4958

50-
p0 = 7000e9 * e /c
59+
pp.p0 = 7000e9 * e /c
5160

5261
elif machine_configuration =='LHC-collision':
53-
alpha_x = 0.
54-
beta_x = 92.7
55-
D_x = 0.
56-
alpha_y = 0.
57-
beta_y = 93.2
58-
D_y = 0.
62+
pp.alpha_x = 0.
63+
pp.beta_x = 92.7
64+
pp.D_x = 0.
65+
pp.alpha_y = 0.
66+
pp.beta_y = 93.2
67+
pp.D_y = 0.
5968

60-
accQ_x = 62.31
61-
accQ_y = 60.32
69+
pp.accQ_x = 62.31
70+
pp.accQ_y = 60.32
6271

63-
h_RF = 35640
64-
V_RF = 12e6
65-
dphi_RF = 0.
72+
pp.h_RF = 35640
73+
pp.V_RF = 12e6
74+
pp.dphi_RF = 0.
6675

67-
p0 = 7000e9 * e /c
76+
pp.p0 = 7000e9 * e /c
6877

6978

7079
else:
7180
raise ValueError('ERROR: unknown machine configuration', machine_configuration)
7281

7382
# detunings
74-
Qp_x = 0
75-
Qp_y = 0
83+
pp.Qp_x = 0
84+
pp.Qp_y = 0
7685

77-
app_x = 0
78-
app_y = 0
79-
app_xy = 0
86+
pp.app_x = 0
87+
pp.app_y = 0
88+
pp.app_xy = 0
8089

81-
i_octupole_focusing = None
82-
i_octupole_defocusing = None
83-
octupole_knob = None
90+
pp.i_octupole_focusing = None
91+
pp.i_octupole_defocusing = None
92+
pp.octupole_knob = None
8493

8594
for attr in kwargs.keys():
8695
if kwargs[attr] is not None:
@@ -89,74 +98,80 @@ def __init__(self, n_segments, machine_configuration, optics_dict=None, **kwargs
8998
else:
9099
str2print = repr(kwargs[attr])
91100
self.prints('Synchrotron init. From kwargs: %s = %s'
92-
% (attr, str2print))
93-
temp = kwargs[attr]
94-
exec('%s = temp'%attr)
101+
% (attr, str2print))
102+
103+
if not hasattr(pp, attr):
104+
raise NameError("I don't understand %s"%attr)
105+
106+
setattr(pp, attr, kwargs[attr])
95107

96108

97-
if i_octupole_focusing is not None or i_octupole_defocusing is not None:
98-
if octupole_knob is not None:
109+
if pp.i_octupole_focusing is not None or pp.i_octupole_defocusing is not None:
110+
if pp.octupole_knob is not None:
99111
raise ValueError('octupole_knobs and octupole currents cannot be used at the same time!')
100-
app_x, app_y, app_xy = self._anharmonicities_from_octupole_current_settings(i_octupole_focusing, i_octupole_defocusing)
101-
self.i_octupole_focusing = i_octupole_focusing
102-
self.i_octupole_defocusing = i_octupole_defocusing
112+
pp.app_x, pp.app_y, pp.app_xy = self._anharmonicities_from_octupole_current_settings(
113+
pp.i_octupole_focusing, pp.i_octupole_defocusing)
114+
self.i_octupole_focusing = pp.i_octupole_focusing
115+
self.i_octupole_defocusing = pp.i_octupole_defocusing
103116

104-
if octupole_knob is not None:
105-
if i_octupole_focusing is not None or i_octupole_defocusing is not None:
117+
if pp.octupole_knob is not None:
118+
if pp.i_octupole_focusing is not None or pp.i_octupole_defocusing is not None:
106119
raise ValueError('octupole_knobs and octupole currents cannot be used at the same time!')
107-
i_octupole_focusing, i_octupole_defocusing = self._octupole_currents_from_octupole_knobs(octupole_knob, p0)
108-
app_x, app_y, app_xy = self._anharmonicities_from_octupole_current_settings(i_octupole_focusing, i_octupole_defocusing)
109-
self.i_octupole_focusing = i_octupole_focusing
110-
self.i_octupole_defocusing = i_octupole_defocusing
120+
pp.i_octupole_focusing, pp.i_octupole_defocusing = self._octupole_currents_from_octupole_knobs(pp.octupole_knob, pp.p0)
121+
pp.app_x, pp.app_y, pp.app_xy = self._anharmonicities_from_octupole_current_settings(
122+
pp.i_octupole_focusing, pp.i_octupole_defocusing)
123+
self.i_octupole_focusing = pp.i_octupole_focusing
124+
self.i_octupole_defocusing = pp.i_octupole_defocusing
111125

112-
if optics_dict is not None:
113-
if n_segments is not None: raise ValueError('n_segments cannot be provided if optics_mode = "non-smooth"')
126+
if pp.optics_dict is not None:
127+
if pp.n_segments is not None: raise ValueError('n_segments cannot be provided if optics_mode = "non-smooth"')
114128

115129
for vv in 'beta_x beta_y D_x D_y alpha_x alpha_y s accQ_x accQ_y'.split():
116130
if vv in kwargs:
117131
if kwargs[vv] is not None:
118132
raise ValueError('%s cannot be provided when optics_dict is given!'%vv)
119133

120134

121-
n_segments = None
122-
circumference = None
123-
name = optics_dict['name']
135+
pp.n_segments = None
136+
pp.circumference = None
137+
pp.name = pp.optics_dict['name']
124138

125-
beta_x = optics_dict['beta_x']
126-
beta_y = optics_dict['beta_y']
139+
pp.beta_x = pp.optics_dict['beta_x']
140+
pp.beta_y = pp.optics_dict['beta_y']
127141

128142
try:
129-
D_x = optics_dict['D_x']
143+
pp.D_x = pp.optics_dict['D_x']
130144
except KeyError:
131-
D_x = 0*np.array(optics_dict['s'])
145+
pp.D_x = 0*np.array(pp.optics_dict['s'])
132146
try:
133-
D_y = kwargs['D_y']
147+
pp.D_y = kwargs['D_y']
134148
except KeyError:
135-
D_y = 0*np.array(optics_dict['s'])
149+
pp.D_y = 0*np.array(pp.optics_dict['s'])
136150

137-
alpha_x = optics_dict['alpha_x']
138-
alpha_y = optics_dict['alpha_y']
151+
pp.alpha_x = pp.optics_dict['alpha_x']
152+
pp.alpha_y = pp.optics_dict['alpha_y']
139153

140-
s = optics_dict['s']
154+
pp.s = pp.optics_dict['s']
141155

142-
accQ_x = optics_dict['accQ_x']
143-
accQ_y = optics_dict['accQ_y']
144-
optics_mode = 'non-smooth'
156+
pp.accQ_x = pp.optics_dict['accQ_x']
157+
pp.accQ_y = pp.optics_dict['accQ_y']
158+
pp.optics_mode = 'non-smooth'
145159
else:
146-
optics_mode = 'smooth'
147-
s = None
148-
name = None
160+
pp.optics_mode = 'smooth'
161+
pp.s = None
162+
pp.name = None
149163

150164

151165

152166

153-
super(LHC, self).__init__(optics_mode=optics_mode, circumference=circumference, n_segments=n_segments,
154-
s=s, name=name,
155-
alpha_x=alpha_x, beta_x=beta_x, D_x=D_x, alpha_y=alpha_y, beta_y=beta_y, D_y=D_y,
156-
accQ_x=accQ_x, accQ_y=accQ_y, Qp_x=Qp_x, Qp_y=Qp_y, app_x=app_x, app_y=app_y, app_xy=app_xy,
157-
alpha_mom_compaction=alpha, longitudinal_mode=longitudinal_mode,
158-
h_RF=np.atleast_1d(h_RF), V_RF=np.atleast_1d(V_RF), dphi_RF=np.atleast_1d(dphi_RF), p0=p0, p_increment=p_increment,
159-
charge=charge, mass=mass, RF_at='end_of_transverse')
167+
super(LHC, self).__init__(optics_mode=pp.optics_mode, circumference=pp.circumference, n_segments=pp.n_segments,
168+
s=pp.s, name=pp.name,
169+
alpha_x=pp.alpha_x, beta_x=pp.beta_x, D_x=pp.D_x, alpha_y=pp.alpha_y, beta_y=pp.beta_y, D_y=pp.D_y,
170+
accQ_x=pp.accQ_x, accQ_y=pp.accQ_y, Qp_x=pp.Qp_x, Qp_y=pp.Qp_y, app_x=pp.app_x, app_y=pp.app_y, app_xy=pp.app_xy,
171+
alpha_mom_compaction=pp.alpha, longitudinal_mode=pp.longitudinal_mode,
172+
h_RF=np.atleast_1d(pp.h_RF), V_RF=np.atleast_1d(pp.V_RF), dphi_RF=np.atleast_1d(pp.dphi_RF),
173+
p0=pp.p0, p_increment=pp.p_increment,
174+
charge=pp.charge, mass=pp.mass, RF_at='end_of_transverse')
160175

161176
def _anharmonicities_from_octupole_current_settings(self, i_octupole_focusing, i_octupole_defocusing):
162177
"""Calculate the constants of proportionality app_x, app_y and

Save_Load_Status.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import h5py
2+
3+
import os
4+
5+
class SimulationStatus(object):
6+
def __init__(self, N_turns_per_run=None, N_turns_target=None, check_for_resubmit=False):
7+
self.N_turns_target = N_turns_target
8+
self.N_turns_per_run = N_turns_per_run
9+
self.check_for_resubmit = check_for_resubmit
10+
11+
self.filename = 'simulation_status.sta'
12+
13+
def dump_to_file(self):
14+
lines = []
15+
lines.append('present_simulation_part = %d\n'%self.present_simulation_part)
16+
lines.append('first_turn_part = %d\n'%self.first_turn_part)
17+
lines.append('last_turn_part = %d\n'%self.last_turn_part)
18+
lines.append('present_part_done = %s\n'%repr(self.present_part_done))
19+
lines.append('present_part_running = %s\n'%repr(self.present_part_running))
20+
21+
with open(self.filename, 'w') as fid:
22+
fid.writelines(lines)
23+
24+
def load_from_file(self):
25+
ddd = {}
26+
with open(self.filename) as fid:
27+
exec(fid.read(), ddd)
28+
self.present_simulation_part = ddd['present_simulation_part']
29+
self.first_turn_part = ddd['first_turn_part']
30+
self.last_turn_part = ddd['last_turn_part']
31+
self.present_part_done = ddd['present_part_done']
32+
self.present_part_running = ddd['present_part_running']
33+
34+
def print_from_file(self):
35+
with open(self.filename) as fid:
36+
print fid.read()
37+
38+
def before_simulation(self):
39+
self.first_run = False
40+
try:
41+
self.load_from_file()
42+
self.present_simulation_part+=1
43+
self.first_turn_part += self.N_turns_per_run
44+
self.last_turn_part += self.N_turns_per_run
45+
except IOError:
46+
print 'Simulation Status not found --> initializing simulation'
47+
self.first_run = True
48+
self.present_simulation_part = 0
49+
self.first_turn_part = 0
50+
self.last_turn_part = self.N_turns_per_run-1
51+
self.present_part_done = True
52+
self.present_part_running = False
53+
54+
if not(self.present_part_done) or self.present_part_running:
55+
raise ValueError('The previous simulation part seems not finished!!!!')
56+
57+
self.present_part_done = False
58+
self.present_part_running = True
59+
60+
self.dump_to_file()
61+
62+
print 'Starting part:\n\n'
63+
self.print_from_file()
64+
print '\n\n'
65+
66+
def after_simulation(self):
67+
self.load_from_file()
68+
self.present_part_done = True
69+
self.present_part_running = False
70+
self.dump_to_file()
71+
72+
print 'Done part:\n\n'
73+
self.print_from_file()
74+
print '\n\n'
75+
76+
if self.check_for_resubmit:
77+
78+
if self.last_turn_part+1<self.N_turns_target:
79+
print 'resubmit the job'
80+
os.system('bsub < job.cmd')
81+
82+
def restart_last(self):
83+
84+
self.load_from_file()
85+
self.N_turns_per_run = self.last_turn_part - self.first_turn_part + 1
86+
87+
self.present_simulation_part-=1
88+
self.first_turn_part -= self.N_turns_per_run
89+
self.last_turn_part -= self.N_turns_per_run
90+
91+
self.present_part_done = True
92+
self.present_part_running = False
93+
94+
self.dump_to_file()
95+
96+
print 'Restored status:\n\n'
97+
self.print_from_file()
98+
print '\n\n'
99+

Simulation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def init_master(self):
304304
if pp.N_turns!=pp.N_turns_target:
305305
raise ValueError('In footprint mode you need to set N_turns_target=N_turns_per_run!')
306306

307-
import Save_Load_Status as SLS
307+
import PyPARIS_sim_class.Save_Load_Status as SLS
308308
SimSt = SLS.SimulationStatus(N_turns_per_run=pp.N_turns, check_for_resubmit = True, N_turns_target=pp.N_turns_target)
309309
SimSt.before_simulation()
310310
self.SimSt = SimSt

__init__.py

Whitespace-only changes.

__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.0.0'
1+
__version__ = '1.1.0'

0 commit comments

Comments
 (0)