source: trunk/examples/app-fermi/R/fermi.R

Last change on this file was 3177, checked in by mmc, 12 years ago

Updated all of the copyright notices to reference the transfer to
the new HUBzero Foundation, LLC.

File size: 2.2 KB
Line 
1# ----------------------------------------------------------------------
2#  EXAMPLE: Fermi-Dirac function in R.
3#
4#  This simple example shows how to use Rappture within a simulator
5#  written in R.
6# ======================================================================
7#  AUTHOR:  Derrick Kearney, Purdue University
8#  Copyright (c) 2004-2012  HUBzero Foundation, LLC
9#
10#  See the file "license.terms" for information on usage and
11#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12# ======================================================================
13
14require(Rappture)
15
16# save the command line arguments
17args <- commandArgs();
18
19# pick out the driver file which is the 7th element in the array
20# our array looks like this:
21# [1] "/usr/lib/R/bin/exec/R"
22# [2] "--slave"
23# [3] "--no-save"
24# [4] "--no-restore"
25# [5] "--silent"
26# [6] "--args"
27# [7] "driver.xml"
28
29# open the XML file containing the run parameters
30driver <- rp_lib(args[7])
31
32rp_utils_progress(10,"reading inputs")
33
34T <- rp_lib_get_string(driver,"input.number(temperature).current")
35T <- rp_units_convert_double(T,"K")
36Ef <- rp_lib_get_string(driver,"input.number(Ef).current")
37Ef <- rp_units_convert_double(Ef,"eV")
38
39rp_utils_progress(30,"performing calculations")
40
41kT <- 8.61734e-5 * T
42Emin <- Ef - 10*kT
43Emax <- Ef + 10*kT
44
45dE <- 0.005*(Emax-Emin)
46
47rp_utils_progress(60,"performing calculations")
48
49y <- seq(Emin,Emax,by=dE)
50x <- sapply(y,function(E) 1.0/(1.0 + exp((E - Ef)/kT)))
51
52rp_utils_progress(80,"storing results")
53
54# Label output graph with title, x-axis label,
55# y-axis lable, and y-axis units
56rp_lib_put_string(driver,"output.curve(f12).about.label","Fermi-Dirac Factor",FALSE)
57rp_lib_put_string(driver,"output.curve(f12).xaxis.label","Fermi-Dirac Factor",FALSE)
58rp_lib_put_string(driver,"output.curve(f12).yaxis.label","Energy",FALSE)
59rp_lib_put_string(driver,"output.curve(f12).yaxis.units","eV",FALSE)
60
61rp_utils_progress(90,"storing results")
62
63# coerce our arrays into a string of the form:
64# "x1 y1 \n x2 y2 \n x3 y3\n"...
65# store the string in the Rappture library object
66s <- paste(sprintf("%g %g\n",x,y),collapse="")
67rp_lib_put_string(driver,"output.curve(f12).component.xy",s,FALSE)
68
69
70rp_utils_progress(100,"preparing graphs")
71
72# save the updated XML describing the run...
73rp_lib_result(driver)
Note: See TracBrowser for help on using the repository browser.