Skip to content

Commit af9c7aa

Browse files
author
Guido Sterbini
committed
Merge branch 'master' of github.com:lhcopt/lhcmask
2 parents 6ed4624 + 11842a9 commit af9c7aa

File tree

4 files changed

+66
-25
lines changed

4 files changed

+66
-25
lines changed

pymask/pymasktools.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,13 +408,13 @@ def generate_xsuite_line(mad, seq_name, bb_df,
408408
folder_name=None, skip_mad_use=False,
409409
prepare_line_for_xtrack=True,
410410
steps_for_finite_diffs={'dx': 1e-8, 'dpx': 1e-11,
411-
'dy': 1e-8, 'dpy': 1e-11, 'dzeta': 1e-7, 'ddelta': 1e-8},
411+
'dy': 1e-8, 'dpy': 1e-11, 'dzeta': 1e-7, 'ddelta': 1e-8},
412412
deferred_expressions=True):
413413

414414
# Build xsuite model
415415
print('Start building xtrack line...')
416416
line = xt.Line.from_madx_sequence(
417-
mad.sequence[seq_name], apply_madx_errors=True,
417+
mad.sequence[seq_name], apply_madx_errors=True,
418418
deferred_expressions=deferred_expressions)
419419
print('Done building xtrack.')
420420

@@ -478,7 +478,7 @@ def generate_xsuite_line(mad, seq_name, bb_df,
478478
with open(folder_name +
479479
'/line_bb_for_tracking.json', 'w') as fid:
480480
json.dump(line_bb_for_tracking_dict, fid, cls=JEncoder)
481-
481+
return tracker, line_bb_for_tracking_dict
482482

483483
def save_mad_sequence_and_error(mad, seq_name, filename='lhc'):
484484
mad.select(flag="error",clear=True)

python_examples/hl_lhc_collisions_python/001_reload.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
import numpy as np
33
import xtrack as xt
4-
4+
import pandas as pd
55
sys.path.append('./modules')
66
import pymask as pm
77

@@ -15,3 +15,23 @@
1515
mad.set(format=".15g")
1616
mad.twiss(rmatrix = True)
1717

18+
sequence_to_track = 'lhcb1'
19+
bb_df_track = pd.read_pickle('bb_df_track.pickle')
20+
optics_and_co_at_start_ring_from_madx = pm.get_optics_and_orbit_at_start_ring(
21+
mad, sequence_to_track, skip_mad_use=True)
22+
#with open('./optics_orbit_at_start_ring_from_madx.json', 'w') as fid:
23+
# json.dump(optics_and_co_at_start_ring_from_madx, fid, cls=pm.JEncoder
24+
25+
########################
26+
# Generate xtrack line #
27+
########################
28+
#if enable_bb_legacy:
29+
# print('xtrack line is not generated with bb legacy macros')
30+
# else:
31+
32+
tracker, line_bb_for_tracking_dict = pm.generate_xsuite_line(mad,
33+
sequence_to_track, bb_df_track,
34+
optics_and_co_at_start_ring_from_madx,
35+
folder_name = './xsuite_lines',
36+
skip_mad_use=True,
37+
prepare_line_for_xtrack=True)

python_examples/hl_lhc_collisions_python/checks_and_doc/t004_check_output_consistency.py

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
'path_ref': '../',
3131
'type_ref': 'sixtrack',
3232
'rtol': 4e-7,
33-
'atol': 1e-100,
33+
'atol': 1e-13,
3434
'strict': True,
3535
}
3636
]
@@ -153,16 +153,22 @@ def prepare_line(path, input_type):
153153
# Check if the relative error is small
154154
val_test = dtest[kk]
155155
val_ref = dref[kk]
156-
try:
157-
if not np.isscalar(val_ref) and len(val_ref) != len(val_test):
158-
diff_rel = 100
159-
#lmin = min(len(val_ref), len(val_test))
160-
#val_test = val_test[:lmin]
161-
#val_ref = val_ref[:lmin]
156+
if not np.isscalar(val_ref):
157+
if len(val_ref) != len(val_test):
158+
diff_rel = 100
162159
else:
163-
diff_rel = norm(np.array(val_test) - np.array(val_ref)) / norm(val_test)
164-
except ZeroDivisionError:
165-
diff_rel = 100.0
160+
for iiii, (vvr, vvt) in enumerate(list(zip(val_ref, val_test))):
161+
if not np.isclose(vvr, vvt, atol=atol, rtol=rtol):
162+
print(f'Issue found on `{kk}[{iiii}]`')
163+
diff_rel = 1000
164+
else:
165+
diff_rel = 0
166+
else:
167+
if val_ref > 0:
168+
diff_rel = np.abs((val_test - val_ref)/val_ref)
169+
else:
170+
diff_rel = 100
171+
166172
if diff_rel < rtol:
167173
continue
168174

