- Home
- Optimization
- How To Write Your Own Optimization Operand
- Home
- Programming ZEMAX
- ZPL
- How To Write Your Own Optimization Operand
How To Write Your Own Optimization Operand
- By Dan Hill
- Published 1 February 2006
- Optimization , ZPL
-
Rating:




Using the ZPLM Operand To Return the Value Calculated by the Macro
If you are unfamiliar with how to create, edit, save, and execute macros, you may want to refer to the following knowledgebase article prior to proceeding with this exercise.
http://www.zemax.com/kb/articles/40/1/How-to-Automate-Keyboard-and-Mouse-Actions-with-ZPL
Let’s assume that we would like to calculate and optimize for a specific Working F/#. Of course, ZEMAX contains a pre-programmed operand for this operation (WFNO), but for demonstration purposes, let’s assume that this operand was not available to us. As an alternative, we can calculate this value via a macro, return the calculated value to the Merit Function, and optimize for a desired target. The Working F/#, as defined in ZEMAX, is:

where n is the index of refraction of image space, and θ is the real marginal ray angle in image space. Given this relationship, we may trace a real marginal ray within a macro and calculate the Working F/#:
Note the use of the OPRETURN keyword. This keyword stores the resulting value for “X” in the global array position 0. It is this global array position number which is entered into the “Dat#” column of the ZPLM operand in the MFE.
The “Mac#” in the diagram above corresponds to the macro number. Each macro which is created for the purposes of being called by a ZPLM operand must follow a specific file name: “ZPLxx.ZPL.” The macro number may be any number between 0 and 99, so ZPL17.ZPL is a valid filename which may be executed by specifying a “Mac#” of 17 in appropriate cell in the MFE.
In the current example, name the macro ZPL10.ZPL and save it into the appropriate directory. In the Merit Function Editor, insert a ZPLM operand and insert a “Mac#” and “Dat#” of 10 and 0, respectively.

To execute the macro and return the extracted value, update the Merit Function Editor.
Our calculated and returned Working F/# is 4.9782. Let’s see how we did. Insert a WFNO operand (the built-in calculation of the Working F/#) and update the Merit Function Editor. Note that the values are identical!
This technique may be used to return multiple values in a single call to the macro (to learn more, please refer to the section of the User’s Guide referenced earlier), which may be used for optimization or simply for the purposes of reporting a value to the MFE for further analysis. ZPLM provides incredible speed and flexibility for returning and optimizing values which are not currently supported by one of the many built-in optimization operands.