Tutorial - How To Use Script Functions
Tutorial - How To Use Script Functions
1
How to Use Script Functions
PSIM provides script functions that can be used in the Script Tool to perform calculation, run
simulation, and plot graphs. The Script Tool is in the Utilities menu in PSIM.
The script tool supports the following functions:
• Mathematical operators
• Computational functions
• Formula functions
• Control functions
• Array functions
• String functions
• Complex numbers
• File functions
• Graph function
• Simulation function
This tutorial describes how the functions are defined and used.
Below is an example of a script. It shows the current loop design of a buck converter.
// Buck converter - current loop design
PI = 3.14159;
// Parameters
Vin = 250;
L1 = 200u;
C1 = 245u;
R1 = 0.6;
Ksen_i = 1/165; // current sensor gain
Ksen_v = 1/100; // voltage sensor gain
// PWM gain: Kpwm = Vin/Vcarrier, with Vcarrier=1
Kpwm = Vin;
fsw = 20k;
// Current PI controller
Ki_pi = 1.24292;
Ti_pi = 142.139u;
// Plant: Hi(s) = iL / Vos
Hi = formula(1/(s*L1 + R1/(s*R1*C1+1))); // define Hi(s) as a formula
// Current PI: Gi = Ki * (1+sTi) / (sTi)
Gi = formula(Ki_pi*(1+s*Ti_pi)/(s*Ti_pi)) // define Gi(s) as a formula
// Current loop transfer function Ti_loop
Ti_loop = Hi*Gi*Ksen_i*Kpwm // perform formula calculation
// Bode plot
wmin = 100 *2*PI;
wmax = 50e3 *2*PI;
Freq_rad = ArrayLog(wmin, wmax); // define frequency array (rad/sec) from wmin to wmax, in log
2
How to Use Script Functions
Mathematical Operators
The following mathematical operators are supported:
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo operator that returns the remainder after division (e.g. 5 % 2 = 1)
^ To the power of (i.e. x^y = x to the power of y)
** To the power of (i.e. x**y = x to the power of y)
= Equal
== Conditional equal
!= Not equal
!= Not equal
> Greater than
3
How to Use Script Functions
Computational Functions
The following computational functions are supported:
sin(x) Sine (x in rad.)
cos(x) Cosine (x in rad.)
tan(x) Tangent (x in rad.)
asin(x) Arcsine
arcsin(x) Arcsine
acos(x) Arccosine
arccos(x) Arccosine
atan(x) Arctangent
arctan(x) Arctangent
atan2(y,x) Arctangent with x and y defined
sinh(x) Hyperbolic sine
cosh(x) Hyperbolic cosine
tanh(x) Hyperbolic tangent
pow(x,y) x to the power of y
pwr(x,y) Absolute value of x to the power of y, i.e. abs(x)^y
sqr(x) Square of x, that is, x^2
sqrt(x) Square root
hypot(x1,x2,x3,…) Square root of x1 squared plus x2 squared, plus x3 squared, etc., i.e.
sqrt(x1^2 + x2^2 + x3^2 + …)
hypot(x_array) The input is an array, and it returns the square root of the sum of the array
cells squared, i.e. sqrt(x_array[0]^2 + x_array[1]^2 + x_array[2]^2 + …)
exp(x) Exponential of x (with base e, that is, e^x)
ln(x) Natural logarithm of x (with base e)
log(x) Natural logarithm of x (with base e)
log10(x) Common logarithm of x (with base 10)
abs(x) Absolute value
sign(x) Sign function that returns 1 if x > 0; -1 if x < 0; and 0 if x = 0
ceil(x) Function that returns the integer larger than x
floor(x) Function that returns the integer smaller than x
min(x1,x2,x3,…) Minimum value of x1, x2, x3, etc. (no limit on the number of inputs)
min(x_array) The input is an array, and it returns the minimum value of the array cells
max(x1,x2,x3…) Maximum value of x1, x2, x3, etc. (no limit on the number of inputs)
max(x_array) The input is an array, and it returns the maximum value of the array cells
4
How to Use Script Functions
Formula Functions
Formula functions define a formula and evaluate a formula. The functions are described below.
Formula(math expression)
This function defines a formula as a function of variables. Values of the variables have not been
defined. For example,
f1 = Formula(a+b*c); // f1=a+b*c where a, b, and c are variables with unknown values
iif (condition, value1, value2) Inline if statement (note it is “iif”, not “if”)
Error (“text %f, %f”, var1, var2) Error statement. Up to 5 variables are supported.
Warning (“text %f, %f”, var1, var2) Warning statement. Up to 5 variables are supported.
Return (var1) Return the value var1, and end the execution.
Example:
Below is an example of the if and while statements:
5
How to Use Script Functions
Array Functions
An array is defined using a pair of curly brackets, with items separately by comma. Below are some
examples:
Months = {"Jan", "Feb" , "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
a = {1, 2, 3, 5, 7, 11};
b = {10.5, 8.3, 33.59};
The value of a cell in an array is accessed by using the square bracket [ ]. For example, in the
examples above, Months[0] is “Jan”, a[1] is 2, and b[1] is 8.3.
A cell value can be changed in the same way. For example, a[1] = 5;
A multi-dimensional array can be defined by nesting brackets inside each other. For example:
M = {{1,2}, {3,4}, {5,6}};
where M[0][1] is 2. It can be changed to 50 by the statement: M[0][1] = 50;
Some examples are shown below.
a = {1, 2, 3, 4, 5}; // original array
a[1] = 10; // This change a[1] to 10 also.
c = a; // A new array is created. Changes to c does not affect a.
c[2] = 50; // a[2] is not affected.
Supported array functions are described below.
Array(size)
Array(size, InitialValue)
Array(FirstValue, LastValue, increment)
6
How to Use Script Functions
The Array function defines a one dimensional array of size ‘size’. It can initialize all cell values to
‘InitialValue’. It is also possible to initialize the array to a set of numbers starting at ‘FirstValue’ and
increasing by ‘increment’ at each cell until it reaches ‘LastValue’.
For example, the following example defines an array of 10 cells and initializes them with a while
loop. The result array is: x1 = {15,17,19,21,23,25,27,29,31,33}
x1 = Array(10);
i = 0;
while(i < 10)
{
x1[i] = 15 + (2 * i);
i++;
}
In another example, x2 = Array(15, 33, 2) defines an array of 10 cells and initializes them with the
Array function. The result array is: x2 = {15,17,19,21,23,25,27,29,31,33}
This function also works for decreasing numbers. For example, x = Array(9, 3, 1.5) defines the array
x = {9, 7.5, 6, 4.5, 3}.
The following example defines an array of 10 cells and initializes it to “Row “. It then adds numbers
in a while loop. The result array is : L = {Row 1,Row 2,Row 3,Row 4,Row 5,Row 6,Row 7,Row 8,Row
9,Row 10}.
L = Array(10, "Row ");
i = 0;
while(i < 10)
{
L[i] = L[i] + string(i+1);
i++;
}
ArrayLog(FirstValue, LastValue)
This function defines a one dimensional array and initializes it to set of numbers starting at
‘FirstValue’ and ending at ‘LastValue’. Numbers are increased in a manner suitable for use with
exponential calculations.
For example, X = ArrayLog(8,300) defines X as: X = {8,9,10,20,30,40,50,60,70,80,90,100,200,300},
and Y = ArrayLog(10000, 500) defines Y as: Y =
{10000,9000,8000,7000,6000,5000,4000,3000,2000,1000,900,800,700,600,500}
SizeOf(Var1)
If ‘Var1’ is a one dimensional array, this function returns the size of the array. If ‘Var1’ is a string,
this function returns the size of the string. If ‘Var1’ is a curve, it returns the numbers of rows.
7
How to Use Script Functions
Copy(arr)
Copy(arr, StartIndex, Length)
The Copy function returns a full or partial copy of an array.
The parameters are:
arr : Input array.
StartIndex: Zero based index of location of array cell to start the copy from.
Length: Length of returned array
The function Copy(arr) returns a full copy of an array. The function Copy(arr, StartIndex, Length)
returns a partial copy of the array starting from ‘StartIndex’ and continuing ‘Length’ number of
cells.
If 'StartIndex' is -1, then it copies the last 'Length' cells. If 'Length' is -1, then it copies all cells
after 'StartIndex' (including 'StartIndex') to the end of the input array
Array arithmetic
When two arrays of different sizes are used in an arithmetic operation, larger array is shrunk to
the size of smaller array. The resulting array’s size is equal to the size of the smaller array.
Below is an example:
arr1 = {10, 100, 1000, 10000};
arr2 = {-1, -2};
arrA = arr1 + arr2; // result is {9,98}
arrB = arr1 - arr2; // result is {11,102}
arrC = arr1 * arr2; // result is {-10,-200}
arrD = arr1 / arr2; // result is {-10,-50}
arrE = arr2 / arr1; // result is {-0.1,-0.02}
arrF = log10(arr1); // result is {1,2,3,4}
arrG = 6 * arr2; // result is {-6,-12}
arrH = 6 / arr2; // result is {-6,-3}
8
How to Use Script Functions
String Functions
A string is defined by characters between two quote " signs, for example, "This is a string".
Strings can be placed in variables, as shown below.
Var1 = "Apple";
Var2 = "Orange";
The plus sign + is used for combining strings. Other arithmetic signs - * / are not valid when
dealing with strings. For example,
Var3 = Var1 + Var2; // Content of Var3 is: "AppleOrange"
A special object, _CRLF, is defined as carriage return line feed, and can be used in the string
operations.
Functions related to strings are described below.
String(val)
The function converts a numerical value to a string. For example,
a = String(12.7m); // a is "0.0127"
b = String( 15 + 6 ); // b is "21"
arr = {1, 5, 9};
c = String(arr); // c is "{1,5,9}"
Value(str)
The function converts a string value to a number.
When reading numbers from a text file, it is usually read as strings. These strings must be
converted to numbers using the ‘Value()’ function before they are used in arithmetic operations.
For example,
a = "12";
b = "5";
r = a + b; // r is the string “125”. “12” and “5” are concatenated and result in “125”.
s = Value(a) + Value(b); // s is the number 17. “12” and “5” are first converted to numbers then
added together.
9
How to Use Script Functions
The function returns the zero based index of the location of the first character of the matched
substring, or -1 if it does not find anything. Examples are:
Var3 = "AppleOrange"
iA = Find(Var3, "A"); // iA is 0 in "AppleOrange", 'A' is the first character
ia = Find(Var3, "a"); // ia is 7 pointing to 'a' in Orange
ie1 = Find(Var3, "e"); // ie1 is 4
ie2 = Find(Var3, "e", ie1+1); // ie2 is 10. Find function starts the search at the character after the
first 'e'
SizeOf(str)
The function returns the size of the string.
10
How to Use Script Functions
Split(Input_string, Separator_characters)
This function splits a string into pieces using the separator characters. When any of the characters
in the string is encountered, the input string is split into another piece. The return value is an
array of strings.
For example,
Split("22,33,44,55,66", ","); //return value is array of strings: {"22"," 33"," 44"," 55"," 66"}
Complex Numbers
The following complex number functions are provided:
Complex(a, b)
This function returns a complex number. ‘a’ is the real part, ‘b’ is the imaginary part. To create an
array of complex numbers, each parameter can be an array. For example,
c1 = complex(5, 3); // c1 is: 5 + 3j
arr1 = complex(Array(0,6, 2), Array(-20, -14, 2)); // arr1 is: { 0–20j, 2-18j, 4-16j, 6-14j }
w1 = complex(5, {1, -5, -7, 22}); // arr1 is: { 5+j, 5-5j, 5-7j, 5+22j }
Polar(mag, ang)
This function returns a complex number. ‘mag’ is the magnitude and ‘ang’ is the angle in rad. To
create an array of complex numbers, each parameter can be an array. For example,
c1 = Polar(10, 1.4); // c1 is: 9.85 + 1.7j
arr1 = Polar({10, 20, 30}, {0, 0.7, 1.4});
Real(c1)
This function returns the real part of a complex number.
Imag(c1)
This function returns the imaginary part of a complex number.
Abs(c1)
This function returns the magnitude of a complex number defined as sqrt(a^2 + b^2)
11
How to Use Script Functions
Angle(c1)
This function returns the angle of a complex number in rad, and it is defined as atan(b, a).
File Functions
The following file functions are provided:
FileRead(FilePath)
The function reads a text file and place the content in a string. For example,
str = FileRead(“C:\Powersim\MyText.txt”); // str is a string containing the file content
FileReadLines(FilePath)
The function reads a text file and place the content in an array of strings. Each array cell contains
one line of text. For example,
arr = FileReadLines(“C:\Powersim\MyText.txt”); // arr is an array of strings.
[Mosfet]
Forward Voltage = 0.7V
12
How to Use Script Functions
GetLocal(Name)
The function returns a value that is specific to the local computer.
The parameter Name can have the following options:
Name Return value
PSIMPATH Full PSIM folder path, for example, "C:\Powersim\PSIM11.0.2\"
PARAMPATH Full folder path of the parameter file, for example, "C:\Data\"
SCHPATH Full folder path of the schematic file, for example, "C:\Schematic files\".
If this value is used in a Parameter tool window, it returns an empty string.
SCHNAME File name of the schematic file including extension, for example,
"file1.psimsch".
If this value is used in a Parameter tool window, it returns an empty string.
Graph Functions
The following curve data type and file functions are provided:
MakeCurve(CurveName, NumberOfRows)
MakeCurve(CurveName, ArrayOfValues)
MakeCurve(CurveName, NumberOfRows, Formula,
VarName1, ArrayOfValues,
VarName2, StartValue, Increment
…)
The function creates a new curve data.
Function parameters are:
13
How to Use Script Functions
GraphRead(FilePath)
The function reads a SIMVIEW file with .smv or .txt extension and places the content in an array of
curves. The return value is an array of curves. The first cell is the X-Axis and subsequent cells are
the rest of curves in file. For example,
arr = GraphRead("C:\Powersim\MyGrpah.smv");
14
How to Use Script Functions
GetCurve(ArrayOfCurves, CurveName)
The function searches for CurveName in ArrayOfCurves and returns the curve. For example,
str = GetCurve(graph, "V1");
GetCurveName(Curve1)
The function returns the name of a curve. For example,
str = GetCurveName(Curve1);
SetCurveName(Curve1, NewName)
The function sets a new name for the curve. For example,
SetCurveName(Curve1, "XAmplitude");
Simulation Functions
The following simulation functions are provided:
15
How to Use Script Functions
The parameter “ReturnGraph” must be a variable that is set to an array of curves. In case of ac
sweep simulation, ‘ReturnGraph’ will be the ac sweep graph.
SimulationOptions is a string, and it has to be enclosed in "". Valid options are:
TotalTime
TimeStep
PrintTime
PrintStep
SaveFlag
LoadFlag
In addition, normal variables can be added here too. For example,
Simulate("C:\Powersim\files\active filter.psimsch", "C:\Powersim\Graphs\active filter.txt",
"TotalTime=100m; TimeStep=10u; PrintTime=10m; PrintStep=2; SaveFlag=1; LoadFlag=0; R1=11K;",
graph1);
In addition to the variable list, any variables that come before the Simulate function are passed to
the schematic.
16