This article explains how to utilize a user-defined macro to calculate and return a value to the Merit Function Editor for optimization.
This article is also available in Japanese.
Authored By: Dan Hill
Introduction
Even though there are 300+ built-in optimization operands, there are cases in which you wish to return and optimize a value that the existing operands do not calculate. For this reason, Zemax supports the flexibility of a user defined program for defining the data computed by an operand in the Merit Function Editor (MFE).
This article is also available in Japanese.
This piece of data may be something which is calculated independently from Zemax, or it may be a value which Zemax already calculates and reports (via various functions, for example) but not made available through an optimization operand.
In either case, the two methods for defining the data to be computed include the following:
1. Through the use of a ZPL macro
2. Through the use of an externally defined and compiled program
ZPL macros are simple to program, fast to execute, well integrated into Zemax, and require little programming experience. In addition, the ZPLM optimization operand may be used to call a ZPL macro from with the Merit Function.
In this article, we will illustrate how to utilize a macro to calculate and return a value to the Merit Function, which may be targeted for optimization via the ZPLM operand. Full details of how to use the ZPLM operand are discussed in the “User defined operands” section of Chapter 14 in the Zemax User’s Guide
Attached to the last page of this article is the Cooke Triplet example file (also available in your {Zemax root}SamplesSequentialObjectives folder) which we will use to demonstrate the flexibility the ZPLM operand provides. Please download this file and open it within Zemax.
 | FILE: Cooke 40 degree field.ZMX |
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.
/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.
Do User Defined Operands Slow Down Execution of the Merit Function?
You might find yourself asking how significant the computation times will be affected when you have user defined operands in the Merit Function Editor. This really depends upon the complexity of the computation performed within the macro, but in general the execution of a macro during optimization is extremely fast. To demonstrate, we can optimize the Cooke Triplet twice: once using the ZPLM operand and our constructed macro, and once using the pre-programmed operand, WFNO.
For the first case, Target the ZPLM operand to be 5, and give it a Weight of 1. You may leave the WFNO operand in place, but leave its Weight at zero. Construct a Default Merit Function using the following settings:

Run DLS optimization. The approximate execution time is 4 seconds:

Undo the changes made by the optimization (press F3 to do so). Change the Weight of the ZPLM operand to 0, set the Target of the WFNO operand to 5 and its Weight to 1. Re-optimize.

The execution time with the native operand is roughly 3.6 seconds. The difference between the two is roughly one-half-second! As you can see, Zemax can still optimize very efficiently, even in cases which call user-defined macros or an externally compiled program.
Summary and References
There are times when the current built-in optimization operands might not correspond to the data you wish to return/optimize for. With the flexibility of the ZPLM and User Defined operands, data can be calculated or extracted from a user-defined macro or program and returned to the Merit Function Editor. Macros, the simpler of the two solutions, are well integrated within Zemax and require little programming experience.
References
Zemax Optical Design Program User’s Guide, Zemax Development Corporation