source: branches/blt4/examples/objects/app-fermi/python/ex3/fermi.py @ 1890

Last change on this file since 1890 was 1890, checked in by gah, 14 years ago
File size: 3.7 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
23from fermi_io import fermi_io
24
25def 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
35def 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
66    x1 = Rappture.Interface.connect(name="Fermi-Dirac Factor");
67    y1 = Rappture.Interface.connect(name="Energy");
68    x2 = Rappture.Interface.connect(name="Fermi-Dirac Factor * 2");
69    y2 = Rappture.Interface.connect(name="Energy * 2");
70
71    # check the global interface for errors
72    if (Rappture.Interface.error() != 0) {
73        # there were errors while retrieving input data values
74        # dump the tracepack
75        o = Rappture.Interface.outcome()
76        print >>sys.stderr, "%s", o.context()
77        print >>sys.stderr, "%s", o.remark()
78        return (Rappture.Interface.error())
79    }
80
81    # declare program variables
82    nPts = 200;
83    EArr = list() # [nPts]
84    fArr = list() # [nPts]
85    EArr2 = list() # [nPts]
86    fArr2 = list() # [nPts]
87
88    # do science calculations
89    kT = 8.61734e-5 * T
90    Emin = Ef - (10*kT)
91    Emax = Ef + (10*kT)
92
93    dE = (1.0/nPts)*(Emax-Emin)
94
95    E = Emin;
96    for idx in xrange(nPts):
97        E = E + dE
98        f = 1.0/(1.0 + exp((E - Ef)/kT))
99        fArr.append(f)
100        fArr2.append(f*2)
101        EArr.append(E)
102        EArr2.append(E*2)
103        progress = (int)((E-Emin)/(Emax-Emin)*100)
104        Rappture.Utils.progress(progress,"Iterating")
105
106    # store results in the results table
107    # add data to the table pointed to by the variable result.
108    # put the fArr data in the column named "Fermi-Dirac Factor"
109    # put the EArr data in the column named "Energy"
110
111    x1.store(fArr)
112    y1.store(EArr)
113    x2.store(fArr2)
114    y2.store(EArr2)
115
116    # close the global interface
117    # signal to the graphical user interface that science
118    # calculations are complete and to display the data
119    # as described in the views
120    Rappture.Interface.close()
121
122    return 0
123
124if __name__ == '__main__':
125    sys.exit(main())
126
Note: See TracBrowser for help on using the repository browser.