Email: [email protected]
Carlos Guerrero P., Morgan Rhaí Najera Roa, Anais Sotelo B., Ilse Plauchu F., José A. Araiza
It would be appreciated if a reference to the following work, for which this package was originally built, is included whenever this code is used for a publication: (https://www.spiedigitallibrary.org/journals/optical-engineering/volume-61/issue-1/015101/KrakenOS-Python-based-general-exact-ray-tracing-library/10.1117/1.OE.61.1.015101.short)
And the book "Optical Simulation Using Python and KrakenOS": (https://spie.org/Publications/Book/2672426#_=_)
KrakenOS (Kraken - Optical Simulator) is a Python library based on NumPy, Matplotlib, PyVTK, and PyVista. It provides exact ray tracing and 2D/3D visualization for optical systems. KrakenOS supports sequential and non-sequential tracing, optical surfaces defined by parameters or mathematical functions, STL solid objects with optical properties, glass and lens catalogs, off-axis systems, wavefront analysis with Zernike polynomials, Seidel sums, entrance and exit pupil calculations, paraxial optics, and practical instrument-level examples.
Start with the KrakenOS Manual. It integrates installation, first systems, surfaces, materials, ray tracing, visualization, pupils, analysis, advanced workflows, API orientation, images, and links to runnable examples.
For a shorter map of the supporting documentation, see docs/README.md.
For regular use, install KrakenOS with pip:
python -m pip install KrakenOSThis installs KrakenOS and its Python dependencies, including NumPy, SciPy, Matplotlib, PyVista, VTK, PyVTK, csv342, and pandas.
For development from a cloned repository, run this command from the repository
root, where pyproject.toml is located:
python -m pip install -e .To verify the installation:
import KrakenOS as Kos
print(Kos.__file__)
print(Kos.surf())For the complete learning path, see the KrakenOS Manual. To refresh the generated example images used by the documentation:
python tools/generate_example_images.py --all"""Examp Doublet Lens Pupil"""
# Loading the library
import KrakenOS as Kos# Creating an object of the surf class for the object plane
P_Obj = Kos.surf()
P_Obj.Thickness = 100
P_Obj.Glass = "AIR"
P_Obj.Diameter = 30.0
# Creating a surface for the first face in BK7 Glass
L1a = Kos.surf()
L1a.Rc = 92.847
L1a.Thickness = 6.0
L1a.Glass = "BK7"
L1a.Diameter = 30.0
# Creating a surface for the second face in F2 Glass
L1b = Kos.surf()
L1b.Rc = -30.716
L1b.Thickness = 3.0
L1b.Glass = "F2"
L1b.Diameter = 30
# Creating a surface for the third interface to air
L1c = Kos.surf()
L1c.Rc = -78.197
L1c.Thickness = 97.376 - 40
L1c.Glass = "AIR"
L1c.Diameter = 30
# Creating a surface to exemplify a pupil
pupila = Kos.surf()
pupila.Rc = 30
pupila.Thickness = 40.
pupila.Glass = "AIR"
pupila.Diameter = 5
pupila.Name = "Pupil"
pupila.DespY = 0.
pupila.Nm_Pos=(-10,10)
# Creating a surface for image plane
P_Ima = Kos.surf()
P_Ima.Rc = 0.0
P_Ima.Thickness = 0.0
P_Ima.Glass = "AIR"
P_Ima.Diameter = 20.0
P_Ima.Name = "P_Ima"
P_Ima.Nm_Pos=(-10,10)Creating a list with all the surfaces and loading the default glass catalogs (See user manual)
A = [P_Obj, L1a, L1b, L1c, pupila, P_Ima]
config_1 = Kos.Setup()# Creating the system with previous information
Doublet = Kos.system(A, config_1)Creating a ray container
Rays = Kos.raykeeper(Doublet)Defining parameters to configure pupil on surface 4 (Again.., see user manual)
W = 0.4
sur = 4
AperVal = 10
AperType = "EPD"
Pup = Kos.PupilCalc(Doublet, sur, W, AperType, AperVal)
# Configuring field and ray array type
Pup.Samp = 3
Pup.Ptype = "fan"
Pup.FieldType = "angle"
Pup.FieldY = 2.0Generating and tracing rays
# ray origin coordinates and direction cosines
x, y, z, L, M, N = Pup.Pattern2Field()
# Tracing the rays with a loop
for i in range(0, len(x)):
pSource_0 = [x[i], y[i], z[i]]
dCos = [L[i], M[i], N[i]]
Doublet.Trace(pSource_0, dCos, W)
Rays.push()# Saving rays
# Configuring (-field) and ray array type,.. etc
Pup.FieldY = -Pup.FieldY
x, y, z, L, M, N = Pup.Pattern2Field()
for i in range(0, len(x)):
pSource_0 = [x[i], y[i], z[i]]
dCos = [L[i], M[i], N[i]]
Doublet.Trace(pSource_0, dCos, W)
Rays.push() # Saving rays3D plotting
Kos.display3d(Doublet, Rays,2)Or for 2D plotting
Kos.display2d(Doublet, Rays,0,1)| class Attribute | Short description |
|---|---|
| surf.Name = "" | Name of the element. |
| surf.Nm_Pos = (0,0) | "Name" position in the 2D diagram. |
| surf.Note = "None" | Useful for adding user notes to a surface. |
| surf.Rc = 0 | Paraxial radius of curvature in millimeters. |
| surf.Cylinder_Rxy_Ratio = 1 | Ratio between the axial and sagittal radius of curvature. |
| surf.Axicon = 0 | Values other than zero an axicon is generated with the angle defined |
| surf.Thickness = 0.0 | Distance between this surface and the next surface. |
| surf.Diameter = 1.0 | Outside diameter of the surface. |
| surf.InDiameter = 0.0 | Internal diameter of the surface. |
| surf.Glass = "AIR" | String for the glass name. If a float is used instead, that float is the refractive index. |
| surf.k = 0.0 | Conicity constant for classical conic surfaces, k = 0 for spherical, k = -1 for parabola, etc. Default value: 0.0 |
| surf.DespX = 0.0 | Displacement of the surface in the X, Y and Z axis |
| surf.DespY = 0.0 | |
| surf.DespZ = 0.0 | |
| surf.TiltX = 0.0 | Rotation of the surface in the X, Y and Z axis |
| surf.TiltY = 0.0 | |
| surf.TiltZ = 0.0 | |
| surf.Order = 0 | Define the order of the transformations. |
| surf.AxisMove = 1 | Defines what will happen to the optical axis after a coordinate transformation. |
| surf.Diff_Ord = 0.0 | Diffraction order. |
| surf.Grating_D = 0.0 | Separation between the lines of the diffraction grating. |
| surf.Grating_Angle = 0.0 | Angle of the grating lines in the plane of the surface |
| surf.ZNK = np.zeros (#) | Zernike polynomials coefficients |
| surf.ShiftX = 0 | Offset the surface function on the X or Y axis. |
| surf.ShiftY = 0 | |
| surf.Mask = 0 | (0) Do not apply mask, (1) Use mask as aperture, (2) Use mask as obstruction. Default value: 0 |
| surf.Mask_Shape = Object_3D | Form of the mask to apply on surface |
| surf.AspherData = np.zeros (#) | Asphericity coefficients. |
| self.ExtraData = [f, coef] | User-defined function for optical surface |
| Surf.Error_map = [X, Y, Z, SPACE] | Error map array |
| surf.Drawing = 1 | 1 for drawn in the 3D plot, 0 to omit. |
| surf.Color = [0,0,0] | Element color for 3D Plot. [R,G,B] |
| surf.Solid_3d_stl = "None" | Path to the 3D solid STL file. |
| Attribute or method | Short description |
|---|---|
system.Trace(pS, dC, wV) |
Sequential ray tracing. pS is the ray origin [x, y, z], dC is the direction cosine vector [L, M, N], and wV is the wavelength in micrometers. |
system.NsTrace(pS, dC, wV) |
Non-sequential ray tracing. |
system.Parax(w) |
Paraxial optics calculations. |
system.disable_inner |
Enables the central aperture. |
system.enable_inner |
Disables the central aperture. |
system.SURFACE |
Surfaces the ray passed through. |
system.NAME |
Surface names the ray passed through. |
system.GLASS |
Materials the ray passed through. |
system.XYZ |
Ray coordinates from origin to image plane. |
system.OST_XYZ |
Ray intersection coordinates in the local surface coordinate system. |
system.DISTANCE |
Distances traveled by the ray. |
system.OP |
Optical path list. |
system.TOP |
Total optical path. |
system.TOP_S |
Optical path by sections. |
system.ALPHA |
Material absorption coefficients. |
system.BULK_TRANS |
Bulk transmission through the system. |
system.S_LMN |
Surface normal direction cosines [L, M, N]. |
system.LMN |
Incident ray direction cosines [L, M, N]. |
system.R_LMN |
Resulting ray direction cosines [L, M, N]. |
system.N0 |
Refractive indices before each interface. |
system.N1 |
Refractive indices after each interface. |
system.WAV |
Ray wavelength in micrometers. |
system.G_LMN |
Direction cosines defining diffraction grating lines on the surface. |
system.ORDER |
Ray diffraction order. |
system.GRATING_D |
Diffraction grating line spacing in micrometers. |
system.RP |
Fresnel reflection coefficient for P polarization. |
system.RS |
Fresnel reflection coefficient for S polarization. |
system.TP |
Fresnel transmission coefficient for P polarization. |
system.TS |
Fresnel transmission coefficient for S polarization. |
system.TTBE |
Total energy transmitted or reflected by element. |
system.TT |
Total transmitted or reflected energy. |
system.targ_surf(int) |
Limits ray tracing to the selected surface. |
system.flat_surf(int) |
Changes a surface to flat. |
The current user manual starts at docs/manual/README.md. The complete documentation index is docs/README.md.
Example scripts are available in KrakenOS/Examples:
Examp_Axicon.pyExamp_Axicon_And_Cylinder.pyExamp_CzernyTurner.pyExamp_Diffraction_Grating_Reflection.pyExamp_Diffraction_Grating_Reflection_Single.pyExamp_Diffraction_Grating_Transmission.pyExamp_Dispersion_By_AbbeNumber.pyExamp_Doublet_Lens.pyExamp_Doublet_Lens_3Dcolor.pyExamp_Doublet_Lens_CommandsSystem.pyExamp_Doublet_Lens_Cylinder.pyExamp_Doublet_Lens_NonSec.pyExamp_Doublet_Lens_NonSec-AR_Coating.pyExamp_Doublet_Lens_Pupil.pyExamp_Doublet_Lens_Pupil_Seidel.pyExamp_Doublet_Lens_Tilt.pyExamp_Doublet_Lens_Tilt_non_sec.pyExamp_Doublet_Lens_Tilt_non_sec-AR-Coating.pyExamp_Doublet_Lens_Tilt-Nulls.pyExamp_Doublet_Lens_Zernike.pyExamp_Doublet_Lens-ParaxMatrix.pyExamp_Doublet_Optimization.pyExamp_ExtraShape_Micro_Lens_Array.pyExamp_ExtraShape_Radial_Sine.pyExamp_ExtraShape_UserFacets.pyExamp_ExtraShape_XY_Cosines.pyExamp_ExtraShape_XY_Cosines_UDA.pyExamp_Flat_Mirror_45Deg.pyExamp_Flat_NonSec_AR-caoating.pyExamp_Fresnel.pyExamp_Glass_Catalog_Order.pyExamp_ParaboleMirror_Shift.pyExamp_ParaboleMirror_Shift_UDA.pyExamp_Perfect_lens.pyExamp_Perfect_lens_Telescope.pyExamp_Pickle_Doublet_Lens_3Dcolor.pyExamp_Prism_STL.pyExamp_Prism_STL-AR_coating.pyExamp_Ray.pyExamp_Refraction_Prism.pyExamp_Refraction_Prism_OneRay.pyExamp_Refraction_Prism_solid.pyExamp_Refraction_Prism_solid_Generation.pyExamp_RonchiTest.pyExamp_Solid_Object_STL.pyExamp_Solid_Object_STL_ARRAY.pyExamp_Source_Distribution_Function.pyExamp_Sphere.pyExamp_Spruce-tone_Github_User (Manually enter the refractive index, dispersion and alpha).pyExamp_Spruce-tone_Github_User(Loading_Zemax_and_Catalogs).pyExamp_Tel_2M.pyExamp_Tel_2M_Atmospheric_Refraction_Corrector_Adaptable.pyExamp_Tel_2M_Atmospheric_Refraction_Corrector_Static.pyExamp_Tel_2M_Cuña.pyExamp_Tel_2M_Echelle.pyExamp_Tel_2M_Optimization_Variables.pyExamp_Tel_2M_Pupila.pyExamp_Tel_2M_Spyder_Spot_Diagram.pyExamp_Tel_2M_Spyder_Spot_RMS.pyExamp_Tel_2M_Wavefront_Fitting.pyExamp_Tel_2M_Wavefront_Fitting_optimization.pyExamp_Tel_2M-STL_ImageSlicer.py
Enjoy it!


