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
• 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.
// In this script, the transfer functions of a boost converter with
// voltage feedback are derived and their Bode plots are generated.
//
Freq = ArrayLog(10, 10000); // define the frequency array from 10 Hz to 10 kHz, in log scale
R = 16;
C = 6000u;
pi = 3.141592653589793;
s = Complex(0, 2*pi*Freq); // define the Laplace operator
H = R / ((s * R * C) + 1); // define the plant transfer function
kpi = 3.8;
Tpi = 0.0095;
G = kpi * ( (s*Tpi)+1) / (s*Tpi); // define the voltage loop PI controller
D = 0.5;
kv = 1;
T = kv*G*H*(1-D); // define the loop transfer function
BodePlot("Test1", Freq, T , G , H); // generate Bode plots for T(s), G(s), and H(s)
In the script, a double forward slash represents comments. It is strongly recommended that each
line of the script statement end with a semicolon “;”. This makes it much easier to parse the script
and less likely to have a mistake.
Supported data types in the script are:
float, integer, complex, string, and array
Note that data type declaration is not needed. When a variable is assigned, its data type is
determined automatically. The same variable can change its data type through another
assignment. For example, below is an example of data type assignment in the script:
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
= Equal
== Conditional equal
!= Not equal
!= Not equal
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
! Not operator
&& And
|| Or
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.)
3
How to Use Script Functions
asin(x) Arcsine
acos(x) Arccosine
atan(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
sqrt(x) Square root
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
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:
if (k1 > 10)
{
a1 = 10
4
How to Use Script Functions
}
else
{
a1 = 5
}
iflag = 1
b1 = 0
while (iflag == 1)
{
b1 = b1 + 0.1
If (b1 > 10)
iflag = 0
}
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.
5
How to Use Script Functions
Array(size)
Array(size, InitialValue)
Array(FirstValue, LastValue, increment)
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}
6
How to Use Script Functions
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.
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};
7
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 a string containing “125”. “12” and “5” are concatenated and result in
“125”.
8
How to Use Script Functions
s = Value(a) + Value(b); // s is the number 17. “12” and “5” are first converted to
numbers then added together.
SizeOf(str)
The function returns the size of the string.
9
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"}
10
How to Use Script Functions
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)
Angle(c1)
This function returns the angle of a complex number in rad, and it is defined as atan(b, a).
11
How to Use Script Functions
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
If Object is a string or a number, it is added to the text file. If Object is an array, each cell is written
as a separate line. If Object is a graph, first line is the graph name followed by one number per
line.
If the file already exists, existing format is used. Only when the file is being created, if the optional
"A" character is used, file is written in ANSI format otherwise, default Unicode format is used.
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.
13
How to Use Script Functions
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 create a new curve data.
Function parameters are:
CurveName: Name of the curve
NumberOfRows: It is not possible to resize a Curve therefore it is important that
correct number is used here.
ArrayOfValues: Size of array is used as number of rows and graph is initialized to the
array values.
Formula (optional). This formula is used to initialize the curve. Variables in the formula
are defined either as array of values or a start value and increment
value. To set a variable to a constant, one must use the constant as
the start value and zero as increment. Any variable in the formula
that is not defined as parameter, is taken from the main script.
14
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");
15
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:
16
How to Use Script Functions
17