def create_wavefront_generic(size_factor=1,pixel_size=1e-6,wavelength=1.5e-10): w = GenericWavefront2D.initialize_wavefront_from_steps(x_start=-0.5*pixel_size*512*size_factor,x_step=pixel_size, y_start=-0.5*pixel_size*512*size_factor,y_step=pixel_size, number_of_points=(512*size_factor,512*size_factor),wavelength=wavelength) w.set_plane_wave_from_complex_amplitude(complex_amplitude=(1.0+0.0j)) w.clip_square(x_min=-100e-6,x_max=100e-6,y_min=-10e-6,y_max=10e-6) return w
def test_gaussianhermite_mode(self, do_plot=do_plot): # # plane wave # print("# ") print("# Tests for a 2D Gaussian Hermite mode ") print("# ") wavelength = 1.24e-10 # 2D sigma_x = 100e-6 mode_x = 0 npixels_x = 100 sigma_y = 50e-6 mode_y = 3 npixels_y = 100 wavefront_length_x = 10 * sigma_x wavefront_length_y = 10 * sigma_y x = numpy.linspace(-0.5 * wavefront_length_x, 0.5 * wavefront_length_x, npixels_x) y = numpy.linspace(-0.5 * wavefront_length_y, 0.5 * wavefront_length_y, npixels_y) wf1 = GenericWavefront2D.initialize_wavefront_from_steps( x_start=x[0], x_step=numpy.abs(x[1] - x[0]), y_start=y[0], y_step=numpy.abs(y[1] - y[0]), number_of_points=(npixels_x, npixels_y), wavelength=wavelength) wf1.set_gaussian_hermite_mode(sigma_x, sigma_y, mode_x, mode_y, amplitude=1.0) numpy.testing.assert_almost_equal(wf1.get_amplitude()[30, 40], 1383.76448118, 3) if do_plot: from srxraylib.plot.gol import plot_image plot_image(wf1.get_amplitude(), wf1.get_coordinate_x(), wf1.get_coordinate_y(), title="Amplitude of gaussianhermite mode", show=1)
def test_interpolator(self, do_plot=do_plot): # # interpolator # print("# ") print("# Tests for 2D interpolator ") print("# ") x = numpy.linspace(-10, 10, 100) y = numpy.linspace(-20, 20, 50) xy = numpy.meshgrid(x, y) X = xy[0].T Y = xy[1].T sigma = 3.0 Z = 3 * numpy.exp(-(X**2 + Y**2) / 2 / sigma**2) + 4j print("shape of Z", Z.shape) wf = GenericWavefront2D.initialize_wavefront_from_steps( x[0], x[1] - x[0], y[0], y[1] - y[0], number_of_points=(100, 50)) print("wf shape: ", wf.size()) print("wf polarized: ", wf.is_polarized()) wf.set_complex_amplitude(Z) x1 = 3.2 y1 = -2.5 z1 = 3 * numpy.exp(-(x1**2 + y1**2) / 2 / sigma**2) + 4j print("complex ampl at (%g,%g): %g+%gi (exact=%g+%gi)" % (x1, y1, wf.get_interpolated_complex_amplitude( x1, y1).real, wf.get_interpolated_complex_amplitude( x1, y1).imag, z1.real, z1.imag)) self.assertAlmostEqual( wf.get_interpolated_complex_amplitude(x1, y1).real, z1.real, 4) self.assertAlmostEqual( wf.get_interpolated_complex_amplitude(x1, y1).imag, z1.imag, 4) # print( "intensity at (%g,%g): %g (exact=%g)" % (x1, y1, wf.get_interpolated_intensity(x1, y1), numpy.abs(z1)**2)) self.assertAlmostEqual(wf.get_interpolated_intensity(x1, y1), numpy.abs(z1)**2, 3) # interpolate on same grid interpolated_complex_amplitude_on_same_grid = wf.get_interpolated_complex_amplitudes( X, Y) print("Shape interpolated at same grid: ", interpolated_complex_amplitude_on_same_grid.shape) numpy.testing.assert_array_almost_equal( wf.get_complex_amplitude(), interpolated_complex_amplitude_on_same_grid, 4) print( "Total intensity original wavefront: %g, interpolated on the same grid: %g" % (wf.get_intensity().sum(), (numpy.abs(interpolated_complex_amplitude_on_same_grid)** 2).sum())) if do_plot: from srxraylib.plot.gol import plot_image plot_image(wf.get_intensity(), wf.get_coordinate_x(), wf.get_coordinate_y(), title="Original", show=0) plot_image(wf.get_interpolated_intensity(X, Y), wf.get_coordinate_x(), wf.get_coordinate_y(), title="interpolated on same grid", show=1) # rebin wavefront # wf.set_plane_wave_from_complex_amplitude(3+4j) wf_rebin = wf.rebin(2.0, 5.0, 0.5, 0.8, keep_the_same_intensity=1, set_extrapolation_to_zero=1) print("Shape before rebinning: ", wf.size()) print("Shape after rebinning: ", wf_rebin.size()) print("Total intensity original wavefront: %g, rebinned: %g" % (wf.get_intensity().sum(), wf_rebin.get_intensity().sum())) if do_plot: from srxraylib.plot.gol import plot_image plot_image(wf.get_intensity(), wf.get_coordinate_x(), wf.get_coordinate_y(), title="BEFORE REBINNING", show=0) plot_image(wf_rebin.get_intensity(), wf_rebin.get_coordinate_x(), wf_rebin.get_coordinate_y(), title="REBINNED", show=1)
def test_spherical_wave(self, do_plot=do_plot): # # plane wave # print("# ") print("# Tests for a 2D spherical wave ") print("# ") wavelength = 1.24e-10 wavefront_length_x = 400e-6 wavefront_length_y = wavefront_length_x npixels_x = 1024 npixels_y = npixels_x x = numpy.linspace(-0.5 * wavefront_length_x, 0.5 * wavefront_length_x, npixels_x) y = numpy.linspace(-0.5 * wavefront_length_y, 0.5 * wavefront_length_y, npixels_y) wf1 = GenericWavefront2D.initialize_wavefront_from_steps( x_start=x[0], x_step=numpy.abs(x[1] - x[0]), y_start=y[0], y_step=numpy.abs(y[1] - y[0]), number_of_points=(npixels_x, npixels_y), wavelength=wavelength) numpy.testing.assert_almost_equal(x, wf1.get_coordinate_x(), 9) numpy.testing.assert_almost_equal(y, wf1.get_coordinate_y(), 9) wf2 = GenericWavefront2D.initialize_wavefront_from_steps( x_start=x[0], x_step=numpy.abs(x[1] - x[0]), y_start=y[0], y_step=numpy.abs(y[1] - y[0]), number_of_points=(npixels_x, npixels_y), wavelength=wavelength) numpy.testing.assert_almost_equal(x, wf2.get_coordinate_x(), 9) numpy.testing.assert_almost_equal(y, wf2.get_coordinate_y(), 9) # an spherical wavefront is obtained 1) by creation, 2) focusing a planewave wf1.set_spherical_wave(-5.0, 3 + 0j) wf1.clip_square(-50e-6, 10e-6, -20e-6, 40e-6) wf2.set_plane_wave_from_complex_amplitude(3 + 0j) ideal_lens = WOIdealLens("test", 5.0, 5.0) ideal_lens.applyOpticalElement(wf2) wf2.clip_square(-50e-6, 10e-6, -20e-6, 40e-6) if do_plot: from srxraylib.plot.gol import plot_image plot_image(wf1.get_phase(), wf2.get_coordinate_x(), wf2.get_coordinate_y(), title="Phase of spherical wavefront", show=0) plot_image(wf2.get_phase(), wf2.get_coordinate_x(), wf2.get_coordinate_y(), title="Phase of focused plane wavefront", show=0) plot_image( wf1.get_phase(from_minimum_intensity=0.1), wf2.get_coordinate_x(), wf2.get_coordinate_y(), title="Phase of spherical wavefront (for intensity > 0.1)", show=0) plot_image( wf2.get_phase(from_minimum_intensity=0.1), wf2.get_coordinate_x(), wf2.get_coordinate_y(), title="Phase of focused plane wavefront (for intensity > 0.1)", show=1) numpy.testing.assert_almost_equal(wf1.get_phase(), wf2.get_phase(), 5)
def test_plane_wave(self, do_plot=do_plot): # # plane wave # print("# ") print( "# Tests for a 2D plane wave " ) print("# ") wavelength = 1.24e-10 wavefront_length_x = 400e-6 wavefront_length_y = wavefront_length_x npixels_x = 1024 npixels_y = npixels_x x = numpy.linspace(-0.5 * wavefront_length_x, 0.5 * wavefront_length_x, npixels_x) y = numpy.linspace(-0.5 * wavefront_length_y, 0.5 * wavefront_length_y, npixels_y) wavefront = GenericWavefront2D.initialize_wavefront_from_steps( x_start=x[0], x_step=numpy.abs(x[1] - x[0]), y_start=y[0], y_step=numpy.abs(y[1] - y[0]), number_of_points=(npixels_x, npixels_y), wavelength=wavelength) # possible modifications wavefront.set_plane_wave_from_amplitude_and_phase(5.0, numpy.pi / 2) numpy.testing.assert_almost_equal(wavefront.get_intensity(), 25, 5) wavefront.set_plane_wave_from_complex_amplitude(2.0 + 3j) numpy.testing.assert_almost_equal(wavefront.get_intensity(), 13, 5) phase_before = wavefront.get_phase() wavefront.add_phase_shift(numpy.pi / 2) phase_after = wavefront.get_phase() numpy.testing.assert_almost_equal(phase_before + numpy.pi / 2, phase_after, 5) intensity_before = wavefront.get_intensity() wavefront.rescale_amplitude(10.0) intensity_after = wavefront.get_intensity() numpy.testing.assert_almost_equal(intensity_before * 100, intensity_after, 5) # interpolation wavefront.set_plane_wave_from_complex_amplitude(2.0 + 3j) test_value1 = wavefront.get_interpolated_complex_amplitude(0.01, 1.3) self.assertAlmostEqual((2.0 + 3j).real, test_value1.real, 5) self.assertAlmostEqual((2.0 + 3j).imag, test_value1.imag, 5) if do_plot: from srxraylib.plot.gol import plot_image plot_image(wavefront.get_intensity(), wavefront.get_coordinate_x(), wavefront.get_coordinate_y(), title="Intensity (plane wave)", show=0) plot_image(wavefront.get_phase(), wavefront.get_coordinate_x(), wavefront.get_coordinate_y(), title="Phase (plane wave)", show=1)
def test_initializers(self, do_plot=do_plot): print("# ") print("# Tests for initializars (2D) ") print("# ") x = numpy.linspace(-100, 100, 50) y = numpy.linspace(-50, 50, 200) XY = numpy.meshgrid(x, y) X = XY[0].T Y = XY[1].T sigma = 10 Z = numpy.exp(-(X**2 + Y**2) / 2 / sigma**2) * 1j print("Shapes x,y,z: ", x.shape, y.shape, Z.shape) wf0 = GenericWavefront2D.initialize_wavefront_from_steps( x[0], numpy.abs(x[1] - x[0]), y[0], numpy.abs(y[1] - y[0]), number_of_points=Z.shape) wf0.set_complex_amplitude(Z) wf1 = GenericWavefront2D.initialize_wavefront_from_range( x[0], x[-1], y[0], y[-1], number_of_points=Z.shape) wf1.set_complex_amplitude(Z) wf2 = GenericWavefront2D.initialize_wavefront_from_arrays(x, y, Z) if do_plot: from srxraylib.plot.gol import plot_image plot_image(wf0.get_intensity(), wf0.get_coordinate_x(), wf0.get_coordinate_y(), title="initialize_wavefront_from_steps", show=0) plot_image(wf1.get_intensity(), wf1.get_coordinate_x(), wf1.get_coordinate_y(), title="initialize_wavefront_from_range", show=0) plot_image(wf2.get_intensity(), wf2.get_coordinate_x(), wf2.get_coordinate_y(), title="initialize_wavefront_from_arrays", show=1) numpy.testing.assert_almost_equal( numpy.abs(Z)**2, wf0.get_intensity(), 11) numpy.testing.assert_almost_equal( numpy.abs(Z)**2, wf1.get_intensity(), 11) numpy.testing.assert_almost_equal( numpy.abs(Z)**2, wf2.get_intensity(), 11) numpy.testing.assert_almost_equal(x, wf0.get_coordinate_x(), 11) numpy.testing.assert_almost_equal(x, wf1.get_coordinate_x(), 11) numpy.testing.assert_almost_equal(x, wf2.get_coordinate_x(), 11) numpy.testing.assert_almost_equal(y, wf0.get_coordinate_y(), 11) numpy.testing.assert_almost_equal(y, wf1.get_coordinate_y(), 11) numpy.testing.assert_almost_equal(y, wf2.get_coordinate_y(), 11)
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