source: trunk/examples/objects/app-fermi/fermi4.py @ 1586

Last change on this file since 1586 was 1586, checked in by dkearney, 11 years ago

updating potential apis for c cpp and python. adding random() and diff()
functions to the rappture objects. objects should be able to generate a random
value for itself based on how it was configured. objects should be able to
compare itself with another object and tell us what the differences are.

File size: 2.8 KB
Line 
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
16import sys
17import os
18import getopt
19
20import Rappture
21from math import *
22
23
24def 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
34def 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(argv[1])
58
59    nPts = 200;
60    EArr = list() # [nPts];
61    fArr = list() # [nPts];
62
63    if (lib.error() != 0) {
64        # cannot open file or out of memory
65        print >>sys.stderr, lib.traceback()
66        exit(lib.error());
67    }
68
69    T = Rappture.connect(lib,"temperature");
70    Ef = lib.value("Ef","units=eV");
71
72    if (lib.error() != 0) {
73        # there were errors while retrieving input data values
74        # dump the tracepack
75        print >>sys.stderr, lib.traceback()
76        exit(lib.error());
77    }
78
79    kT = 8.61734e-5 * T.value("K");
80    Emin = Ef - 10*kT;
81    Emax = Ef + 10*kT;
82
83    dE = (1.0/nPts)*(Emax-Emin);
84
85    E = Emin;
86    for (size_t idx = 0; idx < nPts; idx++) {
87        E = E + dE;
88        f = 1.0/(1.0 + exp((E - Ef)/kT));
89        fArr.append(f);
90        EArr.append(E);
91        Rappture.Utils.progress((int)((E-Emin)/(Emax-Emin)*100),"Iterating");
92    }
93
94    curveLabel = "Fermi-Dirac Curve"
95    curveDesc = "Plot of Fermi-Dirac Calculation";
96
97    # do it the easy way,
98    # create a plot to add to the library
99    # plot is registered with lib upon object creation
100    # p1->add(nPts,xArr,yArr,format,curveLabel,curveDesc);
101
102    p1 = Rappture.Plot(lib);
103    p1.add(nPts,fArr,EArr,"",curveLabel,curveDesc);
104    p1.propstr("xlabel","Fermi-Dirac Factor");
105    p1.propstr("ylabel","Energy");
106    p1.propstr("yunits","eV");
107
108    lib.result();
109
110    return 0;
111}
112
113
114if __name__ == '__main__':
115    sys.exit(main())
116
Note: See TracBrowser for help on using the repository browser.