Skip to main content

gtrace — a Gaussian beam ray-tracing package in Python

gtrace traces Gaussian beams through a two-dimensional arrangement of optics. It follows the q-parameter rather than a geometric ray, so a result carries the beam radius, the wavefront curvature and the Gouy phase everywhere along the path, not just the geometry. Beams that are transmitted, reflected and internally reflected inside a wedged substrate are all followed, which is what makes it useful for chasing ghost beams in a real interferometer layout.

It was written for KAGRA and is used to lay out its optical benches.

Installation

From PyPI

pip install gtrace              # the library and the HTML viewer
pip install "gtrace[notebook]"  # ... and the viewer as a Jupyter widget

Python 3.9 or newer. gtrace itself needs only numpy, scipy and traits.

What the viewer needs

The viewer has two front ends, and they do not cost the same:

front end needs
self-contained HTML — render_html(), show(backend='html') nothing beyond gtrace and a web browser
Jupyter widget — widget(), or show() inside a notebook anywidget (≥ 0.9), which brings ipywidgets with it, and a Jupyter front end

Neither route involves Node.js, a build step, a CDN or any network access: the viewer's JavaScript and CSS ship inside the package and are inlined into the page it writes. No CAD software either — not needing it is rather the point.

show() picks the widget when it finds itself in a Jupyter kernel with anywidget installed, and writes the HTML file otherwise, so the same code works both ways. Without anywidget the widget raises WidgetNotAvailable and says so.

From a clone

git clone https://github.com/asoy01/gtrace.git
cd gtrace
pip install ".[notebook]"

Use pip install -e ".[notebook]" instead if you intend to change gtrace itself. The quotes matter in zsh, which would otherwise try to expand the brackets as a glob.

Running the tutorial

The tutorial is a Jupyter notebook, and it lives in the source tree, so it needs the clone above and a Jupyter front end:

pip install jupyterlab
jupyter lab docs/source/tutorial/gtrace-tutorial.ipynb

VS Code's notebook editor works just as well: open the file and select the interpreter you installed gtrace into.

Run the cells from the top. The last chapter opens the viewer in the cell output, which is the part that wants anywidget; everything before it works without. The notebook writes its results next to itself — SeqTrace.dxf, NonSeq.dxf and MC.dxf from the tracing sections, and tutorial_viewer.html, tutorial_layout.json and tutorial_layout.dxf from the layout section.

To read it rather than run it: https://gtrace.readthedocs.io/en/latest/tutorial.html.

Usage

Build the optics with ordinary Python, collect them into a layout, and look at the result:

import gtrace.beam as beam
import gtrace.optcomp as opt
import gtrace.optics.gaussian as gauss
from gtrace.layout import OpticalLayout, TraceRules
from gtrace.unit import *
import numpy as np

src = beam.GaussianBeam(q0=gauss.Rw2q(ROC=np.inf, w=1*mm), wl=1064*nm,
                        pos=[0, 0], dirAngle=0, name='src')

M1 = opt.Mirror(HRcenter=[0.5, 0.0], normAngleHR=deg2rad(180-45),
                diameter=10*cm, thickness=5*cm, wedgeAngle=deg2rad(0.25),
                inv_ROC_HR=0.0, n=1.45, name='M1')
M2 = opt.Mirror(HRcenter=[0.5, 0.4], normAngleHR=deg2rad(-45),
                diameter=10*cm, thickness=5*cm, wedgeAngle=deg2rad(0.25),
                inv_ROC_HR=1.0/2.0, n=1.45, name='M2')

layout = OpticalLayout(optics=[M1, M2], sources=[src],
                       rules=TraceRules(order=10, power_threshold=1e-3))

layout.show()

show() opens an interactive viewer: click anywhere along a beam — not only at a vertex — and it reports the beam radius, the wavefront ROC, the complex q, the waist and its distance, the Rayleigh range, the Gouy phase and the accumulated optical path length at that point, separately in x and y.

In a Jupyter notebook it renders in the cell output and can be edited: drag an element to move it, shift-drag to rotate it, click it to edit its properties. The layout holds your objects by reference, so a mirror you move in the browser is the object your own code named, and the trace and the drawing follow.

Outside a notebook it writes one self-contained HTML file — no server, no CDN, nothing to install — which you can send to a collaborator as it stands:

layout.render_html('trace.html')

DXF output is unchanged and still the way to hand a layout to the rest of an engineering workflow:

import gtrace.draw.renderer as renderer
renderer.renderDXF(layout.draw(), 'trace.dxf')

Documentation

Full documentation, including the tutorial, is at https://gtrace.readthedocs.io/.

The Manuals directory holds the slides the package was first presented with, on ABCD matrices, the q-parameter and the basic concepts.

License

BSD 2-Clause. See LICENSE.

Metadata

Release files for gtrace 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gtrace 0.3.1
File Size Uploaded
gtrace-0.3.1.tar.gz 1.3 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for gtrace 0.3.1
File Interpreter ABI Platform
gtrace-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 1.4 MB

Release files / gtrace-0.3.1.tar.gz

Download URL gtrace-0.3.1.tar.gz
Size 1.3 MB
Tags Source
SHA-256 checksum
How to use checksums
784124cdd7dfe8c19206815d16d9a45acaba6e46c0e4b8ce3648b91685643a9d
BLAKE2b-256 checksum
How to use checksums
b45c72584e6b9327716e9dc6544c3ba0082c3d9ffd9464d102a0ec3b3dfe79ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / gtrace-0.3.1-py3-none-any.whl

Download URL gtrace-0.3.1-py3-none-any.whl
Size 101.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cf45b1ee56fdd5f49ed4feb6a70a8d0c8570619522b71a16edc100ce0a027f95
BLAKE2b-256 checksum
How to use checksums
7c7af20efedee1bdebe9f636c742c6192e62226b4e707e00a6ff69046f79f7df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release history Release notifications | RSS feed

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page