3. Quickstart

The library comes with a variety of different example scripts that are described in Section 1. You can download the full examples.zip archive here.

The spherical_aberration.py script provides a good quickstart introduction. Below you can find its code with detailed comments.

Content of examples/spherical_aberration.py:

 1#!/usr/bin/env python3
 2# ^-- shebang for simple execution on Unix systems
 3
 4# This quickstart example demonstrates some features of optrace
 5# The goal is to investigate the spherical aberration of a simple lens
 6
 7# make sure the optrace library is installed in your system
 8
 9# first we import the optrace package, as well as the gui
10# gui and tracer are seperated so we don't have the overhead of
11# always loading all multiple external libraries
12import optrace as ot
13from optrace.gui import TraceGUI
14
15# a Raytracer object provides the raytracing functionality and also controls the tracing geometry
16# the outline parameter specifies a three dimensional box, in which all rays and geometry are located
17# the values are specified as [x0, x1, y0, y1, z0, z1], with x1 > x0, y1 > y0 and z1 > z0
18# all coordinates in the tracing geometry are specified in millimeters
19RT = ot.Raytracer(outline=[-10, 10, -10, 10, -25, 40])
20
21# all elements in the geometry (sources, lenses, detectors, ...) consist of Surfaces
22# a Surface object describes a specific height behaviour depending on 2D x,y-coordinates relative 
23# to the surface center for the raysource we define a circle surface, which is perpendicular
24# to the z-axis with a radius of 1
25RSS0 = ot.CircularSurface(r=1)
26
27# a raysource creates the rays for raytracing, it consists of a surface, a ray divergence behaviour, 
28# a specific light spectrum, a specific base orientation of the rays as well as a specific polarization
29# in our case we want parallel light (divergence="None") parallel to the z-axis, 
30# which is specified by the orientation vector s=[0, 0, 1]
31# the light spectrum is chosen as daylight spectrum D65, which can be found in the presets submodule
32# the absolute position of its surface is controlled by the parent object, in this case the ray source
33RS0 = ot.RaySource(RSS0, divergence="None", spectrum=ot.presets.light_spectrum.d65,
34                  pos=[0, 0, -15], s=[0, 0, 1])
35# after creation the element needs to be added to the tracing geometry
36RT.add(RS0)
37
38# we create a similar ray source, now with a ring surface
39# a ring is additionally parametrized by an inner circle with radius ri
40RSS1 = ot.RingSurface(r=4.5, ri=1)
41RS1 = ot.RaySource(RSS1, divergence="None", spectrum=ot.presets.light_spectrum.d65,
42                  pos=[0, 0, -15], s=[0, 0, 1])
43RT.add(RS1)
44
45# next we define a lens
46# the lens should have a constant refraction index of 1.5,
47# this is specified as RefractionIndex with mode "Constant" and a value of 1.5
48# generally, a RefractionIndex can also have wavelength dependent behaviour
49n = ot.RefractionIndex("Constant", n=1.5)
50
51# a lens consists of a front and back surface
52# in the case of spherical lens surfaces, we need to specify a surface radius r and a curvature radius R
53# For a biconvex lens the front has a positive curvature and the back a negative one
54front = ot.SphericalSurface(r=5, R=15)
55back = ot.SphericalSurface(r=5, R=-15)
56# the creation of the lens requires both surfaces, as well as a position and a refractive index
57# there multiple ways to define the overall lens thickness, or rather the distance between both surfaces
58# in our case we provide de, which is a thickness extension between front and back surface
59# this means there is a spacing of de=0.2mm between the end of the front surface and the start 
60# of the back surface the lens position defined by parameter pos is then exactly in the center of these 0.2mm
61L = ot.Lens(front, back, de=0.2, pos=[0, 0, 0], n=n)
62RT.add(L)
63
64# a detector renders images inside the raytracer and is defined by a single surface
65# in our case we want a rectangular detector with side lengths 20mm
66# for this a RectangularSurface with a "dim" sides list of [20, 20] is initialized
67DETS = ot.RectangularSurface(dim=[20, 20])
68# a detector takes a surface and a position as arguments
69DET = ot.Detector(DETS, pos=[0, 0, 23.])
70RT.add(DET)
71
72# after the geometry definition, the optical setup must be traced next
73# this could be done by calling RT.trace(N), with parameter N being the number of rays
74# but we also can create a graphical frontend, that automatically traces the scene.
75
76# For this, a TraceGUI object is needed. 
77# It takes the raytracer as parameter, while additional parameters can provide graphical settings
78# For example, rays will be colored according to their source number by providing color_type="Source"
79# and a higher relative ray opacity is set by ray_opacity=0.2
80sim = TraceGUI(RT, coloring_mode="Source", ray_opacity=0.2)
81
82# the frontend is now created and needs to be started explicitly
83sim.run()
84
85# You can now experiment with different features, e.g.
86# 1. Navigate in the three dimensional geometry scene
87# 2. Click on ray-surface intersections to display ray properties
88# 3. Play around with visual settings in the main tab
89# 4. Move the detector and render detector images in the Imaging-Tab
90# 5. Focus search for each of the two sources. 
91#    In the "Focus" Tab select "Rays From Selected Source Only" and click on "Find Focus" Button.
92#    Select the other ray source to find the other focus
93

Screenshots

_images/example_spherical_aberration1.png
_images/example_spherical_aberration2.png