1 |
# Extpy script for displaying the transfer matrix eigenvalues for a DAE |
2 |
# system from within ASCEND. |
3 |
# |
4 |
# This script must be run via the PyGTK GUI or it will throw an |
5 |
# exception at extpy.getbrowser(). |
6 |
# |
7 |
# This script in turn calls roots_subproc.py, in order to work around a |
8 |
# bug in scipy (used for the computation of matrix eigenvalues) |
9 |
|
10 |
import extpy; |
11 |
from solverreporter import * |
12 |
import os |
13 |
import os.path |
14 |
import subprocess |
15 |
import sys |
16 |
|
17 |
derivs = ['dg/dz','dg/dx','df/dz','df/dx',"df/dx'"] |
18 |
|
19 |
def createfiles(): |
20 |
fff = {} |
21 |
for d in derivs: |
22 |
fff[d] = os.tempnam() |
23 |
return fff |
24 |
|
25 |
def deletefiles(fff): |
26 |
for f in fff.values(): |
27 |
os.unlink(f) |
28 |
|
29 |
def roots(self): |
30 |
"""Plot the complex eigenvalues of a DAE system""" |
31 |
|
32 |
# the following is an unfortunate necessity in the current system architecture: |
33 |
browser = extpy.getbrowser() |
34 |
M = browser.sim |
35 |
M.setSolver(ascpy.Solver('QRSlv')) |
36 |
|
37 |
# get IDA to analyse the DAE structure |
38 |
I = ascpy.Integrator(M) |
39 |
I.setEngine('IDA') |
40 |
I.setReporter(ascpy.IntegratorReporterConsole(I)) |
41 |
I.analyse() |
42 |
|
43 |
# write the results of analysis to some tempfiles |
44 |
|
45 |
fff = createfiles() |
46 |
|
47 |
for k,v in fff.iteritems(): |
48 |
F = file(v,'w') |
49 |
I.writeMatrix(F,k) |
50 |
|
51 |
print "WROTE MATRICES TO FILE. NOW PROCESSING..." |
52 |
|
53 |
# we can't import scipy here due to a crash. so we must use a subprocess... |
54 |
|
55 |
script = os.path.expanduser('~/ascend/models/johnpye/roots_subproc.py') |
56 |
if os.path.exists(script): |
57 |
P = subprocess.Popen(['python',script]+[fff[d] for d in derivs],stdout=subprocess.PIPE,close_fds=True) |
58 |
ret = P.wait() |
59 |
if ret: |
60 |
print "GOT ERROR CODE FROM roots_subproc.py" |
61 |
browser.reporter.reportError(P.stdout.read()) |
62 |
deletefiles(fff) |
63 |
return 1 |
64 |
|
65 |
print "OK" |
66 |
else: |
67 |
browser.reporter.reportError("Couldn't find script '%s'" % script) |
68 |
deletefiles(fff) |
69 |
return 1 |
70 |
|
71 |
deletefiles(fff) |
72 |
return 0 |
73 |
|
74 |
extpy.registermethod(roots) |
75 |
#the above method can be called using "EXTERNAL roots(SELF)" in ASCEND. |