## Photonics

Topics related to Lumerical and more

#### Lumerical scripting

• Color
Subscriber

Hello,

I have 144 variables that are numbered like a 12*12 matrix, i.e., z_1_1, z_1_12, z_12_1, z_12_12, and each of these variables have a number as their value, say z_12_10=0.9. I want to now put these variables in a matrix with the same format. So, element (1,1) of the matrix is z_1_1 while element (5,5) is z_5_5.  So I use two for loops that sweep over the 12 elements along each direction and try to attribiute each variable to an element. But that seems to not work, because I am using something like this:

for(ii=1:12)

{

for(jj=1:12)

{zmatrix(ii,jj)=str2num("z_"+num2str(ii)+"_"+num2str(jj));

}

}

The softare does not like this type of attribute. It seems that it is OK to have str2num("2") but if instead we have str2num("z_1_1), it does not work even though that will be a avariable with a number.

Thanks

• Guilin Sun
Ansys Employee

Zmatrix is a numerical matrix which cannot hold strings.

Your script is to covert "z_ii_jj" to numerical, but its numeric of "z_ii_jj".

I am not sure why you assign one single value to one single variable.

If your intention is to hold the string z_ii_jj, you can use "struct" : struct - Script command – Ansys Optics

• Color
Subscriber

The intention is to optimize all these parameters, using an optimizer. It is a 12*12 matrix and each element of the matrix is used as an x position for a rectangle in the GUI model. When I create the two for loops, I can sweep over these positions easily and assign the x position of the rectangles. But the problem is that these are defined as numeric variables and I cannot sweep over them by any means right now. So, I would have to manually create the matrix and then sweep over all variables which is extremely time-inefficient. So, I am looking for a way to do this properly.

• Guilin Sun
Ansys Employee

It is not very clear for me: you use 12by12 matrix to describe the x position of a rectangle, and want to sweep the value? or want to assign the x position by the matrix value from the 12 by 12 matix? you can index the matrix, and change the matrix value.

eg, to change the matrix value, Zmatrix(ii,jj)=3, or any other values;

to index the location of Zmatrix, it is the same thing: ii and jj are the indecies. Of course you should have values for the Zmatrix beforehand, or you can assign new value to it.

" When I create the two for loops, I can sweep over these positions easily and assign the x position of the rectangles"

Suppose you have already specified the x positions in the Zmatrix. that is, each value in the Zmatrix elements is the x position. then next you want to change the position of the rectangle, you can

for(ii=1:12){

for(jj=1:12){

setnamed("rectangle","x",Zmatrix(ii,jj));

.

.

.

}}

If this is not you want, please draw a diagram and elabrate more.

• Color
Subscriber

What I mean is that I have a 12*12 matrix that carries the positions of the elements and I want to optimize these positions. But the optimizer in Lumerical does not optimize a matrix, it only optimizes single values not matrices. So, I am forced to instead define 12*12=144 variables to be able to do my optimization. But now, I have 144 variables and so I have to manually assign them to my elements. I cannot do it in a more automated way, like a for loop. That is why I am trying to do all this str2num etc to make it possible to do this assignment in a more automated way.

• Guilin Sun
Ansys Employee

If the 12*12 position data is related to 12*12 elements/geometries, then each element has one such value in its geometry settings. You can directly optimize them;

If this 12*12 position data is to form a geometry, such as a polygon, then you will first need to define such polygon with parameteried location. each pair of a coordinate must be a variable and included in the vetex matrix.eg

vtx(i,j)=Zij; where Zij is the numerical locaiton.

Anyway, the 12*12 values are associated with the element positions and they should be defined as variables in order to sweep or optimize.