Skip to main content

A high-performance optical ray tracer with C++ and CUDA backends.

Project description

Welcome to cuda_optics!

PyPI version Language Platform License

cuda_optics is a high-performance ray-tracing simulation framework designed to bridge the gap between Python's approachability and raw hardware acceleration. It functions as a scriptable, 2D optical CAD library , optimized for simulating optical benches, lens barrels, and telescope/microscope designs.

The system features a hybrid architecture flexible enough for any hardware environment. It serves as a physics sandbox, offering students, educators, hobbyists, and researchers a large selection of built-in functionalities while maintaining maximum control over ray generation and element geometry. For detailed documentation, find the file cuda_optics_documentation.ipynb in the examples folder on my github linked below.

Table of Contents

Why use this framework?

  1. Cross-Platform Core: Native support for both Linux and Windows systems ensures consistent deployment across diverse development environments.

  2. Hardware Acceleration & Optimization: Optimized for NVIDIA GPUs (Compute Capability 6.1+), supporting architectures from Pascal to Hopper. This high-throughput capability allows iterative optimization workflows such as Monte Carlo tolerance analysis or global optimization loops (e.g., Genetic Algorithms).

  3. Hybrid Compatibility: Includes a highly optimized C++ CPU engine that can be activated if a GPU is unavailable, ensuring code portability across high-performance workstations and standard educational laptops.

  4. Zero-Restriction Geometry & Synthetic Data: Unlike standard matrix-optics tools, this framework poses no restrictions on element orientation or position. This geometric freedom makes it an ideal engine for procedurally generating massive synthetic datasets, crucial for training Machine Learning models in computational photography, depth estimation, and aberration correction.

  5. Rich Component Library: Built-in OOP support for 11 optical elements including parabolic mirrors, thick lenses (meniscus/biconvex), prisms, and diffraction gratings. This modularity supports niche applications, such as designing folded spectrometers or simulating complex beam delivery systems for high-power lasers.

  6. Education & Visualization: Integrated matplotlib plotting tools provide instant visual feedback. The library functions as a risk-free "Virtual Optical Bench", allowing students to visualize abstract concepts like spherical aberration, chromatic dispersion, and spatial filtering in real-time. The framework can also be utilised by LLMs to generate nice plots while explaining topics related to optics.

  7. Pipeline Integration (Python Native): A user-friendly Python interface utilizes intuitive commands that integrate natively with the scientific stack (NumPy, SciPy, PyTorch). This allows the ray tracer to act as a direct data generator within AI training pipelines or automated control loops without the bottleneck of file I/O or proprietary API wrappers.

  8. Open & Free: Released under the MIT License, it is free for both educational instruction and commercial industrial use.

Motivation behind the framework and design

While working on a project regarding spectroscopes, I required a software/library to simulate various configurations and validate my calculations. Even after spending a lot of time searching for such resources, I couldn't find one which could solve my very specific problem. This prompted me to write a python code for my immediate requirements and while doing that, I realised how I can code an actual generalised physics engine for optical system simulation. I then researched about various optical simulators available and found that existing resources were either too simplistic for niche problems or were expensive proprietary software inaccessible to students.

I focused on both educational and industrial value of this project. The plotting functionalities help in learning and build confidence through instant visualisation and easy to interpret feedback. The C++ and CUDA backends allow for high throughput ray tracing suitable for rigorous analysis. The OOP based architecture allows more optical elements and functionalities to be added as and when required.

Designed effectively as a virtual optical bench, the framework currently focuses on 2D arrangements to prioritize clarity and speed. This allows for precise Meridional Plane Analysis which is ideal for symmetric systems like telescopes or planar experiments. However, the core physics logic is built on dimension-agnostic vector algebra (Linear Algebra), meaning the simulation code is ready for N-dimensional expansion with minimal architectural changes.

Installation

Option 1: Install via Pip (Recommended)

The easiest way to install on Windows and Linux. This includes pre-compiled binaries for both CPU and GPU.

pip install cuda_optics

Option 2: Build from Source

For macOS users or those interested in modifying the core engine:

git clone [https://github.com/Adityavardhan-Srivastava/cuda_optics.git](https://github.com/Adityavardhan-Srivastava/cuda_optics.git)
cd cuda_optics

Then compile the cpp/cuda file in src_native/ and place them in cuda_optics/bin/ and delete the binaries already present there. Compile using appropriate compilers.

Quick Start

Here is a simple example of how to create a lens and trace a ray

import cuda_optics as co
import matplotlib.pyplot as plt

# 1. Create Elements
# A lens centered at x=0.5, with radius of curvature 0.5 (front) and -0.5 (back)
lens = co.Lens(name="Lens", center=[0.5, 0.0], normal=[1.0, 0.0], 
               aperture=0.4, refractive_index=1.5, 
               R1=0.5, R2=-0.5, thickness=0.1)

screen = co.Screen(name="Sensor", center=[1.0, 0.0], normal=[-1.0, 0.0], aperture=0.5)

# 2. Create Rays
# Generating 3 parallel rays manually
rays = [
    co.Ray(pos=[0.0, -0.1], dir=[1.0, 0.0], wavelength=550, color='green'),
    co.Ray(pos=[0.0,  0.0], dir=[1.0, 0.0], wavelength=550, color='green'),
    co.Ray(pos=[0.0,  0.1], dir=[1.0, 0.0], wavelength=550, color='green')
]

# 3. Run Simulation 
# (Set use_gpu=True if you have an NVIDIA GPU)
co.run_simulation(rays, [lens, screen], bounces=5, use_gpu=False)

# 4. Visualize
fig, ax = plt.subplots()
co.plot_system(rays, [lens, screen], ax)
plt.title("Quick Start: Simple Lens")
plt.show()

For more such examples, explore the folder examples/. The python file cuda_optics_original.py contains all elements and functionalities in python to take reference from. The jupyter notebook Documentation.ipynb contains documentation and example codes for this library.

Links

  1. Github repo: https://github.com/Adityavardhan-Srivastava/cuda_optics

  2. PyPI page: https://pypi.org/project/cuda-optics/#description

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cuda_optics-0.1.4.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cuda_optics-0.1.4-py3-none-any.whl (1.1 MB view details)

Uploaded Python 3

File details

Details for the file cuda_optics-0.1.4.tar.gz.

File metadata

  • Download URL: cuda_optics-0.1.4.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for cuda_optics-0.1.4.tar.gz
Algorithm Hash digest
SHA256 c4f0845aeb677d6e57279cc669098e704830d84f16c5675e31241bf1bcfe19b5
MD5 4815f6a6418a24f5e5646ffd89369d0b
BLAKE2b-256 c66d26c4244b12da2645cfd896e5bdec31e16950ff4de593892b6182d42093eb

See more details on using hashes here.

File details

Details for the file cuda_optics-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: cuda_optics-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for cuda_optics-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 60d0a1b09a65e0d048793eb3401dcae50f1454f5816512218a1d6dddb56e4c1c
MD5 ebef637ea33094ded31b3ea270186cf0
BLAKE2b-256 099043de6f9463cc49b9873751acc6d405ee27c56a3bfeb69b7012df26b07619

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page