Ignore:
Timestamp:
Jun 10, 2015 8:54:05 AM (9 years ago)
Author:
ldelgass
Message:

Merge new Python examples from 1.3 branch (uq sync)

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/examples/app-fermi/python/fermi.py

    r4462 r5681  
    66# ======================================================================
    77#  AUTHOR:  Michael McLennan, Purdue University
    8 #  Copyright (c) 2004-2012  HUBzero Foundation, LLC
     8#           Martin Hunt, Purdue University
     9#  Copyright (c) 2004-2015  HUBzero Foundation, LLC
    910#
    1011#  See the file "license.terms" for information on usage and
     
    1314import Rappture
    1415import sys
    15 from math import *
     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')
    1623
    1724# open the XML file containing the run parameters
    18 driver = Rappture.library(sys.argv[1])
     25rx = Rappture.PyXml(sys.argv[1])
    1926
    20 Tstr = driver.get('input.(temperature).current')
    21 T = Rappture.Units.convert(Tstr, to="K", units="off")
     27temp_str = rx['input.(temperature).current'].value
     28temp = Rappture.Units.convert(temp_str, to='K', units='off')
    2229
    23 Efstr = driver.get('input.(Ef).current')
    24 Ef = Rappture.Units.convert(Efstr, to="eV", units="off")
     30ef_str = rx['input.(Ef).current'].value
     31ef = Rappture.Units.convert(ef_str, to='eV', units='off')
    2532
    26 kT = 8.61734e-5 * T
    27 Emin = Ef - 10*kT
    28 Emax = Ef + 10*kT
    29 
    30 E = Emin
    31 dE = 0.005*(Emax-Emin)
     33kt = 8.61734e-5 * temp
     34emin = ef - 10*kt
     35emax = ef + 10*kt
    3236
    3337# Label the output graph with a title, x-axis label,
    3438# y-axis label, and y-axis units
    35 driver.put('output.curve(f12).about.label','Fermi-Dirac Factor',append=0)
    36 driver.put('output.curve(f12).xaxis.label','Fermi-Dirac Factor',append=0)
    37 driver.put('output.curve(f12).yaxis.label','Energy',append=0)
    38 driver.put('output.curve(f12).yaxis.units','eV',append=0)
     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'
    3944
    40 while E < Emax:
    41     f = 1.0/(1.0 + exp((E - Ef)/kT))
    42     line = "%g %g\n" % (f, E)
    43     Rappture.Utils.progress(((E-Emin)/(Emax-Emin)*100),"Iterating")
    44     driver.put('output.curve(f12).component.xy', line, append=1)
    45     E = E + dE
     45# How many points to use to define our curve
     46numpts = 200
    4647
    47 Rappture.result(driver)
    48 sys.exit()
     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 TracChangeset for help on using the changeset viewer.