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 | |
---|
14 | require(Rappture) |
---|
15 | |
---|
16 | # save the command line arguments |
---|
17 | args <- 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 |
---|
30 | driver <- rp_lib(args[7]) |
---|
31 | |
---|
32 | rp_utils_progress(10,"reading inputs") |
---|
33 | |
---|
34 | T <- rp_lib_get_string(driver,"input.number(temperature).current") |
---|
35 | T <- rp_units_convert_double(T,"K") |
---|
36 | Ef <- rp_lib_get_string(driver,"input.number(Ef).current") |
---|
37 | Ef <- rp_units_convert_double(Ef,"eV") |
---|
38 | |
---|
39 | rp_utils_progress(30,"performing calculations") |
---|
40 | |
---|
41 | kT <- 8.61734e-5 * T |
---|
42 | Emin <- Ef - 10*kT |
---|
43 | Emax <- Ef + 10*kT |
---|
44 | |
---|
45 | dE <- 0.005*(Emax-Emin) |
---|
46 | |
---|
47 | rp_utils_progress(60,"performing calculations") |
---|
48 | |
---|
49 | y <- seq(Emin,Emax,by=dE) |
---|
50 | x <- sapply(y,function(E) 1.0/(1.0 + exp((E - Ef)/kT))) |
---|
51 | |
---|
52 | rp_utils_progress(80,"storing results") |
---|
53 | |
---|
54 | # Label output graph with title, x-axis label, |
---|
55 | # y-axis lable, and y-axis units |
---|
56 | rp_lib_put_string(driver,"output.curve(f12).about.label","Fermi-Dirac Factor",FALSE) |
---|
57 | rp_lib_put_string(driver,"output.curve(f12).xaxis.label","Fermi-Dirac Factor",FALSE) |
---|
58 | rp_lib_put_string(driver,"output.curve(f12).yaxis.label","Energy",FALSE) |
---|
59 | rp_lib_put_string(driver,"output.curve(f12).yaxis.units","eV",FALSE) |
---|
60 | |
---|
61 | rp_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 |
---|
66 | s <- paste(sprintf("%g %g\n",x,y),collapse="") |
---|
67 | rp_lib_put_string(driver,"output.curve(f12).component.xy",s,FALSE) |
---|
68 | |
---|
69 | |
---|
70 | rp_utils_progress(100,"preparing graphs") |
---|
71 | |
---|
72 | # save the updated XML describing the run... |
---|
73 | rp_lib_result(driver) |
---|