Programming Notes¶
Aborting Python Execution¶
To abort Python execution press the Escape key. This will execute the PythonAbort command which signals the abort process.
Note that this will only work if the Python code calls a SIMetrix function at some point during the execution. If you run some code that takes a long time to execute and does not call SIMetrix functions, the only way it can be aborted will be by forcibly terminating the whole application using task manager.
If you need to run some Python code in a long loop for example, you should call the SIMetrix function simetrix.processEvents() periodically.
This function processes the GUI event loop so that the menus will be active and also tests the abort flag and abort if requested.
If execution of your code is likely to take a long time, it is worth considering using a simetrix.ProgressBox to display progress.
Note that not all SIMetrix functions test for an abort request. If in doubt, call simetrix.processEvents().
Using Multiple Modules¶
If you use multiple modules (i.e .py files) for Python code within the SIMetrix/SIMPLIS environment, you will need to be aware of how Python manages sub-modules.
Any sub-module called by the main module will get cached meaning that any changes made during the current SIMetrix/SIMPLIS session will not be seen until SIMetrix/SIMPLIS is restarted.
This problem can be overcome by using the importlib module.
For example, suppose you have two modules main.py and hello.py:
main.py
import hello
hello.hello()
hello.py
def hello() :
print("Hello world!")
When main.py is run for the first time, the message “Hello world!” will displayed in the command shell. However, if hello.py is modified to:
def hello() :
print("Hello Python!")
running main.py again in the same SIMetrix session will still display the message “Hello world!”. This is because Python caches the code for hello.py.
This problem can be resolved by changing main.py to the following:
import importlib
import hello
importlib.reload(hello)
hello.hello()
In the above, hello.py will be reloaded on each call.
Calling Python from a SIMetrix Script¶
Python scripts can be called using the SIMetrix script function PythonExecFile or its alias PyExec. You can supply arguments to be passed to the Python script in the second argument of PythonExecFile. These can then be read in the Python script using sys.argv. For example:
In the SIMetrix script:
Let result = PythonExecFile('my_python_script.py', ['test 1', '23.456'])
In the Python script:
import sys
print(sys.argv[1])
print(sys.argv[2])
will print:
test 1
23.456
Accessing SIMetrix Script Variables in Python¶
SIMetrix script variables can be read, written and created in a Python script.
In Python, SIMetrix script variables are simetrix.GroupVector objects.
As with simulation data, SIMetrix script variables reside in a data group which is created for each
script execution. So there will be a data group that holds the variables in the script that
calls the currently executing Python code.
Two functions may be used to read, write and create SIMetrix script variables:
simetrix.script.getVariable()gets an existing named script variable or creates one with the given name if the variable does not exist.simetrix.GroupVector.assign()assigns a script variable.
Example 1¶
In the SIMetrix script, we call a python script called python_a.py. The variable var_a is created and assigned prior to calling the Python script.
let var_a='A string variable'
Let PyExec('python_a.py')
Echo {'Running SIMetrix script code'}
echo {'var_a=' & var_a}
Python script “python_a.py”:
import simetrix as sx
import simetrix.script as scr
print("Running Python")
var_a = scr.getVariable("var_a")
print(var_a.string)
# assign a complex vector
var_a.assign( [1.1+1j,2.1+3j,3.2+4j])
The above will print the following:
Running Python
A string variable
Running SIMetrix script code
(1.1,1) (2.1,3) (3.2,4)
Running Python
A string variable
Running SIMetrix script code
var_a=(1.1,1) var_a=(2.1,3) var_a=(3.2,4)
Example 2¶
In the SIMetrix script, we call a python script called python_b.py. No variables are created prior to calling the script but two variables are created in the Python script then displayed and plotted in the SIMetrix script.
Let PyExec('python_b.py')
show var_b
plot var_c
Python script “python_b.py”:
import simetrix.script as scr
# Create var_b, var_c, var_d
var_b = scr.getVariable("var_b")
var_c = scr.getVariable("var_c")
# assign a string array
var_b.assign( ["R1", "C23", "Q10"])
# assign plottable value
var_c.assign([1,4,9,16,25,36,49], [1e-6,2e-6,3e-6,4e-6,5e-6,6e-6,7e-6])
The above will print:
Index var_b
0 'R1'
1 'C23'
2 'Q10'
and will also plot the results of var_c.
Calling a SIMetrix Script from Python¶
A SIMetrix script can be called from Python using the simetrix.script.runScript() function or the simetrix.script.execute(). See documentation on
thos functions for further details.
API Reference¶
SIMetrix and SIMetrix/SIMPLIS Python Support |