.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/simple_dipole.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_dipole.py: Simple dipole magnet ==================== Basic example of computation of the field emitted by an electron beam deflected by a simple dipole magnet. .. GENERATED FROM PYTHON SOURCE LINES 10-12 Import modules required for simulations and for comparison with the theoretical distribution of dipole radiation. .. GENERATED FROM PYTHON SOURCE LINES 12-21 .. 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 23-25 We take as example the parameters of a typical bending magnet of a third-generation light source (ALBA-Spain) .. GENERATED FROM PYTHON SOURCE LINES 25-31 .. code-block:: default energy = 3.0 # GeV rho = 7.047 # m length = 1.384 # m gap = 36e-3 # m .. GENERATED FROM PYTHON SOURCE LINES 32-33 which produce a particle deflection of .. GENERATED FROM PYTHON SOURCE LINES 33-36 .. code-block:: default delta = 2 * np.arcsin(length / 2 / rho) print(f"Deflection angle: {delta*1e3:.2f} mrad") .. rst-class:: sphx-glr-script-out .. code-block:: none Deflection angle: 196.71 mrad .. GENERATED FROM PYTHON SOURCE LINES 37-38 The magnet is created with the default SRW model for dipoles .. GENERATED FROM PYTHON SOURCE LINES 38-41 .. code-block:: default dipole = srw.magnets.Dipole(energy=energy, bendingR=rho, coreL=length, edgeL=gap) .. GENERATED FROM PYTHON SOURCE LINES 42-43 This dipole emits a broad band radiation with a critical wavelength .. GENERATED FROM PYTHON SOURCE LINES 43-46 .. code-block:: default wl_c = dipole.getLambdaCritical() print(f"Critical wavelength: {wl_c:.2f} nm") .. rst-class:: sphx-glr-script-out .. code-block:: none Critical wavelength: 0.15 nm .. GENERATED FROM PYTHON SOURCE LINES 47-50 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 50-52 .. code-block:: default mag_container = srw.magnets.MagnetsContainer([dipole]) .. GENERATED FROM PYTHON SOURCE LINES 53-55 The last input for the simulation is the emitter, instance of :py:func:`~pysrw.emitters.ParticleBeam` .. GENERATED FROM PYTHON SOURCE LINES 55-57 .. code-block:: default beam = srw.emitters.ParticleBeam(energy, xPos=0, yPos=0, zPos=0) .. GENERATED FROM PYTHON SOURCE LINES 58-61 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 61-64 .. code-block:: default traj = srw.computeTrajectory(beam, mag_container) srw.plotTrajectory(traj) .. image-sg:: /examples/images/sphx_glr_simple_dipole_001.png :alt: simple dipole :srcset: /examples/images/sphx_glr_simple_dipole_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 65-67 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 67-73 .. code-block:: default observer = srw.wavefronts.Observer(centerCoord=[0, 0, 5], obsXextension=5e-3, obsYextension=5e-3, obsXres=200e-6, obsYres=20e-6) .. GENERATED FROM PYTHON SOURCE LINES 74-77 We can finally simulate the wavefront and derive the optical intensity, for example at the critical wavelength. Note that, dealing with a dipole, we use the "auto-wigggler" value for :py:func:`~pysrw.configuration.SR_APPROX` .. GENERATED FROM PYTHON SOURCE LINES 77-85 .. code-block:: default wl = wl_c wfr = srw.computeSrWfrMultiProcess(4, particleBeam=beam, magnetsContainer=mag_container, observer=observer, wavelength=wl, srApprox="auto-wiggler") intensity = wfr.getWfrI() srw.plotI(intensity) .. image-sg:: /examples/images/sphx_glr_simple_dipole_002.png :alt: simple dipole :srcset: /examples/images/sphx_glr_simple_dipole_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 86-89 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 89-92 .. code-block:: default cuts = srw.extractCrossCuts(intensity) yax = cuts["yax"] .. GENERATED FROM PYTHON SOURCE LINES 93-94 and we derive the theoretical expression (Schwinger distributions) .. GENERATED FROM PYTHON SOURCE LINES 94-110 .. code-block:: default r_p = observer.zPos theta = np.arctan(yax / r_p) 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 111-112 Simulated and theoretical distributions are finally plotted .. GENERATED FROM PYTHON SOURCE LINES 112-119 .. code-block:: default fig, ax = plt.subplots() ax.plot(theta*1e3, cuts["ydata"]) ax.plot(theta*1e3, int_theo, label="theo", linestyle="--") ax.set_xlabel("Polar angle [mrad]") ax.set_ylabel("Intensity [ph / (s mm^2 nm)]") ax.legend() plt.show() .. image-sg:: /examples/images/sphx_glr_simple_dipole_003.png :alt: simple dipole :srcset: /examples/images/sphx_glr_simple_dipole_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 13.838 seconds) **Estimated memory usage:** 33 MB .. _sphx_glr_download_examples_simple_dipole.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_dipole.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: simple_dipole.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_