def show_diagnostics(FELsource_out_number): # read FELsource_out_.h5 if not FELsource_out_number == 'FELsource_out_0000001.h5': #FELsource_out_file = "FELsource_out_{}.h5".format(FELsource_out_number.zfill(7)) FELsource_out_file = "{}.h5".format(FELsource_out_number.zfill(7)) else: FELsource_out_file = FELsource_out_number if not os.path.exists(FELsource_out_file): print 'Input file {} not found.'.format(FELsource_out_file) return wf = Wavefront() wf.load_hdf5(FELsource_out_file) # show two figures window 1: image of I(x,y) integral intensity, with real # x and y axis and title with file name J2eV = 6.24150934e18; mesh = wf.params.Mesh tmin = mesh.sliceMin; tmax = mesh.sliceMax; dt = (tmax - tmin) / (mesh.nSlices - 1); dx = (mesh.xMax - mesh.xMin) / (mesh.nx - 1); dy = (mesh.yMax - mesh.yMin) / (mesh.ny - 1); wf_intensity = wf.get_intensity(polarization='horizontal'); total_intensity = wf_intensity.sum(axis=-1); data = total_intensity * dt plt.figure() plt.imshow(data*dx*dy*1e6*J2eV/wf.params.photonEnergy,extent=[mesh.xMin*1e6,mesh.xMax*1e6,mesh.yMin*1e6,mesh.yMax * 1e6]) title = 'Number of photons per %.2f x %.2f $\mu m ^2$ pixel' % (dx*1e6, dx*1e6) plt.title(title) plt.colorbar(); plt.xlabel('[$\mu m$]'); # window 2: plot of 2 curves: #(1) history/parent/temporal_struct - FAST post-processing temporal_struct = wf.custom_fields['history']['parent']['misc']['temporal_struct'] t0 = (temporal_struct[:, 0].max() + temporal_struct[:, 0].min()) / 2 plt.figure() plt.plot(temporal_struct[:, 0] - t0, temporal_struct[:, 1] * 1e-9, 'b',label = 'output FAST-pp') plt.hold(True) #(2) integral intensity I(t) calculated for wavefront written in h5 t = np.linspace(tmin, tmax, wf.params.Mesh.nSlices) pulse_energy = wf.get_intensity().sum(axis=0).sum(axis=0) #check it plt.plot(t * 1e15, pulse_energy*dx*dy*1e6*1e-9,'ro', label = 'wavefront data') title = 'FEL pulse energy %.2f %s ' % (pulse_energy.sum(axis=0) * dx * dy * 1e6 * dt * 1e3, 'mJ') plt.title(title) plt.xlabel('time [fs]'); plt.ylabel('Instantaneous power [GW]'); plt.legend() plt.grid(True) plt.show()
def show_diagnostics(FELsource_out_number): FELsource_out_file = FELsource_out_number if not os.path.exists(FELsource_out_file): print('Input file {} not found.'.format(FELsource_out_file)) return wf = Wavefront() wf.load_hdf5(FELsource_out_file) plot_t_wf(wf) look_at_q_space(wf) # show two figures window 1: image of I(x,y) integral intensity, with real # x and y axis and title with file name J2eV = 6.24150934e18; mesh = wf.params.Mesh tmin = mesh.sliceMin; tmax = mesh.sliceMax; dt = (tmax - tmin) / (mesh.nSlices - 1); dx = (mesh.xMax - mesh.xMin) / (mesh.nx - 1); dy = (mesh.yMax - mesh.yMin) / (mesh.ny - 1); wf_intensity = wf.get_intensity(polarization='horizontal'); total_intensity = wf_intensity.sum(axis=-1); data = total_intensity * dt plt.figure() plt.imshow(data*dx*dy*1e6*J2eV/wf.params.photonEnergy,extent=[mesh.xMin*1e6,mesh.xMax*1e6,mesh.yMin*1e6,mesh.yMax * 1e6], cmap="YlGnBu_r") title = 'Number of photons per %.2f x %.2f $\mu m ^2$ pixel' % (dx*1e6, dx*1e6) plt.title(title) plt.colorbar(); plt.xlabel('[$\mu m$]'); # window 2: plot of 2 curves: #(1) history/parent/temporal_struct - FAST post-processing temporal_struct = wf.custom_fields['history']['parent']['misc']['temporal_struct'] t0 = (temporal_struct[:, 0].max() + temporal_struct[:, 0].min()) / 2 plt.figure() plt.plot(temporal_struct[:, 0] - t0, temporal_struct[:, 1] * 1e-9, 'b',label = 'output FAST-pp') plt.hold(True) #(2) integral intensity I(t) calculated for wavefront written in h5 t = np.linspace(tmin, tmax, wf.params.Mesh.nSlices) pulse_energy = wf.get_intensity().sum(axis=0).sum(axis=0) #check it plt.plot(t * 1e15, pulse_energy*dx*dy*1e6*1e-9,'ro', label = 'wavefront data') title = 'FEL pulse energy %.2f %s ' % (pulse_energy.sum(axis=0) * dx * dy * 1e6 * dt * 1e3, 'mJ') plt.title(title) plt.xlabel('time [fs]'); plt.ylabel('Instantaneous power [GW]'); plt.legend() plt.grid(True) plt.show()
def plot_intensity(wavefront): """IN 2D PLOT THE INTENSITY OF A WAVFRONT""" print("******plotting wavefront intensity") # calculate intensity wavefront_intensity = Wavefront.get_intensity(wavefront) wavefront_intensity = wavefront_intensity.sum(-1) plt.figure() plt.set_cmap("jet") plt.imshow(wavefront_intensity) # remove ticks plt.gca().set_xticks([]) plt.gca().set_yticks([]) # color bar cbar = plt.colorbar() cbar.ax.set_ylabel("Intensity")
def propToBeamParameters(prop_output_path): """ Utility to setup a PhotonBeamParameters instance from propagation output. """ # Check prop out exists. if not os.path.isfile(prop_output_path): raise IOError("File not found: %s." % (prop_output_path)) # Construct the wavefront. wavefront = Wavefront() wavefront.load_hdf5(prop_output_path) pulse_energy = wpg_uti_wf.calc_pulse_energy(wavefront) mesh = wavefront.params.Mesh dx = (mesh.xMax - mesh.xMin) / (mesh.nx - 1) dy = (mesh.yMax - mesh.yMin) / (mesh.ny - 1) int0 = wavefront.get_intensity().sum(axis=0).sum(axis=0) # I(slice_num) total_intensity = int0 * dx * dy # [J] times = numpy.linspace(mesh.sliceMin, mesh.sliceMax, mesh.nSlices) t = times[:-1] dt = times[1:] - times[:-1] It = total_intensity[:-1] m0 = numpy.sum(It * dt) m1 = numpy.sum(It * t * dt) / m0 m2 = numpy.sum(It * t**2 * dt) / m0 rms = math.sqrt(m2 - m1**2) spike_fwhm_J = constants.hbar / rms spike_fwhm_eV = spike_fwhm_J / constants.e # Switch to energy domain srwl.SetRepresElecField(wavefront._srwl_wf, 'f') mesh = wavefront.params.Mesh spectrum = wavefront.get_intensity().sum(axis=0).sum( axis=0) # I(slice_num) energies = numpy.linspace(mesh.sliceMin, mesh.sliceMax, mesh.nSlices) w = energies[:-1] dw = energies[1:] - energies[:-1] Iw = spectrum[:-1] m0 = numpy.sum(Iw * dw) m1 = numpy.sum(Iw * w * dw) / m0 m2 = numpy.sum(Iw * w**2 * dw) / m0 rms = math.sqrt(m2 - m1**2) photon_energy = m1 #spec_fwhm_eV = rms # Extract beam diameter fwhm xy_fwhm = wpg_uti_wf.calculate_fwhm(wavefront) # Extract divergence # Switch to reciprocal space srwl.SetRepresElecField(wavefront._srwl_wf, 'a') qxqy_fwhm = wpg_uti_wf.calculate_fwhm(wavefront) del wavefront beam_parameters = PhotonBeamParameters( photon_energy=photon_energy * electronvolt, photon_energy_relative_bandwidth=spike_fwhm_eV / photon_energy, pulse_energy=pulse_energy * joule, divergence=max([qxqy_fwhm['fwhm_x'], qxqy_fwhm['fwhm_y']]) / 2. * radian, beam_diameter_fwhm=max([xy_fwhm['fwhm_x'], xy_fwhm['fwhm_y']]) * meter, photon_energy_spectrum_type="SASE", ) return beam_parameters
def testGaussianReference(self, debug=False): """ Check that propagation of a Gaussian pulse (in t,x,y) through vacuum reproduces reference data. """ # Central photon energy. ekev = 8.4 # Energy [keV] # Pulse parameters. qnC = 0.5 # e-bunch charge, [nC] pulse_duration = 9.0e-15 # [s] pulseEnergy = 1.5e-3 # total pulse energy, J # Coherence time coh_time = 0.25e-15 # [s] # Distance in free space. z0 = 10. # (m), position where to build the wavefront. z1 = 10. # (m), distance to travel in free space. # Beam divergence. theta_fwhm = 2.5e-6 # rad wlambda = 12.4*1e-10/ekev # wavelength, m w0 = wlambda/(numpy.pi*theta_fwhm) # beam waist, m zR = (math.pi*w0**2)/wlambda #Rayleigh range, m fwhm_at_zR = theta_fwhm*zR #FWHM at Rayleigh range, m sigmaAmp = w0/(2.0*math.sqrt(math.log(2.0))) #sigma of amplitude, m if debug: print (" *** Pulse properties ***") print (" lambda = %4.3e m" % (wlambda) ) print (" w0 = %4.3e m" % (w0) ) print (" zR = %4.3e m" % (zR) ) print (" fwhm at zR = %4.3e m" % (fwhm_at_zR) ) print (" sigma = %4.3e m" % (sigmaAmp) ) # expected beam radius after free space drift. expected_beam_radius = w0*math.sqrt(1.0+(z0/zR)**2) # Number of points in each x and y dimension. np=400 # Sampling window = 6 sigma of initial beam. range_xy = 6.*expected_beam_radius dx = range_xy / (np-1) nslices = 20 if debug: print (" Expected beam waist at z=%4.3f m : %4.3e m." % (z0, expected_beam_radius) ) print ("Setting up mesh of %d points per dimension on a %4.3e x %4.3e m^2 grid with grid spacing %4.3e m." % (np, range_xy, range_xy, dx) ) # Construct srw wavefront. srwl_wf = build_gauss_wavefront(np, np, nslices, ekev, -range_xy/2., range_xy/2., -range_xy/2., range_xy/2., coh_time/math.sqrt(2.), sigmaAmp, sigmaAmp, z0, pulseEn=pulseEnergy, pulseRange=8.) # Convert to wpg. wf = Wavefront(srwl_wf) if debug: print('*** z=%4.3e m ***' % (z0)) fwhm = calculate_fwhm(wf) print('fwhm_x = %4.3e\nfwhm_y = %4.3e' % (fwhm['fwhm_x'], fwhm['fwhm_y']) ) plot_t_wf(wf) look_at_q_space(wf) # Construct the beamline. beamline = Beamline() # Add free space drift. drift = Drift(z1) beamline.append( drift, Use_PP(semi_analytical_treatment=1)) # Propagate srwl.SetRepresElecField(wf._srwl_wf, 'f') # <---- switch to frequency domain beamline.propagate(wf) srwl.SetRepresElecField(wf._srwl_wf, 't') if debug: print('*** z=%4.3e m ***' % (z0+z1)) fwhm = calculate_fwhm(wf) print('fwhm_x = %4.3e\nfwhm_y = %4.3e' % (fwhm['fwhm_x'], fwhm['fwhm_y']) ) plot_t_wf(wf) look_at_q_space(wf) # Get propagated wavefront data. wf_intensity = wf.get_intensity() # Project on t axis. wf_onaxis = wf_intensity.sum(axis=(0,1)) # Get hash of the data. wf_hash = hash( wf_intensity.tostring() ) # Load reference hash. with open(TestUtilities.generateTestFilePath("reference_wf_gauss_10m.hash.txt"), 'r') as hashfile: ref_hash = hashfile.readline() hashfile.close() ref_onaxis = numpy.loadtxt(TestUtilities.generateTestFilePath("reference_wf_gauss_onaxis_10m.txt")) # Weak test. for x,y in zip(wf_onaxis, ref_onaxis): self.assertAlmostEqual( x, y, 14 ) # Strong test. self.assertEqual( str(wf_hash), ref_hash)
def show_diagnostics(prop_out_number): # read prop_out_.h5 if not prop_out_number == 'prop_out_1.h5': #prop_out_file = "prop_out_{}.h5".format(prop_out_number.zfill(7)) prop_out_file = "{}.h5".format(prop_out_number.zfill(7)) else: prop_out_file = prop_out_number if not os.path.exists(prop_out_file): print 'Input file {} not found.'.format(prop_out_file) return wf = Wavefront() wf.load_hdf5(prop_out_file) # show two figures window 1: image of I(x,y) integral intensity, with real # x and y axis and title with file name J2eV = 6.24150934e18 mesh = wf.params.Mesh tmin = mesh.sliceMin tmax = mesh.sliceMax dt = (tmax - tmin) / (mesh.nSlices - 1) dx = (mesh.xMax - mesh.xMin) / (mesh.nx - 1) dy = (mesh.yMax - mesh.yMin) / (mesh.ny - 1) wf_intensity = wf.get_intensity(polarization='horizontal') total_intensity = wf_intensity.sum(axis=-1) data = total_intensity * dt plt.figure() plt.imshow(data*dx*dy*1e6*J2eV/wf.params.photonEnergy,extent=[mesh.xMin*1e6,mesh.xMax*1e6,mesh.yMin*1e6,mesh.yMax * 1e6]) title = 'Number of photons per %.2f x %.2f $nm ^2$ pixel' % (dx*1e9, dx*1e9) plt.title(title) plt.colorbar() plt.xlabel(r'[$\mu$m]') # window 2: plot of 2 curves: #(1) history/parent/parent/temporal_struct - before propagating temporal_struct = wf.custom_fields['history']['parent']['parent']['misc']['temporal_struct'] t0 = (temporal_struct[:, 0].max() + temporal_struct[:, 0].min()) / 2 plt.figure() plt.plot(temporal_struct[:, 0] - t0, temporal_struct[:, 1] * 1e-9, 'b',label = 'original') plt.hold(True) #(2) integral intensity I(t) after propagating t = np.linspace(tmin, tmax, wf.params.Mesh.nSlices) pulse_energy = wf.get_intensity().sum(axis=0).sum(axis=0) #check it plt.plot(t * 1e15, pulse_energy*dx*dy*1e6*1e-9,'r', label = 'propag') title = 'The propagated pulse energy %.2f %s ' % (pulse_energy.sum(axis=0) * dx * dy * 1e6 * dt * 1e3, 'mJ') plt.title(title) plt.xlabel('time [fs]') plt.ylabel('Instantaneous power [GW]') plt.legend() plt.grid(True) sz0 = wf.custom_fields['misc']['spectrum0'] sz1 = wf.custom_fields['misc']['spectrum1'] plt.figure() plt.plot(sz0[:,0],sz0[:,1], label='before propagating') plt.hold(True) plt.plot(sz1[:,0],sz1[:,1],label='after propagating') plt.grid(True) plt.title('Spectrum (x=y=0)') plt.xlabel('[eV]') plt.ylabel('[arb. unit]') plt.legend() plt.show()