Scripted (User-defined) Optimization Variable
FRED includes a list of pre-defined variables types for optimization, but there can be times when the parameter we wish to vary during an optimization is not listed in the table. In these cases we must create a scripted variable.
Example
In this example a simple case will be given of creating a scripted variable for a mirror semi-diameter in order to optimize to maximize the power on an analysis surface.
Download the FRED file here: MirrorApertureOptimization.frd
The initial file starting point consists of a source with a greater extent than the semi aperture of the mirror, leading to lower power at the analysis plane from the rays that miss the mirror:
![](http://d33v4339jhl8k0.cloudfront.net/docs/assets/5b19b3b60428632c466aab9b/images/66869eb6e1989867dcefec97/file-y7Qt1hUPbH.png)
From the Optimize... Define/Edit menu we can see there is already a variable defined for the Mirror Aperture which is based on a user-defined script variable.
![](http://d33v4339jhl8k0.cloudfront.net/docs/assets/5b19b3b60428632c466aab9b/images/6686a38ddd0f8c60bb3fdf06/file-9OIxykRNql.png)
To create our own script variables all we need to do is add a row and choose the User-defined Script as the type. To set or see the script, right-click on the row and choose the option to "Edit User-defined Script Variable...". All script variables have the same preset global variables:
g_entity - stores the node number of the entity the script is acting on, as set in the "Define Optimization", dialog. In this case the script is being called on Geometry.Mirror 1, and so will feature the node number of that entity.
g_setvar - Boolean flag that defines whether the script is being executed in the context of trying to retrieve the existing value (g_setvar = false) or set a new value (g_setvar = true) for the variable.
g_success - Boolean flag for error handling and making sure the code has executed completely. Generally we set to False at the top of the subroutine, and then set it to True just before we reach the end of the subroutine.
g_var - the actual variable.
In terms of our script to vary the mirror aperture, it looks like so:
Because we're working with a mirror we need to use the T_MIRROR and T_ENTITY structures to be able to get/set data, these variables are defined on lines 15-16. On line 19 a test is conducted to check the entity is indeed a mirror, before retrieving the mirror information on line 22.
Following this the next block of code on lines 25-32 is based on the value of g_setvar.
Where g_setvar = True then the context is that FRED is trying to set a new value for the variable (as during optimization) and so we need to set the semi-diameter of the mirror to the next value the optimization routine wants to try (which is stored in g_var), this is done on lines 26-27, where the semiApe member of the T_MIRROR structure is set to the value of g_var and then the mirror set to use the updated structure. Note that we do not need an update command, the scripted variable will automatically update the system with the new variable(s) at the end of running the user-defined script(s).
Where g_setvar = False then the context is that FRED is trying to retrieve the existing value from the mirror and report it (as when we call the r-click menu to "Retrieve Current Value(s) from Doc")
![](http://d33v4339jhl8k0.cloudfront.net/docs/assets/5b19b3b60428632c466aab9b/images/6686b08c5173914f806c2990/file-cASHsWj6T9.png)
Running the optimization from Optimize... Optimize, results in a short optimization process where the aperture of the mirror is indeed increased to maximize the power on the analysis surface:
![](http://d33v4339jhl8k0.cloudfront.net/docs/assets/5b19b3b60428632c466aab9b/images/6686b177d593d719515cb5dc/file-fCGIiRX65A.png)