-
Notifications
You must be signed in to change notification settings - Fork 63
Splineboris (Cleaned) #758
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
dec4ebb
4b464d8
1aa77a2
d529149
14505c7
8e24191
7fe1615
f8d7767
aeb4ec6
99658c8
bb4a6c9
ff46ecc
a743d7d
6aeaa55
9944b80
687feb7
e898d0a
a498e6f
66683d2
c55d8eb
d29698a
4b8a8ec
2ae1350
cb5a88c
3edab6c
c44d5e4
beac8bf
81d7dd5
ae050ef
4321e2d
14af5cd
b8d8b8f
336fea2
70b7bfb
c80497f
155f176
64a7d35
1a97e36
60bb3fd
647c48c
6ff3ba9
232ffec
88a6f05
04ffc4c
ff2b9dc
6d98a6e
529a7db
6cd0b36
b8095d3
477ea49
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ | |
| from scipy.constants import electron_volt | ||
| from scipy.constants import c as clight | ||
|
|
||
| env = xt.load('b075_2024.09.25.madx') | ||
| env = xt.load('sls.madx') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same, was incorrect, corrected it. |
||
| line = env.ring | ||
| line.particle_ref = xt.Particles(energy0=2.7e9, mass0=xt.ELECTRON_MASS_EV) | ||
| line.configure_bend_model(num_multipole_kicks=20) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| """ | ||
| SLS simulation without undulators. | ||
|
|
||
| This script loads the SLS MADX file, creates a copy of the ring without wigglers, | ||
| computes twiss with radiation integrals, and prints the results. | ||
| """ | ||
|
|
||
| import xtrack as xt | ||
| from pathlib import Path | ||
|
|
||
| # Particle reference | ||
| p0 = xt.Particles(mass0=xt.ELECTRON_MASS_EV, q0=1, p0c=2.7e9) | ||
|
|
||
| # Load SLS MADX file | ||
| madx_file = Path(__file__).resolve().parent.parent.parent / 'test_data' / 'sls' / 'sls.madx' | ||
| env = xt.load(str(madx_file)) | ||
| line_sls = env.ring | ||
|
|
||
| # Create copy of ring without wigglers | ||
| line_no_wiggler = line_sls.copy(shallow=True) | ||
| env['ring_no_wiggler'] = line_no_wiggler | ||
|
|
||
| # Configure bend model | ||
| line_no_wiggler.configure_bend_model(core='mat-kick-mat') | ||
|
|
||
| # Set particle reference | ||
| line_no_wiggler.particle_ref = p0.copy() | ||
|
|
||
| # Compute twiss with radiation integrals | ||
| tw_no_wiggler = line_no_wiggler.twiss4d(radiation_integrals=True) | ||
|
|
||
| # Plotting: | ||
| import matplotlib.pyplot as plt | ||
| plt.close('all') | ||
| tw_no_wiggler.plot('x y') | ||
| tw_no_wiggler.plot('betx bety', 'dx dy') | ||
| tw_no_wiggler.plot('betx2 bety2') | ||
| plt.show() | ||
|
|
||
| #['name', 's', 'x', 'px', 'y', 'py', 'zeta', 'delta', 'ptau', 'W_matrix', 'kin_px', 'kin_py', 'kin_ps', 'kin_xprime', | ||
| # 'kin_yprime', 'env_name', 'betx', 'bety', 'alfx', 'alfy', 'gamx', 'gamy', 'dx', 'dpx', 'dy', 'dpy', 'dx_zeta', 'dpx_zeta', | ||
| # 'dy_zeta', 'dpy_zeta', 'betx1', 'bety1', 'betx2', 'bety2', 'alfx1', 'alfy1', 'alfx2', 'alfy2', 'gamx1', 'gamy1', | ||
| # 'gamx2', 'gamy2', 'mux', 'muy', 'muzeta', 'nux', 'nuy', 'nuzeta', 'phix', 'phiy', 'phizeta', 'dmux', 'dmuy', 'dzeta', | ||
| # 'bx_chrom', 'by_chrom', 'ax_chrom', 'ay_chrom', 'wx_chrom', 'wy_chrom', 'ddx', 'ddpx', 'ddy', 'ddpy', 'c_minus_re', | ||
| # 'c_minus_im', 'c_r1', 'c_r2', 'c_phi1', 'c_phi2', 'k0l', 'k1l', 'k2l', 'k3l', 'k4l', 'k5l', 'k0sl', 'k1sl', 'k2sl', | ||
| # 'k3sl', 'k4sl', 'k5sl', 'angle_rad', 'rot_s_rad', 'hkick', 'vkick', 'ks', 'length', '_angle_force_body', 'element_type', 'isthick', 'parent_name'] | ||
|
|
||
| # Extract and print results | ||
| print("=" * 80) | ||
| print("SLS WITHOUT UNDULATORS") | ||
| print("=" * 80) | ||
| print(f"Tunes:") | ||
| print(f" qx = {tw_no_wiggler.qx:.4f}") | ||
| print(f" qy = {tw_no_wiggler.qy:.4f}") | ||
| print(f" qs = {tw_no_wiggler.qs:.4f}") | ||
| print() | ||
| print(f"Chromaticity:") | ||
| print(f" dqx = {tw_no_wiggler.dqx:.4f}") | ||
| print(f" dqy = {tw_no_wiggler.dqy:.4f}") | ||
| print() | ||
| print(f"Partition numbers:") | ||
| print(f" J_x = {tw_no_wiggler.rad_int_partition_number_x:.4f}") | ||
| print(f" J_y = {tw_no_wiggler.rad_int_partition_number_y:.4f}") | ||
| print(f" J_zeta = {tw_no_wiggler.rad_int_partition_number_zeta:.4f}") | ||
| print() | ||
| print(f"Damping constants per second:") | ||
| print(f" alpha_x = {tw_no_wiggler.rad_int_damping_constant_x_s:.4f}") | ||
| print(f" alpha_y = {tw_no_wiggler.rad_int_damping_constant_y_s:.4f}") | ||
| print(f" alpha_zeta = {tw_no_wiggler.rad_int_damping_constant_zeta_s:.4f}") | ||
| print() | ||
| print(f"Equilibrium emittances:") | ||
| print(f" eq_gemitt_x = {tw_no_wiggler.rad_int_eq_gemitt_x:.4f}") | ||
| print(f" eq_gemitt_y = {tw_no_wiggler.rad_int_eq_gemitt_y:.4f}") | ||
| print(f" eq_gemitt_zeta = {tw_no_wiggler.rad_int_eq_gemitt_zeta:.4f}") | ||
| print() | ||
| print(f"Energy loss per turn: {tw_no_wiggler.rad_int_eneloss_turn:.4f} eV") | ||
| print() | ||
| print(f"C^-: {tw_no_wiggler.c_minus:.4e}") | ||
| print() | ||
| print("=" * 80) | ||
|
|
||
| # Write results to file | ||
| output_dir = Path("/home/simonfan/cernbox/Documents/Presentations/Section_Meeting_Undulators") | ||
| output_dir.mkdir(parents=True, exist_ok=True) | ||
| output_file = output_dir / "SLS_WITHOUT_UNDULATORS.txt" | ||
|
|
||
| with open(output_file, 'w') as f: | ||
| f.write("=" * 80 + "\n") | ||
| f.write("SLS WITHOUT UNDULATORS\n") | ||
| f.write("=" * 80 + "\n") | ||
| f.write(f"Tunes:\n") | ||
| f.write(f" qx = {tw_no_wiggler.qx:.4e}\n") | ||
| f.write(f" qy = {tw_no_wiggler.qy:.4e}\n") | ||
| f.write(f" qs = {tw_no_wiggler.qs:.4e}\n") | ||
| f.write("\n") | ||
| f.write(f"Chromaticity:\n") | ||
| f.write(f" dqx = {tw_no_wiggler.dqx:.4e}\n") | ||
| f.write(f" dqy = {tw_no_wiggler.dqy:.4e}\n") | ||
| f.write("\n") | ||
| f.write(f"Partition numbers:\n") | ||
| f.write(f" J_x = {tw_no_wiggler.rad_int_partition_number_x:.4e}\n") | ||
| f.write(f" J_y = {tw_no_wiggler.rad_int_partition_number_y:.4e}\n") | ||
| f.write(f" J_zeta = {tw_no_wiggler.rad_int_partition_number_zeta:.4e}\n") | ||
| f.write("\n") | ||
| f.write(f"Damping constants per second:\n") | ||
| f.write(f" alpha_x = {tw_no_wiggler.rad_int_damping_constant_x_s:.4e}\n") | ||
| f.write(f" alpha_y = {tw_no_wiggler.rad_int_damping_constant_y_s:.4e}\n") | ||
| f.write(f" alpha_zeta = {tw_no_wiggler.rad_int_damping_constant_zeta_s:.4e}\n") | ||
| f.write("\n") | ||
| f.write(f"Equilibrium emittances:\n") | ||
| f.write(f" eq_gemitt_x = {tw_no_wiggler.rad_int_eq_gemitt_x:.4e}\n") | ||
| f.write(f" eq_gemitt_y = {tw_no_wiggler.rad_int_eq_gemitt_y:.4e}\n") | ||
| f.write(f" eq_gemitt_zeta = {tw_no_wiggler.rad_int_eq_gemitt_zeta:.4e}\n") | ||
| f.write("\n") | ||
| f.write(f"Energy loss per turn: {tw_no_wiggler.rad_int_eneloss_turn:.4e} eV\n") | ||
| f.write("=" * 80 + "\n") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| """ | ||
| SLS simulation without undulators. | ||
|
|
||
| This script loads the SLS MADX file, creates a copy of the ring without wigglers, | ||
| computes twiss with radiation integrals, and prints the results. | ||
| """ | ||
|
|
||
| import xtrack as xt | ||
| from pathlib import Path | ||
|
|
||
| # Particle reference | ||
| p0 = xt.Particles(mass0=xt.ELECTRON_MASS_EV, q0=1, p0c=2.7e9) | ||
|
|
||
| # Load SLS MADX file | ||
| madx_file = Path(__file__).resolve().parent.parent.parent / 'test_data' / 'sls' / 'sls.madx' | ||
| env = xt.load(str(madx_file)) | ||
| line_sls = env.ring | ||
|
|
||
| # Create copy of ring without wigglers | ||
| line_no_wiggler = line_sls.copy(shallow=True) | ||
| env['ring_no_wiggler'] = line_no_wiggler | ||
|
|
||
| # Configure bend model | ||
| line_no_wiggler.configure_bend_model(core='mat-kick-mat') | ||
|
|
||
| # Set particle reference | ||
| line_no_wiggler.particle_ref = p0.copy() | ||
|
|
||
| # Compute twiss with radiation integrals | ||
| tw_no_wiggler = line_no_wiggler.twiss4d(radiation_integrals=True, spin=True, polarization=True) | ||
|
|
||
| # Plotting: | ||
| import matplotlib.pyplot as plt | ||
| plt.close('all') | ||
| tw_no_wiggler.plot('x y') | ||
| tw_no_wiggler.plot('betx bety', 'dx dy') | ||
| tw_no_wiggler.plot('betx2 bety2') | ||
| tw_no_wiggler.plot('spin_x spin_y spin_z') | ||
| plt.show() | ||
|
|
||
| #['name', 's', 'x', 'px', 'y', 'py', 'zeta', 'delta', 'ptau', 'W_matrix', 'kin_px', 'kin_py', 'kin_ps', 'kin_xprime', | ||
| # 'kin_yprime', 'env_name', 'betx', 'bety', 'alfx', 'alfy', 'gamx', 'gamy', 'dx', 'dpx', 'dy', 'dpy', 'dx_zeta', 'dpx_zeta', | ||
| # 'dy_zeta', 'dpy_zeta', 'betx1', 'bety1', 'betx2', 'bety2', 'alfx1', 'alfy1', 'alfx2', 'alfy2', 'gamx1', 'gamy1', | ||
| # 'gamx2', 'gamy2', 'mux', 'muy', 'muzeta', 'nux', 'nuy', 'nuzeta', 'phix', 'phiy', 'phizeta', 'dmux', 'dmuy', 'dzeta', | ||
| # 'bx_chrom', 'by_chrom', 'ax_chrom', 'ay_chrom', 'wx_chrom', 'wy_chrom', 'ddx', 'ddpx', 'ddy', 'ddpy', 'c_minus_re', | ||
| # 'c_minus_im', 'c_r1', 'c_r2', 'c_phi1', 'c_phi2', 'k0l', 'k1l', 'k2l', 'k3l', 'k4l', 'k5l', 'k0sl', 'k1sl', 'k2sl', | ||
| # 'k3sl', 'k4sl', 'k5sl', 'angle_rad', 'rot_s_rad', 'hkick', 'vkick', 'ks', 'length', '_angle_force_body', 'element_type', 'isthick', 'parent_name'] | ||
|
|
||
| # Extract and print results | ||
| print("=" * 80) | ||
| print("SLS WITHOUT UNDULATORS") | ||
| print("=" * 80) | ||
| print(f"Tunes:") | ||
| print(f" qx = {tw_no_wiggler.qx:.4f}") | ||
| print(f" qy = {tw_no_wiggler.qy:.4f}") | ||
| print(f" qs = {tw_no_wiggler.qs:.4f}") | ||
| print() | ||
| print(f"Chromaticity:") | ||
| print(f" dqx = {tw_no_wiggler.dqx:.4f}") | ||
| print(f" dqy = {tw_no_wiggler.dqy:.4f}") | ||
| print() | ||
| print(f"Partition numbers:") | ||
| print(f" J_x = {tw_no_wiggler.rad_int_partition_number_x:.4f}") | ||
| print(f" J_y = {tw_no_wiggler.rad_int_partition_number_y:.4f}") | ||
| print(f" J_zeta = {tw_no_wiggler.rad_int_partition_number_zeta:.4f}") | ||
| print() | ||
| print(f"Damping constants per second:") | ||
| print(f" alpha_x = {tw_no_wiggler.rad_int_damping_constant_x_s:.4f}") | ||
| print(f" alpha_y = {tw_no_wiggler.rad_int_damping_constant_y_s:.4f}") | ||
| print(f" alpha_zeta = {tw_no_wiggler.rad_int_damping_constant_zeta_s:.4f}") | ||
| print() | ||
| print(f"Equilibrium emittances:") | ||
| print(f" eq_gemitt_x = {tw_no_wiggler.rad_int_eq_gemitt_x:.4f}") | ||
| print(f" eq_gemitt_y = {tw_no_wiggler.rad_int_eq_gemitt_y:.4f}") | ||
| print(f" eq_gemitt_zeta = {tw_no_wiggler.rad_int_eq_gemitt_zeta:.4f}") | ||
| print() | ||
| print(f"Energy loss per turn: {tw_no_wiggler.rad_int_eneloss_turn:.4f} eV") | ||
| print() | ||
| print(f"C^-: {tw_no_wiggler.c_minus:.4e}") | ||
| print() | ||
| print(f"Spin polarization: {tw_no_wiggler.spin_polarization_eq:.4e}") | ||
| print() | ||
| print("=" * 80) | ||
|
|
||
| # Write results to file | ||
| output_dir = Path("/home/simonfan/cernbox/Documents/Presentations/Section_Meeting_Undulators") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This path refers to your computer
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Corrected! |
||
| output_dir.mkdir(parents=True, exist_ok=True) | ||
| output_file = output_dir / "SLS_WITHOUT_UNDULATORS.txt" | ||
|
|
||
| with open(output_file, 'w') as f: | ||
| f.write("=" * 80 + "\n") | ||
| f.write("SLS WITHOUT UNDULATORS\n") | ||
| f.write("=" * 80 + "\n") | ||
| f.write(f"Tunes:\n") | ||
| f.write(f" qx = {tw_no_wiggler.qx:.4e}\n") | ||
| f.write(f" qy = {tw_no_wiggler.qy:.4e}\n") | ||
| f.write(f" qs = {tw_no_wiggler.qs:.4e}\n") | ||
| f.write("\n") | ||
| f.write(f"Chromaticity:\n") | ||
| f.write(f" dqx = {tw_no_wiggler.dqx:.4e}\n") | ||
| f.write(f" dqy = {tw_no_wiggler.dqy:.4e}\n") | ||
| f.write("\n") | ||
| f.write(f"Partition numbers:\n") | ||
| f.write(f" J_x = {tw_no_wiggler.rad_int_partition_number_x:.4e}\n") | ||
| f.write(f" J_y = {tw_no_wiggler.rad_int_partition_number_y:.4e}\n") | ||
| f.write(f" J_zeta = {tw_no_wiggler.rad_int_partition_number_zeta:.4e}\n") | ||
| f.write("\n") | ||
| f.write(f"Damping constants per second:\n") | ||
| f.write(f" alpha_x = {tw_no_wiggler.rad_int_damping_constant_x_s:.4e}\n") | ||
| f.write(f" alpha_y = {tw_no_wiggler.rad_int_damping_constant_y_s:.4e}\n") | ||
| f.write(f" alpha_zeta = {tw_no_wiggler.rad_int_damping_constant_zeta_s:.4e}\n") | ||
| f.write("\n") | ||
| f.write(f"Equilibrium emittances:\n") | ||
| f.write(f" eq_gemitt_x = {tw_no_wiggler.rad_int_eq_gemitt_x:.4e}\n") | ||
| f.write(f" eq_gemitt_y = {tw_no_wiggler.rad_int_eq_gemitt_y:.4e}\n") | ||
| f.write(f" eq_gemitt_zeta = {tw_no_wiggler.rad_int_eq_gemitt_zeta:.4e}\n") | ||
| f.write("\n") | ||
| f.write(f"Energy loss per turn: {tw_no_wiggler.rad_int_eneloss_turn:.4e} eV\n") | ||
| f.write("\n") | ||
| f.write(f"C^-: {tw_no_wiggler.c_minus:.4e}\n") | ||
| f.write("\n") | ||
| f.write(f"Spin polarization: {tw_no_wiggler.spin_polarization_eq:.4e}\n") | ||
| f.write("\n") | ||
| f.write("=" * 80 + "\n") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| from pathlib import Path | ||
|
|
||
| # Simple local import so this file can be run directly (e.g. via IDE "Run" button) | ||
| from xtrack._temp.field_fitter import FieldFitter | ||
|
|
||
|
|
||
| ''' | ||
| Basic usage of FieldFitter. | ||
|
|
||
| This script fits a field map and saves the fit parameters to a file. | ||
|
|
||
| It plots the fit results for each derivative order. | ||
| It also plots the integrated field along the longitudinal direction. | ||
|
|
||
| The raw data only has three transverse x positions, which means the highest order polynomial that we can fit is 2. | ||
| This also means that we can only incorporate up to the second derivative of the field into the fit (sextupole components). | ||
| ''' | ||
|
|
||
| dz = 0.001 # Step size in the z (longitudinal) direction for numerical differentiation | ||
|
|
||
| here = Path(__file__).resolve().parent | ||
|
|
||
| # Standard 6-column format (X Y Z Bx By Bs) | ||
| # Use the shared test-data knot-map file | ||
| file_path = Path(__file__).resolve().parent.parent.parent / "test_data" / "sls" / "undulator_field_map.txt" | ||
|
|
||
| deg = 2 | ||
|
|
||
| if __name__ == "__main__": | ||
| # Build and run the fitter (file path is parsed inline by FieldFitter) | ||
| fitter = FieldFitter( | ||
| raw_data=file_path, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still taking a path, let's use a DataFrame
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's not Taylor the fitter to a specific format
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed the "path acceptance" from the FieldFitter. Now only accepts DataFrames. The user has to convert their data to a DataFrame of the correct format. |
||
| xy_point=(0.0, 0.0), | ||
| distance_unit=dz, | ||
| min_region_size=10, | ||
| deg=deg, | ||
| ) | ||
|
|
||
| fitter.fit() | ||
|
|
||
| for der in range(0, deg + 1): | ||
| fitter.plot_fields(der=der) | ||
|
|
||
| fitter.plot_integrated_fields() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check that this uses the file in test_data folder
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checked, was incorrect, corrected it.