Diffractsim
✨🔬 A flexible diffraction simulator for exploring and visualizing physical optics.
Install / Use
/learn @rafael-fuente/DiffractsimREADME
Diffractsim: A diffraction simulator for exploring and visualizing physical optics
Flexible, 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.
The basic use of this simulator using the angular spectrum method is described in this article. Take a look to the videos to see the animated simulations!
Features
- [x] Arbitrary apertures and light spectrums
- [x] Full-path optical propagation and arbitrary zoom in the region of interest
- [x] Lenses
- [x] Phase holograms generation and reconstruction
- [x] GPU acceleration
- [x] Differentiable propagation
- [ ] Incoherent Light (coming soon)
Installation
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
Examples
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
The examples from the video about diffraction with lenses can be found here.
GPU acceleration requires having either CuPy or JAX installed along with CUDA on your computer.
To use GPU acceleration with CuPy in your simulations, after import diffractsim add the line:
diffractsim.set_backend("CUDA")
Or when using JAX:
diffractsim.set_backend("JAX")
JAX, in addition to offering GPU acceleration, will allow differentiable programming. Cupy, JAX 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 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.
python phase_hologram_github_logo_generation_and_reconstruction.py
Alternatively, 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, 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.
python flat-top_beam_generation.py
python ring_shaped_beam_generation.py
Citing Diffractsim
R. de la Fuente Herrezuelo, <i>diffractsim: A flexible python diffraction simulator</i>, GitHub (2022), https://doi.org/10.5281/zenodo.6147771
Related Skills
claude-opus-4-5-migration
82.7kMigrate prompts and code from Claude Sonnet 4.0, Sonnet 4.5, or Opus 4.1 to Opus 4.5
model-usage
335.8kUse CodexBar CLI local cost usage to summarize per-model usage for Codex or Claude, including the current (most recent) model or a full model breakdown. Trigger when asked for model-level usage/cost data from codexbar, or when you need a scriptable per-model summary from codexbar cost JSON.
TrendRadar
49.7k⭐AI-driven public opinion & trend monitor with multi-platform aggregation, RSS, and smart alerts.🎯 告别信息过载,你的 AI 舆情监控助手与热点筛选工具!聚合多平台热点 + RSS 订阅,支持关键词精准筛选。AI 智能筛选新闻 + AI 翻译 + AI 分析简报直推手机,也支持接入 MCP 架构,赋能 AI 自然语言对话分析、情感洞察与趋势预测等。支持 Docker ,数据本地/云端自持。集成微信/飞书/钉钉/Telegram/邮件/ntfy/bark/slack 等渠道智能推送。
mcp-for-beginners
15.6kThis open-source curriculum introduces the fundamentals of Model Context Protocol (MCP) through real-world, cross-language examples in .NET, Java, TypeScript, JavaScript, Rust and Python. Designed for developers, it focuses on practical techniques for building modular, scalable, and secure AI workflows from session setup to service orchestration.












