1 | # ---------------------------------------------------------------------- |
---|
2 | # EXAMPLE: Fermi-Dirac function in Python. |
---|
3 | # |
---|
4 | # This simple example shows how to use Rappture to wrap up a |
---|
5 | # simulator written in Matlab or some other language. |
---|
6 | # |
---|
7 | # ====================================================================== |
---|
8 | # AUTHOR: Michael McLennan, Purdue University |
---|
9 | # Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
10 | # |
---|
11 | # See the file "license.terms" for information on usage and |
---|
12 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
13 | # ====================================================================== |
---|
14 | import Rappture |
---|
15 | import sys, os, commands, string |
---|
16 | |
---|
17 | # open the XML file containing the run parameters |
---|
18 | driver = Rappture.library(sys.argv[1]) |
---|
19 | |
---|
20 | Tstr = driver.get('input.(temperature).current') |
---|
21 | T = Rappture.Units.convert(Tstr, to="k", units="off") |
---|
22 | |
---|
23 | Efstr = driver.get('input.(Ef).current') |
---|
24 | Ef = Rappture.Units.convert(Efstr, to="eV", units="off") |
---|
25 | |
---|
26 | fid = open('indeck','w') |
---|
27 | infile = "%(Ef)s\n%(T)s\n" % { 'Ef':Ef, 'T':T } |
---|
28 | fid.write(infile) |
---|
29 | fid.close() |
---|
30 | |
---|
31 | try: |
---|
32 | out = commands.getoutput('octave --silent fermi.m < indeck') |
---|
33 | except: |
---|
34 | sys.stderr.write('Error during execution of fermi.m') |
---|
35 | exit(1); |
---|
36 | |
---|
37 | driver.put('output.log',out) |
---|
38 | |
---|
39 | fid = open('out.dat','r') |
---|
40 | info = fid.readlines() |
---|
41 | fid.close() |
---|
42 | |
---|
43 | # Label output graph with title, x-axis label, |
---|
44 | # y-axis lable, and y-axis units |
---|
45 | driver.put('output.curve(f12).about.label','Fermi-Dirac Factor') |
---|
46 | driver.put('output.curve(f12).xaxis.label','Fermi-Dirac Factor') |
---|
47 | driver.put('output.curve(f12).yaxis.label','Energy') |
---|
48 | driver.put('output.curve(f12).yaxis.units','eV') |
---|
49 | |
---|
50 | # skip over the first 4 header lines |
---|
51 | for line in info[6:]: |
---|
52 | f,E = string.split(line[:-1]) |
---|
53 | f,E = float(f),float(E) |
---|
54 | xy = "%g %g\n" % (f,E) |
---|
55 | driver.put('output.curve(f12).component.xy',xy,append=1) |
---|
56 | |
---|
57 | os.remove('indeck'); os.remove('out.dat') |
---|
58 | |
---|
59 | Rappture.result(driver) |
---|
60 | sys.exit(0) |
---|