Translator Disclaimer
Open Access Paper
8 October 2015 Optics simulations with Python: diffraction
Author Affiliations +
Proceedings Volume 9793, Education and Training in Optics and Photonics: ETOP 2015; 97930K (2015)
Event: Education and Training in Optics and Photonics: ETOP 2015, 2015, Bordeaux, France
Python is an easy open source software that can be used to simulate various optical phenomena. We have developed a suite of programs, covering both geometrical and physical optics. These simulations follow the experimental modules used in the ALOP (Active Learning in Optics and Photonics) UNESCO program in the sense that they complement it and help with student prediction of results. We present these programs and the student reactions to these simulations.



The UNESCO program “Active Learning in Optics and Photonics” (ALOP) promotes a friendly and interactive method for teaching optics using simple and inexpensive equipment [1-3]. The full ALOP manual can be downloaded from UNESCO [4]. Despite the gain provided by these new conceptual experiments, diffraction remains a difficult concept for students. Numerical simulation is a valuable help in this direction and many simulations are now available for free on the web. Numerical tools have been developed to accompany the ALOP modules [5,6]. The dazzling progress in this area allow us, thanks to free distributions like python and the notebook associated with it, to implement the ALOP modules with the numerical code that simulate the various phenomena..


Teaching with Python

Among the multitude of programming languages available, Python has experienced the most rapid growth these past few years. One reason for that is due to the fact that interfacing modules are multiplatform so the same program can be used to generate applications for different Operating Systems (OS). Another important reason is python is a free and open source software. In addition to these two major points python has several advantages over other programming language such as easy syntax. In addition, it comes with various packages (all open source) such as Scipy (Scientific Python), Matplotlib (a plotting program), Numpy (Numerical Python), Statpy (Statistical Python), etc. There are numerous texts on Python [7,8]. It should be emphasized that Python is a very intuitive programming language, which is very flexible and powerful.


Versions of Python

One of the most critical points is the installation of the python distribution. Since most of python distributions are/were developed as open source a wide variety of possibilities are offered to users and accordingly it is difficult to select the one that suits the user most. This is particularly true when organizing an event such as an optics educational workshop. In any case, the most recommended version of python to use is python 2.7 because of its stability. The newer version (Python 3.0 or 3.4) can run version 2.7 with a few minor modifications. The software can be downloaded from the Python Foundation at:



Notebook is a powerful tool to write and generate lessons in various formats as latex, html or slides. In addition to that it is possible to insert parts of codes, execute them and generate the outputs. This ability facilitates greatly the explanation of programs. The notebook can be downloaded from: The code given in the section 3.1.3 illustrates how to interactively affects a value to one of the parameters.


Fraunhofer diffraction

In this example we will deal with diffraction. Diffraction is one of the modules of the ALOP program[9]. Fraunhofer is an application built using python programming language and its packages like Numpy, Matplotlib and wxpython. This program was developed under windows OS and the same code can be compiled under both linux and OSX.


Single slit diffraction


Mathematical formulation

Single slit diffraction is modeled as sinc() function with the suitable parameters. Let I(x) be the distribution of the intensity and I(0) its value at x=0. Let b be the slit width, A the wavelength and D the distance of the screen to the slit. The expression of the intensity normalized to I(0) is:


Making these parameters interactive, it is possible to perform numerical experiments and test a wide variety of configurations. For instance, this allows us to test the effect of different wavelengths, which would be difficult to do in an experimental situation due to lack of light sources with variable wavelengths. We face the same problem with the slits. For example, in ALOP workshop the slits are manually fabricated. So it is not possible to produce a large number of slits different slit widths.


Plotting within application

Creating an application with interactive tools as buttons, menus, sliders make this possible. Figure.1 shows how it is possible to vary the three parameters (b, λ, D) with the help of the sliders at the bottom. Moreover the values of these three parameters, displayed inside the plot along with the formula, change simultaneously with the values in the slider. We show here, as an example, the interface created for this application.


Fraunhofer interface application for a single slit



Using notebook

Notebook allows us to explain in a friendly way how the program is built. We can follow the progress, as we will do it with a debugger.


We get as an output:

Enter the slit width (microns)= 1

b = 1

This first part affects the value to the three parameters (d, λ, D] and computes the sinc() function. The following part displays the result.



Multiple slit diffraction


Young’s Double slit diffraction

