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

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

updates for the rappture objects, object examples, and object apis. small fix for rpunits c interface to check string length. there should probably be more of these checks in the c interface, but units should also be rewritten. added folders to separate out octave2 and octave3 app-fermi examples

File size: 2.9 KB
Line 
1#! /usr/bin/env python
2# ----------------------------------------------------------------------
3#  EXAMPLE: Fermi-Dirac function in C++.
4#
5#  This simple example shows how to use Rappture within a simulator
6#  written in C++.
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#    T = Rappture.Number();
60#    Ef = 0.0;
61#    dE = 0.0;
62#    kT = 0.0;
63#    Emin = 0.0;
64#    Emax = 0.0;
65    nPts = 200;
66    EArr = list() # [nPts];
67    fArr = list() # [nPts];
68
69    if (lib.error() != 0) {
70        # cannot open file or out of memory
71        print >>sys.stderr, lib.traceback()
72        exit(lib.error());
73    }
74
75    T = Rappture.connect(lib,"temperature");
76    Ef = lib.value("Ef","units=eV");
77
78    if (lib.error() != 0) {
79        # there were errors while retrieving input data values
80        # dump the tracepack
81        print >>sys.stderr, lib.traceback()
82        exit(lib.error());
83    }
84
85    kT = 8.61734e-5 * T.value("K");
86    Emin = Ef - 10*kT;
87    Emax = Ef + 10*kT;
88
89    dE = (1.0/nPts)*(Emax-Emin);
90
91    E = Emin;
92    for (size_t idx = 0; idx < nPts; idx++) {
93        E = E + dE;
94        f = 1.0/(1.0 + exp((E - Ef)/kT));
95        fArr.append(f);
96        EArr.append(E);
97        Rappture.Utils.progress((int)((E-Emin)/(Emax-Emin)*100),"Iterating");
98    }
99
100    curveLabel = "Fermi-Dirac Curve"
101    curveDesc = "Plot of Fermi-Dirac Calculation";
102
103    # do it the easy way,
104    # create a plot to add to the library
105    # plot is registered with lib upon object creation
106    # p1->add(nPts,xArr,yArr,format,curveLabel,curveDesc);
107
108    p1 = Rappture.Plot(lib);
109    p1.add(nPts,fArr,EArr,"",curveLabel,curveDesc);
110    p1.propstr("xlabel","Fermi-Dirac Factor");
111    p1.propstr("ylabel","Energy");
112    p1.propstr("yunits","eV");
113
114    lib.result();
115
116    return 0;
117}
118
119
120if __name__ == '__main__':
121    sys.exit(main())
122
Note: See TracBrowser for help on using the repository browser.