Solved

Quicker way to set parameters in the LDE

  • 25 October 2022
  • 4 replies
  • 268 views

Badge

Hi,

I am using the ZOS-API interface with Python (pythonnet 2.5.2.) to set the coefficients for a Zernike Standard Sag surface, one at a time. It takes several seconds!! Is there any faster way, or a way to set all parameters at once?

I’d like to run a Monte Carlo analysis with thousands of istances of zernike surfaces, so a faster way would be really beneficial.

%timeit M1_surf.GetCellAt(30).DoubleValue = 6.46449601150208e-06
386 ms ± 28.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

%timeit M1_surf.SurfaceData.SetNthZernikeCoefficient(4, 6.46449601150208e-06)
354 ms ± 27.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

 

As a comparison, reading a value is three orders of magnitude faster:

%timeit M1_surf.GetCellAt(30).DoubleValue
513 µs ± 78.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

 

The LDE is the only open window, so there are no analyses being updated.

 

Any help would be very much appreciated!

 

Paolo

icon

Best answer by MichaelH 25 October 2022, 23:23

View original

4 replies

Userlevel 6
Badge +2

Hi Paolo,

Do you have LensUpdateMode set to None (and the UI changes disabled):

from System import Enum
TheSystem.UpdateMode = Enum.Parse(ZOSAPI.LensUpdateMode, "None")
TheApplication.ShowChangesInUI = False

Beyond these small changes, there is no way of speeding this up, at least based on how OpticStudio and the ZOS-API are written. 

For almost every set command in OpticStudio (either via the GUI or the API), a system level update needs to be performed; very low-level operations, mostly involving paraxial ray tracing, such where the Stop is located or the system F/#, are updated with each change to the system.  Although these low-level operations are extremely fast, there is computation overhead which will add up inside a for loop.

For any get command, there is no low-level update to the system so these commands are almost instantaneous.

Outside of Zemax rewriting the core code or adding complex logic to the ZOS-API (which probably won’t happen...the testing alone from a change like this would take months to verify), the set will always be longer than the get.  Zemax might be able to implement a case-by-case batch update for something like Zernike Coefficients, but this is probably very low on their roadmap.

The only work-around for this which is extremely hacky (READ: try at your own risk) would be to ensure your MC files are saved as the text-based ZMX format, open the raw ZMX file outside of the ZOS-API, modify the coefficients with simple Python write commands, and then load the modified ZMX in OpticStudio.  There is no guarantee that the ZMX format will stay consistent, that Zemax won’t implement internal checks to future versions of OpticStudio to prevent the ability to load a modified ZMX files, or even that the ZMX format will be around in the future.  So this would potentially be a lot of effort which might not pay off in the end.

Badge

Thank you Michael for the quick answer! It appears to do the trick:

%timeit M1_surf.SurfaceData.SetNthZernikeCoefficient(4, 6.46449601150208e-06)
12.7 ms ± 241 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

I get an even greater (4x) speedup by disabling the undo function from the Project Preferences menu: any way to do this programmatically?

 

Thanks again,

 

Paolo

Userlevel 6
Badge +2

Hi Paolo,

Unfortunately, there is not a way to programmatically turn on/off the undo function.  The only related options in the Project Preferences via the ZOS-API would be saving session file but since the Undo only saves the ZMX/ZOS and not the ZDA, this setting will have no impact.  

A workaround would be to save a Preferences CFG file with the undo turned off before running your API, use the TheConnection.PreferencesFile to load the CFG with the undo turned off, and then to run your script.  This is similar to using ModifySettings() with a CFG for analysis which do not have their individual settings hooked up to the API.  The following articles talks about how to use a use Preferences CFG file:

Get and Set Folders in Project Preferences of OpticStudio from ZOS API | Zemax Community

Badge

Thank you!

Reply