# What does “not enough segments allocated to trace all possible ray paths” mean?

This article will cover the reasons that one might receive the error message "not enough segments allocated to trace all possible ray paths!" or "not enough intersections allocated to finish ray trace!". These are two errors that should NOT be ignored, and this article will discuss how they may be fixed, and outlines the difference between a segment and an intersection.

Authored By Dan Hill

## Introduction

Periodically, in complex systems with many objects, or in designs where rays strike a single object multiple times, the default values for the maximum number of intersections and/or segments is not sufficient. As a result, you might receive one of the following errors:

• Not enough segments allocated to trace all possible ray paths!
• Not enough intersections allocated to finish ray trace!

Both of these constitute “hard” errors that should NOT be ignored; ray trace results can be wrong if the maximums are not increased to a sufficient level.

## What is an intersection? What is a segment?

First, let’s define what each one of the terms mean. An intersection is a point at which a ray strikes the face of an object.  A single ray may strike the same object many times, and each time the ray hits the face of the object, it is tallied towards the total number of intersections.

A segment is the portion of a ray path from one intersection to the next.  When a ray is launched from the source, it travels to the first object.  This is the first segment, or segment number 1.  At the point of intersection on the face of the object, the ray may split into 2 more rays (one transmitted and one reflected, for example), and each of those rays are another segment (for a total of 3).

To make this distinction clear, observe the diagram below.  A single ray is incident onto the front face of a plane parallel plate.  In this scenario (ignoring ray splitting), there are a total of 3 ray segments, and 2 intersections (one at the front face, and one at the back face).  Each segment is colored differently in the plot below.

How are rays colored by segments?  In the settings of the NSC Shaded Mode or NSC 3D Layout, you may choose to “Color Rays By” Source #, Wave #, Configuration #, Wavelength or Segment #.

It is sometimes helpful to think of ray segments and intersections as a “family” of rays. After any point of intersection, a “parent” ray can be split into a number of “child” rays.  Each one of these child rays can be the parent of another set of child rays, much like a family tree.  Since the maximum intersections and segments are applied per ray launched, you can think of each ray launched from the source as having its own family tree.  If a maximum is reached, OpticStudio will issue an error, indicating that not enough segments or intersections have been allotted.

As you can imagine, ray splitting quickly populates the number of segments OpticStudio needs to trace. If we revisit the example shown above, the total number of segments quickly increases.  For example, if we consider the Fresnel reflected rays from both the front and back surfaces of the plate, then there are a total of five segments.  If each child ray then hits another interface and splits into two of its own child rays, we have a total of 9 segments, and so on.  Moreover, it’s possible that we may scatter into multiple rays at a given interface, and each of those scattered child rays hits another scattering surface, adding more generations of child rays.

## Maximum number of intersections and segments per ray

So where do we change the parameters which control the number of intersections and segments which can be used in the ray trace?  Along with a number of other parameters, these two user-defined values can be modified in the System Explorer, under Non-Sequential:

Every control within this dialog defines how rays are traced within an NSC group in OpticStudio.  For details on any specific control, you may refer to the following section of OpticStudio help files: The Setup Tab...System Group...System Explorer...Non-Sequential.

The maximum number of segments and intersections are used to set the maximum number of allotted segments or intersections per ray launched. This means that each ray launched from a source can have up to the maximum number of segments and intersections defined. So, if the maximum number of segments is 1000, and 5000 individual rays are launched, OpticStudio can store up to 5 million ray segments.

So why does OpticStudio allow these parameters to be user-defineable?  Why doesn’t OpticStudio just trace as many segments as is needed to complete the trace? The reason for this is that OpticStudio would have to reserve a lot of memory to potentially support a huge number of segments. In most cases, this isn’t necessary, and would slow down the ray trace.

The total RAM requirement is about 140 bytes multiplied by the maximum number of segments. Setting the limit at 100000 segments would require 14 Mb of RAM per ray. For this reason, do not needlessly set the maximum to very large number! The limit that OpticStudio will allow is 2 million ray segments.

Note that with even a modest number of segments, OpticStudio can still trace millions of rays. The maximum number of segments is only a limit on how many segments are allows in one single ray. Most OpticStudio features only use one ray at a time, so the total RAM requirement is driven by the maximum number of segments rather than the number of rays traced.

## Why the segment and intersection errors should NOT be ignored

Given what we now know about segments and intersections, there is one other important piece of information to fully understand why the segment and intersection error messages should not be ignored. This piece of information has to do with the order in which OpticStudio traces Non-Sequential rays.

A ray will be trace until it reaches one of the limits/maximum controls specified under the Non-Sequential tab of the System Explorer. However, if not enough segments or intersections are allocated to trace all of the possible ray paths, OpticStudio will issue an error and terminate the trace (when Ignore Errors is turned off). To reiterate, these error messages should NOT be ignored.

Here’s why. Let’s break down a ray trace into a family tree, much like the analogy which was made earlier in this article. After each intersection, let’s assume the energy is equally divided into two rays, a "reflected" and a transmitted ray.  To demonstrate the hierarchy, the “reflected” rays from each intersection are drawn in red, while the transmitted rays are drawn in black.

Each intersection is drawn as a blue bar (NOTE:  Each bar represents 1 intersection in this model.  The blue bars do NOT represent a volumetric plate with both front and back faces). Given this sequence of ray tracing, upon each intersection, the total number of segments is equal to 2^(n+1) – 1, so after the third intersection, we have a total of 15 segments, or 2^4 – 1 = 15.

Note the numbering scheme for the segments in the diagram above. Ray segment 1 is incident on the first interface, and splits into 2 rays:  a transmitted and reflected ray. OpticStudio continues to trace the transmitted energy, until that ray reaches its minimum relative ray intensity, for example. So, OpticStudio continually tracks the transmitted portion of the energy (as in segments 2, 3, and 4), then returns back to the last point in which splitting occurred. The transmitted energy of that ray is then traced until its minimum is met, and so on.

As you can see from this numbering scheme, if not enough segments are initially defined, we may be neglecting rays which carry a significant portion of our initial energy. As in the example given, if a maximum of 8 segments were defined, OpticStudio could not even trace ray segment number 9, which has half of our initial energy. If this were the case, how could we trust the total energy calculation collected by our detector?

So why don’t we determine which segment to trace first by its intensity? Well, to do this, we would have to store a large list of data in memory, which would significantly slow down the ray trace. So, OpticStudio traces rays in a recursive loop, which is proven to significantly increase ray tracing speed!

The bottom line is, never ignore the segment and/or intersection errors that OpticStudio issues. Always make sure that there are enough segments and intersections allocated to trace all possible ray paths. However, don’t arbitrarily set the values to high. Instead, only increase these values as needed.

KA-01562