{"id":33142751,"url":"https://github.com/rafael-fuente/diffractsim","last_synced_at":"2026-06-27T21:08:05.679Z","repository":{"id":37985607,"uuid":"323979736","full_name":"rafael-fuente/diffractsim","owner":"rafael-fuente","description":"✨🔬 A flexible diffraction simulator for exploring and visualizing physical optics.","archived":false,"fork":false,"pushed_at":"2025-10-07T00:48:32.000Z","size":45948,"stargazers_count":1044,"open_issues_count":21,"forks_count":141,"subscribers_count":20,"default_branch":"main","last_synced_at":"2026-06-08T09:17:29.684Z","etag":null,"topics":["angular-spectrum-method","diffraction","diffraction-pattern","diffraction-simulations","holography","optics","physics","physics-simulations","python","wave-optics"],"latest_commit_sha":null,"homepage":"https://rafael-fuente.github.io/simulating-diffraction-patterns-with-the-angular-spectrum-method-and-python.html","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rafael-fuente.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":"CITATION.cff","codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":["rafael-fuente"]}},"created_at":"2020-12-23T18:56:00.000Z","updated_at":"2026-06-04T10:20:09.000Z","dependencies_parsed_at":"2023-02-01T05:00:51.402Z","dependency_job_id":"9aef5ea2-76b5-4099-a736-54f0281438cc","html_url":"https://github.com/rafael-fuente/diffractsim","commit_stats":{"total_commits":104,"total_committers":5,"mean_commits":20.8,"dds":0.08653846153846156,"last_synced_commit":"b5349a6ec6bfd48857ba73eb78a20516c0d35f9b"},"previous_names":["rafael-fuente/diffraction-simulations--angular-spectrum-method"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/rafael-fuente/diffractsim","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafael-fuente%2Fdiffractsim","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafael-fuente%2Fdiffractsim/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafael-fuente%2Fdiffractsim/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafael-fuente%2Fdiffractsim/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rafael-fuente","download_url":"https://codeload.github.com/rafael-fuente/diffractsim/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafael-fuente%2Fdiffractsim/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34867838,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-27T02:00:06.362Z","response_time":126,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["angular-spectrum-method","diffraction","diffraction-pattern","diffraction-simulations","holography","optics","physics","physics-simulations","python","wave-optics"],"created_at":"2025-11-15T13:00:22.990Z","updated_at":"2026-06-27T21:08:05.674Z","avatar_url":"https://github.com/rafael-fuente.png","language":"Python","funding_links":["https://github.com/sponsors/rafael-fuente"],"categories":["simulation"],"sub_categories":[],"readme":"# Diffractsim: A diffraction simulator for exploring and visualizing physical optics\n[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.6147771.svg)](https://doi.org/10.5281/zenodo.6147771)\n\n[![animation](/images/diffraction_animated.gif)](https://www.youtube.com/watch?v=Ft8CMEooBAE\u0026list=PLYkZehxPE_IhyO6wC21nFP0q1ZYGIW4l1\u0026index=1)\n\n\nFlexible, and easy-to-use Python diffraction simulator that focuses on visualizing physical optics phenomena. The simulator uses mainly scalar diffraction techniques for light propagation, provides a nice interface for simulation set up, and includes several plotting options, counting with CIE Color matching functions for accurate color reproduction. The implemented methods are differentiable when using the JAX-backend, enabling optimization and design of the optical setups.\n\nThe basic use of this simulator using the angular spectrum method is described in this [article](https://rafael-fuente.github.io/simulating-diffraction-patterns-with-the-angular-spectrum-method-and-python.html). Take a look to the [videos](https://www.youtube.com/watch?v=Ft8CMEooBAE\u0026list=PLYkZehxPE_IhyO6wC21nFP0q1ZYGIW4l1\u0026index=1) to see the animated simulations!\n\n## Features\n\n- [x] Arbitrary apertures and light spectrums\n- [x] Full-path optical propagation and arbitrary zoom in the region of interest\n- [x] Lenses\n- [x] Phase holograms generation and reconstruction\n- [x] GPU acceleration\n- [x] Differentiable propagation\n- [ ] Incoherent Light (coming soon)\n\n\n## Installation\n\n\nAlternatively, 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:\n```\npython setup.py install\n```\n\n## Examples\n\nTo perform the simulations, just run from the [examples subdirectory](https://github.com/rafael-fuente/Diffraction-Simulations--Angular-Spectrum-Method/tree/main/examples) the corresponding Python scripts on the command prompt. \nTo compute your own diffraction pattern, you'll need to specify in the script the aperture as an image and input its size.\n\n```\npython hexagon_monochromatic.py\n```\n\n[![N|Solid](/images/hexagon_monochromatic.png)](/examples/hexagon_monochromatic.py)\n\n```\npython hexagon_polychromatic.py\n```\n\n[![N|Solid](/images/hexagon_polychromatic.png)](/examples/hexagon_polychromatic.py)\n\n```\npython rectangular_grating_small.py\n```\n\n[![N|Solid](/images/rectangular_grating_small.png)](/examples/rectangular_grating_small.py)\n\n```\npython rectangular_grating_big.py\n```\n\n[![N|Solid](/images/rectangular_grating_big.png)](/examples/rectangular_grating_big.py)\n\n```\npython bahtinov_mask.py\n```\n\n[![N|Solid](/images/bahtinov_mask.png)](/examples/bahtinov_mask.py)\n\n```\npython rings.py\n```\n\n[![N|Solid](/images/rings.png)](/examples/rings.py)\n\n```\npython hexagonal_grating.py\n```\n\n[![N|Solid](/images/hexagonal_grating.png)](/examples/hexagonal_grating.py)\n\n```\npython diffraction_text.py\n```\n\n[![N|Solid](/images/text.png)](/examples/text.py)\n\nThe examples from the video about diffraction with lenses can be found [here](https://github.com/rafael-fuente/Diffraction-Simulations--Angular-Spectrum-Method/blob/main/Simulations%20with%20lenses.md).\n\nGPU acceleration requires having either [CuPy](https://docs.cupy.dev/en/stable/install.html) or [JAX](https://jax.readthedocs.io/en/latest/quickstart.html) installed along with [CUDA](https://developer.nvidia.com/cuda-downloads) on your computer. \nTo use GPU acceleration with CuPy in your simulations, after import `diffractsim` add the line:\n\n```python\ndiffractsim.set_backend(\"CUDA\")\n```\n\nOr when using JAX:\n\n```python\ndiffractsim.set_backend(\"JAX\")\n```\n\nJAX, in addition to offering GPU acceleration, will allow differentiable programming.\nCupy, JAX and CUDA aren't required to install and use this package, but they can offer a significant speed boost.\n\nThe 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.\n\n\nDiffractsim 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.\n\n```\npython lens_longitudinal_profile.py\n```\n\n[![N|Solid](/images/lens_longitudinal_profile.png)](/examples/lens_longitudinal_profile.py)\n\n\n```\npython axicon_longitudinal_profile.py\n```\n\n[![N|Solid](/images/axicon_longitudinal_profile.png)](/examples/axicon_longitudinal_profile.py)\n\n\nThe 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.\n\nIn 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 colourmap and then placed on the aperture to reconstruct the desired image. The phase mask can be reproduced with, for example, spatial light modulators (SLMs) and metasurfaces.\n\n```\npython phase_hologram_github_logo_generation_and_reconstruction.py\n```\n\n[![animation](/images/github_logo.gif)](/examples/phase_hologram_github_logo_generation_and_reconstruction.py)\n\n\nAlternatively, to phase retrieval based on iterative algorithms, there is a high-accuracy method for beam shaping using phase profiles, described  in [https://doi.org/10.1364/OE.559542](https://doi.org/10.1364/OE.559542), which allows, for example, generating flat-top or ring-shaped beams from Gaussian beams. Diffractsim also provides an implementation of this method, and was used for the experimental comparision.\n\n```\npython flat-top_beam_generation.py\n```\n```\npython ring_shaped_beam_generation.py\n```\n\n[![N|Solid](/images/cylindrical_beam_shaping.png)](/examples/flat-top_beam_generation.py)\n\n\n## Citing `Diffractsim`\n\nR. de la Fuente Herrezuelo, \u003ci\u003ediffractsim: A flexible python diffraction simulator\u003c/i\u003e, GitHub (2022), https://doi.org/10.5281/zenodo.6147771","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frafael-fuente%2Fdiffractsim","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frafael-fuente%2Fdiffractsim","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frafael-fuente%2Fdiffractsim/lists"}