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) 2005-2009 Purdue Research Foundation |
---|
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 | |
---|
24 | def help(argv=sys.argv): |
---|
25 | return """%(prog)s [-h] |
---|
26 | |
---|
27 | -h | --help - print the help menu |
---|
28 | |
---|
29 | Examples: |
---|
30 | %(prog)s |
---|
31 | |
---|
32 | """ % {'prog':os.path.basename(argv[0])} |
---|
33 | |
---|
34 | def main(argv=None): |
---|
35 | |
---|
36 | if argv is None: |
---|
37 | argv = sys.argv |
---|
38 | |
---|
39 | if len(argv) < 1: |
---|
40 | msg = "%(prog)s requires at least 0 argument" % {'prog':argv[0]} |
---|
41 | print >>sys.stderr, msg |
---|
42 | print >>sys.stderr, help() |
---|
43 | return 2 |
---|
44 | |
---|
45 | longOpts = ["help"] |
---|
46 | shortOpts = "h" |
---|
47 | try: |
---|
48 | opts,args = getopt.getopt(argv[1:],shortOpts,longOpts) |
---|
49 | except getopt.GetOptError, msg: |
---|
50 | print >>sys.stderr, msg |
---|
51 | print >>sys.stderr, help() |
---|
52 | return 2 |
---|
53 | |
---|
54 | |
---|
55 | |
---|
56 | # create a rappture library from the file filePath |
---|
57 | lib = Rappture.Library() |
---|
58 | |
---|
59 | nPts = 200; |
---|
60 | EArr = list() # [nPts] |
---|
61 | fArr = list() # [nPts] |
---|
62 | |
---|
63 | lib.loadFile(sys.argv[1]) |
---|
64 | if (lib.error() != 0) { |
---|
65 | # cannot open file or out of memory |
---|
66 | o = lib.outcome() |
---|
67 | print >>sys.stderr, "%s", o.context() |
---|
68 | print >>sys.stderr, "%s", o.remark() |
---|
69 | return (lib.error()); |
---|
70 | } |
---|
71 | |
---|
72 | T = Rappture.Connect(lib,"temperature") |
---|
73 | Ef = lib.value("Ef","units=eV") |
---|
74 | |
---|
75 | if (lib.error() != 0) { |
---|
76 | # there were errors while retrieving input data values |
---|
77 | # dump the tracepack |
---|
78 | o = lib.outcome() |
---|
79 | print >>sys.stderr, "%s", o.context() |
---|
80 | print >>sys.stderr, "%s", o.remark() |
---|
81 | return (lib.error()) |
---|
82 | } |
---|
83 | |
---|
84 | kT = 8.61734e-5 * T.value("K") |
---|
85 | Emin = Ef - 10*kT |
---|
86 | Emax = Ef + 10*kT |
---|
87 | |
---|
88 | dE = (1.0/nPts)*(Emax-Emin) |
---|
89 | |
---|
90 | E = Emin; |
---|
91 | for (size_t idx = 0; idx < nPts; idx++) { |
---|
92 | E = E + dE |
---|
93 | f = 1.0/(1.0 + exp((E - Ef)/kT)) |
---|
94 | fArr.append(f) |
---|
95 | EArr.append(E) |
---|
96 | Rappture.Utils.progress((int)((E-Emin)/(Emax-Emin)*100),"Iterating") |
---|
97 | } |
---|
98 | |
---|
99 | # do it the easy way, |
---|
100 | # create a plot to add to the library |
---|
101 | # plot is registered with lib upon object creation |
---|
102 | # p1->add(nPts,xArr,yArr,format,name); |
---|
103 | |
---|
104 | p1 = Rappture.Plot(lib) |
---|
105 | p1.add(nPts,fArr,EArr,"","fdfactor") |
---|
106 | p1.propstr("label","Fermi-Dirac Curve") |
---|
107 | p1.propstr("desc","Plot of Fermi-Dirac Calculation") |
---|
108 | p1.propstr("xlabel","Fermi-Dirac Factor") |
---|
109 | p1.propstr("ylabel","Energy") |
---|
110 | p1.propstr("yunits","eV") |
---|
111 | |
---|
112 | lib.result() |
---|
113 | |
---|
114 | return 0 |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | if __name__ == '__main__': |
---|
119 | sys.exit(main()) |
---|
120 | |
---|