Scripting ASCEND using Python

From ASCEND
Revision as of 00:26, 8 November 2010 by Jpye (talk | contribs)
Jump to navigation Jump to search

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.

Invalid language.

You need to specify a language like this: <source lang="html">...</source>

Supported languages for syntax highlighting:

a4c, abap, abc, abnf, actionscript, ada, agda, alan, algol, ampl, amtrix, applescript, arc, arm, as400cl, ascend, asciidoc, asp, aspect, assembler, ats, autohotkey, autoit, avenue, awk, ballerina, bat, bbcode, bcpl, bibtex, biferno, bison, blitzbasic, bms, bnf, boo, c, carbon, ceylon, charmm, chill, chpl, clean, clearbasic, clipper, clojure, clp, cmake, cobol, coffeescript, coldfusion, conf, cpp2, critic, crk, crystal, cs_block_regex, csharp, css, d, dart, delphi, diff, dockerfile, dts, dylan, ebnf, ebnf2, eiffel, elixir, elm, email, erb, erlang, euphoria, exapunks, excel, express, factor, fame, fasm, felix, fish, fortran77, fortran90, frink, fsharp, fstab, fx, gambas, gdb, gdscript, go, graphviz, haml, hare, haskell, haxe, hcl, html, httpd, hugo, icon, idl, idlang, inc_luatex, informix, ini, innosetup, interlis, io, jam, jasmin, java, javascript, js_regex, json, jsp, jsx, julia, kotlin, ldif, less, lhs, lilypond, limbo, lindenscript, lisp, logtalk, lotos, lotus, lua, luban, makefile, maple, markdown, matlab, maya, mercury, meson, miranda, mod2, mod3, modelica, moon, ms, msl, mssql, mxml, n3, nasal, nbc, nemerle, netrexx, nginx, nice, nim, nix, nsis, nxc, oberon, objc, ocaml, octave, oorexx, org, os, oz, paradox, pas, pdf, perl, php, pike, pl1, plperl, plpython, pltcl, po, polygen, pony, pov, powershell, pro, progress, ps, psl, pure, purebasic, purescript, pyrex, python, q, qmake, qml, qu, r, rebol, rego, rexx, rnc, rpg, rpl, rst, ruby, rust, s, sam, sas, scad, scala, scilab, scss, shellscript, slim, small, smalltalk, sml, snmp, snobol, solidity, spec, spn, sql, squirrel, styl, svg, swift, sybase, tcl, tcsh, terraform, tex, toml, tsql, tsx, ttcn3, txt, typescript, upc, vala, vb, verilog, vhd, vimscript, vue, wat, whiley, wren, xml, xpp, yaiff, yaml, yaml_ansible, yang, zig, znn

This script can be run using the command

Invalid language.

You need to specify a language like this: <source lang="html">...</source>

Supported languages for syntax highlighting:

a4c, abap, abc, abnf, actionscript, ada, agda, alan, algol, ampl, amtrix, applescript, arc, arm, as400cl, ascend, asciidoc, asp, aspect, assembler, ats, autohotkey, autoit, avenue, awk, ballerina, bat, bbcode, bcpl, bibtex, biferno, bison, blitzbasic, bms, bnf, boo, c, carbon, ceylon, charmm, chill, chpl, clean, clearbasic, clipper, clojure, clp, cmake, cobol, coffeescript, coldfusion, conf, cpp2, critic, crk, crystal, cs_block_regex, csharp, css, d, dart, delphi, diff, dockerfile, dts, dylan, ebnf, ebnf2, eiffel, elixir, elm, email, erb, erlang, euphoria, exapunks, excel, express, factor, fame, fasm, felix, fish, fortran77, fortran90, frink, fsharp, fstab, fx, gambas, gdb, gdscript, go, graphviz, haml, hare, haskell, haxe, hcl, html, httpd, hugo, icon, idl, idlang, inc_luatex, informix, ini, innosetup, interlis, io, jam, jasmin, java, javascript, js_regex, json, jsp, jsx, julia, kotlin, ldif, less, lhs, lilypond, limbo, lindenscript, lisp, logtalk, lotos, lotus, lua, luban, makefile, maple, markdown, matlab, maya, mercury, meson, miranda, mod2, mod3, modelica, moon, ms, msl, mssql, mxml, n3, nasal, nbc, nemerle, netrexx, nginx, nice, nim, nix, nsis, nxc, oberon, objc, ocaml, octave, oorexx, org, os, oz, paradox, pas, pdf, perl, php, pike, pl1, plperl, plpython, pltcl, po, polygen, pony, pov, powershell, pro, progress, ps, psl, pure, purebasic, purescript, pyrex, python, q, qmake, qml, qu, r, rebol, rego, rexx, rnc, rpg, rpl, rst, ruby, rust, s, sam, sas, scad, scala, scilab, scss, shellscript, slim, small, smalltalk, sml, snmp, snobol, solidity, spec, spn, sql, squirrel, styl, svg, swift, sybase, tcl, tcsh, terraform, tex, toml, tsql, tsx, ttcn3, txt, typescript, upc, vala, vb, verilog, vhd, vimscript, vue, wat, whiley, wren, xml, xpp, yaiff, yaml, yaml_ansible, yang, zig, znn

As well as large amounts 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.