In this topic:
Any one who has learnt the 'C' programming language will be familiar with the now celebrated "Hello World" program - possibly the simplest program that can be written. Here we will write and execute a SIMetrix "Hello World" script.
echo "Hello World!"To execute and run this script start by selecting the menu this will open the in built script editor. Type:
echo "Hello World!"Now save the text to a file called hello.sxscr. To execute the script, click on the Run toolbar button or type "hello" at the command line. You should see the message:
Hello World!Appear in the message window. A script is executed by typing its filename at the command line. If the file has the extension .sxscr the extension may be omitted. You can also assign a key or menu to execute a script. Type at the command line:
DefKey F6 HELLONow press the F6 key. The message should appear again. For information on defining menus see User Defined Key and Menu Definitions.
This example adds up all the elements in a vector (or array). To create a vector we will run a simulation on one of the example circuits. The whole process will be put into a script except opening the schematic which we will do manually. (But this can be done from a script as well).
To start with, open the example circuit General/AMP.sxsch. Make sure it is selected to run a transient analysis.
Netlist design.net Run design.net let sum = 0 for idx=0 to length(vout)-1 let sum = sum + vout[idx] next idx echo The sum of all values in vout is {sum}Save the script to the file name SUM.sxscr. Now type SUM at the command line. A simulation will run and the message:
The sum of all values in vout is -6.1663737561Should appear in the message window. The exact value given may be different if you have modified the circuit or set up different model libraries. This script introduces four new concepts:
The first two lines carry out the simulation and in fact something similar is done each time a simulation is run using the menu or F9 key. Netlist design.net generates a netlist of the circuit and saves it in a file called design.net. Then Run design.net runs the simulation on the netlist design.net.
let sum = 0creates and initialises the variable sum which will ultimately hold the final result. The next three lines is a simple for statement. The variable idx is incremented by one each time around the loop starting at zero and ending at length(vout)-1. vout is a variable - actually a vector - which was generated by the simulator and holds the simulated values of the voltage on the VOUT net. This net is marked with a terminal symbol. length(vout) returns the number of elements in vout (1 is subtracted because idx starts at 0). In the line:
let sum = sum + vout[idx]vout[idx] is an indexed expression which returns element number idx of the vector vout. sum is of course the accumulative total. The final line:
echo The sum of all values in vout is {sum}contains the braced substitution {sum}. sum is evaluated and the result replaces expression and the braces. See Braced Substitutions for more information.
let start=1 do while probe() if start then plot {netname()} else curve {netname()} endif let start=0 probe loopThis script introduces if statements, while statements, functions and the features that allow voltage cross-probing, namely the functions NetName and Probe and the command Probe.
if start then plot {netname()} else curve {netname()} endifthe first time around the loop start is equal to 1 and the Plot command is executed. This creates a new graph. Subsequently, start is set to zero and the Curve command is executed which adds new curves to the graph already created.
The argument to the Plot and Curve commands, {netname()} is a braced substitution which we saw in the previous example. The NetName function returns a string which is the name of the nearest net to the cursor at the time the function is executed. The function is executed soon after the user presses the left mouse key so the string returned by NetName will be the net the user is pointing to. The value returned by NetName is a string, but the Plot command requires a numeric expression. By putting netname() in braces the result of evaluating it is substituted as if it were typed in. So if the user pointed at a the net named VOUT, netname() would return 'VOUT' and that would be placed after plot or curve i.e. plot vout would be executed.
probecalls the Probe command. This does the same as the Probe function but doesn't return a result. It is needed because both the Probe function and the Probe command return on both up and down clicks of the mouse. The second occurrence of Probe simply waits for the up click of the mouse button.
There are four other functions which are used for cross-probing. These are GetNearestNet, NearestInst, PinName and Branch.
plot Vec(netname())Note that there are no curly braces used here. This is because the Vec() function returns a numeric vector containing the actual data to be plotted. The NetName function returns the name of the vector not its actual data.
* mk_bom.txt Display parts list in message window if NOT SelSchem() then echo There are no schematics open exit all endif let refs = PropValues('ref', 'ref') for idx=0 to length(refs)-1 let val = PropValues('value', 'ref', refs[idx]) * check for duplicate ref if length(val)==1 then echo {refs[idx]} {val} else echo Duplicate reference {refs[idx]}. Ignoring endif next idxThe first line:
* do_bom.txt Display parts list in message windowis a comment. Any line beginning with a '*' will be ignored.
if NOT SelSchem() thenis the start of an if statement. SelSchem() is a function which returns 1 if there are schematics open and 0 if there are not. if NOT SelSchem() then means 'if there are no schematics open'. This is an initial check that the user has actually opened a schematic.
echo There are no schematics open exit allwill be executed. The first line calls the echo command. This echoes to the message window all subsequent text on the same line. The second line is an exit statement. In this case it causes execution to abort and the rest of the script will be ignored.
endifterminates the if statement. For every if there must be a matching endif or end if.
let refs = PropValues('ref', 'ref')calls the let command. This expects an assignment expression which it evaluates. In this case it assigns refs with the result of the a call to the function PropValues. In this example it returns the component reference for all instances (i.e. symbols) on the schematic that have one.
for idx=0 to length(refs)-1starts a for loop. The block of statements between this line and the matching next will be repeated with values of idx incrementing by 1 each time around the loop until idx reaches length(refs)-1.The length function returns the number of elements in the refs variable so the loop is repeated for all elements in refs.
let val = PropValues('value', 'ref', refs[idx])This calls the PropValues function again. This time it returns the value of the value property for any instance with the property ref which has the value refs[idx]. Assuming the schematic has been annotated (unique references assigned to all components) the result of this call should be a single value which is assigned to val.
if length(val)==1 then echo {refs[idx]} {val}The if statement checks that val has length one which means that the reference is unique. If it is then the Echo command is called which displays on the message window all the text following it. In this instance the echo command is followed by two braced substitutions. A braced substitution is an expression enclosed in curly braces '{' and '}'. The braces and the enclosed expression are replaced by the result of evaluating the expression as if it had been typed in. Braced substitutions are a very important feature of the SIMetrix scripting language. Here the result is the component's reference and value are displayed in the message window.
else echo Duplicate reference {refs[idx]}. Ignoring endifThis is executed if the if expression length(val)==1 is false. This means that there is more than one component with that component reference. A message is output saying that it is being ignored. The final line
next idxterminates the for loop.
◄ Introduction | Variables, Constants and Types ▶ |