def run_script(argv): if not srwl_uti_proc_is_master(): exit() try: load_existing = int(argv[0]) == 1 except: load_existing = False try: ne = int(argv[1]) except: ne = spectrum_energy_ne try: do_propagation = int(argv[2]) == 1 except: do_propagation = True t0 = time.time() if not load_existing: print("Calculating 3D E-field on " + str(ne) + " energy points, in the range [" + str(spectrum_energy_from) + ", " + str(spectrum_energy_to) + "]") wfrEXY = calculate_initial_multi_energy_radiation( get_electron_beam(x0=-2e-6), get_magnetic_field_container(magnetic_field_file_name), energy_from=spectrum_energy_from, energy_to=spectrum_energy_to, ne=ne, aperturex=0.015, aperturey=0.015) print("done in", round(time.time() - t0, 3)) print("Check values", max(wfrEXY.arEx), max(wfrEXY.arEy)) if do_propagation: wfrEXY_T = calculate_multi_energy_radiation_at_focus( wfrEXY, get_beamline(), resize=False, t0=t0) extract_data_multi_electron_radiation_at_focus(wfrEXY_T, polarization="s") extract_data_multi_electron_radiation_at_focus(wfrEXY_T, polarization="p") else: wfrEXY_T = load_3D_wavefront(t0=t0, filename=WAVEFRONT_T_3D_FILE) extract_data_multi_electron_radiation_at_focus(wfrEXY_T, polarization="s") extract_data_multi_electron_radiation_at_focus(wfrEXY_T, polarization="p")
def run_script(argv): if not srwl_uti_proc_is_master(): exit() if len(argv) == 3: e_in = float(argv[0]) e_fin = float(argv[1]) n_e = int(argv[2]) else: e_in = 0.1 e_fin = 200.1 n_e = 201 app = QApplication([]) energies = numpy.linspace(e_in, e_fin, n_e) delta_energy = energies[1] - energies[0] electron_beam = get_electron_beam(x0=-2e-6) magnetic_field_container = get_magnetic_field_container( magnetic_field_file_name) dim_x = 1001 dim_y = 1001 # um in mm plot_coordinates_x = numpy.linspace(-0.1, 0.1, dim_x) plot_coordinates_y = numpy.linspace(-0.1, 0.1, dim_y) spectrum = numpy.zeros((n_e, 2)) spectrum[:, 0] = energies source_parameters = default_source_parameters propagation_parameters = auto_parameters total_re = None total_im = None for energy, ie in zip(energies, range(n_e)): wfr = calculate_initial_single_energy_radiation( electron_beam, magnetic_field_container, energy=energy, source_parameters=source_parameters) wfr = calculate_single_energy_radiation_at_focus( wfr, get_beamline(parameters=propagation_parameters)) x_coord = numpy.linspace(wfr.mesh.xStart, wfr.mesh.xFin, wfr.mesh.nx) * 1000 # mm y_coord = numpy.linspace(wfr.mesh.yStart, wfr.mesh.yFin, wfr.mesh.ny) * 1000 # mm pixel_area = (x_coord[1] - x_coord[0]) * (y_coord[1] - y_coord[0] ) # mm^2 intensity, re_field, im_field = get_wavefront_data( wfr) # photons/s/mm^2/0.1%BW intensity[numpy.where(numpy.isnan(intensity))] = 0.0 re_field[numpy.where(numpy.isnan(re_field))] = 0.0 im_field[numpy.where(numpy.isnan(im_field))] = 0.0 integrated_flux = intensity.sum() * pixel_area # photons/s/0.1%BW spectrum[ie, 1] = integrated_flux re_field_de = re_field * numpy.sqrt(1e3 * delta_energy / energy) im_field_de = im_field * numpy.sqrt(1e3 * delta_energy / energy) # to cumulate we need the same spatial mesh interpolator = RectBivariateSpline(x_coord, y_coord, re_field_de) re_field_de = interpolator(plot_coordinates_x, plot_coordinates_y) re_field_de[numpy.where(numpy.isnan(re_field))] = 0.0 interpolator = RectBivariateSpline(x_coord, y_coord, im_field_de) im_field_de = interpolator(plot_coordinates_x, plot_coordinates_y) im_field_de[numpy.where(numpy.isnan(im_field))] = 0.0 if total_re is None: total_re = re_field else: total_re += re_field if total_im is None: total_im = im_field else: total_im += im_field print("Energy", round(energy, 2), ", S.F.", round(integrated_flux, 2), "ph/s/0.1%BW") total_amplitude = numpy.abs(total_re + 1j * total_im) total_intensity = total_amplitude**2 total_power_density = (total_intensity * codata.e * delta_energy) * 1e9 # nW/mm^2 power = total_power_density.sum() * ( plot_coordinates_x[1] - plot_coordinates_x[0]) * ( plot_coordinates_y[1] - plot_coordinates_y[0] ) # power in nW in the interval E + dE print("Total Power", power, "nW") # cumulative quantities ################################## outdir = os.path.join(base_output_dir, "frequency_domain") if not os.path.exists(outdir): os.mkdir(outdir) numpy.savetxt(os.path.join(outdir, "Spectrum_at_focus.txt"), spectrum) numpy.savetxt(os.path.join(outdir, "Power_Density_at_Focus_coord_x.txt"), plot_coordinates_x) numpy.savetxt(os.path.join(outdir, "Power_Density_at_Focus_coord_y.txt"), plot_coordinates_y) numpy.savetxt(os.path.join(outdir, "Power_Density_at_Focus.txt"), total_power_density) plot_spectrum(spectrum) plot_power_density(plot_coordinates_x, plot_coordinates_y, total_power_density) app.exec_()
'Horizontal Position [mm]', 'Vertical Position [mm]', 'Intensity ' + where + ' Propagation, E=' + str(energy) ]) if show: uti_plot_show() import sys if __name__ == "__main__": if not srwl_uti_proc_is_master(): exit() try: energy = float(sys.argv[1]) except: energy = 0.1 try: shiftx = float(sys.argv[2]) except: shiftx = 0.0 wfr = calculate_initial_single_energy_radiation( get_electron_beam(x0=2e-06), get_magnetic_field_container(magnetic_field_file_name, from_file=True), energy=energy, aperturex=0.015, aperturey=0.015, shiftx=shiftx) plot_single_energy_radiation(wfr)
# IT IS IMPORTANT TO CHECK IF THE ELECTRIC FIELD IS RESOLVED EVERYWHERE arIIe = array('f', [0] * wfrEXY.mesh.ne) srwl.CalcIntFromElecField(arIIe, wfrEXY, 0, 5, 0, wfrEXY.mesh.eStart, x0, y0) uti_plot1d(arIIe, [wfrEXY.mesh.eStart, wfrEXY.mesh.eFin, wfrEXY.mesh.ne], labels=['Photon Energy', 'Re(E)', '3D Electric Field at (' + str(x0) + "," + str(y0) + ")"], units=['eV', '(ph/s/.1%bw/mm^2)^-1/2']) if show: uti_plot_show() if __name__=="__main__": if not srwl_uti_proc_is_master(): exit() wfrEXY = calculate_initial_multi_energy_radiation(get_electron_beam(), get_magnetic_field_container(magnetic_field_file_name), energy_from=1, energy_to=121, ne=120, aperturex=0.015, aperturey=0.015) check_electric_field(wfrEXY, x0=oe1_aperturex/2, show=True) plot_single_energy_radiation(wfrEXY)
return wfr if __name__ == "__main__": if not srwl_uti_proc_is_master(): exit() try: energy = float(sys.argv[1]) except: energy = 20 try: shiftx = float(sys.argv[2]) except: shiftx = 0.0 wfr = calculate_initial_single_energy_radiation( get_electron_beam(x0=-2e-6), get_magnetic_field_container(magnetic_field_file_name), energy=energy, source_parameters=default_source_parameters, shiftx=shiftx, aperturex=0.015, aperturey=0.015) plot_single_energy_radiation(wfr, where="Before", show=False) wfr = calculate_single_energy_radiation_at_focus(wfr, get_beamline()) plot_single_energy_radiation(wfr, where="After", show=True)
def plot_trajectory(partTraj, show=True): for i in range(len(partTraj.arZ)): partTraj.arBy[i] *= 1e4 partTraj.arZ[i] *= 1e2 partTraj.arX[i] *= 1e2 uti_plot1d_ir(partTraj.arBy, partTraj.arZ, labels=['z', 'Vertical Magnetic Field'], units=['cm', 'G', '']) uti_plot1d_ir(partTraj.arX, partTraj.arZ, labels=['z', 'Horizontal Position'], units=['cm', 'cm', '']) uti_plot1d_ir(partTraj.arXp, partTraj.arZ, labels=['z', 'Horizontal Angle'], units=['cm', 'rad', '']) if show: uti_plot_show() if __name__ == "__main__": if not srwl_uti_proc_is_master(): exit() partTraj = calculate_trajectory( get_electron_beam(), get_magnetic_field_container(magnetic_field_file_name, from_file=True)) plot_trajectory(partTraj)
'Spectrum at (' + str(x0) + "," + str(y0) + ")" ], units=['eV', 'ph/s/.1%bw/mm^2']) arReEe = array('f', [0] * mesh0.ne) srwl.CalcIntFromElecField(arReEe, wfr_spectrum, 0, 5, 0, mesh0.eStart, x0, y0) # IT IS IMPORTANT TO CHECK IF THE ELECTRIC FIELD IS RESOLVED EVERYWHERE uti_plot1d(arReEe, [ wfr_spectrum.mesh.eStart, wfr_spectrum.mesh.eFin, wfr_spectrum.mesh.ne ], labels=[ 'Photon Energy', 'Re(E)', 'Electric Field at (' + str(x0) + "," + str(y0) + ")" ], units=['eV', '(ph/s/.1%bw/mm^2)^-1/2']) if show: uti_plot_show() if __name__ == "__main__": if not srwl_uti_proc_is_master(): exit() x0 = oe1_aperturex / 2 calculate_spectrum(get_electron_beam(), get_magnetic_field_container(magnetic_field_file_name), x0=0.0)
def run_script(argv): if not srwl_uti_proc_is_master(): exit() if len(argv) == 3: e_in = float(argv[0]) e_fin = float(argv[1]) n_e = int(argv[2]) else: e_in = 0.1 e_fin = 200.1 n_e = 201 app = QApplication([]) energies = numpy.linspace(e_in, e_fin, n_e) delta_energy = energies[1] - energies[0] electron_beam = get_electron_beam(x0=-2e-6) magnetic_field_container = get_magnetic_field_container(magnetic_field_file_name) beamline = get_beamline(parameters=auto_parameters) dim_x = 2001 dim_y = 2001 # um in mm plot_coordinates_x = numpy.linspace(-0.1, 0.1, dim_x) plot_coordinates_y = numpy.linspace(-0.1, 0.1, dim_y) plot_pixel_area = (plot_coordinates_x[1] - plot_coordinates_x[0]) * (plot_coordinates_y[1] - plot_coordinates_y[0]) total_power_density = numpy.zeros((dim_x, dim_y)) spectrum = numpy.zeros((n_e, 2)) spectrum[:, 0] = energies total_power = 0.0 total_power_interp = 0.0 source_parameters = default_source_parameters for energy, ie in zip(energies, range(n_e)): wfr = calculate_initial_single_energy_radiation(electron_beam, magnetic_field_container, energy=energy, source_parameters=source_parameters, aperturex=0.015, aperturey=0.015) wfr = calculate_single_energy_radiation_at_focus(wfr, beamline) x_coord = numpy.linspace(wfr.mesh.xStart, wfr.mesh.xFin, wfr.mesh.nx) * 1000 # mm y_coord = numpy.linspace(wfr.mesh.yStart, wfr.mesh.yFin, wfr.mesh.ny) * 1000 # mm pixel_area = (x_coord[1] - x_coord[0]) * (y_coord[1] - y_coord[0]) # mm^2 intensity = get_intensity(wfr) # photons/s/mm^2/0.1%BW intensity[numpy.where(numpy.isnan(intensity))] = 0.0 intensity[numpy.where(intensity < 0.0)] = 0.0 integrated_flux = intensity.sum() * pixel_area # photons/s/0.1%BW power_density = (intensity * 1e3 * delta_energy * codata.e) * 1e9 # nW/mm^2 power = (integrated_flux * 1e3 * delta_energy * codata.e) * 1e9 # power in nW in the interval E + dE print("Energy", round(energy, 2), ", S.F.", round(integrated_flux, 2), "ph/s/0.1%BW, Max Intensity", round(numpy.max(intensity), 2), "ph/s/0.1%BW/mm^2, Power (before fit)", round(power, 6), "nW, Max P.D. (before fit)", round(numpy.max(power_density), 4), "nW/mm^2") total_power += power # cumulative quantities ################################## spectrum[ie, 1] = integrated_flux renorm_factor_pd = power # to cumulate we need the same spatial mesh interpolator = RectBivariateSpline(x_coord, y_coord, power_density, kx=3, ky=3) power_density = interpolator(plot_coordinates_x, plot_coordinates_y) power_density[numpy.where(numpy.isnan(power_density))] = 0.0 power_density[numpy.where(power_density < 0.0)] = 0.0 power = power_density.sum() * plot_pixel_area renorm_factor_pd /= power power_density *= renorm_factor_pd total_power_density += power_density total_power_interp += power print(" ", "Power (after fit)", round(power, 6), "nW, Max P.D. (after fit)", round(numpy.max(power_density), 4), "nW/mm^2\n") print("Calculation completed", "Total Power", round(total_power, 6), round(total_power_interp, 6), "nW, Min/Max Total P.D.", round(numpy.min(total_power_density), 4), round(numpy.max(total_power_density), 4), "nW/mm^2\n") outdir = os.path.join(base_output_dir, "frequency_domain") if not os.path.exists(outdir): os.mkdir(outdir) numpy.savetxt(os.path.join(outdir, "Spectrum_at_focus.txt"), spectrum) numpy.savetxt(os.path.join(outdir, "Power_Density_at_Focus_coord_x.txt"), plot_coordinates_x) numpy.savetxt(os.path.join(outdir, "Power_Density_at_Focus_coord_y.txt"), plot_coordinates_y) numpy.savetxt(os.path.join(outdir, "Power_Density_at_Focus.txt"), total_power_density) plot_spectrum(spectrum) x_indexes = numpy.where(numpy.logical_and(plot_coordinates_x>= -0.025, plot_coordinates_x <= 0.025)) y_indexes = numpy.where(numpy.logical_and(plot_coordinates_y>= -0.025, plot_coordinates_y <= 0.025)) power_density_reduced = numpy.zeros((len(x_indexes[0]), len(y_indexes[0]))) for i in range(len(x_indexes[0])): for j in range(len(y_indexes[0])): power_density_reduced[i, j] = total_power_density[x_indexes[0][i], y_indexes[0][j]] print(power_density_reduced.shape) #plot_power_density(plot_coordinates_x, plot_coordinates_y, total_power_density, total_power) plot_power_density(plot_coordinates_x[x_indexes], plot_coordinates_y[y_indexes], power_density_reduced, total_power) app.exec_()