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

Last change on this file since 5681 was 5681, checked in by ldelgass, 9 years ago

Merge new Python examples from 1.3 branch (uq sync)

  • Property svn:keywords set to Date Rev URL
File size: 2.1 KB
RevLine 
[66]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
[5681]8#           Martin Hunt, Purdue University
9#  Copyright (c) 2004-2015  HUBzero Foundation, LLC
[115]10#
11#  See the file "license.terms" for information on usage and
12#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
[66]13# ======================================================================
14import Rappture
15import sys
[5681]16import numpy as np
[66]17
[5681]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
[66]24# open the XML file containing the run parameters
[5681]25rx = Rappture.PyXml(sys.argv[1])
[66]26
[5681]27temp_str = rx['input.(temperature).current'].value
28temp = Rappture.Units.convert(temp_str, to='K', units='off')
[66]29
[5681]30ef_str = rx['input.(Ef).current'].value
31ef = Rappture.Units.convert(ef_str, to='eV', units='off')
[66]32
[5681]33kt = 8.61734e-5 * temp
34emin = ef - 10*kt
35emax = ef + 10*kt
[66]36
[555]37# Label the output graph with a title, x-axis label,
38# y-axis label, and y-axis units
[5681]39f12 = rx['output.curve(f12)']  # a shortcut to save typing
40f12['label'] = 'Fermi-Dirac Factor'
41f12['xaxis.label'] = 'Fermi-Dirac Factor'
42f12['yaxis.label'] = 'Energy'
43f12['yaxis.units'] = 'eV'
[555]44
[5681]45# How many points to use to define our curve
46numpts = 200
[66]47
[5681]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.