def __init__(self, slicer=None, response_data_file=None, coord=None, kick_factor=1., n_terms_to_be_kept=None, n_tail_cut=0): ob_responses = mfm.myloadmat_to_obj(response_data_file) z_resp = ob_responses.z_slices # Clean-up NaNs r_resp_mat = ob_responses.r_mat r_resp_mat[np.isnan(r_resp_mat)] = 0. dpr_resp_mat = ob_responses.dpr_mat dpr_resp_mat[np.isnan(dpr_resp_mat)] = 0. # Prepare submatrices FF = r_resp_mat[:, :].T # base functions are in the columns MM = dpr_resp_mat[:, :].T # response to base functions are in the columns RR = np.dot(FF.T, FF) # Matrix of cross products RR_inv = np.linalg.inv(RR) # Its inverse # Prepare cut on the harmonics if n_terms_to_be_kept is not None: self.n_terms_to_be_kept = n_terms_to_be_kept else: self.n_terms_to_be_kept = len(z_resp) CC = 0*MM for ii in range(self.n_terms_to_be_kept): CC[ii, ii] = 1 # Prepare cut for the tails CC_tails = np.identity(len(z_resp)) for ii in range(n_tail_cut): CC_tails[ii, ii] = 0. CC_tails[-ii, -ii] = 0. # Build response matrix WW_no_harmonic_cut = np.dot(MM, np.dot(RR_inv, FF.T)) WW = np.dot(MM, np.dot(CC, np.dot(RR_inv, np.dot(FF.T, CC_tails)))) # Bind to object self.slicer = slicer self.coord = coord self.kick_factor = kick_factor self.n_terms_to_be_kept = n_terms_to_be_kept self.n_tail_cut = n_tail_cut self.z_resp = z_resp self.FF = FF self.MM = MM self.RR = RR self.RR_inv = RR_inv self.CC = CC self.CC_tails = CC_tails self.WW_no_harmonic_cut = WW_no_harmonic_cut self.WW = WW
import pickle import numpy as np import matplotlib.pyplot as plt import PyECLOUD.myfilemanager as mfm frac_tune_0 = .27 max_strength_plot = 0.9 fname_pic = './processed_data/compact_pic_fit.mat' fname_vlasov_dipquad = '../008a1_scan_strength_wlampldet/eigenvalues.mat' fname_vlasov_diponly = '../008_eigenvalues/eigenvalues.mat' obpic = mfm.myloadmat_to_obj(fname_pic) obdipquad = mfm.myloadmat_to_obj(fname_vlasov_dipquad) obdiponly = mfm.myloadmat_to_obj(fname_vlasov_diponly) i_ref = 5 stren_array = np.linspace(0, max_strength_plot, 1000) tune_shift_dipquad = (obdipquad.M00_array[i_ref].real / obdipquad.omega0 / obdipquad.strength_scan[i_ref] * stren_array) tune_shift_diponly = (obdiponly.M00_array[i_ref].real / obdiponly.omega0 / obdiponly.strength_scan[i_ref] * stren_array) plt.close('all') fig1 = plt.figure(1) ax1 = fig1.add_subplot(111) ax1.plot(stren_array, tune_shift_diponly,
from matplotlib.ticker import FormatStrFormatter from scipy.constants import e as qe import PyECLOUD.myfilemanager as mfm import PyECLOUD.mystyle as ms plane = 'y' folder_sims = f'simulations_{plane}' response_summary_file = f'./response_data_{plane}_processed.mat' vmax_edens = 2.5e13 i_plot_list = list(range(1, 200)) close_figures = True obsum = mfm.myloadmat_to_obj(response_summary_file) plt.close('all') ms.mystyle_arial(fontsz=14, dist_tick_lab=5, traditional_look=False) for ii in i_plot_list: n_osc = obsum.n_osc_list[ii] cos_ampl = obsum.cos_ampl_list[ii] sin_ampl = obsum.sin_ampl_list[ii] current_sim_ident = f'n_{n_osc:.1f}_c{cos_ampl:.2e}_s{sin_ampl:.2e}' ob = mfm.myloadmat_to_obj(folder_sims + '/' + current_sim_ident + '/response.mat') if ob.plane == 'x': rg = ob.xg elif ob.plane == 'y':
cos_amplitude * np.cos(2 * np.pi * N_oscillations * z_slices / z_range)) # Add sinusoidal distortion to particles for ss in slices: if ss.macroparticlenumber > 0: #if ss.mean_z() < 0: ss.x += sin_amplitude * np.sin( 2 * np.pi * N_oscillations * ss.z / z_range) ss.x += cos_amplitude * np.cos( 2 * np.pi * N_oscillations * ss.z / z_range) # Measure x_slices = np.array([ss.mean_x() for ss in slices]) int_slices = np.array([ss.intensity for ss in slices]) obfmap = mfm.myloadmat_to_obj(field_map_file) from PyECLOUD.Transverse_Efield_map_for_frozen_cloud import Transverse_Efield_map fmap = Transverse_Efield_map(xg=obfmap.xg, yg=obfmap.yg, Ex=obfmap.Ex_L_map, Ey=obfmap.Ey_L_map, L_interaction=1. / sim_content.n_segments, slicer=None, flag_clean_slices=False, wrt_slice_centroid=False, x_beam_offset=0., y_beam_offset=0., slice_by_slice_mode=True) assert (len(sim_content.parent_eclouds) == 0)
import matplotlib.pyplot as plt from matplotlib.ticker import MaxNLocator from scipy.constants import e as qe N_discard = 10 tag = 'movie_ecloud' gen_movie = True z_single_frame_interactive = 1e-2 # ====================== folder_movie = 'temp_' + tag + '_frames' os.mkdir(folder_movie) ob = mfm.myloadmat_to_obj('pinch_pic_data.mat') ob.xg = ob.xg[N_discard:-N_discard] ob.yg = ob.yg[N_discard:-N_discard] ob.rho = ob.rho[:, N_discard:-N_discard, N_discard:-N_discard] ob.phi = ob.phi[:, N_discard:-N_discard, N_discard:-N_discard] ob.Ex = ob.Ex[:, N_discard:-N_discard, N_discard:-N_discard] ob.Ey = ob.Ey[:, N_discard:-N_discard, N_discard:-N_discard] ob.rho /= -qe x_obs = 0. ix_obs = np.argmin(np.abs(ob.xg - x_obs)) y_obs = 0. iy_obs = np.argmin(np.abs(ob.yg - y_obs))
from scipy.constants import e as qe import PyECLOUD.myfilemanager as mfm import PyECLOUD.geom_impact_poly_fast_impact as geom import PyPIC.FiniteDifferences_ShortleyWeller_SquareGrid as FDSW chamber = geom.polyg_cham_geom_object( filename_chm='../reference_simulation/pyecloud_config/LHC_chm_ver.mat', flag_non_unif_sey=False) pic = FDSW.FiniteDifferences_ShortleyWeller_SquareGrid(chamb=chamber, Dh=0.1e-3) obp = mfm.myloadmat_to_obj( '../quad_combined_distribution/combined_distribution_sey_1.40_VRF_4MV_intensity_1.2e11ppb_450GeV_N_mp_500000_symm.mat' ) pic.scatter(x_mp=obp.x_mp, y_mp=obp.y_mp, nel_mp=obp.nel_mp) plt.close('all') fig = plt.figure() ax = fig.add_subplot(111) fig.subplots_adjust(bottom=.122, top=.82) mbl = ax.pcolormesh(1e3 * pic.xg, 1e3 * pic.yg, -pic.rho.T / qe, cmap=plt.cm.jet, vmax=5e13) ax.plot(1e3 * chamber.Vx, 1e3 * chamber.Vy, lw=2, color='yellow') cb = plt.colorbar(mbl, ax=ax)
import scipy.io as sio import PyECLOUD.mystyle as ms import PyECLOUD.myfilemanager as mfm # Plot settings l_min_plot = -5 l_max_plot = 3 min_strength_plot = 0 max_strength_plot = 1.55 tau_min_plot = 0 tau_max_plot = 300 flag_tilt_lines = False flag_mode0 = True ob = mfm.myloadmat_to_obj('eigenvalues.mat') Omega_mat = ob.Omega_mat strength_scan = ob.strength_scan DQ_0_vect = ob.DQ_0_vect M00_array = ob.M00_array omega0 = ob.omega0 omega_s = ob.omega_s l_min = ob.l_min l_max = ob.l_max m_max = ob.m_max N_max = ob.N_max import matplotlib.pyplot as plt plt.close('all') ms.mystyle(fontsz=14, traditional_look=False)
import numpy as np folder_sims = 'simulations' sin_ampl = 0 cos_ampl = 1e-4 n_osc_list = range(200) #cos_ampl = 0 #sin_ampl = 1e-4 #n_osc_list = range(1,200) for n_osc in n_osc_list: current_sim_ident = f'n_{n_osc:.1f}_c{cos_ampl:.2e}_s{sin_ampl:.2e}' ob = mfm.myloadmat_to_obj(folder_sims + '/' + current_sim_ident + '/response.mat') import matplotlib.pyplot as plt plt.close('all') ms.mystyle(fontsz=14, traditional_look=False) fig1 = plt.figure(1) ax1 = fig1.add_subplot(3, 1, 1) ax2 = fig1.add_subplot(3, 1, 2, sharex=ax1) ax3 = fig1.add_subplot(3, 1, 3, sharex=ax1) #ax1.plot(ob.z_slices, ob.int_slices) ax2.plot(ob.z_slices, ob.x_slices) ax3.plot(ob.z_slices, ob.dpx_slices_all_clouds) for ax in [ax1, ax2, ax3]: ax.grid(True)
pl.close('all') pic_singlegrid = ecloud_singlegrid.spacech_ele.PyPICobj pic_multigrid = ecloud_multigrid.spacech_ele.PyPICobj # Plot field at probes import matplotlib.gridspec as gridspec fig = pl.figure(1, figsize=(7, 8)) fig.patch.set_facecolor('w') gs1 = gridspec.GridSpec(1, 1) gs2 = gridspec.GridSpec(2, 1) sp1 = fig.add_subplot(gs1[0]) obcham = mlm.myloadmat_to_obj( 'LHC_chm_ver.mat') sp1.plot(obcham.Vx * 1e3, obcham.Vy * 1e3, 'b') sp1.plot(ecloud_singlegrid.x_probes * 1e3, ecloud_singlegrid.y_probes * 1e3, 'or', markersize=3) sp1.plot(x_beam_offset, y_beam_offset, '*k', markersize=4) for ii in range(pic_multigrid.n_grids): sp1.plot(pic_multigrid.pic_list[ii].pic_internal.chamb.Vx * 1e3, pic_multigrid.pic_list[ii].pic_internal.chamb.Vy * 1e3, '.-k') sp1.set_ylabel('y [mm]') sp1.set_xlabel('x [mm]') sp1.axis('equal') ms.sciy() ms.scix() sp1.grid('on') sp2 = fig.add_subplot(gs2[0]) sp2.plot(np.array(n_probes * list(slices.z_centers)) * 1e2,
import numpy as np from scipy.constants import e as qe # fname = 'Pyecltest_highQmax_100us_3turns.mat'; n_slots_plot = 10500 # fname = 'Pyecltest_example_20eV_1e-13C_0.5us.mat'; n_slots_plot = 700 # fname = 'Pyecltest_example_20eV_1e-13C_1us.mat'; n_slots_plot = 700 # fname = 'Pyecltest_example_20eV_1e-13C_2us.mat'; n_slots_plot = 700 # fname = 'Pyecltest_example_20eV_1e-13C.mat'; n_slots_plot = 700 # fname = 'Pyecltest_example_0.01eV_1e-13C.mat'; n_slots_plot = 700 fname = 'Pyecltest.mat' n_slots_plot = 700 Trev = 88.9e-6 ob = mfm.myloadmat_to_obj(fname) compare_against_charge_in_chamber = False plot_charge_from_post_processing = False import matplotlib.pyplot as plt plt.close('all') ms.mystyle_arial(fontsz=16) fig2 = plt.figure(2, figsize=(8, 6 * 1.5)) fig2.set_facecolor('w') sp1 = plt.subplot(3, 1, 1) if compare_against_charge_in_chamber: sub = ob.Nel_timep[0]
import os import numpy as np import matplotlib.pyplot as plt import PyECLOUD.myfilemanager as mfm plane = 'y' # Load response data ob = mfm.myloadmat_to_obj(f'./response_dc_{plane}.mat') z_resp = ob.z_slices r_resp_mat = ob.r_ideal r_resp_mat[np.isnan(r_resp_mat)] = 0. dpr_resp_mat = ob.dpr_slices_all_clouds dpr_resp_mat[np.isnan(dpr_resp_mat)] = 0. k_z = dpr_resp_mat/r_resp_mat import scipy.io as sio sio.savemat(f'linear_strength_{plane}.mat', { 'z_slices': z_resp, 'k_z_integrated': k_z}) plt.close('all') fig100 = plt.figure(100) ax101 = fig100.add_subplot(111) ax101.plot(z_resp, k_z) mask_fit = np.array(len(z_resp)*[True]) #if n_cut_fit > 0:
# m_max = 3 # n_phi = 3*360 # n_r = 3*200 # N_max = 50 # save_pkl_fname = None # n_tail_cut = 0 # response_matrix_file = '../001_sin_response_scan/response_data_processed.mat' # z_strength_file = '../001a_sin_response_scan_unperturbed/linear_strength.mat' # detuning_fit_order = 10 # pool_size = 4 # Detunings with delta beta_N = [0, Qp] # Load detuning with z if detuning_fit_order > 0: obdet = mfm.myloadmat_to_obj(z_strength_file) z_slices = obdet.z_slices p = np.polyfit(obdet.z_slices, obdet.k_z_integrated, deg=detuning_fit_order) alpha_N = cloud_rescale_by * p[::-1] # Here I fit the strength else: alpha_N = alpha_N_custom # Prepare response matrix ob = mfm.myloadmat_to_obj(response_matrix_file) HH = ob.x_mat KK = ob.dpx_mat if n_tail_cut > 0: KK[:, :n_tail_cut] = 0.
import matplotlib.pyplot as plt import PyECLOUD.myfilemanager as mfm obfull = mfm.myloadmat_to_obj('./from_full_map_linear_strength.mat') oblin = mfm.myloadmat_to_obj('./from_lin_map_linear_strength.mat') plt.close('all') fig1 = plt.figure(1) plt.plot(obfull.z_slices, obfull.k_z_integrated) plt.plot(obfull.z_slices, oblin.k_z_integrated) plt.show()
import numpy as np import matplotlib.pyplot as plt import PyECLOUD.myfilemanager as mfm import PyECLOUD.mystyle as ms obfol = mfm.myloadmat_to_obj('followed_electrons.mat') ob = mfm.myloadmat_to_obj('Pyecltest.mat') i_obs = 1 plt.close('all') ms.mystyle(fontsz=14, traditional_look=False) Dz_all = [] nel_all = [] nele_no_nan = obfol.nel.copy() nele_no_nan[np.isnan(obfol.nel)] = 0. for i_ele in range(obfol.nel.shape[1]): nel_ele = nele_no_nan[:, i_ele] z_ele = obfol.z[:, i_ele] i_changes = np.where(np.abs(np.diff(nel_ele)) > 0.)[0] for ii in range(1, len(i_changes) - 1): z_part = z_ele[i_changes[ii] + 1:i_changes[ii + 1] - 1] if len(z_part) > 0 and not np.isnan(z_part[0]): Dz_all.append(np.max(z_part) - np.min(z_part)) nel_all.append(nel_ele[i_changes[ii] + 1])
import pickle import copy import numpy as np import matplotlib.pyplot as plt import PyECLOUD.myfilemanager as mfm n_sigma_ave = 2.5 ob = mfm.myloadmat_to_obj('field_map.mat') ix_zero = np.argmin(np.abs(ob.xg)) iy_zero = np.argmin(np.abs(ob.yg)) mask_x = np.abs(ob.xg) <= n_sigma_ave * ob.sigma_x mask_y = np.abs(ob.yg) <= n_sigma_ave * ob.sigma_y Ex_lin = ob.Ex_L_map * 0 Ey_lin = ob.Ey_L_map * 0 k_x_list = [] k_y_list = [] for ss in range(len(ob.zg)): p_fit_x = np.polyfit(ob.xg[mask_x], ob.Ex_L_map[ss, mask_x, iy_zero], deg=1) p_fit_y = np.polyfit(ob.yg[mask_y], ob.Ey_L_map[ss, ix_zero, mask_y], deg=1) for iy in range(len(ob.yg)):
from scipy.constants import c as clight from scipy.constants import e as qe import PyECLOUD.myfilemanager as mfm import PyECLOUD.mystyle as ms n_phi = 360 n_r = 200 beta_fun = 92.7 omega0 = 2 * np.pi * clight / 27e3 # Revolution angular frquency omega_s = 4.9e-3 * omega0 sigma_b = 0.097057 vmax_edens = 1.5e14 ob = mfm.myloadmat_to_obj( '../001a_sin_response_scan_unperturbed/linear_strength.mat') z_slices = ob.z_slices p = np.polyfit(ob.z_slices, ob.k_z_integrated, deg=10) alpha_N = p[::-1] # Here I fit the strength A_N = -beta_fun * alpha_N / 4 / np.pi # I go to the coefficient in the tune N_terms = len(A_N) r_max = np.max(np.abs(z_slices)) dz = z_slices[1] - z_slices[0] r_vect = np.linspace(0, r_max, n_r) phi_vect = np.linspace(0, 2 * np.pi, n_phi + 1)[:-1] dphi = phi_vect[1] - phi_vect[0] sin_PHI = np.sin(phi_vect) cos_PHI = np.cos(phi_vect)
add_alpha_0_to_tune = True factor_alpha_0_to_tune = 0.8 z_strength_file = '../001a_sin_response_scan_unperturbed/linear_strength.mat' detuning_fit_order = 10 N_poly_cut = 1 alpha_N_custom = [] include_non_linear_map = True flag_wrt_bunch_centroid = True field_map_file = '../003_generate_field_map/field_map.mat' # end-settings-section # Load and fit detuning with z if include_detuning_with_z: if detuning_fit_order > 0: obdet = mfm.myloadmat_to_obj(z_strength_file) z_slices = obdet.z_slices p = np.polyfit(obdet.z_slices, obdet.k_z_integrated, deg=detuning_fit_order) alpha_N = p[::-1] * ecloud_strength_scale # Here I fit the strength print('Detuning cefficients alpha_N:') print(alpha_N) else: alpha_N = alpha_N_custom alpha_N = alpha_N[:N_poly_cut] * alpha_scale # Instantiate simulation sim_content = sim_mod.Simulation(param_file=sim_param_file)
all_freq_indep += this_f_indep[1:] all_aps_indep += this_ap_indep[1:] all_stre_indep += this_stren_indep[1:] return all_freq_indep, all_aps_indep, all_stre_indep plt.close('all') ms.mystyle_arial(fontsz=14, dist_tick_lab=5, traditional_look=False) fig1 = plt.figure(1, figsize=(6.4 * 1.2, 4.8)) ax1 = fig1.add_subplot(111) axshare = None figharm_list = [] figintra_list = [] for ii, ll in enumerate(dict_plot.keys()): oo = mfm.myloadmat_to_obj(dict_plot[ll]['fname']) if flag_mode_unstab: insta_thresh = dict_plot[ll]['insta_thresh'] tilt_lines = dict_plot[ll]['tilt_lines'] scale_x = dict_plot[ll]['scale_x'] kwargs = {} if colorlist is not None: kwargs['color'] = colorlist[ii] # ax1.plot(oo.strength_list*scale_x, oo.p_list_centroid/T_rev, label=ll, # linewidth=2, **kwargs) ax1.plot(oo.strength_list, oo.p_list_centroid / T_rev, '.', alpha=.5, markeredgewidth=0, **kwargs)
}, } import matplotlib import matplotlib.pyplot as plt plt.close('all') ms.mystyle_arial(fontsz=14, dist_tick_lab=5, traditional_look=False) fig1 = plt.figure(1, figsize=(6.4 * 1.2, 4.8)) ax1 = fig1.add_subplot(111) fig_re_list = [] fig_im_list = [] for ill, ll in enumerate(dict_plot.keys()): fname = dict_plot[ll]['fname'] ob = mfm.myloadmat_to_obj(fname) Omega_mat = ob.Omega_mat strength_scan = ob.strength_scan strength_scan_0 = strength_scan # Omega_mat = np.zeros((ob.Omega_mat.shape[0]*2, ob.Omega_mat.shape[1]), # dtype=np.complex) # strength_scan = np.zeros(Omega_mat.shape[0]) # strength_scan_0 = ob.strength_scan # # Sort by real part # for iss in range(ob.Omega_mat.shape[0]): # isorted = np.argsort(ob.Omega_mat[iss, :].real # + 1e-10 * ob.Omega_mat[iss, :].imag) # Omega_mat[2*iss] = np.take(ob.Omega_mat[iss, :], isorted) # strength_scan[2*iss] = ob.strength_scan[iss] # # Interpolate # for iss in range(ob.Omega_mat.shape[0]-2):
import os import numpy as np import matplotlib.pyplot as plt import PyECLOUD.myfilemanager as mfm import PyECLOUD.mystyle as ms from scipy.signal import savgol_filter n_terms_to_be_kept = 200 n_tail_cut = 20 # Load response data response_data_file = 'response_data.mat' ob_responses = mfm.myloadmat_to_obj(response_data_file) z_resp = ob_responses.z_slices x_resp_mat = ob_responses.x_mat x_resp_mat[np.isnan(x_resp_mat)] = 0. dpx_resp_mat = ob_responses.dpx_mat dpx_resp_mat[np.isnan(dpx_resp_mat)] = 0. # Combine all matrices together FF = x_resp_mat[:, :].T MM = dpx_resp_mat[:, :].T RR = np.dot(FF.T, FF) RR_inv = np.linalg.inv(RR) CC = 0*MM for ii in range(n_terms_to_be_kept): CC[ii, ii] = 1
sim_content.init_all() # Initialize master to get the beam if os.path.exists('simulation_status.sta'): os.remove('simulation_status.sta') # Initialize beam sim_content.init_master() # Get bunch and slicer bunch = sim_content.bunch slicer = sim_content.slicer # Get simulation data obsim = mfm.myloadmat_to_obj(test_data_file) x_test = obsim.x_slices int_test = obsim.int_slices x_test[np.isnan(x_test)] = 0. xg = obsim.xg yg = obsim.yg plt.close('all') ms.mystyle_arial(fontsz=14, dist_tick_lab=5, traditional_look=False) figglob = plt.figure(1, figsize=(6.4, 4.8*1.15)) #axg1 = figglob.add_subplot(3,1,1) axg2 = figglob.add_subplot(2,1,1) axg3 = figglob.add_subplot(2,1,2, sharex=axg2)
from scipy.constants import c as clight from scipy.constants import e as qe import PyECLOUD.myfilemanager as mfm import PyECLOUD.mystyle as ms n_phi = 360 n_r = 200 beta_fun = 92.7 omega0 = 2 * np.pi * clight / 27e3 # Revolution angular frquency omega_s = 4.9e-3 * omega0 sigma_b = 0.097057 vmax_edens = 1.5e14 ob = mfm.myloadmat_to_obj( '../000a_sin_response_unperturbed_pinch/linear_strength_y.mat') z_slices = ob.z_slices p = np.polyfit(ob.z_slices, ob.k_z_integrated, deg=10) alpha_N = p[::-1] # Here I fit the strength A_N = -beta_fun * alpha_N / 4 / np.pi # I go to the coefficient in the tune N_terms = len(A_N) r_max = np.max(np.abs(z_slices)) dz = z_slices[1] - z_slices[0] r_vect = np.linspace(0, r_max, n_r) phi_vect = np.linspace(0, 2 * np.pi, n_phi + 1)[:-1] dphi = phi_vect[1] - phi_vect[0] sin_PHI = np.sin(phi_vect) cos_PHI = np.cos(phi_vect)