Scripting ASCEND using Python
It's possible to write scripts that 'drive' ASCEND, using the Python language. These scripts can be used for any task you can think of, with the ASCEND engine providing the core capability of loading a system of equations, getting it ready to solve, and then solving it. For example:
- you have a model and you want to run a large number of parameter studies on it. You can write a Python script to loop through different cases and solve the model for each one and extract the results and put them into a table.
- you want to plot curves for thermodynamic property data, but the built-in plotting capability of ASCEND doesn't seem right for the job
- you want to incorporate an ASCEND model into a program with a custom GUI to make it easier for non-technical users to use it
- you want to write a test suite to check that a model still does what it did when you first write it, and you want to run that test suite automatically every night.
As well as the current suppport for Python scripting of ASCEND, it's also possible to write ASCEND scripts using Tcl/Tk. See for more information on that.
There are two different ways of accessing the Python API for ASCEND. The first way is the more natural one, which is to simply write a standalone Python script that wraps the ASCEND engine and makes it do things. The second way is via the ExtPy mechanism, which lets you write fragments of Python that can be run embedded within the METHODS interface in the PyGTK GUI. The first is better for quick hacks, the second is better for providing reusable functionality, although it still represents code at a somewhat experimental stage.
Currently, the best place to find examples of what can be done with the ASCEND Python API is in the code of the file test.py from the code repository. There is not yet any good documentation for the Python API, unfortunately.
Example of a standalone Python script with ASCEND
Here is an example of a simple script that loads a model (namely models/johnpye/testlog10.a4c) and solves it, then runs the 'self_test' method on the model, then prints out the value of a variable from the model.
#!/usr/bin/env python # file 'example.py': import ascpy L = ascpy.Library() L.load("johnpye/testlog10.a4c") T = L.findType("testlog10") M = T.getSimulation('sim') M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter()) M.run(T.getMethod('self_test')) print "After solving, z =",float(M.z)
This script can be run using the command
PYTHONPATH=~/ascend/pygtk python example.py
As well as stacks of debug output, this also finally outputs:
-----------------solve---------------- pygtk/simulation.cpp:804 (solve): Elapsed time: 0.000 (solver completed) Converged pygtk/simulation.cpp:889 (processVarStatus): There are 2 blocks EMPTY METHOD CREATED pygtk/simulation.cpp:214 (run): Running method self_test... base/generic/utilities/error.c:161 (error_reporter_tree_end): TREE END SUCCESS: /home/john/ascend/models/johnpye/testlog10.a4c:24:ASSERT: Assertion OK SUCCESS: /home/john/ascend/models/johnpye/testlog10.a4c:25:ASSERT: Assertion OK SUCCESS: /home/john/ascend/models/johnpye/testlog10.a4c:26:ASSERT: Assertion OK base/generic/utilities/error.c:308 (va_error_reporter): DONE WRITING TREE SUCCESS: Method 'self_test' returned 'all_ok' and output no errors. After solving, z = 2.30258509299
With care, one can set up the model then repeatedly solve it for different variable values, etc.
Example of a script embedded within an ASCEND model
See ExtPy.