自学内容网 自学内容网

Lumerical Script Language

Lumerical Script Language

一、基本语法

1.1 运算符

All operators and commands can be found in Scripting Language – Ansys Optics

几个重点运算符
1. ? 用于打印/输出

? (print, display)

SyntaxDescription
?command;Displays the output of the command on the screen This function does not return any data.
?(5+5);
result: 
10 

?"file_"+"name_"+num2str(1);
file_name_1

?1:2001;
matrices of length greater than 2000 are not displayed to the output

?num2str(1:2001)
1
2
3
⋮
2000
2001
2. : 用于表示数组

: (array operator, colon)

syntaxDescription
x = 2 : 10;x will be an array of numbers that start at 2 and increase by 1 for each consecutive number. The last entry will be <= 10. x will equal 2,3,…,9,10.
x = 6 : -1.5 : 2;x will be the array were the first element is 6, and consecutive elements decrease by 1.5. All elements will be >=2. In this example, the array will be [6, 4.5, 3].
B=A(:, 2)B will be the array containing all the elements from the second dimension of A.
a=l:5;     # a will be vector (1, 2, 3, 4, 5)
?b=a(4:-1:2);  # b will be vector (4, 3, 2) 

a=[1,2,3;4,5,6]; 
?b=a(:, 1); # b will be vector (1, 4)
3. ^ 用于表示指数

^ (exponent, power, caret)

SyntaxDescription
y = x^3;x cubed.
x=1:3;
y=2:4;
?z=x^y;
result: 
1  
8  
81   

NOTE: In expression A^B, if B is complex, the phase of A is evaluated from − π -\pi π to π \pi π.

4. % 用于表示带空格的变量名
CommandDescription
%variable with space%To create a variable name that contains spaces, such as “variable with space”, put a percentage sign before and after the variable name.
%variable with space%=2;
?%variable with space%*3;
result: 
6  

%x span% = get("x span");
?%x span% * 1e6;  # x span in um
result:
4.8  
5. # 注释

# (comment, sharp, hashtag, pound)

SyntaxDescription
x=1; # set x to 1Anything after the # character is ignored.
?"this string contains a # symbol";
6. . 从数据集中获取参数和属性

. (dataset dot)

SyntaxDescription
result = A.result;Retrieves the parameter or attribute “result” from the existing dataset A. The result is a scalar matrix.
E = getresult("profile","E");
f = E.getparameter("f");  # the parameter f
Ex = E.getattribute("Ex"); # the x component of the electric field
E2 = E.getattribute("E2"); # the electric field intensity, note that this only works if E is a vector

E = getresult("profile","E");
f = E.f;  # the parameter f
Ex = E.Ex; # the x component of the electric field
E2 = E.E2; # the electric field intensity, note that this only works if E is a vector
7. almostequal 用于浮点数判断相等
SyntaxDescription
out = almostequal(A, B);Returns 1 if |A - B| is less than or equal to |A + B|/2*1e-15. Returns 0 otherwise.
out = almostequal(A, B, relative diff);Returns 1 if |A - B| is less than or equal to |A + B|/2 times relative diff. Returns 0 otherwise.
out = almostequal(A, B, relative diff, absolute diff);Returns 1 if |A - B| is less than or equal to |A + B|/2 times relative diff or if |A - B| is less than or equal to absolute diff. Returns 0 otherwise.
A=[1,2];
B=[1,1];
?almostequal(A,B);
result: 
1 0 
?almostequal(A,B,0.01,2);
result: 
1 1 
 
?almostequal(1,2,1);
result: 
1   
8. [] 用于创建数组

[] (specify matrix element, square bracket)

CommandDescription
x = [u11,…,u1N; u21,…,u2N; uM1,…,uMN]Create an N by M matrix. Columns are separated with semicolons. Elements in a row are separated with commas. The entries can either be scalars or matrices of compatible dimension.
?x=[1,2;3,4;5,6];
result: 
1 2 
3 4 
5 6
?x(1:3,1);
result: 
1 
3 
5   

a=matrix(2,2,2); 
a(1,1)=1;
a(2,2)=2;
b=a+1;
?c=[a,b];
result(i,j,1):
1 0 2 1 
0 2 1 3 
result(i,j,2):
0 0 1 1 
0 0 1 1 

1.2 分支

if
SyntaxDescription
if(x < 5) { y = x^2; }Simple if statement on one line.
if(x < 5) {y = x^2;}Multi-line if statement
if(x < 5) {y = x^2;} else {y = x^3;}If else statement.
if(x < 5) {if(x > 0) {y = x^2;}} else {y = x^3;}Nested if statement with else.
if(x < 5) {y = x^2;} else if ( x > 10 ) {y = 2*x;}Chained if else if statement.
clear;
a=1;
b=2;
d=3;
if((a==1)|(b==2)&(d==3)){
?"correct";
}
else{?"not correct";}
try
SyntaxDescription
try {Command1;Command2;…}Runs the block of commands. If an error occurs, the error message is displayed and the script continues.
try {Command1;Command2:…} catch(errMsg);Runs the block of commands. If an error occurs, the error message is stored in the variable “errMsg” and the script continues.
a=1;
try {
?C;
}
?a;
Error: prompt line 3: C is not a valid function or variable name
Result:
1

a=1;
try {
?C;
} catch(errMsg);
?a;
?errMsg;
Result:
1
Error: prompt line 3, C is not a valid function or variable name

1.3 循环

for
SyntaxDescription
for(x=1:100) { ?x; }Single argument for loop. The loop will be sequentially executed for each value of x.
for(x=1; x<= 100; x=x+1) {?x;}Three argument for loop.x=1 at the start of the loop. The loop continues while x <=100 and sets x=x+1 at each pass.
x=1;for(0; x<10; 0) {?x;x=x+1;}This is equivalent to a while loop that will execute
a=1:2:10;
for(x=a) { 
?x; 
} 

for(i=1:100) { 
for(j=1:100) { 
x(i,j) = i^2+j; 
?x; 
} 
} 

run;
mNames = splitstring(getresult,endl);

for (i=1:length(mNames)) {
if (haveresult(mNames{i},"E")) {
E=getresult(mNames{i},"E");   # get a result from that monitor
} else {
E = mNames{i} + " did not contain the specified data.";
}
savedata(mNames{i},E);     # save data to file
}

# implementation of a while loop in languages that support while loops
x=1;
while(x<10) {
?x;
x=x+1;
}
# equivalent implementation of a while loop in Lumerical script language
x=1;
for(0; x<10; 0) {
?x;
x=x+1;
}

原文地址:https://blog.csdn.net/xzy3150787/article/details/140372811

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!