[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 | # ====================================================================== |
---|
| 14 | import Rappture |
---|
| 15 | import sys |
---|
[5681] | 16 | import 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] | 25 | rx = Rappture.PyXml(sys.argv[1]) |
---|
[66] | 26 | |
---|
[5681] | 27 | temp_str = rx['input.(temperature).current'].value |
---|
| 28 | temp = Rappture.Units.convert(temp_str, to='K', units='off') |
---|
[66] | 29 | |
---|
[5681] | 30 | ef_str = rx['input.(Ef).current'].value |
---|
| 31 | ef = Rappture.Units.convert(ef_str, to='eV', units='off') |
---|
[66] | 32 | |
---|
[5681] | 33 | kt = 8.61734e-5 * temp |
---|
| 34 | emin = ef - 10*kt |
---|
| 35 | emax = 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] | 39 | f12 = rx['output.curve(f12)'] # a shortcut to save typing |
---|
| 40 | f12['label'] = 'Fermi-Dirac Factor' |
---|
| 41 | f12['xaxis.label'] = 'Fermi-Dirac Factor' |
---|
| 42 | f12['yaxis.label'] = 'Energy' |
---|
| 43 | f12['yaxis.units'] = 'eV' |
---|
[555] | 44 | |
---|
[5681] | 45 | # How many points to use to define our curve |
---|
| 46 | numpts = 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 | |
---|
| 57 | import time |
---|
| 58 | energy = [] |
---|
| 59 | fermi = [] |
---|
| 60 | for 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) |
---|
| 66 | f12['component.xy'] = (fermi, energy) |
---|
| 67 | rx.close() |
---|