Example #1
0
    def test_spherical_wave(self, do_plot=do_plot):
        #
        # plane wave
        #
        print("#                                                             ")
        print("# Tests for a 1D spherical wave                               ")
        print("#                                                             ")

        wavelength = 1.24e-10

        wavefront_length_x = 400e-6

        npixels_x = 1024

        wavefront_x = numpy.linspace(-0.5 * wavefront_length_x,
                                     0.5 * wavefront_length_x, npixels_x)

        wf1 = GenericWavefront1D.initialize_wavefront_from_steps(
            x_start=wavefront_x[0],
            x_step=numpy.abs(wavefront_x[1] - wavefront_x[0]),
            number_of_points=npixels_x,
            wavelength=wavelength)

        wf2 = GenericWavefront1D.initialize_wavefront_from_steps(
            x_start=wavefront_x[0],
            x_step=numpy.abs(wavefront_x[1] - wavefront_x[0]),
            number_of_points=npixels_x,
            wavelength=wavelength)

        # an spherical wavefront is obtained 1) by creation, 2) focusing a planewave

        wf1.set_spherical_wave(radius=-5.0, complex_amplitude=3 + 0j)
        wf1.clip(-50e-6, 10e-6)

        wf2.set_plane_wave_from_complex_amplitude(3 + 0j)
        ideal_lens = WOIdealLens1D("test", 5.0)
        ideal_lens.applyOpticalElement(wf2)
        wf2.clip(-50e-6, 10e-6)

        if do_plot:
            from srxraylib.plot.gol import plot
            plot(wf1.get_abscissas(),
                 wf1.get_phase(),
                 title="Phase of spherical wavefront",
                 show=0)
            plot(wf2.get_abscissas(),
                 wf2.get_phase(),
                 title="Phase of focused plane wavefront",
                 show=0)
            plot(wf1.get_abscissas(),
                 wf1.get_phase(from_minimum_intensity=0.1),
                 title="Phase of spherical wavefront (for intensity > 0.1)",
                 show=0)
            plot(
                wf2.get_abscissas(),
                wf2.get_phase(from_minimum_intensity=0.1),
                title="Phase of focused plane wavefront (for intensity > 0.1)",
                show=1)

        numpy.testing.assert_almost_equal(wf1.get_phase(), wf2.get_phase(), 5)
Example #2
0
    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)
Example #3
0
    def test_interpolator(self, do_plot=do_plot):
        #
        # interpolator
        #
        print("#                                                             ")
        print("# Tests for 1D interpolator                                   ")
        print("#                                                             ")

        x = numpy.linspace(-10, 10, 100)

        sigma = 3.0
        Z = numpy.exp(-1.0 * x**2 / 2 / sigma**2)

        print("shape of Z", Z.shape)

        wf = GenericWavefront1D.initialize_wavefront_from_steps(
            x[0], numpy.abs(x[1] - x[0]), number_of_points=100)
        print("wf shape: ", wf.size())
        wf.set_complex_amplitude(Z)

        x1 = 3.2
        z1 = numpy.exp(x1**2 / -2 / sigma**2)
        print("complex ampl at (%g): %g+%gi (exact=%g)" %
              (x1, wf.get_interpolated_complex_amplitude(x1).real,
               wf.get_interpolated_complex_amplitude(x1).imag, z1))
        self.assertAlmostEqual(
            wf.get_interpolated_complex_amplitude(x1).real, z1, 4)

        print("intensity  at (%g):   %g (exact=%g)" %
              (x1, wf.get_interpolated_intensity(x1), z1**2))
        self.assertAlmostEqual(wf.get_interpolated_intensity(x1), z1**2, 4)

        if do_plot:
            from srxraylib.plot.gol import plot
            plot(wf.get_abscissas(),
                 wf.get_intensity(),
                 title="Original",
                 show=1)
            xx = wf.get_abscissas()
            yy = wf.get_interpolated_intensities(wf.get_abscissas() - 1e-5)
            plot(xx, yy, title="interpolated on same grid", show=1)
Example #4
0
    def test_gaussianhermite_mode(self, do_plot=do_plot):
        #
        # plane wave
        #
        print("#                                                             ")
        print("# Tests for a 1D Gaussian Hermite mode                        ")
        print("#                                                             ")

        wavelength = 1.24e-10

        # 2D
        sigma_x = 100e-6
        mode_x = 0
        npixels_x = 100

        wavefront_length_x = 10 * sigma_x

        x = numpy.linspace(-0.5 * wavefront_length_x, 0.5 * wavefront_length_x,
                           npixels_x)

        wf1 = GenericWavefront1D.initialize_wavefront_from_steps(
            x_start=x[0],
            x_step=numpy.abs(x[1] - x[0]),
            number_of_points=npixels_x,
            wavelength=wavelength)

        wf1.set_gaussian_hermite_mode(sigma_x, mode_x, amplitude=1.0)

        numpy.testing.assert_almost_equal(wf1.get_amplitude()[30],
                                          23.9419082194, 5)

        if do_plot:
            from srxraylib.plot.gol import plot
            plot(wf1.get_abscissas(),
                 wf1.get_amplitude(),
                 title="Amplitude of gaussianhermite",
                 show=0)
Example #5
0
    def test_plane_wave(self, do_plot=do_plot):
        #
        # plane wave
        #
        print("#                                                             ")
        print("# Tests for a 1D plane wave                                   ")
        print("#                                                             ")

        wavelength = 1.24e-10

        wavefront_length_x = 400e-6

        npixels_x = 1024

        wavefront_x = numpy.linspace(-0.5 * wavefront_length_x,
                                     0.5 * wavefront_length_x, npixels_x)

        wavefront = GenericWavefront1D.initialize_wavefront_from_steps(
            x_start=wavefront_x[0],
            x_step=numpy.abs(wavefront_x[1] - wavefront_x[0]),
            number_of_points=npixels_x,
            wavelength=wavelength)

        numpy.testing.assert_almost_equal(wavefront_x,
                                          wavefront.get_abscissas(), 9)

        # 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)
        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
            plot(wavefront.get_abscissas(),
                 wavefront.get_intensity(),
                 title="Intensity (plane wave)",
                 show=0)
            plot(wavefront.get_abscissas(),
                 wavefront.get_phase(),
                 title="Phase (plane wave)",
                 show=1)
Example #6
0
    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