.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/simple_undulator.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_simple_undulator.py: Simple planar undulator ======================= Basic example of computation of the field emitted by an undulator of a synchrotron light source. .. GENERATED FROM PYTHON SOURCE LINES 10-11 Import modules required for simulations .. GENERATED FROM PYTHON SOURCE LINES 11-18 .. code-block:: default import matplotlib.pyplot as plt import numpy as np import pysrw as srw .. GENERATED FROM PYTHON SOURCE LINES 20-22 We take as example the parameters of an undulator installed in a light source (ALBA-Spain, NCD beamline) .. GENERATED FROM PYTHON SOURCE LINES 22-28 .. code-block:: default energy = 3.0 # GeV lambda_u = 0.0216 # m N_u = 92 B_0 = 0.75 # T .. GENERATED FROM PYTHON SOURCE LINES 29-30 The magnet is created with the default SRW model for planar undulators .. GENERATED FROM PYTHON SOURCE LINES 30-34 .. code-block:: default und = srw.magnets.UndulatorPlanar(energy=energy, undPeriod=lambda_u, numPer=N_u, magField=B_0) .. GENERATED FROM PYTHON SOURCE LINES 35-36 The deflection parameter and fundamental harmonic of such undulator are .. GENERATED FROM PYTHON SOURCE LINES 36-41 .. code-block:: default K = und.getK() lambda_1 = und.getLambda1() print(f"Deflection parameter: {K:.2f}") print(f"Fundamental harmonic: {lambda_1:.2f} nm") .. rst-class:: sphx-glr-script-out .. code-block:: none Deflection parameter: 1.51 Fundamental harmonic: 0.67 nm .. GENERATED FROM PYTHON SOURCE LINES 42-45 The magnetic element must be included in a magnetic container. In this simple example, the `dipole` is the only device and the limits for the numerical integration as left as default. .. GENERATED FROM PYTHON SOURCE LINES 45-47 .. code-block:: default mag_container = srw.magnets.MagnetsContainer([und], lengthPadFraction=2) .. GENERATED FROM PYTHON SOURCE LINES 48-50 The last input for the simulation is the emitter, instance of :py:func:`~pysrw.emitters.ParticleBeam` .. GENERATED FROM PYTHON SOURCE LINES 50-52 .. code-block:: default beam = srw.emitters.ParticleBeam(energy, xPos=0, yPos=0, zPos=-1.5) .. GENERATED FROM PYTHON SOURCE LINES 53-56 Before computing the wavefront, it is a good practice to obtain and plot the particle trajectory and check that the simulation objects properly defined .. GENERATED FROM PYTHON SOURCE LINES 56-59 .. code-block:: default traj = srw.computeTrajectory(beam, mag_container, sStart=0, sEnd=3) srw.plotTrajectory(traj) .. image-sg:: /examples/images/sphx_glr_simple_undulator_001.png :alt: simple undulator :srcset: /examples/images/sphx_glr_simple_undulator_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 60-62 Create a 10 mm x 10 mm observation mesh, with a resolution of 100 um, placed 1 m downstream of the source .. GENERATED FROM PYTHON SOURCE LINES 62-68 .. code-block:: default observer = srw.wavefronts.Observer(centerCoord=[0, 0, 10], obsXextension=2e-3, obsYextension=2e-3, obsXres=5e-6, obsYres=5e-6) .. GENERATED FROM PYTHON SOURCE LINES 69-71 We can finally simulate the wavefront and derive the optical intensity, for example at the fundamental harmonics. .. GENERATED FROM PYTHON SOURCE LINES 71-78 .. code-block:: default wl = lambda_1 wfr = srw.computeSrWfrMultiProcess(4, particleBeam=beam, magnetsContainer=mag_container, observer=observer, wavelength=wl) intensity = wfr.getWfrI() srw.plotI(intensity) .. image-sg:: /examples/images/sphx_glr_simple_undulator_002.png :alt: simple undulator :srcset: /examples/images/sphx_glr_simple_undulator_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 79-82 The distribution of SR emitted by a particle in a uniform field has an analytical expression. Let's compare it to the simulation result. We extract a vertical slice from the simulation radiation .. GENERATED FROM PYTHON SOURCE LINES 82-86 .. code-block:: default cuts = srw.extractCrossCuts(intensity) yax = cuts["yax"] int_srw = cuts["ydata"] .. GENERATED FROM PYTHON SOURCE LINES 87-89 and compute the theoretical distribution for undulator radiation (tuned at fundamental wavelength) as .. GENERATED FROM PYTHON SOURCE LINES 89-96 .. code-block:: default r_p = observer.zPos theta = np.arctan(yax / r_p) gamma = beam.nominalParticle.gamma e_theo = np.sinc((theta**2 / 2 * (N_u * lambda_u) / (wl*1e-9))) int_theo = e_theo**2 .. GENERATED FROM PYTHON SOURCE LINES 97-98 Simulated and theoretical distributions are finally plotted .. GENERATED FROM PYTHON SOURCE LINES 98-105 .. code-block:: default fig, ax = plt.subplots() ax.plot(theta*1e3, int_srw / np.max(int_srw)) ax.plot(theta*1e3, int_theo / np.max(int_theo), label="theo", linestyle="--") ax.set_xlabel("Polar angle [mrad]") ax.set_ylabel("Intensity [arb. unit]") ax.legend() plt.show() plt.tight_layout() .. image-sg:: /examples/images/sphx_glr_simple_undulator_003.png :alt: simple undulator :srcset: /examples/images/sphx_glr_simple_undulator_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 8.301 seconds) **Estimated memory usage:** 11 MB .. _sphx_glr_download_examples_simple_undulator.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: simple_undulator.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: simple_undulator.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_