source: trunk/examples/app-fermi/python/fermi.py

Last change on this file was 6021, checked in by ldelgass, 8 years ago

Merge UQ and fixes from 1.4 branch

  • Property svn:keywords set to Date Rev URL
File size: 2.1 KB
Line 
1# ----------------------------------------------------------------------
2#  EXAMPLE: Fermi-Dirac function in Python.
3#
4#  This simple example shows how to use Rappture within a simulator
5#  written in Python.
6# ======================================================================
7#  AUTHOR:  Michael McLennan, Purdue University
8#           Martin Hunt, Purdue University
9#  Copyright (c) 2004-2015  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# ======================================================================
14import Rappture
15import sys
16import numpy as np
17
18# Uncomment these lines to redirect
19# python output and errors to files
20# for easier debugging.
21# sys.stderr = open('fermi.err', 'w')
22# sys.stdout = open('fermi.out', 'w')
23
24# open the XML file containing the run parameters
25rx = Rappture.PyXml(sys.argv[1])
26
27temp_str = rx['input.(temperature).current'].value
28temp = Rappture.Units.convert(temp_str, to='K', units='off')
29
30ef_str = rx['input.(Ef).current'].value
31ef = Rappture.Units.convert(ef_str, to='eV', units='off')
32
33kt = 8.61734e-5 * temp
34emin = ef - 10*kt
35emax = ef + 10*kt
36
37# Label the output graph with a title, x-axis label,
38# y-axis label, and y-axis units
39f12 = rx['output.curve(f12)']  # a shortcut to save typing
40f12['about.label'] = 'Fermi-Dirac Factor'
41f12['xaxis.label'] = 'Fermi-Dirac Factor'
42f12['yaxis.label'] = 'Energy'
43f12['yaxis.units'] = 'eV'
44
45# How many points to use to define our curve
46numpts = 200
47
48# The normal python approach would be to simply do this:
49# energy = np.linspace(emin, emax, numpts)
50# f = 1.0/(1.0 + np.exp((energy - ef)/kt))
51# f12['component.xy'] = (f, energy)
52# rx.close()
53
54# But we want to show how to use the progress bar,
55# so lets do things slowly and iteratively...
56
57import time
58energy = []
59fermi = []
60for i, e in enumerate(np.linspace(emin, emax, numpts)):
61    f = 1.0/(1.0 + np.exp((e - ef)/kt))
62    energy.append(e)
63    fermi.append(f)
64    Rappture.Utils.progress(i*100.0/numpts, "Iterating")
65    time.sleep(0.01)
66f12['component.xy'] = (fermi, energy)
67rx.close()
Note: See TracBrowser for help on using the repository browser.