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

.. only:: html

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

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

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

.. _sphx_glr_examples_simple_propagation.py:


Simple propagation
==================

Basic example of physical optics propagation through a simple optical system 
consisting of a square aperture illuminated by a point source and focused by
a thin lens.

.. GENERATED FROM PYTHON SOURCE LINES 11-12

Import libraries required for simulation

.. GENERATED FROM PYTHON SOURCE LINES 12-18

.. code-block:: default


    import matplotlib.pyplot as plt
    import numpy as np

    import pysrw as srw








.. GENERATED FROM PYTHON SOURCE LINES 19-22

Create the wavefront arriving at the first plane of the optical system as 
described in :doc:`point_source`. Check the section about propagation for 
some tips about the definition of the mesh parameters.

.. GENERATED FROM PYTHON SOURCE LINES 22-33

.. code-block:: default


    ptSrc = srw.emitters.PointSource()

    observer = srw.wavefronts.Observer(centerCoord=[0, 0, .5],
                                       obsXextension=10e-3, 
                                       obsYextension=10e-3,
                                       obsXres=1e-6,
                                       obsYres=1e-6)

    wfr = srw.computePtSrcWfr(ptSrc, observer, wavelength=600)








.. GENERATED FROM PYTHON SOURCE LINES 34-36

We use a basic optical system consisting of a square aperture and a thin lens
and we observe the intensity at the image plane of the lens.

.. GENERATED FROM PYTHON SOURCE LINES 36-42

.. code-block:: default


    w = 500e-6
    f = 50e-3
    s1 = observer.zPos
    s2 = s1 * f / (s1 - f)








.. GENERATED FROM PYTHON SOURCE LINES 43-46

The optical system is created as a dictionary representing the three optical 
elements. Note that "aperture", "lens" and "image" are arbitrary names. The 
nature of each optical element is defined by its "type" key

.. GENERATED FROM PYTHON SOURCE LINES 46-64

.. code-block:: default


    optSystem = {
        "aperture": {
            "type": "rectAp",
            "extension": [w, w],
            "centre": [0,0]
        },
        "lens": {
            "type": "lens",
            "f": f,
            "centre": [0,0]
        },
        "image": {
            "type": "drift",
            "length": s2
        }
    }








.. GENERATED FROM PYTHON SOURCE LINES 65-70

The propagation is performed using the 
:py:func:`~pysrw.computations.propagateWfr` function. Since we leave all 
optional arguments to their default values, the sequence ["lens", "drift"] 
will be grouped, the intensity stored on all planes and the wavefront only
at the last "image" plane.

.. GENERATED FROM PYTHON SOURCE LINES 70-73

.. code-block:: default


    propData = srw.propagateWfr(wfr, optSystem)








.. GENERATED FROM PYTHON SOURCE LINES 74-76

We plot the resulting intensity distribution at the "aperture" and
"image" planes

.. GENERATED FROM PYTHON SOURCE LINES 76-79

.. code-block:: default

    srw.plotI(propData["aperture"]["intensity"])
    srw.plotI(propData["image"]["intensity"])




.. rst-class:: sphx-glr-horizontal


    *

      .. image-sg:: /examples/images/sphx_glr_simple_propagation_001.png
         :alt: simple propagation
         :srcset: /examples/images/sphx_glr_simple_propagation_001.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /examples/images/sphx_glr_simple_propagation_002.png
         :alt: simple propagation
         :srcset: /examples/images/sphx_glr_simple_propagation_002.png
         :class: sphx-glr-multi-img





.. GENERATED FROM PYTHON SOURCE LINES 80-87

As expected, the final spot is much smaller with respect to the simulated 
wavefront. The extended wavefront is necessary at the entrance of the optical
system, to leave enough margin around the aperture. However, the extension 
can be significantly reduced at the image plane, as the light is focused by
the lens. This can be achieved by adding some resize parameters to reduce 
by a factor 10 the extension and increase by a factor 2 the resolution to 
obtain a smoother profile

.. GENERATED FROM PYTHON SOURCE LINES 87-92

.. code-block:: default


    optSystem["image"]["resParam"] = {"hRangeChange": 1/10, "hResChange": 2.0,
                                      "vRangeChange": 1/10, "vResChange": 2.0,
                                      "hCentreChange": 0.0, "vCentreChange": 0.0}








.. GENERATED FROM PYTHON SOURCE LINES 93-95

The computation is then repeated. To save time and memory, we store the 
intensity only at the final "image" plane

.. GENERATED FROM PYTHON SOURCE LINES 95-99

.. code-block:: default


    propData = srw.propagateWfr(wfr, optSystem, saveIntAt=["image"])
    srw.plotI(propData["image"]["intensity"])




.. image-sg:: /examples/images/sphx_glr_simple_propagation_003.png
   :alt: simple propagation
   :srcset: /examples/images/sphx_glr_simple_propagation_003.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 100-102

We can finally extract a profile and compare the simulated pattern with the
sinc profile of the diffraction from a rectangular aperture

.. GENERATED FROM PYTHON SOURCE LINES 102-115

.. code-block:: default


    cuts = srw.extractCrossCuts(propData["image"]["intensity"])
    yax = cuts["yax"]
    int_srw = cuts["ydata"]
    int_theo = np.sinc(w * yax / (s2 * wfr.wavelength*1e-9))**2

    fig, ax = plt.subplots()
    ax.plot(yax*1e3, int_srw / np.max(int_srw))
    ax.plot(yax*1e3, int_theo, label="theo", linestyle="--")
    ax.set_xlabel("y position [mm]")
    ax.set_ylabel("Intensity [arb. unit]")
    ax.legend()
    plt.show()
    plt.tight_layout()


.. image-sg:: /examples/images/sphx_glr_simple_propagation_004.png
   :alt: simple propagation
   :srcset: /examples/images/sphx_glr_simple_propagation_004.png
   :class: sphx-glr-single-img






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

   **Total running time of the script:** (1 minutes 41.740 seconds)

**Estimated memory usage:**  4596 MB


.. _sphx_glr_download_examples_simple_propagation.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_propagation.py <simple_propagation.py>`

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

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


.. only:: html

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

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