.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/spectrum.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_examples_spectrum.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_spectrum.py:


Spectrum
========

Basic example of computation of synchrotron radiation spectrum.

.. GENERATED FROM PYTHON SOURCE LINES 9-11

Import modules required for simulations and for comparison with the 
theoretical distribution of dipole radiation.

.. GENERATED FROM PYTHON SOURCE LINES 11-20

.. code-block:: default



    import matplotlib.pyplot as plt
    import numpy as np
    from scipy.special import kv
    from scipy.constants import e, epsilon_0, mu_0, c, pi, h

    import pysrw as srw








.. GENERATED FROM PYTHON SOURCE LINES 22-24

We use the same source used for the simulation of the spatial distribution
in the example :doc:`simple_dipole`

.. GENERATED FROM PYTHON SOURCE LINES 24-37

.. code-block:: default


    energy = 3.0    # GeV
    rho = 7.047     # m
    length = 1.384  # m
    gap = 36e-3     # m

    dipole = srw.magnets.Dipole(energy=energy, bendingR=rho, 
                                coreL=length, edgeL=gap)

    mag_container = srw.magnets.MagnetsContainer([dipole])

    beam = srw.emitters.ParticleBeam(energy, xPos=0, yPos=0, zPos=0)








.. GENERATED FROM PYTHON SOURCE LINES 38-41

The simulation of the radiation spectrum requires an 
:py:class:`~pysrw.wavefronts.Observer`. The spectrum is computed at the 
observer centre, all other parameters can be ignored

.. GENERATED FROM PYTHON SOURCE LINES 41-44

.. code-block:: default


    observer = srw.wavefronts.Observer(centerCoord=[0, 0, 5])








.. GENERATED FROM PYTHON SOURCE LINES 45-50

The computation is performed with 
:py:func:`~pysrw.computations.computeSpectrum`. In this example we request 
the computation of 1000 points between 0.01 and 10 nm. Keep in mind that the
points are uniformely sampled along the photon energy axis. The points in the
wavelength axis won't be evenly distributed.

.. GENERATED FROM PYTHON SOURCE LINES 50-58

.. code-block:: default

    wfr = srw.computeSpectrum(particleBeam=beam, 
                              magnetsContainer=mag_container,
                              observer=observer, 
                              fromWavelength=.01, toWavelength=10, numPoints=1000,
                              srApprox="auto-wiggler")
    spectrum = wfr.getSpectrumI()
    srw.plotSpectrum(spectrum, axisScale=["log", "log"])




.. image-sg:: /examples/images/sphx_glr_spectrum_001.png
   :alt: spectrum
   :srcset: /examples/images/sphx_glr_spectrum_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 59-61

We compute the corresponding theoretical spectral distribution from the 
Schwinger model

.. GENERATED FROM PYTHON SOURCE LINES 61-79

.. code-block:: default

    r_p = observer.zPos
    theta = 0
    wl = wfr.wlax # nm
    wl_c = dipole.getLambdaCritical() # nm
    gamma = beam.nominalParticle.gamma

    gt_sqr = (gamma * theta)**2
    xi = wl_c/ (2 * wl) * (1 + gt_sqr)**(3/2)

    e_theo_h = (-np.sqrt(3)*e*gamma) / ((2*pi)**(3/2) * epsilon_0 * c *r_p) *\
        (wl_c / (2 * wl)) * (1 + gt_sqr) * kv(2/3, xi)

    e_theo_v = (1j*np.sqrt(3)*e*gamma) / ((2 * pi)**(3/2) * epsilon_0 * c *r_p) *\
        (wl_c / (2 * wl)) * (gamma * theta) * np.sqrt(1 + gt_sqr) * kv(1/3, xi)

    int_theo = (4 * pi) / (mu_0 * c * wl*1e-9 * h * e) \
        * 1e-15 * (np.abs(e_theo_h)**2 + np.abs(e_theo_v)**2)








.. GENERATED FROM PYTHON SOURCE LINES 80-83

The final plot compares simulated and theoretical results. The curve exhibits
the typical broadband emission of dipole radiation, with the critical 
wavelength dividing the spectrum in two regions of equal radiated power 

.. GENERATED FROM PYTHON SOURCE LINES 83-95

.. code-block:: default

    fig, ax = plt.subplots()
    ax.plot(wl, spectrum["data"], label="sim")
    ax.plot(wl, int_theo, label="theo", linestyle="--")
    ax.axvline(wl_c, label="critical wl", color="red", linestyle="--")
    ax.set_xlabel("Wavelength [nm]")
    ax.set_ylabel("Intensity [ph / (s mm^2 nm)]")
    ax.set_xscale("log")
    ax.set_yscale("log")
    ax.grid()
    ax.legend()
    plt.tight_layout()
    plt.show()



.. image-sg:: /examples/images/sphx_glr_spectrum_002.png
   :alt: spectrum
   :srcset: /examples/images/sphx_glr_spectrum_002.png
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 7.124 seconds)

**Estimated memory usage:**  27 MB


.. _sphx_glr_download_examples_spectrum.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example




    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: spectrum.py <spectrum.py>`

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: spectrum.ipynb <spectrum.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_