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) 2004-2012 HUBzero Foundation, LLC |
---|
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 |
---|
15 | require "Rappture" |
---|
16 | |
---|
17 | # Create an instance of the Rappture I/O library; |
---|
18 | # Pass the driver file name as an argument |
---|
19 | rp = Rappture.new(ARGV[0]) |
---|
20 | |
---|
21 | # Get the input temperature |
---|
22 | strT = rp.get("input.(temperature).current") |
---|
23 | numT = rp.convert(strT, "K", Rappture::UNITS_OFF) |
---|
24 | |
---|
25 | # Get the input Fermi energy |
---|
26 | strEf = rp.get("input.(Ef).current") |
---|
27 | numEf = rp.convert(strEf, "eV", Rappture::UNITS_OFF) |
---|
28 | |
---|
29 | # Set the energy range and step size |
---|
30 | kT = 8.61734e-5*numT |
---|
31 | minE = numEf - 10.0*kT |
---|
32 | maxE = numEf + 10.0*kT |
---|
33 | currE = minE |
---|
34 | dE = 0.005*(maxE - minE) |
---|
35 | |
---|
36 | # Set information about the output plot |
---|
37 | rp.put("output.curve(f12).about.label", "Fermi-Dirac Factor", Rappture::OVERWRITE) |
---|
38 | rp.put("output.curve(f12).xaxis.label", "Fermi-Dirac Factor", Rappture::OVERWRITE) |
---|
39 | rp.put("output.curve(f12).yaxis.label", "Energy", Rappture::OVERWRITE) |
---|
40 | rp.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. |
---|
44 | while 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 |
---|
50 | end |
---|
51 | |
---|
52 | # Write the final results |
---|
53 | rp.result(0) |
---|
54 | |
---|