As in the previous section, let I(x) be the distribution of the intensity and I(0) its value at x=0. Let ‘b’ be the slit width, ‘a’ the distance between slits, T the wavelength and ‘D’ the distance of the screen to the slit. The expression of the intensity normalized to I(0) is:


We proceed with the same way as in the case of the single slit. Making these parameters interactive, it is possible to perform numerical experiments and test a wide variety of configurations. Figure. 2 shows how it is possible to vary the four parameters (b, a, λ, D) with the help of the sliders at the bottom. Likewise these four values are displayed inside the plot along with the formula changing simultaneously with the values in the slider.


Direct display of the portion of code given above



Fraunhofer interface application for the douple slits



Diffraction grating

Increasing the number of slits lead us to the case of grating diffraction. In this case the mathematical expression of the normalized intensity is given by:


And as previously done we transform this into an interactive application. Figure.3 shows the interface dedicated to this case.


Fraunhofer interface application for the multiple slits



Workshop session

In order to better spread optical physics together with the new software facilities such as Python, we organized a workshop at the Faculty of Science of Tunis in November 2014 with 15 participants. This was the first of (we hope) a long series. During this first workshop, we presented as a first part the basic physics of diffraction phenomena and the second part was dedicated to Python. Two python distributions were prepared for the participants (windows and linux). After the installation of the distribution we tested the two cases: single slit and the Young double slit.


Most of the participants (53%) found the session beneficial and (33%) very beneficial on a scale of 0 to 5, ranging from very beneficial to useless. The majority of the participants were very enthusiastic and asked for information on the next planned workshop



After the tedious step of the Python distribution installation procedure, it is very easy to develop programs and build applications that are available for various OS. Since this new environment is free, this possibility constitutes a very crucial point for teaching and more generally outreach programs. The next steps will be in two directions, (1) organization of several workshop sessions and (2) to develop other simulations and implement them on smartphones and mobiles devices that will be accompany the ALOP program.



M. Alarcon, E. Arthurs, Z. Ben Lakhdar, I. Culaba, V. Lakshminarayanan, J. Maquiling, A. Mazzolini, J. Niemela, D. Sokoloff, “Active learning in optics and photonics: experiences in Africa,” (2005) Google Scholar


Z. Ben Lakhdar, N. Derbel, Z. Dhaouadi, H. Ghalila, R. Miled, S. Lahmar, K. Berrada, R. Channa, A. Outzourhit, “Active learning in physics a way for rational thinking - a way for development,” (2007) Google Scholar


M. Alarcon, Z. Ben Lakhdar, I. Culaba, S. Lahmar, V. Lakshminarayanan, A. Mazzolini, J. Maquiling, Niemela J., “Active learning in optics and photonics (ALOP): a model for teacher training and professional development,” in Proc SPIE, 1 –8 (2010). Google Scholar


M. Alarcon, Z. Ben Lakhdar, I. Culaba, G. Denardo, V. Lakshminarayanan, J. Maquiling, A.Mazzolini, J. Niemela, D. Sokoloff, “Active learning in optics and photonics: Training Manual,” Google Scholar


H. Ghalila, Z. BenLakhdar, S. Lahmar, Z. Dhouaidi, Y. Majdi, “Active Learning in Optics and Photonics: Fraunhofer diffraction,” 9289 Google Scholar


V. Lakshminarayanan and R.Burman, Python Optics, unpublished technical report, University of Waterloo,2014). Google Scholar


H.P.Langtangen, “A primer on scientific programming with Python,” 4thSpringer, Berlin (2014). Google Scholar


M. Lutz, “Learning Python,” 5thO’Reilly Media, Sebastopol, CA (2013). Google Scholar


Z.Ben Lakshdar, S.Lahmar, V. Lakshminarayanan, “Workshop on active learning: two examples,” in Proc. SPIE, (2013). Google Scholar
© (2015) COPYRIGHT Society of Photo-Optical Instrumentation Engineers (SPIE). Downloading of the abstract is permitted for personal use only.
Ahmed Ammar, Ritambhar Burman, Hassen Ghalila, Zohra Ben Lakhdar, L. Srinivasa Varadharajan, Souad Lahmar, and Vasudevan Lakshminarayanan "Optics simulations with Python: diffraction", Proc. SPIE 9793, Education and Training in Optics and Photonics: ETOP 2015, 97930K (8 October 2015);


Back to Top