Accurate and easy to use Python light diffraction simulator which includes several propagation and visualization methods. You can use it for simulating the diffraction pattern of an arbitrary aperture, both with monochromatic and polychromatic light.
How the main method (angular spectrum) and the simulator work is described in this Article. Take a look to the videos to see the animated simulations!
- Arbitrary apertures
- Arbitrary light spectrums
- Lenses
- Phase holograms generation and reconstruction
- GPU acceleration
- Incoherent Light (coming soon)
pip install diffractsim
Alternatively, to download the examples and the apertures as well, you can also build from source by cloning the repository and running from the main folder project on the command prompt:
python setup.py install
To perform the simulations, just run from the examples subdirectory the corresponding Python scripts on the command prompt. To compute your own diffraction pattern, you'll need to specify in the script the aperture as an image and input its size.
python hexagon_monochromatic.py
python hexagon_polychromatic.py
python rectangular_grating_small.py
python rectangular_grating_big.py
python bahtinov_mask.py
python rings.py
python hexagonal_grating.py
python diffraction_text.py
For a more detailed discussion about simulating diffraction patterns using lenses, take a look at these examples.
GPU acceleration requires having CuPy installed and CUDA in your computer.
To use GPU acceleration in your simulations, after import diffractsim add the line:
diffractsim.set_backend("CUDA")Cupy and CUDA aren't required to install and use this package, but they can offer a significant speed boost.
The first GPU accelerated run can be slow because Python is caching the required functions. The next time it can be about 10x and 100x faster than a CPU backend depending on your GPU. The speed boost raises as the grid gets larger.
Diffractsim can also be used to compute and visualize longitudinal profiles. Since the computation of each propagation distance is independent, it can be fully parallelized, and therefore GPU use is highly recommended.
python lens_longitudinal_profile.py
python axicon_longitudinal_profile.py
The problem of phase retrieval is a classic one in optics and arises when one is interested in retrieving the wavefront from two intensity measurements acquired in two different planes. Diffractsim provides a simple implementation of this problem.
In the following example, the GitHub logo is recovered at the Fourier plane from a coherently illuminated square-shaped aperture. The script generates a phase mask, which is stored as an image using an HSV colormap and then placed on the aperture to reconstruct the desired image.
python phase_hologram_github_logo_generation_and_reconstruction.py










