Skip to main content
Solved

Batch Ray Trace and CreateDirectUnpol returning intensities always equal to 1 in system with black boxes


alexander_dumont

Hi everyone! Long time lurker, first time posting. I have been using Zemax for the past couple of months, and have been getting familiar with the backend, C# ZOS-API. As I’ve been getting more familiar with the ZOS-API, I’ve tried to more and more exciting things, which eventually lead me to use the batch ray tracing function. I can do this successfully and actually get quite a good representation of my input picture at the output image side, using CreateDirectUnpol and AddRays. However, I am trying to get a feel for intensity on the imaging plane, and the only output I get when using ReadNextResult is intensity = 1, even though my system has various elements with different non-100% coatings on them. However, I am using black-box lenses from Thorlabs, and I’m wondering if this causes Zemax not to give out the intensity values? If not, what could I be doing wrong?

Attached is the relevant code I use which gives me 1 for intensity output:


                // Set up Batch Ray Trace
                ZOSAPI.Tools.RayTrace.IBatchRayTrace raytrace = TheSystem.Tools.OpenBatchRayTrace();
                int nsur = TheSystem.LDE.NumberOfSurfaces;
                var dir_x = linspace(-.1f, .1f, 10);
                var dir_y = linspace(-.1f, .1f, 10);
                int max_rays = n * n * dir_x.Length * dir_y.Length;
                //count up all positions and  angles to lauch from
                var c = 0;
                for (int i = 0; i < xl.Length; i++)
                {
                    for (int j = 0; j < yl.Length; j++)
                    {
                        if (listPixelsMask[i][j] > 0)
                        {
                            //then we have surface to launch from so proceed
                            for (int dx = 0; dx < dir_x.Length; dx++)
                            {
                                for (int dy = 0; dy < dir_y.Length; dy++)
                                {
                                    c++;
                                }
                            }
                        }
                    }
                }
                Console.WriteLine("# of rays simulated in batch trace: " + c);

                var directUnPolData = raytrace.CreateDirectUnpol(c, ZOSAPI.Tools.RayTrace.RaysType.Real, 0, nsur-1);

                List<double[]> inputParameters = new List<double[]>();

                for (int i = 0; i < xl.Length; i++)
                {
                    for(int j = 0;j < yl.Length; j++)
                    {
                        if (listPixelsMask[i][j] > 0)
                        {
                            //then we have surface to launch from so proceed
                            for (int dx = 0; dx < dir_x.Length; dx++)
                            {
                                for (int dy = 0; dy < dir_y.Length; dy++)
                                {
                                    //does ray hit lens, if not then dont consider it 
                                    var x = xl[i];
                                    var y = yl[j];
                                    //we have an angle relative to the vertical position
                                    var vector_direction = new double[] { dir_x[dx], dir_y[dy] , 1 };
                                    vector_direction = vector_direction.Select(item => item*item).ToArray();
                                    var vector_sum = vector_direction.Sum();
                                    vector_direction = vector_direction.Select(item=>item / vector_sum).ToArray();
                                    //collect input data
                                    inputParameters.Add(new double[] { xl[i], yl[j], 0, vector_direction[0], vector_direction[1], vector_direction[2] });
                                    //add to batch ray trace 
                                    directUnPolData.AddRay(0, xl[i], yl[j], 0, vector_direction[0], vector_direction[1], vector_direction[2]);
                                }
                            }
                        }
                    }
                }
                //save input parameters to file 
                var inputFile = @"C:\\Users\\AlexanderDumont\\OneDrive - Cytoveris Inc\\Documents\\R&D\\Reference Checks Analysis\\input.txt";
                using (var writer = new StreamWriter(inputFile))
                {
                    foreach(var item in inputParameters)
                    {
                        foreach(var col in item)
                        {
                            writer.Write(col + ",");
                        }
                        writer.WriteLine();
                    }
                }
                raytrace.RunAndWaitForCompletion();
                // Read batch raytrace and save results
                directUnPolData.StartReadingResults();
                int rayNumber, ErrorCode, vignetteCode;
                double double_X, double_Y, double_Z, double_L, double_M, double_N, double_L2, double_M2, double_N2, OPD, Intensity;
                bool success;

                success = directUnPolData.ReadNextResult(out rayNumber, out ErrorCode, out vignetteCode, out double_X, out double_Y, out double_Z, out double_L, out double_M, out double_N, out double_L2, out double_M2, out double_N2, out Intensity);
                ///save output to file as well 
                List<double[]> outputParameters = new List<double[]>();
                outputParameters.Add(new double[] {rayNumber, double_X,double_Y,double_Z,double_L,double_M,double_N, Intensity });
                while (success)
                {
                    success = directUnPolData.ReadNextResult(out rayNumber, out ErrorCode, out vignetteCode, out double_X, out double_Y, out double_Z, out double_L, out double_M, out double_N, out double_L2, out double_M2, out double_N2, out Intensity);
                    outputParameters.Add(new double[] { rayNumber, double_X, double_Y, double_Z, double_L, double_M, double_N, Intensity });
                }
                var outputFile = @"C:\\Users\\AlexanderDumont\\OneDrive - Cytoveris Inc\\Documents\\R&D\\Reference Checks Analysis\\output_" + z + ".txt";
                using (var writer = new StreamWriter(outputFile))
                {
                    foreach (var item in outputParameters)
                    {
                        foreach (var col in item)
                        {
                            writer.Write(col + ",");
                        }
                        writer.WriteLine();
                    }
                }

 

Best answer by MichaelH

Hey Alexander, 

Welcome to the community!  

Unpolarized ray tracing will always have an intensity equal to 1...in order to have a ray trace consider Fresnel reflections at surfaces and Beer’s law bulk absorption, you need to run a polarized ray trace:

  • CreateNormPol
  • CreateDirectPol
  • SingleRayNormPol
  • SingleRayNormPolFull
  • SingleDirectPol

The intensity output is included in the ReadNextResult() method for Unpol interfaces is simply due to internally reusing code (leads to fewer bugs than maintaining 2 separate code paths).  

View original
Did this topic help you find an answer to your question?

2 replies

MichaelH
Ansys Staff
Forum|alt.badge.img+2
  • Ansys Staff
  • 342 replies
  • Answer
  • March 20, 2023

Hey Alexander, 

Welcome to the community!  

Unpolarized ray tracing will always have an intensity equal to 1...in order to have a ray trace consider Fresnel reflections at surfaces and Beer’s law bulk absorption, you need to run a polarized ray trace:

  • CreateNormPol
  • CreateDirectPol
  • SingleRayNormPol
  • SingleRayNormPolFull
  • SingleDirectPol

The intensity output is included in the ReadNextResult() method for Unpol interfaces is simply due to internally reusing code (leads to fewer bugs than maintaining 2 separate code paths).  


Forum|alt.badge.img

How is the polarisation defined if one assumes unpolarised light? 

In the GUI transmission calculator there is a check-box “unpolarised”. What does that do?

I was planning on doing random polarisation states for many rays, but the batch ray trace call only allows for one polarisation state.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings