1 | // ---------------------------------------------------------------------- |
---|
2 | // EXAMPLE: Fermi-Dirac function in C++. |
---|
3 | // |
---|
4 | // This simple example shows how to use Rappture within a simulator |
---|
5 | // written in C++. |
---|
6 | // ====================================================================== |
---|
7 | // AUTHOR: Derrick Kearney, Purdue University |
---|
8 | // Copyright (c) 2005-2009 Purdue Research Foundation |
---|
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 | #include "rappture.h" |
---|
15 | |
---|
16 | #include <stdlib.h> |
---|
17 | #include <stdio.h> |
---|
18 | #include <math.h> |
---|
19 | #include <unistd.h> |
---|
20 | |
---|
21 | int main(int argc, char * argv[]) { |
---|
22 | |
---|
23 | // create a rappture library from the file filePath |
---|
24 | Rappture::Library lib(argv[1]); |
---|
25 | |
---|
26 | Rappture::Number T; |
---|
27 | double Ef = 0.0; |
---|
28 | double dE = 0.0; |
---|
29 | double kT = 0.0; |
---|
30 | double Emin = 0.0; |
---|
31 | double Emax = 0.0; |
---|
32 | size_t nPts = 200; |
---|
33 | double E[nPts]; |
---|
34 | double f[nPts]; |
---|
35 | |
---|
36 | if (lib.error() != 0) { |
---|
37 | // cannot open file or out of memory |
---|
38 | fprintf(stderr, lib.traceback()); |
---|
39 | exit(lib.error()); |
---|
40 | } |
---|
41 | |
---|
42 | Rappture::connect(&lib,"temperature",&T); |
---|
43 | lib.value("Ef", &Ef, "units=eV"); |
---|
44 | |
---|
45 | if (lib.error() != 0) { |
---|
46 | // there were errors while retrieving input data values |
---|
47 | // dump the tracepack |
---|
48 | fprintf(stderr, lib.traceback()); |
---|
49 | exit(lib.error()); |
---|
50 | } |
---|
51 | |
---|
52 | kT = 8.61734e-5 * T->value("K"); |
---|
53 | Emin = Ef - 10*kT; |
---|
54 | Emax = Ef + 10*kT; |
---|
55 | |
---|
56 | dE = (1.0/nPts)*(Emax-Emin); |
---|
57 | |
---|
58 | E = Emin; |
---|
59 | for (size_t idx = 0; idx < nPts; idx++) { |
---|
60 | E = E + dE; |
---|
61 | f = 1.0/(1.0 + exp((E - Ef)/kT)); |
---|
62 | fArr[idx] = f; |
---|
63 | EArr[idx] = E; |
---|
64 | rpUtilsProgress((int)((E-Emin)/(Emax-Emin)*100),"Iterating"); |
---|
65 | } |
---|
66 | |
---|
67 | const char *curveLabel = "Fermi-Dirac Curve" |
---|
68 | const char *curveDesc = "Plot of Fermi-Dirac Calculation"; |
---|
69 | |
---|
70 | // do it the easy way, |
---|
71 | // create a plot to add to the library |
---|
72 | // plot is registered with lib upon object creation |
---|
73 | // p1->add(nPts,xArr,yArr,format,curveLabel,curveDesc); |
---|
74 | |
---|
75 | Rappture::Plot p1(lib); |
---|
76 | p1.add(nPts,fArr,EArr,"",curveLabel,curveDesc); |
---|
77 | p1.propstr("xlabel","Fermi-Dirac Factor"); |
---|
78 | p1.propstr("ylabel","Energy"); |
---|
79 | p1.propstr("yunits","eV"); |
---|
80 | |
---|
81 | lib.result(); |
---|
82 | |
---|
83 | return 0; |
---|
84 | } |
---|