@@ -209,12 +215,15 @@ def prepare_line(path, input_type):
209215
if len(val_ref) == 0 and len(val_test) == 0:
210216
continue
211217
else:
212-
diff_abs = norm(np.array(val_test) - np.array(val_ref))
213-
diff_rel = diff_abs/norm(val_test)
218+
for iiii, (vvr, vvt) in enumerate(list(zip(val_ref, val_test))):
219+
if not np.isclose(vvr, vvt, atol=atol, rtol=rtol):
220+
print(f'Issue found on `{kk}[{iiii}]`')
221+
diff_rel = 1000
222+
break
223+
else:
224+
diff_rel = 0
214225
if diff_rel < rtol:
215226
continue
216-
if diff_abs < atol:
217-
continue
218227

219228
# Exception: correctors involved in lumi leveling
220229
passed_corr = False
@@ -229,9 +238,13 @@ def prepare_line(path, input_type):
229238
'mcbch.5r2.b2', 'mcbch.a5r2.b2', 'mcbyh.4r2.b2', 'mcbxh.3r2',
230239
'mcbyh.a4l2.b2', 'mcbyh.5l2.b2', 'mcbyv.5r8.b2', 'mcbyv.a4r8.b2',
231240
'mcbxv.3r8', 'mcbyv.4l8.b2', 'mcbcv.b5l8.b2']:
232-
if nn_corr in nn_test and diff_rel < 1e-2:
233-
passed_corr = True
234-
break
241+
if nn_corr in nn_test and kk in ['knl','ksl','bal']:
242+
assert len(val_ref)<=2
243+
assert len(val_test)<=2
244+
diff_rel = norm(val_test-val_ref)/norm(val_ref)
245+
passed_corr = (diff_rel < 1e-2)
246+
if passed_corr:
247+
break
235248
if not(strict) and passed_corr:
236249
continue
237250

@@ -276,7 +289,14 @@ def prepare_line(path, input_type):
276289
if kk == "px_co" or kk == 'py_co':
277290
if diff_abs <30e-9:
278291
continue
279-
292+
if isinstance(ee_test, xt.XYShift):
293+
if kk in ['dx','dy']:
294+
if diff_abs <1e-9:
295+
continue
296+
if isinstance(ee_test, xt.SRotation):
297+
if kk in ['sin_z', 'cos_z', 'angle']:
298+
if diff_abs <1e-9:
299+
continue
280300
# If it got here it means that no condition above is met
281301
raise ValueError("Too large discrepancy!")
282302
print(

python_examples/hl_lhc_collisions_python/checks_and_doc/t007_check_orbit_and_lin_normal_form.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
tracker.track(particles)
2222

2323
for nn in 'x px y py zeta delta'.split():
24-
assert np.abs(getattr(particles, nn) - getattr(partCO, nn)) < 2e-11
24+
assert np.abs(getattr(particles, nn) - getattr(partCO, nn)) < 3e-11
2525

2626
WW = np.array(line_dict['WW_finite_diffs'])
2727
WWinv = np.array(line_dict['WWInv_finite_diffs'])
@@ -39,13 +39,14 @@
3939

4040
particles_matched = xp.build_particles(particle_on_co=partCO,
4141
x_norm=x_norm, px_norm=px_norm,
42-
R_matrix=np.array(line_dict['RR_finite_diffs']))
42+
R_matrix=np.array(line_dict['RR_finite_diffs']),
43+
symplectify=True)
4344
particles_test = particles_matched.copy()
4445
tracker.track(particles_test, num_turns=10)
4546

4647
i_matched = np.argmax(particles_matched.x)
4748
i_test = np.argmax(particles_test.x)
48-
assert np.abs(particles_test.x[i_test] - particles_matched.x[i_matched]) < 1e-6
49+
assert np.abs(particles_test.x[i_test] - particles_matched.x[i_matched]) < 2e-6
4950
assert np.abs(particles_test.px[i_test] - particles_matched.px[i_matched]) < 1e-7
5051

5152

0 commit comments

Comments
 (0)