def calculate_wavefront1D(wavelength=1e-10, undulator_length=1.0, undulator_distance=10.0, x_min=-0.1, x_max=0.1, number_of_points=101, wavefront_position=0, add_random_phase=0): from wofry.propagator.wavefront1D.generic_wavefront import GenericWavefront1D sigma_r = 2.740 / 4 / numpy.pi * numpy.sqrt(wavelength * undulator_length) sigma_r_prime = 0.69 * numpy.sqrt(wavelength / undulator_length) wavefront1D = GenericWavefront1D.initialize_wavefront_from_range(x_min=x_min, x_max=x_max, number_of_points=number_of_points) wavefront1D.set_wavelength(wavelength) if wavefront_position == 0: # Gaussian source wavefront1D.set_gaussian(sigma_x=sigma_r, amplitude=1.0, shift=0.0) elif wavefront_position == 1: # Spherical source, Gaussian intensity wavefront1D.set_spherical_wave(radius=undulator_distance, center=0.0, complex_amplitude=complex(1, 0)) # weight with Gaussian X = wavefront1D.get_abscissas() A = wavefront1D.get_complex_amplitude() sigma = undulator_distance * sigma_r_prime sigma_amplitude = sigma * numpy.sqrt(2) Gx = numpy.exp(-X * X / 2 / sigma_amplitude ** 2) wavefront1D.set_complex_amplitude(A * Gx) if add_random_phase: wavefront1D.add_phase_shifts(2 * numpy.pi * numpy.random.random(wavefront1D.size())) return wavefront1D
def run_beamline_2(error_flag=0,error_file=""): # # main # create input_wavefront # # from wofry.propagator.wavefront1D.generic_wavefront import GenericWavefront1D input_wavefront = GenericWavefront1D.initialize_wavefront_from_range(x_min=-0.0002, x_max=0.0002, number_of_points=5000) input_wavefront.set_photon_energy(250) input_wavefront.set_gaussian(sigma_x=0.000012, amplitude=1.000000, shift=0.000000) output_wavefront, abscissas_on_mirror, height = calculate_output_wavefront_after_grazing_reflector1D( input_wavefront, shape=2, p_focus=15.0, q_focus=15.0, grazing_angle_in=0.02181661, p_distance=15.0, q_distance=15.0, zoom_factor=10, #0.3, error_flag=error_flag, error_file=error_file, write_profile=0) # from srxraylib.plot.gol import plot # plot(output_wavefront.get_abscissas(), output_wavefront.get_intensity()) return output_wavefront
def run_beamline(error_flag=1,error_file="/Users/srio/Oasys/dabam_profile_6010353992.dat"): # wf0 = calculate_wavefront1D(wavelength=4.9593679373280105e-09, # wavefront_position=0, # undulator_length=2.24, # undulator_distance=13.73, # x_min=-0.00025, # x_max=0.00025, # number_of_points=1000, # add_random_phase=0) # # create input_wavefront # # from wofry.propagator.wavefront1D.generic_wavefront import GenericWavefront1D input_wavefront = GenericWavefront1D.initialize_wavefront_from_range(x_min=-0.0002, x_max=0.0002, number_of_points=1000) input_wavefront.set_photon_energy(250) input_wavefront.set_gaussian(sigma_x=0.000012, amplitude=1.000000, shift=0.000000) wf0 = input_wavefront # from srxraylib.plot.gol import plot # plot(input_wavefront.get_abscissas(), input_wavefront.get_intensity()) # # # wf1 = propagate_in_vacuum(wf0) # plot(1e6 * wf1.get_abscissas(), wf1.get_intensity()) # # # wf2, abscissas_on_mirror, height = calculate_output_wavefront_after_reflector1D(wf1, shape=1,radius=687.6038987249137,grazing_angle=0.02181661, error_flag=error_flag, error_file=error_file, error_edge_management=0,write_profile="") # plot(1e6 * wf2.get_abscissas(),wf2.get_intensity()) # # # wf3 = propagate_in_vacuum(wf2,magnification_x=0.03,propagator_flag='i') return wf3
def test_save_load_h5_file(self): wfr = GenericWavefront1D.initialize_wavefront_from_range( -2.0, 2.0, number_of_points=100) wfr.set_gaussian(.2, amplitude=5 + 8j) print("Saving 1D wavefront to file: tmp.h5") wfr.save_h5_file("tmp.h5", "wfr1", intensity=True, phase=True) print("Reading 1D wavefront from file: tmp.h5") wfr2 = GenericWavefront1D.load_h5_file("tmp.h5", "wfr1") print("Cleaning file tmp.h5") os.remove("tmp.h5") assert (wfr2.is_identical(wfr))
def gaussian_wavefront(center, angle): # # create input_wavefront # # input_wavefront = GenericWavefront1D.initialize_wavefront_from_range( x_min=-0.0028, x_max=0.0028, number_of_points=8192) input_wavefront.set_photon_energy(17225) input_wavefront.set_spherical_wave(radius=28.3, center=center, complex_amplitude=complex(1, 0)) # # apply Gaussian amplitude # input_wavefront = apply_gaussian(input_wavefront, shift=28.3 * angle) return input_wavefront
def test_initializers(self, do_plot=do_plot): print("# ") print("# Tests for initializars (1D) ") print("# ") x = numpy.linspace(-100, 100, 50) y = numpy.abs(x)**1.5 + 1j * numpy.abs(x)**1.8 wf0 = GenericWavefront1D.initialize_wavefront_from_steps( x[0], numpy.abs(x[1] - x[0]), y.size) wf0.set_complex_amplitude(y) wf1 = GenericWavefront1D.initialize_wavefront_from_range( x[0], x[-1], y.size) wf1.set_complex_amplitude(y) wf2 = GenericWavefront1D.initialize_wavefront_from_arrays(x, y) print("wavefront sizes: ", wf1.size(), wf1.size(), wf2.size()) if do_plot: from srxraylib.plot.gol import plot plot(wf0.get_abscissas(), wf0.get_intensity(), title="initialize_wavefront_from_steps", show=0) plot(wf1.get_abscissas(), wf1.get_intensity(), title="initialize_wavefront_from_range", show=0) plot(wf2.get_abscissas(), wf2.get_intensity(), title="initialize_wavefront_from_arrays", show=1) numpy.testing.assert_almost_equal(wf0.get_intensity(), numpy.abs(y)**2, 5) numpy.testing.assert_almost_equal(wf1.get_intensity(), numpy.abs(y)**2, 5) numpy.testing.assert_almost_equal(x, wf1.get_abscissas(), 11) numpy.testing.assert_almost_equal(x, wf2.get_abscissas(), 11)
def test_polarization(self): print("# ") print("# Tests polarization (1D) ") print("# ") wavefront = GenericWavefront1D.initialize_wavefront_from_range( x_min=-0.5e-3, x_max=0.5e-3, number_of_points=2048, wavelength=1.5e-10, polarization=Polarization.TOTAL) ca = numpy.zeros(wavefront.size()) wavefront.set_complex_amplitude(ca + (1 + 0j), ca + (0 + 1j)) numpy.testing.assert_almost_equal( wavefront.get_interpolated_phase(0.1e-3, polarization=Polarization.SIGMA), 0.0) numpy.testing.assert_almost_equal( wavefront.get_interpolated_phase(-0.1e-3, polarization=Polarization.PI), numpy.pi / 2) numpy.testing.assert_almost_equal( wavefront.get_interpolated_intensities( -0.111e-3, polarization=Polarization.TOTAL), 2.0) numpy.testing.assert_almost_equal( wavefront.get_interpolated_intensities( -0.111e-3, polarization=Polarization.SIGMA), 1.0) numpy.testing.assert_almost_equal( wavefront.get_interpolated_intensities( -0.111e-3, polarization=Polarization.PI), 1.0) numpy.testing.assert_almost_equal( wavefront.get_intensity(polarization=Polarization.SIGMA), (ca + 1)**2) numpy.testing.assert_almost_equal( wavefront.get_intensity(polarization=Polarization.PI), (ca + 1)**2) numpy.testing.assert_almost_equal( wavefront.get_intensity(polarization=Polarization.TOTAL), 2 * (ca + 1)**2)
def test_guess_wavefront_curvature(self): print("# ") print("# Tests guessing wavefront curvature ") print("# ") # # create source # wavefront = GenericWavefront1D.initialize_wavefront_from_range( x_min=-0.5e-3, x_max=0.5e-3, number_of_points=2048, wavelength=1.5e-10) radius = -50.0 # 50. wavefront.set_spherical_wave(radius=radius) if do_plot: from srxraylib.plot.gol import plot radii, fig_of_mer = wavefront.scan_wavefront_curvature(rmin=-1000, rmax=1000, rpoints=100) plot(radii, fig_of_mer) guess_radius = wavefront.guess_wavefront_curvature(rmin=-1000, rmax=1000, rpoints=100) assert (numpy.abs(radius - guess_radius) < 1e-3)
def test_propagate_1D_fraunhofer_phase(self, do_plot=do_plot): # aperture_type="square" # aperture_diameter = 40e-6 # wavefront_length = 800e-6 # wavelength = 1.24e-10 # npoints=1024 # propagation_distance=40 show = 1 aperture_type = "square" aperture_diameter = 40e-6 wavefront_length = 800e-6 wavelength = 1.24e-10 propagation_distance = 30.0 npoints = 1024 print( "\n# ") print( "# far field 1D (fraunhofer and zoom) diffraction from a %s aperture " % aperture_type) print("# ") wf = GenericWavefront1D.initialize_wavefront_from_range( x_min=-wavefront_length / 2, x_max=wavefront_length / 2, number_of_points=npoints, wavelength=wavelength) wf.set_plane_wave_from_complex_amplitude( (2.0 + 1.0j)) # an arbitraty value propagation_elements = PropagationElements() if aperture_type == 'square': slit = WOSlit1D(boundary_shape=Rectangle(-aperture_diameter / 2, aperture_diameter / 2, 0, 0)) else: raise Exception("Not implemented! ") propagation_elements.add_beamline_element( BeamlineElement(optical_element=slit, coordinates=ElementCoordinates( p=0, q=propagation_distance))) propagator = PropagationManager.Instance() propagation_parameters = PropagationParameters( wavefront=wf, propagation_elements=propagation_elements) wf1_franuhofer = propagator.do_propagation(propagation_parameters, Fraunhofer1D.HANDLER_NAME) propagation_parameters.set_additional_parameters( "shift_half_pixel", True) propagation_parameters.set_additional_parameters( "magnification_x", 1.5) wf1_zoom = propagator.do_propagation(propagation_parameters, FresnelZoom1D.HANDLER_NAME) intensity_fraunhofer = wf1_franuhofer.get_intensity( ) / wf1_franuhofer.get_intensity().max() intensity_zoom = wf1_zoom.get_intensity() / wf1_zoom.get_intensity( ).max() if do_plot: from srxraylib.plot.gol import plot plot( wf1_franuhofer.get_abscissas() * 1e6 / propagation_distance, intensity_fraunhofer, wf1_zoom.get_abscissas() * 1e6 / propagation_distance, intensity_zoom, legend=["Fraunhofer", "Zoom"], legend_position=(0.95, 0.95), title= "1D INTENSITY diffraction from aperture of %3.1f um at wavelength of %3.1f A" % (aperture_diameter * 1e6, wavelength * 1e10), xtitle="X (urad)", ytitle="Intensity", xrange=[-20, 20], show=show) plot( wf1_franuhofer.get_abscissas() * 1e6 / propagation_distance, wf1_franuhofer.get_phase(unwrap=1), wf1_zoom.get_abscissas() * 1e6 / propagation_distance, wf1_zoom.get_phase(unwrap=1), legend=["Fraunhofer", "Zoom"], legend_position=(0.95, 0.95), title= "1D diffraction from a %s aperture of %3.1f um at wavelength of %3.1f A" % (aperture_type, aperture_diameter * 1e6, wavelength * 1e10), xtitle="X (urad)", ytitle="Intensity", xrange=[-20, 20], show=show)
def propagate_1D( self, do_plot=do_plot, method='fft', wavelength=1.24e-10, aperture_type='square', aperture_diameter=40e-6, wavefront_length=100e-6, npoints=500, propagation_distance=30.0, normalization=True, # TODO put False show=1, amplitude=(0.0 + 1.0j)): print( "\n# ") print("# 1D (%s) propagation from a %s aperture " % (method, aperture_type)) print("# ") wf = GenericWavefront1D.initialize_wavefront_from_range( x_min=-wavefront_length / 2, x_max=wavefront_length / 2, number_of_points=npoints, wavelength=wavelength) wf.set_plane_wave_from_complex_amplitude( amplitude) # an arbitraty value deltax = wf.get_abscissas()[1] - wf.get_abscissas()[0] propagation_elements = PropagationElements() slit = None if aperture_type == 'square': slit = WOSlit1D(boundary_shape=Rectangle(-aperture_diameter / 2, aperture_diameter / 2, 0, 0)) elif aperture_type == 'gaussian': slit = WOGaussianSlit1D( boundary_shape=Rectangle(-aperture_diameter / 2, aperture_diameter / 2, 0, 0)) else: raise Exception( "Not implemented! (accepted: circle, square, gaussian)") propagation_elements.add_beamline_element( BeamlineElement(optical_element=slit, coordinates=ElementCoordinates( p=0, q=propagation_distance))) propagator = PropagationManager.Instance() propagation_parameters = PropagationParameters( wavefront=wf, propagation_elements=propagation_elements) print("Using propagator method: ", method) fresnel_analytical = True if method == 'fft': wf1 = propagator.do_propagation(propagation_parameters, Fresnel1D.HANDLER_NAME) elif method == 'convolution': wf1 = propagator.do_propagation(propagation_parameters, FresnelConvolution1D.HANDLER_NAME) elif method == 'integral': propagation_parameters.set_additional_parameters( "magnification_x", 1.5) propagation_parameters.set_additional_parameters( "magnification_N", 2.0) wf1 = propagator.do_propagation(propagation_parameters, Integral1D.HANDLER_NAME) elif method == 'fraunhofer': fresnel_analytical = False # propagation_parameters.set_additional_parameters("shift_half_pixel", 0) wf1 = propagator.do_propagation(propagation_parameters, Fraunhofer1D.HANDLER_NAME) elif method == 'zoom': propagation_parameters.set_additional_parameters( "magnification_x", 1.5) wf1 = propagator.do_propagation(propagation_parameters, FresnelZoom1D.HANDLER_NAME) else: raise Exception("Not implemented method: %s" % method) if fresnel_analytical: xx, alpha = fresnel_analytical_rectangle( fresnel_number=None, propagation_distance=propagation_distance, aperture_half=0.5 * aperture_diameter, wavelength=wavelength, detector_array=wf1.get_abscissas(), npoints=None) else: xx, alpha = fraunhofer_analytical_rectangle( fresnel_number=None, propagation_distance=propagation_distance, aperture_half=0.5 * aperture_diameter, wavelength=wavelength, detector_array=wf1.get_abscissas(), npoints=None) angle_x = xx / propagation_distance intensity_theory = numpy.abs(amplitude * alpha)**2 intensity_calculated = wf1.get_intensity() if normalization: intensity_calculated /= intensity_calculated.max() intensity_theory /= intensity_theory.max() if do_plot: from srxraylib.plot.gol import plot plot( wf1.get_abscissas() * 1e6 / propagation_distance, intensity_calculated, angle_x * 1e6, intensity_theory, legend=["%s " % method, "analytical"], legend_position=(0.95, 0.95), title= "1D (%s) diffraction from a %s aperture of %3.1f um at \n wavelength of %3.1f A" % (method, aperture_type, aperture_diameter * 1e6, wavelength * 1e10), xtitle="X (urad)", ytitle="Intensity", xrange=[-20, 20], ylog=True, show=show) plot( wf1.get_abscissas() * 1e6, wf1.get_phase(unwrap=True), 1e6 * xx, numpy.unwrap(numpy.angle(alpha)), legend=["%s " % method, "analytical"], title= "1D (%s) diffraction from a %s aperture of %3.1f um at \n wavelength of %3.1f A NOT ASSERTED!!" % (method, aperture_type, aperture_diameter * 1e6, wavelength * 1e10), xtitle="X (urad)", ytitle="Phase", # xrange=[-20, 20], ) return wf1.get_abscissas( ) / propagation_distance, intensity_calculated, intensity_theory
f = open(write_profile, "w") for i in range(height.size): f.write("%g %g\n" % (abscissas_on_mirror[i], height[i])) f.close() print("File %s written to disk." % write_profile) return output_wavefront, abscissas_on_mirror, height ########## SOURCE ########## # # create output_wavefront # # output_wavefront = GenericWavefront1D.initialize_wavefront_from_range( x_min=-5e-05, x_max=5e-05, number_of_points=1000) output_wavefront.set_photon_energy(1000) output_wavefront.set_gaussian_hermite_mode(sigma_x=7.67757e-06, amplitude=1, mode_x=0, shift=0, beta=1e20) if plot_from_oe <= 0: plot(output_wavefront.get_abscissas(), output_wavefront.get_intensity(), title='SOURCE') ########## OPTICAL SYSTEM ########## ########## OPTICAL ELEMENT NUMBER 1 ##########
def source(photon_energy=250): from wofry.propagator.wavefront1D.generic_wavefront import GenericWavefront1D input_wavefront = GenericWavefront1D.initialize_wavefront_from_range(x_min=-0.00147,x_max=0.00147,number_of_points=1000) input_wavefront.set_photon_energy(photon_energy) input_wavefront.set_spherical_wave(radius=13.73,center=0,complex_amplitude=complex(1, 0)) return input_wavefront
if __name__ == "__main__": try: initialize_default_propagator_1D() except: print("Problems initializing 1D propagators") # pass propagator = PropagationManager.Instance() # # create source # wavefront = GenericWavefront1D.initialize_wavefront_from_range( x_min=-0.5e-3, x_max=0.5e-3, number_of_points=2048, wavelength=1.5e-10) radius = 50.0 # 50. wavefront.set_spherical_wave(radius=radius) distance = 5.0 zoom = 1.0 output_wf_2 = propagate_wavefront(wavefront, distance=distance, handler_name=FresnelZoom1D.HANDLER_NAME, zoom=zoom) output_wf_3 = propagate_wavefront_scaling_new( wavefront, distance=distance, handler_name=FresnelZoomScaling1D.HANDLER_NAME, zoom=zoom,
def get_wavefront(self): # # If making changes here, don't forget to do changes in to_python_code() as well... # if self._initialize_from == 0: if self._dimension == 1: wf = GenericWavefront1D.initialize_wavefront_from_range( x_min=self._range_from_h, x_max=self._range_to_h, number_of_points=self._number_of_points_h) elif self._dimension == 2: wf = GenericWavefront2D.initialize_wavefront_from_range( x_min=self._range_from_h, x_max=self._range_to_h, y_min=self._range_from_v, y_max=self._range_to_v, number_of_points=(self._number_of_points_h, self._number_of_points_v)) else: if self._dimension == 1: wf = GenericWavefront1D.initialize_wavefront_from_steps( x_start=self._steps_start_h, x_step=self._steps_step_h, number_of_points=self._number_of_points_h) elif self._dimension == 2: wf = GenericWavefront2D.initialize_wavefront_from_steps( x_start=self._steps_start_h, x_step=self._steps_step_h, y_start=self._steps_start_v, y_step=self._steps_step_v, number_of_points=(self._number_of_points_h, self._number_of_points_v)) if self._units == 0: wf.set_photon_energy(self._energy) else: wf.set_wavelength(self._wavelength) if self._kind_of_wave == 0: # plane if self._dimension == 1: if self._initialize_amplitude == 0: wf.set_plane_wave_from_complex_amplitude( complex_amplitude=complex(self._complex_amplitude_re, self._complex_amplitude_im), inclination=self._inclination) else: wf.set_plane_wave_from_amplitude_and_phase( amplitude=self._amplitude, phase=self._phase, inclination=self._inclination) elif self._dimension == 2: if self._initialize_amplitude == 0: wf.set_plane_wave_from_complex_amplitude( complex_amplitude=complex(self._complex_amplitude_re, self._complex_amplitude_im)) else: wf.set_plane_wave_from_amplitude_and_phase( amplitude=self._amplitude, phase=self._phase) elif self._kind_of_wave == 1: # spheric if self._dimension == 1: wf.set_spherical_wave(radius=self._radius, center=self._center, complex_amplitude=complex( self._complex_amplitude_re, self._complex_amplitude_im)) elif self._dimension == 2: wf.set_spherical_wave(radius=self._radius, complex_amplitude=complex( self._complex_amplitude_re, self._complex_amplitude_im)) elif self._kind_of_wave == 2: # gaussian if self._dimension == 1: wf.set_gaussian(sigma_x=self._sigma_h, amplitude=self._amplitude, shift=self._gaussian_shift) elif self._dimension == 2: wf.set_gaussian(sigma_x=self._sigma_h, sigma_y=self._sigma_v, amplitude=self._amplitude) elif self._kind_of_wave == 3: # g.s.m. if self._dimension == 1: wf.set_gaussian_hermite_mode( sigma_x=self._sigma_h, mode_x=self._n_h, amplitude=self._amplitude, beta=self._beta_h, shift=self._gaussian_shift, ) elif self._dimension == 2: wf.set_gaussian_hermite_mode( sigma_x=self._sigma_h, sigma_y=self._sigma_v, amplitude=self._amplitude, nx=self._n_h, ny=self._n_v, betax=self._beta_h, betay=self._beta_v, ) if self._add_random_phase: wf.add_phase_shifts(2 * numpy.pi * numpy.random.random(wf.size())) return wf