.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/helical_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_helical_undulator.py: Helical undulator ================= This example shows how to combine two planar undulators to create a helical undulator. The parameters of the single device are similar to the ones already used for the :doc:`simple_undulator` example. .. GENERATED FROM PYTHON SOURCE LINES 11-12 Import modules required for simulations .. GENERATED FROM PYTHON SOURCE LINES 12-19 .. code-block:: default import matplotlib.pyplot as plt import numpy as np import pysrw as srw .. GENERATED FROM PYTHON SOURCE LINES 21-26 A helical undulator is the superposition of two perpendicular planar undulators with a phase delay of :math:`\phi` which produces a rotation of the magnetic field along the device. In this example, the transverse field components are a shorter version of the undulator of NCD beamline at the ALBA light source .. GENERATED FROM PYTHON SOURCE LINES 26-32 .. code-block:: default energy = 3.0 # GeV lambda_u = 0.0216 # m N_u = 5 # instead of 92 to better render the particle trajectory B_0 = 0.75 # T .. GENERATED FROM PYTHON SOURCE LINES 33-34 The magnet is created with the default SRW model for planar undulators .. GENERATED FROM PYTHON SOURCE LINES 34-42 .. code-block:: default phi = np.pi/2 und_h = srw.magnets.UndulatorPlanar(energy=energy, undPlane="h", undPeriod=lambda_u, numPer=N_u, magField=B_0) und_v = srw.magnets.UndulatorPlanar(energy=energy, undPlane="v", undPeriod=lambda_u, numPer=N_u, magField=B_0, initialPhase=phi) .. GENERATED FROM PYTHON SOURCE LINES 43-46 We embed the two components in the a magnetic container. Note that it doesn't matter if the two devices overlap. The total field is going to be a superposition of all component .. GENERATED FROM PYTHON SOURCE LINES 46-49 .. code-block:: default mag_container = srw.magnets.MagnetsContainer([und_h, und_v], lengthPadFraction=2) .. GENERATED FROM PYTHON SOURCE LINES 50-52 The last input for the simulation is the emitter, instance of :py:func:`~pysrw.emitters.ParticleBeam` .. GENERATED FROM PYTHON SOURCE LINES 52-54 .. code-block:: default beam = srw.emitters.ParticleBeam(energy, xPos=0, yPos=0, zPos=-0.1) .. GENERATED FROM PYTHON SOURCE LINES 55-58 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 58-61 .. code-block:: default traj = srw.computeTrajectory(beam, mag_container, sStart=0, sEnd=0.2) srw.plotTrajectory(traj) .. image-sg:: /examples/images/sphx_glr_helical_undulator_001.png :alt: helical undulator :srcset: /examples/images/sphx_glr_helical_undulator_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 62-64 We can plot the trajectory and magnetic field in a 3D plot to better see the particle spiral and the field rotation along the device. .. GENERATED FROM PYTHON SOURCE LINES 64-78 .. code-block:: default skip = 50 # sample few points for better readibility z = traj["z"][::skip] Bx = traj["Bx"][::skip] / np.max(traj["Bx"][::skip]) * np.max(traj["x"][::skip]) By = traj["By"][::skip] / np.max(traj["By"][::skip]) * np.max(traj["y"][::skip]) zeros = np.zeros(len(z)) fig = plt.figure() ax = fig.add_subplot(111, projection="3d") ax.set_box_aspect((4, 1, 1)) ax.plot(traj["z"], traj["x"], traj["y"], linewidth=0.5, color="black") ax.quiver(z, zeros, zeros, zeros, Bx, By, linewidth=0.5, color="red") ax.grid(False) plt.show() .. image-sg:: /examples/images/sphx_glr_helical_undulator_002.png :alt: helical undulator :srcset: /examples/images/sphx_glr_helical_undulator_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 79-81 Create a 5 mm x 5 mm observation mesh, with a resolution of 10 um, placed 10 m downstream of the source .. GENERATED FROM PYTHON SOURCE LINES 81-87 .. code-block:: default observer = srw.wavefronts.Observer(centerCoord=[0, 0, 10], obsXextension=5e-3, obsYextension=5e-3, obsXres=10e-6, obsYres=10e-6) .. GENERATED FROM PYTHON SOURCE LINES 88-91 The simulation of the wavefront is done in the same way as the simple undulator case. The expression for the foundamental harmonic is slightly different .. GENERATED FROM PYTHON SOURCE LINES 91-95 .. code-block:: default gamma = beam.nominalParticle.gamma K = und_h.getK() wl = lambda_u / (2 * gamma**2) * (1 + K**2) * 1e9 .. GENERATED FROM PYTHON SOURCE LINES 96-98 because the motion along the two transverse direction implies a term :math:`K^2` instead of :math:`K^2/2` derived for the planar undulator. .. GENERATED FROM PYTHON SOURCE LINES 100-104 We finally simulate the wavefront and plot the intensity. The peculiarity of the helical undulator is the strong circluar polarization of the emitted light, whereas the plane undulator radiation is essentially linearly polarized .. GENERATED FROM PYTHON SOURCE LINES 104-110 .. code-block:: default wfr = srw.computeSrWfrMultiProcess(4, particleBeam=beam, magnetsContainer=mag_container, observer=observer, wavelength=wl) intensity_left = wfr.getWfrI(pol="circL") intensity_right = wfr.getWfrI(pol="circR") .. GENERATED FROM PYTHON SOURCE LINES 111-117 We can prove this by plotting a the central intensity crosscut for the circular-left and circular-right components. The sense of rotation of the circular polarization can be adjusted with the phase between the two components of the magnetic field. The phase :math:`\pi /2` produces an essentially circular-left polarization, which can be transformed to circular-right by setting an opposite phase of :math:`-\pi /2` .. GENERATED FROM PYTHON SOURCE LINES 117-127 .. code-block:: default fig, ax = plt.subplots() for pol in ["circL", "circR"]: intensity = wfr.getWfrI(pol=pol) cuts = srw.extractCrossCuts(intensity) ax.plot(cuts["xax"]*1e3, cuts["xdata"], label=pol) ax.set_xlabel("x position [mm]") ax.set_ylabel("Intensity [ph / (s mm^2 nm)]") ax.set_yscale("log") ax.legend() plt.tight_layout() plt.show() .. image-sg:: /examples/images/sphx_glr_helical_undulator_003.png :alt: helical undulator :srcset: /examples/images/sphx_glr_helical_undulator_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.391 seconds) **Estimated memory usage:** 11 MB .. _sphx_glr_download_examples_helical_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: helical_undulator.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: helical_undulator.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_