How can I access the results of a nested parametersweep in the custom figure of merit script (for optimization)?



Hi All,

I have edited this post several times, but I think this is the clearest way to state my problem.

I am attempting to use a nested sweep underneath an optimization. I would like to define my own custom figure of merit that requires data from all (two) elements of the nested sweep. Each individual element of the sweep outputs an array of 5000 elements.

As a result, I should have access to two arrays of 5000 elements in my figure of merit script, or a 5000x2 dimensional array, however I only have access to one 5000x1 dimensional array.

How can I get the data from both elements of the sweep to calculate my figure of merit? I have seen an example here: Optimization of geometrical parameters for two possible states with different material properties however this appears to calculate a single number ® and then return an array containing both Rs such that R(1) and R(2) access the two values.

I do not see anything fundamentally different in my code from this example, other than the fact that the elements of the sweep return an array and not a single number. What could be going wrong? Thank you.

Using a nested parameter sweep in an optimization

Hi @cflower,

Can you eventually share your simulation file, so we can have a look at the sweep setup? Normally, the dimension of the result shouldn’t be a problem, as long as the FOM you extract is a number, as my colleague mentioned in the post you are referring to:


Hi gbaethge,

Thanks for the reply! Yes, I can post some screen shots right now and possibly the full file later.

Regarding the optimization, the figure of merit that I calculate is indeed a number. However your comment made me wonder: does the result of the sweep also need to be a number in order to be inherited properly? I have been sending an array from the sweep into the optimization.

And of course, what is not shown here is that I have a custom figure of merit script, which (should) calculate a single number for FOM but makes use of the spectrum arrays.

Here are some images to clarify:

Note: spectrum is an array which for a single element of the sweep would be 5000x1. Since the sweep is 2 elements, I expect some sort of 5000x2 array. f is less important and not the focus here, since it doesn’t change (it was just also convenient to import it.)


I think the problem might be in the result dimension. Something you can try, is extracting a result from the spectrum in each simulation, and use that result in the optimization and the nested sweep.

To do so, you can use the “model” object, the root of the object tree. It is an analysis group, so you can edit it, and define the result in the “Analysis” tab:

You can define parameters, and a result. The result will be defined in the “Script” tab

Then, in your sweep, you just need to select this results, and calculate the FOM in the optimization.

Lastly, if you have Matlab, you can use it to drive the optimization via our Automation API. We have an example on our website, the optimization of a grating coupler.


Thanks for that, I actually tried this already but wasn’t aware that I could use setup variables in model in the analysis script without importing them, so I tried and failed several times to import them somehow ( using things like getnamed etc).

I see now that they’re already defined, so this solves my problem! For anyone reading this who is interested, I simply modified the model analysis group to have a few conditional statements that checked the value of the variable I would be sweeping through (because I was doing slightly different analysis for different sweep values).


Glad I could help! On a side note, you can use model, but you can also add an analysis group (from the “Group” button):

You can retrieve the parameters in the group using the getnamed command, and you can set the parameters using the setnamed command. But in a group, you can only access the elements (structure, monitors, etc) within the group.