source: trunk/examples/app-fermi/ruby/fermi.rb @ 1944

Last change on this file since 1944 was 996, checked in by bhaley, 17 years ago

Added examples/app-fermi/ruby directory, with Ruby simulator

File size: 1.8 KB
Line 
1#
2# Fermi-Dirac function in Ruby
3#
4# This example shows how to use Rappture within a simulator written in Ruby.
5# =============================================================================
6# Author: Benjamin Haley, Purdue University
7# Copyright (c) 2008  Purdue Research Foundation
8#
9# See the file "license.terms" for information on usage and
10# redistributioin of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11# =============================================================================
12# Takes one argument: driver (tool.xml)
13
14# Load Rappture
15require "Rappture"
16
17# Create an instance of the Rappture I/O library;
18# Pass the driver file name as an argument
19rp = Rappture.new(ARGV[0])
20
21# Get the input temperature
22strT = rp.get("input.(temperature).current")
23numT = rp.convert(strT, "K", Rappture::UNITS_OFF)
24
25# Get the input Fermi energy
26strEf = rp.get("input.(Ef).current")
27numEf = rp.convert(strEf, "eV", Rappture::UNITS_OFF)
28
29# Set the energy range and step size
30kT = 8.61734e-5*numT
31minE = numEf - 10.0*kT
32maxE = numEf + 10.0*kT
33currE = minE
34dE = 0.005*(maxE - minE)
35
36# Set information about the output plot
37rp.put("output.curve(f12).about.label", "Fermi-Dirac Factor", Rappture::OVERWRITE)
38rp.put("output.curve(f12).xaxis.label", "Fermi-Dirac Factor", Rappture::OVERWRITE)
39rp.put("output.curve(f12).yaxis.label", "Energy", Rappture::OVERWRITE)
40rp.put("output.curve(f12).yaxis.units", "eV", Rappture::OVERWRITE)
41
42# Calculate the Fermi-Dirac function over the energy range,
43# and add the results to the output plot.
44while currE < maxE
45   f = 1.0/(1.0 + Math.exp((currE - numEf)/kT))
46   currXY = sprintf("%g  %g\n", f, currE)
47   rp.progress(((currE-minE)/(maxE-minE)*100.0), "Iterating")
48   rp.put("output.curve(f12).component.xy", currXY, Rappture::APPEND)
49   currE += dE
50end
51
52# Write the final results
53rp.result(0)
54
Note: See TracBrowser for help on using the repository browser.