1 | #! /usr/bin/env python |
---|
2 | # ---------------------------------------------------------------------- |
---|
3 | # EXAMPLE: Fermi-Dirac function in Python. |
---|
4 | # |
---|
5 | # This simple example shows how to use Rappture within a simulator |
---|
6 | # written in Python. |
---|
7 | # ====================================================================== |
---|
8 | # AUTHOR: Derrick Kearney, 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 | |
---|
15 | |
---|
16 | import sys |
---|
17 | import os |
---|
18 | import getopt |
---|
19 | |
---|
20 | import Rappture |
---|
21 | from math import * |
---|
22 | |
---|
23 | from fermi_io import fermi_io |
---|
24 | |
---|
25 | def help(argv=sys.argv): |
---|
26 | return """%(prog)s [-h] |
---|
27 | |
---|
28 | -h | --help - print the help menu |
---|
29 | |
---|
30 | Examples: |
---|
31 | %(prog)s |
---|
32 | |
---|
33 | """ % {'prog':os.path.basename(argv[0])} |
---|
34 | |
---|
35 | def main(argv=None): |
---|
36 | |
---|
37 | # initialize the global interface |
---|
38 | Rappture.Interface(sys.argv,fermi_io) |
---|
39 | |
---|
40 | # check the global interface for errors |
---|
41 | if (Rappture.Interface.error() != 0) { |
---|
42 | # there were errors while setting up the interface |
---|
43 | # dump the traceback |
---|
44 | o = Rappture.Interface.outcome() |
---|
45 | print >>sys.stderr, "%s", o.context() |
---|
46 | print >>sys.stderr, "%s", o.remark() |
---|
47 | return (Rappture.Interface.error()) |
---|
48 | } |
---|
49 | |
---|
50 | # connect variables to the interface |
---|
51 | # look in the global interface for an object named |
---|
52 | # "temperature", convert its value to Kelvin, and |
---|
53 | # store the value into the address of T. |
---|
54 | # look in the global interface for an object named |
---|
55 | # "Ef", convert its value to electron Volts and store |
---|
56 | # the value into the address of Ef |
---|
57 | # look in the global interface for an object named |
---|
58 | # factorsTable and set the variable result to |
---|
59 | # point to it. |
---|
60 | |
---|
61 | T = Rappture.Interface.connect(name="temperature", |
---|
62 | hints=["units=K"]) |
---|
63 | Ef = Rappture.Interface.connect(name="Ef", |
---|
64 | hints=["units=eV"]); |
---|
65 | p1 = Rappture.Interface.connect(name="fdfPlot"); |
---|
66 | p2 = Rappture.Interface.connect(name="fdfPlot"); |
---|
67 | |
---|
68 | # check the global interface for errors |
---|
69 | if (Rappture.Interface.error() != 0) { |
---|
70 | # there were errors while retrieving input data values |
---|
71 | # dump the tracepack |
---|
72 | o = Rappture.Interface.outcome() |
---|
73 | print >>sys.stderr, "%s", o.context() |
---|
74 | print >>sys.stderr, "%s", o.remark() |
---|
75 | return (Rappture.Interface.error()) |
---|
76 | } |
---|
77 | |
---|
78 | # do science calculations |
---|
79 | nPts = 200; |
---|
80 | EArr = list() # [nPts] |
---|
81 | fArr = list() # [nPts] |
---|
82 | |
---|
83 | kT = 8.61734e-5 * T |
---|
84 | Emin = Ef - (10*kT) |
---|
85 | Emax = Ef + (10*kT) |
---|
86 | |
---|
87 | dE = (1.0/nPts)*(Emax-Emin) |
---|
88 | |
---|
89 | E = Emin; |
---|
90 | for idx in xrange(nPts): |
---|
91 | E = E + dE |
---|
92 | f = 1.0/(1.0 + exp((E - Ef)/kT)) |
---|
93 | fArr.append(f) |
---|
94 | EArr.append(E) |
---|
95 | progress = (int)((E-Emin)/(Emax-Emin)*100) |
---|
96 | Rappture.Utils.progress(progress,"Iterating") |
---|
97 | |
---|
98 | # set up the curves for the plot by using the add command |
---|
99 | # add <name> <xdata> <ydata> -format <fmt> |
---|
100 | # |
---|
101 | # to group curves on the same plot, just keep adding curves |
---|
102 | # to save space, X array values are compared between curves. |
---|
103 | # the the X arrays contain the same values, we only store |
---|
104 | # one version in the internal data table, otherwise a new |
---|
105 | # column is created for the array. for big arrays this may take |
---|
106 | # some time, we should benchmark to see if this can be done |
---|
107 | # efficiently. |
---|
108 | |
---|
109 | p1.add(name="fdfCurve1", xdata=fArr, ydata=EArr, format="g:o") |
---|
110 | |
---|
111 | p2.add(name="fdfCurve2", xdata=fArr, ydata=EArr, format="b-o") |
---|
112 | p2.add(name="fdfCurve3", xdata=fArr, ydata=EArr, format="p--") |
---|
113 | |
---|
114 | # close the global interface |
---|
115 | # signal to the graphical user interface that science |
---|
116 | # calculations are complete and to display the data |
---|
117 | # as described in the views |
---|
118 | Rappture.Interface.close() |
---|
119 | |
---|
120 | return 0 |
---|
121 | |
---|
122 | if __name__ == '__main__': |
---|
123 | sys.exit(main()) |
---|